17
17
public class collabcraft extends JavaPlugin implements Listener ,CommandExecutor {
18
18
private Player controller ;
19
19
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 );
22
22
private PlayerInventory inventory = null ;
23
23
private Location location = null ;
24
24
private Location spawn = null ;
25
+ private float exp = 0 ;
25
26
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 (){
28
33
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 );
36
40
inventory = p .getInventory ();
37
41
location = p .getLocation ();
38
42
spawn = p .getBedSpawnLocation ();
43
+ exp = p .getExp ();
44
+ health = p .getHealth ();
39
45
if (!(waiting .size () == 0 )){
40
46
Player author = waiting .get (0 );
41
- p .setGameMode (GameMode .SPECTATOR );
42
47
author .getInventory ().setArmorContents (p .getInventory ().getArmorContents ());
43
48
author .getInventory ().setExtraContents (p .getInventory ().getExtraContents ());
44
49
author .teleport (p .getLocation ());
50
+ author .setExp (exp );
45
51
author .setBedSpawnLocation (p .getBedSpawnLocation ());
46
52
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 );
49
56
}
50
57
}
51
- progress = progress + 1.0 ;
52
- turnTime .setProgress (progress );
53
- try {
54
- Thread .sleep (time *10 );
55
- }catch (Exception e ){
56
-
57
- }
58
58
}
59
59
}
60
60
};
61
61
return r ;
62
62
}
63
- private Thread turnThread ;
63
+ private Timer turnThread ;
64
64
public void onEnable () {
65
65
Bukkit .getPluginManager ().registerEvents (this , this );
66
66
turnTime .setVisible (true );
67
+ turnTime .setProgress (0.0 );
67
68
}
68
- public void onPlayerJoin (Player p ){
69
+ @ EventHandler
70
+ public void onPlayerJoin (PlayerJoinEvent p ){
69
71
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 ;
73
77
}
74
- p .setGameMode (GameMode .SPECTATOR );
78
+ p .getPlayer ().setGameMode (GameMode .SPECTATOR );
79
+ turnTime .addPlayer (p .getPlayer ());
75
80
}
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 ());
79
89
}
80
90
}
81
91
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
86
96
inventory = controller .getInventory ();
87
97
location = controller .getLocation ();
88
98
spawn = controller .getBedSpawnLocation ();
99
+ health = controller .getHealth ();
100
+ exp = controller .getExp ();
89
101
controller .setGameMode (GameMode .SPECTATOR );
90
102
author .getInventory ().setArmorContents (controller .getInventory ().getArmorContents ());
91
103
author .getInventory ().setExtraContents (controller .getInventory ().getExtraContents ());
92
104
author .teleport (controller .getLocation ());
105
+ author .setExp (exp );
106
+ author .setHealth (health );
93
107
author .setBedSpawnLocation (spawn );
94
108
controller = author ;
95
109
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 );
98
113
return true ;
99
114
}else if (cmd .getName ().equalsIgnoreCase ("endturn" )){
100
115
if (waiting .contains (author )){
101
- if (waiting .get ( 0 ) == author ){
116
+ if (waiting .size ( ) == 1 ){
102
117
inventory = author .getInventory ();
103
118
location = author .getLocation ();
104
119
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 );
105
125
}
106
126
waiting .remove (author );
127
+ author .setGameMode (GameMode .SPECTATOR );
107
128
return true ;
108
129
}else {
109
130
return false ;
@@ -112,13 +133,17 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
112
133
if (!(waiting .contains (author ))){
113
134
waiting .add (author );
114
135
if (waiting .get (0 ) == author ){
136
+ controller = author ;
115
137
author .setGameMode (GameMode .SURVIVAL );
116
138
author .getInventory ().setArmorContents (inventory .getArmorContents ());
117
139
author .getInventory ().setExtraContents (inventory .getExtraContents ());
118
140
author .teleport (location );
141
+ author .setExp (exp );
142
+ author .setHealth (health );
119
143
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 );
122
147
}
123
148
return true ;
124
149
}else {
0 commit comments