From 9e2ddeb55321b09086a5a27254197d783847e6e3 Mon Sep 17 00:00:00 2001 From: Leonard Kugis Date: Mon, 25 Apr 2022 18:50:36 +0200 Subject: Initial commit --- src/main/java/com/encrox/zombie/Commander.java | 56 ++++ src/main/java/com/encrox/zombie/Constants.java | 59 +++++ src/main/java/com/encrox/zombie/Game.java | 294 +++++++++++++++++++++ src/main/java/com/encrox/zombie/GameListener.java | 159 +++++++++++ src/main/java/com/encrox/zombie/Lobby.java | 72 +++++ src/main/java/com/encrox/zombie/Map.java | 118 +++++++++ src/main/java/com/encrox/zombie/Misc.java | 12 + src/main/java/com/encrox/zombie/Mob.java | 41 +++ src/main/java/com/encrox/zombie/Schematic.java | 117 ++++++++ src/main/java/com/encrox/zombie/Zombie.java | 245 +++++++++++++++++ src/main/java/com/encrox/zombie/ZombieWorld.java | 79 ++++++ .../java/com/encrox/zombie/interactable/Chest.java | 27 ++ .../encrox/zombie/interactable/Interactable.java | 23 ++ .../java/com/encrox/zombie/interactable/Lever.java | 38 +++ .../com/encrox/zombie/interactable/Powerup.java | 22 ++ 15 files changed, 1362 insertions(+) create mode 100755 src/main/java/com/encrox/zombie/Commander.java create mode 100755 src/main/java/com/encrox/zombie/Constants.java create mode 100755 src/main/java/com/encrox/zombie/Game.java create mode 100755 src/main/java/com/encrox/zombie/GameListener.java create mode 100755 src/main/java/com/encrox/zombie/Lobby.java create mode 100755 src/main/java/com/encrox/zombie/Map.java create mode 100755 src/main/java/com/encrox/zombie/Misc.java create mode 100755 src/main/java/com/encrox/zombie/Mob.java create mode 100755 src/main/java/com/encrox/zombie/Schematic.java create mode 100755 src/main/java/com/encrox/zombie/Zombie.java create mode 100755 src/main/java/com/encrox/zombie/ZombieWorld.java create mode 100755 src/main/java/com/encrox/zombie/interactable/Chest.java create mode 100755 src/main/java/com/encrox/zombie/interactable/Interactable.java create mode 100755 src/main/java/com/encrox/zombie/interactable/Lever.java create mode 100755 src/main/java/com/encrox/zombie/interactable/Powerup.java (limited to 'src/main/java/com/encrox/zombie') diff --git a/src/main/java/com/encrox/zombie/Commander.java b/src/main/java/com/encrox/zombie/Commander.java new file mode 100755 index 0000000..40e77eb --- /dev/null +++ b/src/main/java/com/encrox/zombie/Commander.java @@ -0,0 +1,56 @@ +package com.encrox.zombie; + +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Commander implements CommandExecutor { + + @Override + public boolean onCommand(CommandSender sender, Command arg1, String arg2, String[] args) { + try { + if(sender instanceof Player) { + Player player = (Player)sender; + switch(args[0]) { + case "lobby": + switch(args[1]) { + case "create": + if(player.hasPermission("zombie.create")) { + player.sendMessage(Zombie.createLobby(player, args[2], args[3])); + } else { + player.sendMessage(ChatColor.RED + Zombie.lang.getProperty("no_permission")); + } + break; + case "join": + if(player.hasPermission("zombie.join")) { + if(args.length == 3) { + player.sendMessage(Zombie.joinLobby(player, args[2])); + } else { + player.sendMessage(Zombie.listLobbies()); + } + } else { + player.sendMessage(ChatColor.RED + Zombie.lang.getProperty("no_permission")); + } + break; + case "leave": + player.sendMessage(Zombie.removePlayer(player)); + break; + case "start": + player.sendMessage(Zombie.start(player)); + break; + } + break; + } + } else { + sender.sendMessage(Zombie.lang.getProperty("no_player")); + } + } catch(Exception e) { + e.printStackTrace(); + return false; + } + return true; + } + +} diff --git a/src/main/java/com/encrox/zombie/Constants.java b/src/main/java/com/encrox/zombie/Constants.java new file mode 100755 index 0000000..50e7491 --- /dev/null +++ b/src/main/java/com/encrox/zombie/Constants.java @@ -0,0 +1,59 @@ +package com.encrox.zombie; + +import org.bukkit.Material; +import org.bukkit.enchantments.Enchantment; + +import com.sk89q.worldedit.blocks.ItemType; + +public class Constants { + + public static class Game { + + public static final Material[] ITEMSET = new Material[] { + Material.WOOD_HOE, + Material.WOOD_PICKAXE, + Material.WOOD_SPADE, + Material.WOOD_SWORD, + Material.STONE_AXE, + Material.STONE_HOE, + Material.STONE_PICKAXE, + Material.STONE_SPADE, + Material.STONE_SWORD, + Material.IRON_AXE, + Material.IRON_HOE, + Material.IRON_PICKAXE, + Material.IRON_SPADE, + Material.IRON_SWORD, + Material.GOLD_AXE, + Material.GOLD_HOE, + Material.GOLD_PICKAXE, + Material.GOLD_SPADE, + Material.GOLD_SWORD, + Material.DIAMOND_AXE, + Material.DIAMOND_HOE, + Material.DIAMOND_PICKAXE, + Material.DIAMOND_SPADE, + Material.DIAMOND_SWORD, + Material.BOW, + Material.ARROW, + Material.LEATHER_HELMET, + Material.LEATHER_CHESTPLATE, + Material.LEATHER_LEGGINGS, + Material.LEATHER_BOOTS, + Material.GOLD_HELMET, + Material.GOLD_CHESTPLATE, + Material.GOLD_LEGGINGS, + Material.GOLD_BOOTS, + Material.IRON_HELMET, + Material.IRON_CHESTPLATE, + Material.IRON_LEGGINGS, + Material.IRON_BOOTS, + Material.CHAINMAIL_HELMET, + Material.CHAINMAIL_CHESTPLATE, + Material.CHAINMAIL_LEGGINGS, + Material.CHAINMAIL_BOOTS + }; + + } + +} 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 players, frozen; + private volatile Map map; + private volatile HashMap stats; + private volatile InstancedProtectedCuboidRegion region; + private volatile Mob[] mobs; + private volatile ArrayList mobsRound; + public volatile ArrayList watchdog; + private volatile int round; + private volatile boolean done; + private volatile Interactable[] inters; + private volatile com.sk89q.worldedit.BlockVector min; + + public Game(ArrayList players, Map map) { + this.players = players; + this.map = map; + stats = new HashMap(); + region = Zombie.zombieWorld.allocate(map.getSchematic()); + mobs = map.getMobs(); + mobsRound = new ArrayList(); + watchdog = new ArrayList(); + frozen = new ArrayList(); + 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 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 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= cost) { + player.sendMessage(ChatColor.YELLOW + Zombie.lang.getProperty("game_paid").replace("%amount%", ""+cost)); + current.substractPoints(player, cost); + if(inter instanceof Chest) { + if(Math.random() > 0.25) { + ItemStack item = new ItemStack((Material)Misc.random(Constants.Game.ITEMSET)); + Enchantment ench; + while(!(ench = (Enchantment)Misc.random(Enchantment.values())).canEnchantItem(item)); + item.addEnchantment(ench, 1+(new Random().nextInt(Integer.MAX_VALUE)%(ench.getMaxLevel()-1))); + player.getInventory().addItem(item); + } else { + player.getInventory().addItem(new ItemStack(Material.SKULL_ITEM)); + current.randomizeChest(); + } + e.setCancelled(true); + } else if(inter instanceof Powerup) { + Powerup powerup = (Powerup)inter; + player.getInventory().addItem(powerup.getItem()); + } else if(inter instanceof Lever) { + Lever lever = (Lever)inter; + if(lever.isToggled()) { + player.sendMessage(ChatColor.RED + Zombie.lang.getProperty("game_impossible")); + current.addPoints(player, cost); + e.setCancelled(true); + } else { + lever.toggle(); + } + } + } else { + player.sendMessage(ChatColor.RED + Zombie.lang.getProperty("game_no_points").replace("%amount%", ""+cost)); + e.setCancelled(true); + } + } + } + } + } + } catch(Exception ex) { + ex.printStackTrace(); + } + } + + @EventHandler + public void onPlayerInteractEntity(PlayerInteractEntityEvent e) { + Game current; + Player from = e.getPlayer(); + if(e.getRightClicked() instanceof Player) { + Player to = (Player)e.getRightClicked(); + for(int i = 0, size = Zombie.games.size(); i players; + private Player creator; + private String identifier; + private Map map; + + public Lobby(Player creator, String identifier, Map map) { + this.creator = creator; + this.identifier = identifier; + this.map = map; + players = new ArrayList(); + players.add(creator); + } + + public String getIdentifier() { + return identifier; + } + + public Player getCreator() { + return creator; + } + + public boolean containsPlayer(Player player) { + if(creator.equals(player)) return true; + return players.contains(player); + } + + public void removePlayer(Player player) { + if(creator.equals(player)) { + for(int i = 0, size = players.size(); i entities, tileEntities; + public File file; + + public Schematic(int width, int height, int length, String materials, byte[] blocks, byte[] data, List entities, List tileEntities) { + this.width = width; + this.height = height; + this.length = length; + this.materials = materials; + this.blocks = blocks; + this.data = data; + this.entities = entities; + this.tileEntities = tileEntities; + blockReadIndex = 0; + blockWriteIndex = 0; + dataReadIndex = 0; + dataWriteIndex = 0; + } + + public Schematic(File file) { + this.file = file; + CompoundTag schematicTag = null; + try { + NBTInputStream in = new NBTInputStream(new FileInputStream(file)); + schematicTag = (CompoundTag)in.readTag(); + in.close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + Map schematic = schematicTag.getValue(); + width = ((ShortTag)schematic.get("Width")).getValue(); + height = ((ShortTag)schematic.get("Height")).getValue(); + length = ((ShortTag)schematic.get("Length")).getValue(); + materials = ((StringTag)schematic.get("Materials")).getValue(); + blocks = ((ByteArrayTag)schematic.get("Blocks")).getValue(); + data = ((ByteArrayTag)schematic.get("Data")).getValue(); + entities = ((ListTag)schematic.get("Entities")).getValue(); + tileEntities = ((ListTag)schematic.get("TileEntities")).getValue(); + blockReadIndex = 0; + blockWriteIndex = 0; + dataReadIndex = 0; + dataWriteIndex = 0; + } + + public byte getBlockIdAt(int x, int y, int z) { + return blocks[(y*length + z)*width + x]; + } + + public byte getNextBlock() { + return blocks[blockReadIndex++]; + } + + public byte getDataAt(int x, int y, int z) { + return data[(y*length + z)*width + x]; + } + + public byte getNextData() { + return data[dataReadIndex++]; + } + + public void setBlockIdAt(int x, int y, int z, byte blockId) { + blocks[(y*length + z)*width + x] = blockId; + } + + public void setNextBlock(byte blockId) { + blocks[blockWriteIndex++] = blockId; + } + + public void setDataAt(int x, int y, int z, byte newData) { + data[(y*length + z)*width + x] = newData; + } + + public void setNextData(byte newData) { + data[dataWriteIndex++] = newData; + } + + public void write(File file) { + + } + + public void reset() { + blockReadIndex = 0; + blockWriteIndex = 0; + dataReadIndex = 0; + dataWriteIndex = 0; + } + +} diff --git a/src/main/java/com/encrox/zombie/Zombie.java b/src/main/java/com/encrox/zombie/Zombie.java new file mode 100755 index 0000000..d687f6e --- /dev/null +++ b/src/main/java/com/encrox/zombie/Zombie.java @@ -0,0 +1,245 @@ +package com.encrox.zombie; + +import java.io.BufferedInputStream; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.lang.reflect.Method; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.ArrayList; +import java.util.Properties; +import java.util.logging.Logger; + +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.PluginDescriptionFile; +import org.bukkit.plugin.java.JavaPlugin; +import org.json.JSONArray; +import org.json.JSONObject; + +import com.sk89q.worldedit.EditSession; +import com.sk89q.worldedit.bukkit.WorldEditPlugin; + +public class Zombie extends JavaPlugin { + + public static Plugin plugin; + public static Logger logger; + public static PluginDescriptionFile pdf; + public static Properties lang; + public static JSONObject config; + public static JSONArray schematicDescriptors; + public static File schematicsDirectory; + public static WorldEditPlugin we; + public static ArrayList maps; + public static ArrayList lobbies; + public static ArrayList games; + public static ZombieWorld zombieWorld; + public static Commander commander; + + public void onEnable() { + pdf = getDescription(); + logger = Logger.getLogger("Minecraft"); + if(setupMyself() && setupWorldEdit()) { + plugin = this; + commander = new Commander(); + getCommand("zombie").setExecutor(commander); + logger.info(pdf.getName() + " " + pdf.getVersion() + " has been enabled."); + } else { + logger.info(pdf.getName() + " " + pdf.getVersion() + " has been disabled."); + } + } + + public boolean setupWorldEdit() { + we = (WorldEditPlugin)Bukkit.getPluginManager().getPlugin("WorldEdit"); + return (we != null); + } + + public boolean setupMyself() { + if(!this.getDataFolder().exists()) + this.getDataFolder().mkdirs(); + schematicsDirectory = new File(this.getDataFolder(), "schematics"); + if(!schematicsDirectory.exists()) + schematicsDirectory.mkdirs(); + File configFile = new File(this.getDataFolder(), "config.json"); + if(!configFile.exists()) { + BufferedInputStream bis; + FileOutputStream out; + try { + bis = new BufferedInputStream(getClass().getClassLoader().getResourceAsStream("config.json")); + out = new FileOutputStream(configFile); + int current; + while((current = bis.read()) != -1) { + out.write(current); + } + bis.close(); + out.close(); + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + BufferedReader br; + StringBuilder sb = new StringBuilder(); + String line; + try { + br = new BufferedReader(new FileReader(configFile)); + while((line = br.readLine()) != null) + sb.append(line); + br.close(); + } catch (Exception e1) { + e1.printStackTrace(); + return false; + } + config = new JSONObject(sb.toString()); + zombieWorld = new ZombieWorld(this, Bukkit.getWorld(config.getString("world"))); + File schematicDescriptorFile = new File(schematicsDirectory, "descriptor.json"); + if(!schematicDescriptorFile.exists()) { + int current; + try { + BufferedInputStream bis = new BufferedInputStream(getClass().getClassLoader().getResourceAsStream("descriptor.json")); + FileOutputStream out = new FileOutputStream(schematicDescriptorFile); + while((current = bis.read()) != -1) { + out.write(current); + } + bis.close(); + out.close(); + } catch(Exception e) { + e.printStackTrace(); + return false; + } + try { + BufferedInputStream bis = new BufferedInputStream(getClass().getClassLoader().getResourceAsStream("schematics/test.schematic")); + FileOutputStream out = new FileOutputStream(new File(schematicsDirectory, "test.schematic")); + while((current = bis.read()) != -1) { + out.write(current); + } + bis.close(); + out.close(); + } catch(Exception e) { + e.printStackTrace(); + return false; + } + } + sb = new StringBuilder(); + try { + br = new BufferedReader(new FileReader(schematicDescriptorFile)); + while((line = br.readLine()) != null) + sb.append(line); + br.close(); + } catch (Exception e1) { + e1.printStackTrace(); + return false; + } + schematicDescriptors = new JSONArray(sb.toString()); + lang = new Properties(); + try { + //TODO + lang.load(getClass().getClassLoader().getResourceAsStream("lang/" + config.getString("locale") + ".properties")); + } catch (FileNotFoundException e) { + e.printStackTrace(); + return false; + } catch (IOException e) { + e.printStackTrace(); + return false; + } + maps = new ArrayList(); + lobbies = new ArrayList(); + games = new ArrayList(); + for(int i = 0, len = schematicDescriptors.length(); i regions; + private int lastX; + + public ZombieWorld(Plugin plugin, World world) { + this.world = world; + this.plugin = plugin; + regions = new ArrayList(); + lastX = 0; + } + + public InstancedProtectedCuboidRegion allocate(int width, int length) { + BlockVector min, max; + InstancedProtectedCuboidRegion region; + for(int i = 0; true; i+=16) { + if(new ProtectedCuboidRegion("current", (min = new BlockVector(lastX+i, 0, 0)), (max = new BlockVector(lastX+i+width, 255, length))).getIntersectingRegions(regions).isEmpty()) { + region = new InstancedProtectedCuboidRegion(plugin, world, ""+min.getBlockX(), min, max); + regions.add(region); + return region; + } + } + } + + public InstancedProtectedCuboidRegion allocate(Schematic schematic) { + BlockVector min, max; + InstancedProtectedCuboidRegion region; + for(int i = 0; true; i+=16) { + if(new ProtectedCuboidRegion("current", (min = new BlockVector(lastX+i, 0, 0)), (max = new BlockVector(lastX+i+schematic.width, 255, schematic.length))).getIntersectingRegions(regions).isEmpty()) { + region = new InstancedProtectedCuboidRegion(plugin, world, ""+min.getBlockX(), min, max); + schematic.reset(); + for(int y = 0; y