diff --git a/src/com/onarandombox/MultiverseCore/MVEntityListener.java b/src/com/onarandombox/MultiverseCore/MVEntityListener.java index 16d56620..3bd6b665 100644 --- a/src/com/onarandombox/MultiverseCore/MVEntityListener.java +++ b/src/com/onarandombox/MultiverseCore/MVEntityListener.java @@ -16,29 +16,29 @@ import org.bukkit.event.entity.EntityListener; //import org.bukkit.event.entity.ExplosionPrimedEvent; public class MVEntityListener extends EntityListener { - + MultiverseCore plugin; - + public MVEntityListener(MultiverseCore plugin) { this.plugin = plugin; } - + // Need to find a way to stop the Ghast Fireballs damaging // surroundings but still doing damage to players. @Override public void onEntityExplode(EntityExplodeEvent event) { - + } @Override public void onEntityDeath(EntityDeathEvent event) { - if(event.getEntity() instanceof Player) { - Player p = (Player)event.getEntity(); + if (event.getEntity() instanceof Player) { + Player p = (Player) event.getEntity(); p.sendMessage("You died!"); } super.onEntityDeath(event); } - + /** * Handle Animal/Monster Spawn settings, seems like a more concrete method than using CraftBukkit. */ @@ -47,24 +47,26 @@ public class MVEntityListener extends EntityListener { World world = event.getEntity().getWorld(); if (event.isCancelled()) return; + + // Check if it's a world which we are meant to be managing. if (!(this.plugin.isMVWorld(world.getName()))) - return; // Check if it's a world which we are meant to be managing. - + return; + CreatureType creature = event.getCreatureType(); - + // event.getEntity().getWorld().spawnCreature(arg0, arg1); - + MVWorld mvworld = this.plugin.getMVWorld(world.getName()); - + // TODO: Look of this and see if there's a cleaner/better method of doing so... - + /** * Animal Handling */ if (event.getEntity() instanceof Animals) { // If we have no exceptions for Animals then we just follow the Spawn setting. if (mvworld.getAnimalList().isEmpty()) { - if (mvworld.hasAnimals()) { + if (mvworld.allowAnimalSpawning()) { return; } else { event.setCancelled(true); @@ -72,8 +74,8 @@ public class MVEntityListener extends EntityListener { } } // The idea of the Exceptions is they do the OPPOSITE of what the Spawn setting is... - if (mvworld.getAnimalList().contains(creature.toString())) { - if (mvworld.hasAnimals()) { + if (mvworld.getAnimalList().contains(creature.toString().toUpperCase())) { + if (mvworld.allowAnimalSpawning()) { event.setCancelled(true); return; } else { @@ -84,10 +86,10 @@ public class MVEntityListener extends EntityListener { /** * Monster Handling */ - if (event.getEntity() instanceof Monster || event.getEntity() instanceof Ghast || event.getEntity() instanceof PigZombie || event.getEntity() instanceof Slime) { + if (event.getEntity() instanceof Monster || event.getEntity() instanceof Ghast || event.getEntity() instanceof Slime) { // If we have no exceptions for Monsters then we just follow the Spawn setting. if (mvworld.getMonsterList().isEmpty()) { - if (mvworld.hasMonsters()) { + if (mvworld.allowMonsterSpawning()) { return; } else { event.setCancelled(true); @@ -95,8 +97,8 @@ public class MVEntityListener extends EntityListener { } } // The idea of the Exceptions is they do the OPPOSITE of what the Spawn setting is... - if (mvworld.getMonsterList().contains(creature.toString())) { - if (mvworld.hasMonsters()) { + if (mvworld.getMonsterList().contains(creature.toString().toUpperCase())) { + if (mvworld.allowMonsterSpawning()) { event.setCancelled(true); return; } else { @@ -105,5 +107,5 @@ public class MVEntityListener extends EntityListener { } } } - + } diff --git a/src/com/onarandombox/MultiverseCore/MVWorld.java b/src/com/onarandombox/MultiverseCore/MVWorld.java index e5c2dc3c..7152a2ff 100644 --- a/src/com/onarandombox/MultiverseCore/MVWorld.java +++ b/src/com/onarandombox/MultiverseCore/MVWorld.java @@ -342,7 +342,7 @@ public class MVWorld { this.config.save(); } - public Boolean hasAnimals() { + public Boolean allowAnimalSpawning() { return this.allowAnimals; } @@ -360,7 +360,7 @@ public class MVWorld { return this.masterList.get("animals"); } - public Boolean hasMonsters() { + public Boolean allowMonsterSpawning() { return this.allowMonsters; } diff --git a/src/com/onarandombox/MultiverseCore/MultiverseCore.java b/src/com/onarandombox/MultiverseCore/MultiverseCore.java index b3c83123..ac827913 100644 --- a/src/com/onarandombox/MultiverseCore/MultiverseCore.java +++ b/src/com/onarandombox/MultiverseCore/MultiverseCore.java @@ -220,7 +220,7 @@ public class MultiverseCore extends JavaPlugin { /** * Purge the Worlds of Entities that are disallowed. */ - private void purgeWorlds() { + public void purgeWorlds() { if (this.worlds.size() <= 0) return; @@ -236,40 +236,41 @@ public class MultiverseCore extends JavaPlugin { List animals = mvworld.getAnimalList(); System.out.print("Monster Size:" + monsters.size() + " - " + "Animal Size: " + animals.size()); for (Entity e : world.getEntities()) { + String creatureName = e.toString().replaceAll("Craft", "").toLowerCase(); // Check against Monsters - if (e instanceof Creeper || e instanceof Skeleton || e instanceof Spider || e instanceof Zombie || e instanceof Ghast || e instanceof PigZombie || e instanceof Giant || e instanceof Slime || e instanceof Monster) { + if (e instanceof Slime || e instanceof Monster) { // If Monsters are disabled and there's no exceptions we can simply remove them. - if (mvworld.hasMonsters() == false && !(monsters.size() > 0)) { + if (mvworld.allowMonsterSpawning() == false && !(monsters.size() > 0)) { e.remove(); continue; } // If monsters are enabled and there's no exceptions we can continue to the next set. - if (mvworld.hasMonsters() == true && !(monsters.size() > 0)) { + if (mvworld.allowMonsterSpawning() == true && !(monsters.size() > 0)) { continue; } - String creature = e.toString().replaceAll("Craft", ""); - if (monsters.contains(creature.toUpperCase())) { - if (mvworld.hasMonsters()) { - System.out.print(creature + " - Removed"); + + if (monsters.contains(creatureName.toUpperCase())) { + if (mvworld.allowMonsterSpawning()) { + System.out.print(creatureName + " - Removed"); e.remove(); continue; } } } // Check against Animals - if (e instanceof Chicken || e instanceof Cow || e instanceof Sheep || e instanceof Pig || e instanceof Squid || e instanceof Animals) { - // If Monsters are disabled and there's no exceptions we can simply remove them. - if (mvworld.hasAnimals() == false && !(animals.size() > 0)) { + if (e instanceof Squid || e instanceof Animals) { + // If Animals are disabled and there's no exceptions we can simply remove them. + if (mvworld.allowAnimalSpawning() == false && !(animals.size() > 0)) { e.remove(); continue; } - // If monsters are enabled and there's no exceptions we can continue to the next set. - if (mvworld.hasAnimals() == true && !(animals.size() > 0)) { + // If Animals are enabled and there's no exceptions we can continue to the next set. + if (mvworld.allowAnimalSpawning() == true && !(animals.size() > 0)) { continue; } - String creature = e.toString().replaceAll("Craft", ""); - if (animals.contains(creature.toUpperCase())) { - if (mvworld.hasAnimals()) { + if (animals.contains(creatureName.toUpperCase())) { + if (mvworld.allowAnimalSpawning()) { + System.out.print(creatureName + " - Removed"); e.remove(); continue; } @@ -301,6 +302,7 @@ public class MultiverseCore extends JavaPlugin { this.commandManager.addCommand(new ReloadCommand(this)); this.commandManager.addCommand(new ModifyCommand(this)); this.commandManager.addCommand(new EnvironmentCommand(this)); + this.commandManager.addCommand(new PurgeCommand(this)); } /** diff --git a/src/com/onarandombox/MultiverseCore/command/commands/PurgeCommand.java b/src/com/onarandombox/MultiverseCore/command/commands/PurgeCommand.java index 43e677e2..3f25c0de 100644 --- a/src/com/onarandombox/MultiverseCore/command/commands/PurgeCommand.java +++ b/src/com/onarandombox/MultiverseCore/command/commands/PurgeCommand.java @@ -36,14 +36,8 @@ public class PurgeCommand extends BaseCommand { sender.sendMessage(this.usage); return; } - - List creatures = new ArrayList(); - - for (String creature : args[0].toUpperCase().split(",")) { - creatures.add(creature); - } - - new PurgeWorlds(plugin).purge(sender, p.getWorld(), creatures); + System.out.println("Purged"); + this.plugin.purgeWorlds(); return; } diff --git a/src/com/onarandombox/utils/PurgeWorlds.java b/src/com/onarandombox/utils/PurgeWorlds.java index bea3deb3..ac06418f 100644 --- a/src/com/onarandombox/utils/PurgeWorlds.java +++ b/src/com/onarandombox/utils/PurgeWorlds.java @@ -1,15 +1,18 @@ package com.onarandombox.utils; import java.util.List; +import java.util.Set; import org.bukkit.World; import org.bukkit.command.CommandSender; +import org.bukkit.entity.Animals; import org.bukkit.entity.Chicken; import org.bukkit.entity.Cow; import org.bukkit.entity.Creeper; import org.bukkit.entity.Entity; import org.bukkit.entity.Ghast; import org.bukkit.entity.Giant; +import org.bukkit.entity.Monster; import org.bukkit.entity.Pig; import org.bukkit.entity.PigZombie; import org.bukkit.entity.Sheep; @@ -19,25 +22,26 @@ import org.bukkit.entity.Spider; import org.bukkit.entity.Squid; import org.bukkit.entity.Zombie; +import com.onarandombox.MultiverseCore.MVWorld; import com.onarandombox.MultiverseCore.MultiverseCore; public class PurgeWorlds { - + MultiverseCore plugin; - + public PurgeWorlds(MultiverseCore plugin) { this.plugin = plugin; } - + public void purge(World w, List creatures) { purge(null, w, creatures); } - + public void purge(CommandSender sender, World w, List creatures) { - + List entities = w.getEntities(); int count = 0; - + for (Entity e : entities) { if ((((e instanceof Creeper)) && (creatures.contains("CREEPER"))) || (((e instanceof Skeleton)) && (creatures.contains("SKELETON"))) || (((e instanceof Spider)) && (creatures.contains("SPIDER"))) || (((e instanceof Zombie)) && (creatures.contains("ZOMBIE"))) || (((e instanceof Ghast)) && (creatures.contains("GHAST"))) || (((e instanceof PigZombie)) && (creatures.contains("PIGZOMBIE"))) @@ -48,10 +52,62 @@ public class PurgeWorlds { count++; } } - + if (sender != null) { sender.sendMessage(count + " Entities Purged from " + w.getName()); } } - + + /** + * Synchronizes the given world with it's settings + */ + public void purgeWorlds(MVWorld world) { + + } + + public void purgeWorlds(CommandSender sender, List worlds, List whatToKill) { + if (worlds.isEmpty()) + return; + + for (MVWorld mvworld : worlds) { + World world = this.plugin.getServer().getWorld(mvworld.getName()); + if (world == null) + continue; + List monsters = mvworld.getMonsterList(); + List animals = mvworld.getAnimalList(); + System.out.print("Monster Size:" + monsters.size() + " - " + "Animal Size: " + animals.size()); + for (Entity e : world.getEntities()) { + String creatureName = e.toString().replaceAll("Craft", "").toLowerCase(); + // Check against Monsters + killMonster(mvworld, e, creatureName); + // Check against Animals + killCreature(mvworld, e, creatureName); + } + } + } + + private boolean killCreature(MVWorld mvworld, Entity e, String creatureName) { + String entityName = e.toString().replaceAll("Craft", "").toUpperCase(); + if (e instanceof Squid || e instanceof Animals) { + if (entityName.contains(creatureName.toUpperCase())) { + System.out.print(creatureName + " - Removed"); + e.remove(); + return true; + } + } + return false; + } + + private boolean killMonster(MVWorld mvworld, Entity e, String creatureName) { + String entityName = e.toString().replaceAll("Craft", "").toUpperCase(); + if (e instanceof Slime || e instanceof Monster) { + if (entityName.contains(creatureName.toUpperCase())) { + System.out.print(creatureName + " - Removed"); + e.remove(); + return true; + } + } + return false; + } + }