summaryrefslogtreecommitdiff
path: root/src/main/java/com/encrox/zombie/Game.java
diff options
context:
space:
mode:
authorLeonard Kugis <leonard@kug.is>2022-04-25 18:50:36 +0200
committerLeonard Kugis <leonard@kug.is>2022-04-25 18:50:36 +0200
commit9e2ddeb55321b09086a5a27254197d783847e6e3 (patch)
tree620e4a7b326df2a77fff51aff390e0a6bef43631 /src/main/java/com/encrox/zombie/Game.java
Initial commitHEADmaster
Diffstat (limited to 'src/main/java/com/encrox/zombie/Game.java')
-rwxr-xr-xsrc/main/java/com/encrox/zombie/Game.java294
1 files changed, 294 insertions, 0 deletions
diff --git a/src/main/java/com/encrox/zombie/Game.java b/src/main/java/com/encrox/zombie/Game.java
new file mode 100755
index 0000000..ae6ff4a
--- /dev/null
+++ b/src/main/java/com/encrox/zombie/Game.java
@@ -0,0 +1,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;
+ }
+
+}