Skip to content

Commit de3dc98

Browse files
authored
Update collabcraft.java
1 parent 7726dbe commit de3dc98

File tree

1 file changed

+60
-35
lines changed

1 file changed

+60
-35
lines changed

src/main/java/io/papermc/collabcraft/collabcraft.java

Lines changed: 60 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,65 +17,75 @@
1717
public class collabcraft extends JavaPlugin implements Listener,CommandExecutor {
1818
private Player controller;
1919
private ArrayList<Player> waiting = new ArrayList<Player>();
20-
private int time = 30; //in seconds
21-
private BossBar turnTime = Bukkit.createBossBar("PlayerNameHere's turn",BarColor.BLUE,BarStyle.SOLID);
20+
private long time = 300L; //ms
21+
private BossBar turnTime = Bukkit.createBossBar("No one is controlling",BarColor.BLUE,BarStyle.SOLID);
2222
private PlayerInventory inventory = null;
2323
private Location location = null;
2424
private Location spawn = null;
25+
private float exp = 0;
2526
private boolean joined = false;
26-
private Runnable createRunnable(Player p){
27-
Runnable r = new Runnable(){
27+
private double progress = 0.0;
28+
private double health = 0.0;
29+
private TimerTask createRunnable(Player p){
30+
turnTime.setTitle(p.getName() + "'s turn");
31+
turnTime.setProgress(0.0);
32+
TimerTask r = new TimerTask(){
2833
public void run(){
29-
String name = p.getName();
30-
double progress = 0.0;
31-
turnTime.setTitle(name + "'s turn");
32-
turnTime.setProgress(0.0);
33-
while (waiting.get(0) == p){
34-
if (progress >= 100.0){
35-
waiting.remove(0);
34+
if (waiting.get(0) == p){
35+
progress = progress + 0.01;
36+
turnTime.setProgress(progress);
37+
if (progress >= 1.0){
38+
p.setGameMode(GameMode.SPECTATOR);
39+
waiting.remove(p);
3640
inventory = p.getInventory();
3741
location = p.getLocation();
3842
spawn = p.getBedSpawnLocation();
43+
exp = p.getExp();
44+
health = p.getHealth();
3945
if (!(waiting.size() == 0)){
4046
Player author = waiting.get(0);
41-
p.setGameMode(GameMode.SPECTATOR);
4247
author.getInventory().setArmorContents(p.getInventory().getArmorContents());
4348
author.getInventory().setExtraContents(p.getInventory().getExtraContents());
4449
author.teleport(p.getLocation());
50+
author.setExp(exp);
4551
author.setBedSpawnLocation(p.getBedSpawnLocation());
4652
author.setGameMode(GameMode.SURVIVAL);
47-
turnThread = new Thread(createRunnable(author));
48-
turnThread.start();
53+
progress = 0.0;
54+
turnThread = new Timer();
55+
turnThread.scheduleAtFixedRate(createRunnable(author),time,time);
4956
}
5057
}
51-
progress = progress + 1.0;
52-
turnTime.setProgress(progress);
53-
try{
54-
Thread.sleep(time*10);
55-
}catch(Exception e){
56-
57-
}
5858
}
5959
}
6060
};
6161
return r;
6262
}
63-
private Thread turnThread;
63+
private Timer turnThread;
6464
public void onEnable() {
6565
Bukkit.getPluginManager().registerEvents(this, this);
6666
turnTime.setVisible(true);
67+
turnTime.setProgress(0.0);
6768
}
68-
public void onPlayerJoin(Player p){
69+
@EventHandler
70+
public void onPlayerJoin(PlayerJoinEvent p){
6971
if (joined == false){
70-
inventory = p.getInventory();
71-
location = p.getLocation();
72-
spawn = p.getBedSpawnLocation();
72+
inventory = p.getPlayer().getInventory();
73+
location = p.getPlayer().getLocation();
74+
spawn = p.getPlayer().getBedSpawnLocation();
75+
health = p.getPlayer().getHealth();
76+
joined = true;
7377
}
74-
p.setGameMode(GameMode.SPECTATOR);
78+
p.getPlayer().setGameMode(GameMode.SPECTATOR);
79+
turnTime.addPlayer(p.getPlayer());
7580
}
76-
public void onPlayerQuit(Player p){
77-
if (waiting.contains(p)){
78-
waiting.remove(p);
81+
@EventHandler
82+
public void onPlayerQuit(PlayerQuitEvent p){
83+
if (waiting.get(0) == p.getPlayer()){
84+
turnTime.setTitle("No one is controlling");
85+
turnTime.setProgress(0.0);
86+
}
87+
if (waiting.contains(p.getPlayer())){
88+
waiting.remove(p.getPlayer());
7989
}
8090
}
8191
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
@@ -86,24 +96,35 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
8696
inventory = controller.getInventory();
8797
location = controller.getLocation();
8898
spawn = controller.getBedSpawnLocation();
99+
health = controller.getHealth();
100+
exp = controller.getExp();
89101
controller.setGameMode(GameMode.SPECTATOR);
90102
author.getInventory().setArmorContents(controller.getInventory().getArmorContents());
91103
author.getInventory().setExtraContents(controller.getInventory().getExtraContents());
92104
author.teleport(controller.getLocation());
105+
author.setExp(exp);
106+
author.setHealth(health);
93107
author.setBedSpawnLocation(spawn);
94108
controller = author;
95109
controller.setGameMode(GameMode.SURVIVAL);
96-
turnThread = new Thread(createRunnable(controller));
97-
turnThread.start();
110+
progress = 0.0;
111+
turnThread = new Timer();
112+
turnThread.scheduleAtFixedRate(createRunnable(author),time,time);
98113
return true;
99114
}else if (cmd.getName().equalsIgnoreCase("endturn")){
100115
if (waiting.contains(author)){
101-
if (waiting.get(0) == author){
116+
if (waiting.size() == 1){
102117
inventory = author.getInventory();
103118
location = author.getLocation();
104119
spawn = author.getBedSpawnLocation();
120+
health = author.getHealth();
121+
exp = author.getExp();
122+
turnTime.setTitle("No one is controlling");
123+
progress = 0.0;
124+
turnTime.setProgress(0.0);
105125
}
106126
waiting.remove(author);
127+
author.setGameMode(GameMode.SPECTATOR);
107128
return true;
108129
}else{
109130
return false;
@@ -112,13 +133,17 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
112133
if (!(waiting.contains(author))){
113134
waiting.add(author);
114135
if (waiting.get(0) == author){
136+
controller = author;
115137
author.setGameMode(GameMode.SURVIVAL);
116138
author.getInventory().setArmorContents(inventory.getArmorContents());
117139
author.getInventory().setExtraContents(inventory.getExtraContents());
118140
author.teleport(location);
141+
author.setExp(exp);
142+
author.setHealth(health);
119143
author.setBedSpawnLocation(spawn);
120-
turnThread = new Thread(createRunnable(author));
121-
turnThread.start();
144+
progress = 0.0;
145+
turnThread = new Timer();
146+
turnThread.scheduleAtFixedRate(createRunnable(author),time,time);
122147
}
123148
return true;
124149
}else{

0 commit comments

Comments
 (0)