1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
package com.encrox.zombie;
import java.util.ArrayList;
import java.util.HashMap;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.material.MaterialData;
import org.bukkit.util.BlockVector;
import com.encrox.instancedregions.InstancedProtectedCuboidRegion;
import com.encrox.zombie.interactable.Chest;
import com.encrox.zombie.interactable.Interactable;
import com.encrox.zombie.interactable.Lever;
import com.encrox.zombie.interactable.Powerup;
public class Game {
private volatile ArrayList<Player> players, frozen;
private volatile Map map;
private volatile HashMap<Player,Integer> stats;
private volatile InstancedProtectedCuboidRegion region;
private volatile Mob[] mobs;
private volatile ArrayList<Mob> mobsRound;
public volatile ArrayList<Integer> watchdog;
private volatile int round;
private volatile boolean done;
private volatile Interactable[] inters;
private volatile com.sk89q.worldedit.BlockVector min;
public Game(ArrayList<Player> players, Map map) {
this.players = players;
this.map = map;
stats = new HashMap<Player,Integer>();
region = Zombie.zombieWorld.allocate(map.getSchematic());
mobs = map.getMobs();
mobsRound = new ArrayList<Mob>();
watchdog = new ArrayList<Integer>();
frozen = new ArrayList<Player>();
BlockVector spawn = map.getSpawn();
min = region.getMinimumPoint();
Powerup[] powerups = map.getPowerups();
Lever[] levers = map.getLevers();
inters = new Interactable[powerups.length+levers.length+1];
for(int i = 0; i<levers.length; i++) {
inters[i] = levers[i];
}
for(int i = 0; i<powerups.length; i++) {
BlockVector bv = powerups[i].getBlockVector();
Block block = Zombie.zombieWorld.getWorld().getBlockAt(min.getBlockX()+bv.getBlockX(), 64+bv.getBlockY(), min.getBlockZ()+bv.getBlockZ());
inters[powerups.length+i] = powerups[i];
block.setType(Material.SIGN_POST);
Sign sign = (Sign)block.getState();
//sign.setData(new MaterialData(Material.WALL_SIGN));
sign.setLine(0, "[ZombieItem]");
String itemStr = powerups[i].getItem().toString();
sign.setLine(1, itemStr);
int len1 = itemStr.length(), len2 = sign.getLine(1).length();
if(len2 < len1)
sign.setLine(2, itemStr.substring(len2));
if((len2 += sign.getLine(2).length()) < len1)
sign.setLine(3, itemStr.substring(len2));
sign.update();
}
Chest relChest = (Chest)Misc.random(map.getChests());
inters[inters.length-1] = relChest;
BlockVector bv = relChest.getBlockVector();
Block block = Zombie.zombieWorld.getWorld().getBlockAt(min.getBlockX()+bv.getBlockX(), 64+bv.getBlockY(), min.getBlockZ()+bv.getBlockZ());
block.setType(Material.CHEST);
org.bukkit.material.Chest c = new org.bukkit.material.Chest();
c.setFacingDirection(relChest.getFacing());
block.setData(c.getData());
block.getState().update();
region.apply();
Player player;
for(int i = 0, size = players.size(); i<size; i++) {
player = players.get(i);
stats.put(player, 1000000);
region.addPlayer(player);
player.teleport(new Location(Zombie.zombieWorld.getWorld(), -1000, 64, -1000));
Bukkit.getScheduler().runTaskLater(Zombie.plugin, new Runnable() {
Player player;
@Override
public void run() {
player.teleport(new Location(Zombie.zombieWorld.getWorld(), min.getBlockX()+spawn.getBlockX()+.5, 64+spawn.getBlockY(), min.getBlockZ()+spawn.getBlockZ()+.5));
}
public Runnable setPlayer(Player player) {
this.player = player;
return this;
}
}.setPlayer(player), 120L);
}
/*
Bukkit.getScheduler().runTask(Zombie.plugin, new Runnable() {
@Override
public void run() {
Mob current;
int amount = 1;
com.sk89q.worldedit.BlockVector min = region.getMinimumPoint();
LivingEntity entity;
for(int round = 1; round < Integer.MAX_VALUE; round++) {
announce(ChatColor.YELLOW + Zombie.lang.getProperty("game_round").replace("%round%", ""+round));
for(int i = 0, size = mobs.size(); i<size; i++) {
current = mobs.get(i);
if(current.spawn(round)) {
mobsRound.add(current);
}
}
for(int i = 0; i<amount; i++) {
for(int x = 0, size = mobsRound.size(); x<size; x++) {
current = mobsRound.get(x);
entity = Zombie.zombieWorld.getWorld().spawnCreature(new Location(Zombie.zombieWorld.getWorld(), min.getBlockX()+current.bv.getBlockX(), 64+current.bv.getBlockY(), min.getBlockZ()+current.bv.getBlockZ()), current.type);
entity.setMaxHealth(entity.getMaxHealth() + round);
entity.setHealth(entity.getMaxHealth());
watchdog.add(entity.getEntityId());
}
}
while(!watchdog.isEmpty());
}
}
});
*/
new Thread(new Runnable() {
@Override
public void run() {
Mob current;
com.sk89q.worldedit.BlockVector min = region.getMinimumPoint();
for(round = 1; round < Integer.MAX_VALUE; round++) {
announce(ChatColor.YELLOW + Zombie.lang.getProperty("game_round").replace("%round%", ""+round));
mobsRound.clear();
for(int i = 0; i<mobs.length; i++) {
if(mobs[i].spawn(round)) {
mobsRound.add(mobs[i]);
}
}
done = false;
Bukkit.getScheduler().runTask(Zombie.plugin, new Runnable() {
@Override
public void run() {
LivingEntity entity;
Mob current;
for(int i = 0, amount = 10 + round; i<amount; i++) {
for(int x = 0, size = mobsRound.size(); x<size; x++) {
current = mobsRound.get(x);
entity = Zombie.zombieWorld.getWorld().spawnCreature(new Location(Zombie.zombieWorld.getWorld(), min.getBlockX()+current.bv.getBlockX(), 64+current.bv.getBlockY(), min.getBlockZ()+current.bv.getBlockZ()), current.type);
entity.setMaxHealth(entity.getMaxHealth() + round);
entity.setHealth(entity.getMaxHealth());
watchdog.add(entity.getEntityId());
}
}
done = true;
}
});
while(!done);
while(!watchdog.isEmpty());
}
}
}).start();
}
public ArrayList<Player> getPlayers() {
return players;
}
public void playerDeath(Player player) {
if(!frozen.contains(player)) {
announce(ChatColor.RED + Zombie.lang.getProperty("game_player_fell").replace("%player%", player.getName()));
player.teleport(player.getLocation().add(0, -1, 0));
player.setHealth(.5);
frozen.add(player);
Bukkit.getScheduler().runTaskTimer(Zombie.plugin, new Runnable() {
int t = 30;
@Override
public void run() {
if(frozen.contains(player)) {
if(t == 0) {
announce(ChatColor.RED + Zombie.lang.getProperty("game_player_died").replace("%player%", player.getName()));
frozen.remove(player);
players.remove(player);
player.setHealth(player.getMaxHealth());
//TODO: Teleport back outta dungeon
} else {
announce(ChatColor.YELLOW + Zombie.lang.getProperty("game_revive_countdown").replace("%player%", player.getName()).replace("%seconds%", ""+t));
t-=5;
}
}
}
}, 0, 100);
}
}
public boolean isFrozen(Player player) {
return frozen.contains(player);
}
public void revive(Player from, Player to) {
if(frozen.contains(to)) {
frozen.remove(to);
to.teleport(to.getLocation().add(0, -1, 0));
to.setHealth(to.getMaxHealth());
}
}
public void randomizeChest() {
Block block = Zombie.zombieWorld.getWorld().getBlockAt(getAbsoluteLocation(inters[inters.length-1].getBlockVector()));
block.setType(Material.AIR);
block.getState().update();
Chest relChest = (Chest)Misc.random(map.getChests());
inters[inters.length-1] = relChest;
block = Zombie.zombieWorld.getWorld().getBlockAt(getAbsoluteLocation(relChest.getBlockVector()));
block.setType(Material.CHEST);
org.bukkit.material.Chest c = new org.bukkit.material.Chest();
c.setFacingDirection(relChest.getFacing());
block.setData(c.getData());
block.getState().update();
}
public void announce(String text) {
for(int i = 0, size = players.size(); i<size; i++) {
players.get(i).sendMessage(text);
}
}
public void addPoints(Player player, int amount) {
int newPoints = stats.get(player)+amount;
stats.put(player, newPoints);
player.sendMessage(ChatColor.YELLOW + Zombie.lang.getProperty("game_points").replace("%points%", ""+newPoints));
}
public void substractPoints(Player player, int amount) {
int newPoints = stats.get(player)-amount;
stats.put(player, newPoints);
player.sendMessage(ChatColor.YELLOW + Zombie.lang.getProperty("game_points").replace("%points%", ""+newPoints));
}
public void addPoints(int amount) {
stats.forEach((k,v) -> stats.put(k, v+amount));
}
public int getPoints(Player player) {
if(stats.containsKey(player))
return stats.get(player);
return -1;
}
public Location getAbsoluteLocation(BlockVector relative) {
return new Location(Zombie.zombieWorld.getWorld(), min.getX()+relative.getBlockX(), 64+relative.getBlockY(), min.getBlockZ()+relative.getBlockZ());
}
/*
public Powerup getPowerup(Block block) {
for(int i = 0; i<powerups.length; i++) {
if(Zombie.zombieWorld.getWorld().getBlockAt(min.getBlockX()+powerups[i].bv.getBlockX(), 64+powerups[i].bv.getBlockY(), min.getBlockZ()+powerups[i].bv.getBlockZ()).equals(block)) {
return powerups[i];
}
}
return null;
}
public Lever getLever(Block block) {
for(int i = 0; i<levers.length; i++) {
if(Zombie.zombieWorld.getWorld().getBlockAt(min.getBlockX()+levers[i].bv.getBlockX(), 64+levers[i].bv.getBlockY(), min.getBlockZ()+levers[i].bv.getBlockZ()).equals(block)) {
return levers[i];
}
}
return null;
}
*/
public Interactable getInteractable(Block block) {
BlockVector bv;
for(int i = 0; i<inters.length; i++) {
bv = inters[i].getBlockVector();
if(Zombie.zombieWorld.getWorld().getBlockAt(min.getBlockX()+bv.getBlockX(), 64+bv.getBlockY(), min.getBlockZ()+bv.getBlockZ()).equals(block)) {
return inters[i];
}
}
return null;
}
}
|