From 05773281d93b826d00990011cb0d927ff71e709c Mon Sep 17 00:00:00 2001 From: Grafe Date: Fri, 11 Jan 2013 00:54:54 +0100 Subject: [PATCH] Added ability to customize the mob-drops --- src/com/dre/dungeonsxl/DMobType.java | 142 ++++++++++++------ src/com/dre/dungeonsxl/DungeonsXL.java | 1 + src/com/dre/dungeonsxl/game/DMob.java | 35 ++++- src/com/dre/dungeonsxl/game/MobSpawner.java | 2 +- .../dungeonsxl/listener/EntityListener.java | 1 + 5 files changed, 131 insertions(+), 50 deletions(-) diff --git a/src/com/dre/dungeonsxl/DMobType.java b/src/com/dre/dungeonsxl/DMobType.java index fbc2bc5f..752c6e07 100644 --- a/src/com/dre/dungeonsxl/DMobType.java +++ b/src/com/dre/dungeonsxl/DMobType.java @@ -10,12 +10,14 @@ import net.minecraft.server.v1_4_6.Item; import org.bukkit.Location; import org.bukkit.Material; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.craftbukkit.v1_4_6.entity.CraftPigZombie; import org.bukkit.craftbukkit.v1_4_6.entity.CraftSkeleton; import org.bukkit.craftbukkit.v1_4_6.entity.CraftZombie; import org.bukkit.craftbukkit.v1_4_6.inventory.CraftItemStack; +import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.EntityType; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Ocelot; @@ -41,6 +43,7 @@ public class DMobType { private Item ItemBoots; private Map drops = new HashMap(); + public Map getDrops() { return this.drops; } /* Extra Values for different Mob Types */ private boolean isWitherSkeleton = false; @@ -123,76 +126,119 @@ public class DMobType { } /* Spawn Mob */ - new DMob(entity, maxHealth, gWorld); + new DMob(entity, maxHealth, gWorld, this); } } } - + //Load Config public static void load(File file){ FileConfiguration configFile = YamlConfiguration.loadConfiguration(file); - DungeonsXL.p.log(configFile.getKeys(true)); - //Read Mobs - for(String mobName:configFile.getKeys(true)){ - if(!mobName.contains(".")){ - DungeonsXL.p.log("Test"+mobName); + for(String mobName:configFile.getKeys(false)){ + EntityType type=EntityType.fromName(configFile.getString(mobName+".Type")); - EntityType type=EntityType.fromName(configFile.getString(mobName+".Type")); + DMobType mobType=new DMobType(mobName, type); - DMobType mobType=new DMobType(mobName, type); + //Load MaxHealth + if(configFile.contains(mobName+".MaxHealth")){ + mobType.maxHealth=configFile.getInt(mobName+".MaxHealth"); + } - //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(); + } - //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+".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+".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+".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 - Set list=configFile.getKeys(true); + 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){ - if(dropPath.contains(mobName+"drops.")){ - //TODO Add Drops funtionality - + 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); + } + } + } + + /* 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); } - } } - } - + + //Clear + public static void clear(){ + mobTypes.clear(); + } + + //Get public static DMobType get(String name){ for(DMobType mobType:DMobType.mobTypes){ diff --git a/src/com/dre/dungeonsxl/DungeonsXL.java b/src/com/dre/dungeonsxl/DungeonsXL.java index 2cd093c0..1d6e800a 100644 --- a/src/com/dre/dungeonsxl/DungeonsXL.java +++ b/src/com/dre/dungeonsxl/DungeonsXL.java @@ -172,6 +172,7 @@ public class DungeonsXL extends JavaPlugin{ DPortal.portals.clear(); DGSign.dgsigns.clear(); LeaveSign.lsigns.clear(); + DMobType.clear(); //Delete Worlds GameWorld.deleteAll(); diff --git a/src/com/dre/dungeonsxl/game/DMob.java b/src/com/dre/dungeonsxl/game/DMob.java index 6cb0658e..5e85e049 100644 --- a/src/com/dre/dungeonsxl/game/DMob.java +++ b/src/com/dre/dungeonsxl/game/DMob.java @@ -1,21 +1,29 @@ 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; + +import com.dre.dungeonsxl.DMobType; public class DMob { //Variables public LivingEntity entity; + public DMobType type; public int live; - public DMob(LivingEntity entity, int live, GameWorld gworld){ + public DMob(LivingEntity entity, int live, GameWorld gworld, DMobType type){ gworld.dmobs.add(this); this.entity=entity; this.live=live; + this.type = type; } //Statics @@ -53,4 +61,29 @@ public class DMob { } } } + + public static void onDeath(EntityDeathEvent 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){ + 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){ + event.getDrops().add(item); + } + } + } + return; + } + } + } + } + } } diff --git a/src/com/dre/dungeonsxl/game/MobSpawner.java b/src/com/dre/dungeonsxl/game/MobSpawner.java index 4aa3f4ee..fb915270 100644 --- a/src/com/dre/dungeonsxl/game/MobSpawner.java +++ b/src/com/dre/dungeonsxl/game/MobSpawner.java @@ -47,7 +47,7 @@ public class MobSpawner { if(EntityType.fromName(this.mob).isAlive()){ LivingEntity entity=(LivingEntity)world.spawnEntity(this.block.getLocation(), EntityType.fromName(this.mob)); if(this.live>0){ - new DMob(entity,this.live,GameWorld.get(world)); + new DMob(entity,this.live,GameWorld.get(world),null); } } } diff --git a/src/com/dre/dungeonsxl/listener/EntityListener.java b/src/com/dre/dungeonsxl/listener/EntityListener.java index e448e614..da5037e7 100644 --- a/src/com/dre/dungeonsxl/listener/EntityListener.java +++ b/src/com/dre/dungeonsxl/listener/EntityListener.java @@ -70,6 +70,7 @@ public class EntityListener implements Listener{ if(gworld.isPlaying){ if(entity.getType()!=EntityType.PLAYER){ event.getDrops().clear(); + DMob.onDeath(event); } } }