From 97a07b0128f5e11bb016d9495fac77f3e2e602ec Mon Sep 17 00:00:00 2001 From: Grafe Date: Fri, 11 Jan 2013 02:41:05 +0100 Subject: [PATCH] Fixed listeners do not reload, fixed item drops do not have item meta, fixed data leak on reload --- src/com/dre/dungeonsxl/DMobType.java | 186 ++++++++++-------- src/com/dre/dungeonsxl/DungeonsXL.java | 29 ++- src/com/dre/dungeonsxl/game/DMob.java | 74 +++---- .../dungeonsxl/listener/EntityListener.java | 27 +-- 4 files changed, 152 insertions(+), 164 deletions(-) diff --git a/src/com/dre/dungeonsxl/DMobType.java b/src/com/dre/dungeonsxl/DMobType.java index 752c6e07..a820d445 100644 --- a/src/com/dre/dungeonsxl/DMobType.java +++ b/src/com/dre/dungeonsxl/DMobType.java @@ -1,10 +1,12 @@ package com.dre.dungeonsxl; import java.io.File; +import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; +import java.util.logging.Level; import net.minecraft.server.v1_4_6.Item; @@ -24,6 +26,7 @@ import org.bukkit.entity.Ocelot; import org.bukkit.entity.Skeleton; import org.bukkit.entity.Skeleton.SkeletonType; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; import com.dre.dungeonsxl.game.DMob; import com.dre.dungeonsxl.game.GameWorld; @@ -55,7 +58,6 @@ public class DMobType { this.name=name; this.type=type; - } public void spawn(GameWorld gWorld, Location loc){ @@ -139,96 +141,108 @@ public class DMobType { //Read Mobs for(String mobName:configFile.getKeys(false)){ EntityType type=EntityType.fromName(configFile.getString(mobName+".Type")); - - DMobType mobType=new DMobType(mobName, type); - - //Load MaxHealth - if(configFile.contains(mobName+".MaxHealth")){ - mobType.maxHealth=configFile.getInt(mobName+".MaxHealth"); - } - - //Load Items - if(configFile.contains(mobName+".ItemHelmet")){ - mobType.ItemHelmet=CraftItemStack.asNMSCopy(new ItemStack(configFile.getInt(mobName+".ItemHelmet"))).getItem(); - } - - if(configFile.contains(mobName+".ItemChestplate")){ - mobType.ItemChestplate=CraftItemStack.asNMSCopy(new ItemStack(configFile.getInt(mobName+".ItemChestplate"))).getItem(); - } - - if(configFile.contains(mobName+".ItemBoots")){ - mobType.ItemBoots=CraftItemStack.asNMSCopy(new ItemStack(configFile.getInt(mobName+".ItemBoots"))).getItem(); - } - - if(configFile.contains(mobName+".ItemLeggings")){ - mobType.ItemLeggings=CraftItemStack.asNMSCopy(new ItemStack(configFile.getInt(mobName+".ItemLeggings"))).getItem(); - } - - if(configFile.contains(mobName+".ItemHand")){ - mobType.ItemHand=CraftItemStack.asNMSCopy(new ItemStack(configFile.getInt(mobName+".ItemHand"))).getItem(); - } - //Load different Mob options - if(configFile.contains(mobName+".isWitherSkeleton")){ - mobType.isWitherSkeleton = configFile.getBoolean(mobName+".isWitherSkeleton"); - } - - if(configFile.contains(mobName+".ocelotType")){ - mobType.ocelotType = configFile.getString(mobName+".ocelotType"); - } - - //Drops - ConfigurationSection configSetion = configFile.getConfigurationSection(mobName+".drops"); - if(configSetion!=null){ - Set list=configSetion.getKeys(false); - for(String dropPath:list){ - ItemStack item = null; - int chance = 100; - - /* Item Stack */ - Material mat = Material.getMaterial(configSetion.getInt(dropPath+".id")); - int amount = 1; - short data = 0; - - if(configSetion.contains(dropPath+".amount")){ - amount = configSetion.getInt(dropPath+".amount"); - } - if(configSetion.contains(dropPath+".data")){ - data = Short.parseShort(configSetion.getString(dropPath+".data")); - } - - item = new ItemStack(mat,amount,data); - - /* Enchantments */ - if (configSetion.contains(dropPath+".enchantments")) { - for(String enchantment:configSetion.getStringList(dropPath+".enchantments")){ - String[] splittedEnchantment = enchantment.split(" "); - if (splittedEnchantment.length>1) { - item.getItemMeta().addEnchant(Enchantment.getByName(splittedEnchantment[0]), Integer.parseInt(splittedEnchantment[1]), true); - } else { - item.getItemMeta().addEnchant(Enchantment.getByName(splittedEnchantment[0]), 1, true); + if (type!=null) { + DMobType mobType=new DMobType(mobName, type); + + //Load MaxHealth + if(configFile.contains(mobName+".MaxHealth")){ + mobType.maxHealth=configFile.getInt(mobName+".MaxHealth"); + } + + //Load Items + if(configFile.contains(mobName+".ItemHelmet")){ + mobType.ItemHelmet=CraftItemStack.asNMSCopy(new ItemStack(configFile.getInt(mobName+".ItemHelmet"))).getItem(); + } + + if(configFile.contains(mobName+".ItemChestplate")){ + mobType.ItemChestplate=CraftItemStack.asNMSCopy(new ItemStack(configFile.getInt(mobName+".ItemChestplate"))).getItem(); + } + + if(configFile.contains(mobName+".ItemBoots")){ + mobType.ItemBoots=CraftItemStack.asNMSCopy(new ItemStack(configFile.getInt(mobName+".ItemBoots"))).getItem(); + } + + if(configFile.contains(mobName+".ItemLeggings")){ + mobType.ItemLeggings=CraftItemStack.asNMSCopy(new ItemStack(configFile.getInt(mobName+".ItemLeggings"))).getItem(); + } + + if(configFile.contains(mobName+".ItemHand")){ + mobType.ItemHand=CraftItemStack.asNMSCopy(new ItemStack(configFile.getInt(mobName+".ItemHand"))).getItem(); + } + + //Load different Mob options + if(configFile.contains(mobName+".isWitherSkeleton")){ + mobType.isWitherSkeleton = configFile.getBoolean(mobName+".isWitherSkeleton"); + } + + if(configFile.contains(mobName+".ocelotType")){ + mobType.ocelotType = configFile.getString(mobName+".ocelotType"); + } + + //Drops + ConfigurationSection configSetion = configFile.getConfigurationSection(mobName+".drops"); + if(configSetion!=null){ + Set list=configSetion.getKeys(false); + for(String dropPath:list){ + ItemStack item = null; + ItemMeta itemMeta = null; + int chance = 100; + + /* Item Stack */ + Material mat = Material.getMaterial(configSetion.getInt(dropPath+".id")); + int amount = 1; + short data = 0; + + if(configSetion.contains(dropPath+".amount")){ + amount = configSetion.getInt(dropPath+".amount"); + } + if(configSetion.contains(dropPath+".data")){ + data = Short.parseShort(configSetion.getString(dropPath+".data")); + } + + item = new ItemStack(mat,amount,data); + itemMeta = item.getItemMeta(); + + /* Enchantments */ + if (configSetion.contains(dropPath+".enchantments")) { + for(String enchantment:configSetion.getStringList(dropPath+".enchantments")){ + String[] splittedEnchantment = enchantment.split(" "); + if(Enchantment.getByName(splittedEnchantment[0].toUpperCase())!=null){ + if (splittedEnchantment.length>1) { + itemMeta.addEnchant(Enchantment.getByName(splittedEnchantment[0].toUpperCase()), Integer.parseInt(splittedEnchantment[1]), true); + } else { + itemMeta.addEnchant(Enchantment.getByName(splittedEnchantment[0].toUpperCase()), 1, true); + } + } else { + DungeonsXL.p.log(Level.WARNING,"Error at loading mob.yml: Enchantmet '"+splittedEnchantment[0]+"' doesn't exist!"); + } } } + + /* Item Name */ + if (configSetion.contains(dropPath+".name")) { + itemMeta.setDisplayName(configSetion.getString(dropPath+".name")); + } + + /* Item Lore */ + if (configSetion.contains(dropPath+".lore")) { + String[] lore=configSetion.getString(dropPath+".lore").split("//"); + itemMeta.setLore(Arrays.asList(lore)); + } + + /* Drop chance */ + if (configSetion.contains(dropPath+".chance")) { + chance = configSetion.getInt(dropPath+".chance"); + } + + /* Add Item to the drops map */ + item.setItemMeta(itemMeta); + mobType.drops.put(item, chance); } - - /* Item Name */ - if (configSetion.contains(dropPath+".name")) { - item.getItemMeta().setDisplayName(configSetion.getString(dropPath+".name")); - } - - /* Item Lore */ - if (configSetion.contains(dropPath+".lore")) { - item.getItemMeta().setDisplayName(configSetion.getString(dropPath+".lore")); - } - - /* Drop chance */ - if (configSetion.contains(dropPath+".chance")) { - chance = configSetion.getInt(dropPath+".chance"); - } - - /* Add Item to the drops map */ - mobType.drops.put(item, chance); } + } else { + DungeonsXL.p.log(Level.WARNING,"Error at loading mob.yml: Mob '"+configFile.getString(mobName+".Type")+"' doesn't exist!"); } } } diff --git a/src/com/dre/dungeonsxl/DungeonsXL.java b/src/com/dre/dungeonsxl/DungeonsXL.java index 1d6e800a..c97f8793 100644 --- a/src/com/dre/dungeonsxl/DungeonsXL.java +++ b/src/com/dre/dungeonsxl/DungeonsXL.java @@ -18,6 +18,7 @@ import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; +import org.bukkit.event.HandlerList; import org.bukkit.event.Listener; import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.java.JavaPlugin; @@ -64,13 +65,13 @@ public class DungeonsXL extends JavaPlugin{ //Init Classes new DCommandRoot(); - + //InitFolders this.initFolders(); - + //Setup Permissions this.setupPermissions(); - + //Listener entitylistener=new EntityListener(); playerlistener=new PlayerListener(); @@ -92,7 +93,6 @@ public class DungeonsXL extends JavaPlugin{ // -------------------------------------------- // // Update Sheduler // -------------------------------------------- // - p.getServer().getScheduler().scheduleSyncRepeatingTask(p, new Runnable() { public void run() { for(GameWorld gworld:GameWorld.gworlds){ @@ -104,7 +104,6 @@ public class DungeonsXL extends JavaPlugin{ } for(EditWorld eworld:EditWorld.eworlds){ if(eworld.world.getPlayers().isEmpty()){ - eworld.delete(); } } @@ -169,14 +168,28 @@ public class DungeonsXL extends JavaPlugin{ } //Delete all Data - DPortal.portals.clear(); + DGroup.dgroups.clear(); DGSign.dgsigns.clear(); - LeaveSign.lsigns.clear(); + DLootInventory.LootInventorys.clear(); DMobType.clear(); - + DOfflinePlayer.players.clear(); + DPlayer.players.clear(); + DPortal.portals.clear(); + LeaveSign.lsigns.clear(); + DCommandRoot.root.commands.clear(); + GameCheckpoint.gcheckpoints.clear(); + GameMessage.gmessages.clear(); + MobSpawner.mobspawners.clear(); + //Delete Worlds GameWorld.deleteAll(); EditWorld.deleteAll(); + + //Disable listeners + HandlerList.unregisterAll(p); + + //Stop shedulers + p.getServer().getScheduler().cancelTasks(this); } diff --git a/src/com/dre/dungeonsxl/game/DMob.java b/src/com/dre/dungeonsxl/game/DMob.java index 5e85e049..035bf6ee 100644 --- a/src/com/dre/dungeonsxl/game/DMob.java +++ b/src/com/dre/dungeonsxl/game/DMob.java @@ -3,9 +3,6 @@ package com.dre.dungeonsxl.game; import java.util.Random; import org.bukkit.entity.LivingEntity; -import org.bukkit.entity.Monster; -import org.bukkit.event.entity.EntityDamageByEntityEvent; -import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.entity.EntityDeathEvent; import org.bukkit.inventory.ItemStack; @@ -21,65 +18,38 @@ public class DMob { public DMob(LivingEntity entity, int live, GameWorld gworld, DMobType type){ gworld.dmobs.add(this); - this.entity=entity; - this.live=live; + this.entity = entity; + this.live = live; this.type = type; - } - - //Statics - public static void onDamage(EntityDamageEvent event){ - if(event.getEntity() instanceof LivingEntity){ - LivingEntity victim=(LivingEntity) event.getEntity(); - - GameWorld gworld=GameWorld.get(victim.getWorld()); - - if(gworld!=null){ - for(DMob dmob:gworld.dmobs){ - if(dmob.entity==victim){ - dmob.live=dmob.live-event.getDamage(); - dmob.entity.damage(1); - - - dmob.entity.setHealth(dmob.entity.getMaxHealth()); - - if(event instanceof EntityDamageByEntityEvent){ - EntityDamageByEntityEvent eByEEvent=(EntityDamageByEntityEvent) event; - if(dmob.entity instanceof Monster && eByEEvent.getDamager() instanceof LivingEntity){ - Monster mob=(Monster)dmob.entity; - mob.setTarget((LivingEntity) eByEEvent.getDamager()); - } - } - - - if(dmob.live<=0){ - dmob.entity.damage(dmob.entity.getMaxHealth()); - gworld.dmobs.remove(dmob); - } - event.setCancelled(true); - } - } - } + + /* Max Health */ + if(live>0){ + this.entity.setMaxHealth(live); + this.entity.setHealth(live); } } - public static void onDeath(EntityDeathEvent event){ - if(event.getEntity() instanceof LivingEntity){ - LivingEntity victim=(LivingEntity) event.getEntity(); + //Statics + public static void onDeath (EntityDeathEvent event) { + if (event.getEntity() instanceof LivingEntity) { + LivingEntity victim = (LivingEntity) event.getEntity(); + GameWorld gworld = GameWorld.get(victim.getWorld()); - GameWorld gworld=GameWorld.get(victim.getWorld()); - - if(gworld!=null){ - for(DMob dmob:gworld.dmobs){ - if(dmob.entity==victim){ - if(dmob.type!=null){ - for(ItemStack item:dmob.type.getDrops().keySet()){ + if (gworld!=null) { + for (DMob dmob:gworld.dmobs) { + if (dmob.entity == victim) { + if (dmob.type != null) { + for (ItemStack item:dmob.type.getDrops().keySet()) { Random randomGenerator = new Random(); - int random = randomGenerator.nextInt(101); - if(dmob.type.getDrops().get(item)<=random){ + int random = randomGenerator.nextInt(100); + + if (dmob.type.getDrops().get(item)>random) { event.getDrops().add(item); } } } + + gworld.dmobs.remove(dmob); return; } } diff --git a/src/com/dre/dungeonsxl/listener/EntityListener.java b/src/com/dre/dungeonsxl/listener/EntityListener.java index da5037e7..a4eb7fba 100644 --- a/src/com/dre/dungeonsxl/listener/EntityListener.java +++ b/src/com/dre/dungeonsxl/listener/EntityListener.java @@ -61,14 +61,13 @@ public class EntityListener implements Listener{ @EventHandler(priority = EventPriority.HIGH) public void onEntityDeath(EntityDeathEvent event){ World world=event.getEntity().getWorld(); - //Deny all drops from mobs - if (event.getEntity() instanceof LivingEntity) - { + + if (event.getEntity() instanceof LivingEntity) { LivingEntity entity = (LivingEntity) event.getEntity(); - GameWorld gworld=GameWorld.get(world); + GameWorld gworld = GameWorld.get(world); if(gworld!=null){ if(gworld.isPlaying){ - if(entity.getType()!=EntityType.PLAYER){ + if(entity.getType() != EntityType.PLAYER){ event.getDrops().clear(); DMob.onDeath(event); } @@ -90,9 +89,6 @@ public class EntityListener implements Listener{ if (event instanceof EntityDamageByEntityEvent) { EntityDamageByEntityEvent sub = (EntityDamageByEntityEvent)event; - - DMob.onDamage(sub); - Entity entity = sub.getDamager(); Entity entity2 = sub.getEntity(); @@ -100,7 +96,7 @@ public class EntityListener implements Listener{ { entity = ((Projectile) entity).getShooter(); } - + if(entity instanceof Player && entity2 instanceof Player) { event.setCancelled(true); @@ -138,7 +134,6 @@ public class EntityListener implements Listener{ } } } - } } } @@ -159,11 +154,9 @@ public class EntityListener implements Listener{ // Zombie/skeleton combustion from the sun. @EventHandler(priority = EventPriority.HIGH) - public void onEntityCombust(EntityCombustEvent event) - { + public void onEntityCombust (EntityCombustEvent event) { GameWorld gworld = GameWorld.get(event.getEntity().getWorld()); - if(gworld != null) - { + if (gworld != null) { event.setCancelled(true); } } @@ -181,9 +174,9 @@ public class EntityListener implements Listener{ //Explosions @EventHandler - public void onEntityExplode(EntityExplodeEvent event) - { + public void onEntityExplode (EntityExplodeEvent event) { GameWorld gworld=GameWorld.get(event.getEntity().getWorld()); + if(gworld!=null){ if(event.getEntity() instanceof LivingEntity){ //Disable Creeper explosions in gameworlds @@ -217,6 +210,4 @@ public class EntityListener implements Listener{ } } } - - }