From ac7468db16dfdda90a6e838429b31796445c53a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leon=20Gr=C3=BCnewald?= Date: Tue, 25 Dec 2018 21:35:26 +0100 Subject: [PATCH] Commit plugin beta --- .classpath | 7 + .gitignore | 1 + .project | 17 ++ config.yml | 199 ++++++++++++++++++ plugin.yml | 5 + .../HealthContainer/HCConfigLoader.java | 136 ++++++++++++ .../HealthContainer/HCController.java | 167 +++++++++++++++ .../HealthContainer/HCEntityConfig.java | 32 +++ .../dhalucario/HealthContainer/HCItem.java | 27 +++ .../HealthContainer/HealthType.java | 15 ++ src/com/dhalucario/HealthContainer/Main.java | 25 +++ 11 files changed, 631 insertions(+) create mode 100644 .classpath create mode 100644 .project create mode 100644 config.yml create mode 100644 plugin.yml create mode 100644 src/com/dhalucario/HealthContainer/HCConfigLoader.java create mode 100644 src/com/dhalucario/HealthContainer/HCController.java create mode 100644 src/com/dhalucario/HealthContainer/HCEntityConfig.java create mode 100644 src/com/dhalucario/HealthContainer/HCItem.java create mode 100644 src/com/dhalucario/HealthContainer/HealthType.java create mode 100644 src/com/dhalucario/HealthContainer/Main.java diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..684bae5 --- /dev/null +++ b/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/.gitignore b/.gitignore index d3ffd6b..027669e 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,4 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* +.settings \ No newline at end of file diff --git a/.project b/.project new file mode 100644 index 0000000..abf3e89 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + ZeldaHeartsReborn + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/config.yml b/config.yml new file mode 100644 index 0000000..4dba6b2 --- /dev/null +++ b/config.yml @@ -0,0 +1,199 @@ +start_health: 3.0 +max_health: 30.0 +drop_config: + ELDER_GUARDIAN: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + WITHER_SKELETON: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + STRAY: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + HUSK: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + ZOMBIE_VILLAGER: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + SKELETON_HORSE: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + ZOMBIE_HORSE: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + DONKEY: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + MULE: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + EVOKER: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + VEX: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + VINDICATOR: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + ILLUSIONER: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + CREEPER: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + SKELETON: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + SPIDER: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + ZOMBIE: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + SLIME: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + GHAST: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + PIG_ZOMBIE: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + ENDERMAN: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + CAVE_SPIDER: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + SILVERFISH: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + BLAZE: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + MAGMA_CUBE: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + ENDER_DRAGON: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + WITHER: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + BAT: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + WITCH: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + ENDERMITE: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + GUARDIAN: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + SHULKER: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + PIG: + health_type: "DROP_VAMPIRE" + health_value: 0.5 + vampire_multiplicator: 0.5 + SHEEP: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + COW: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + CHICKEN: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + SQUID: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + WOLF: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + MUSHROOM_COW: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + SNOWMAN: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + OCELOT: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + IRON_GOLEM: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + HORSE: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + RABBIT: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + POLAR_BEAR: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + LLAMA: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + PARROT: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + VILLAGER: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 + PLAYER: + health_type: "DIRECT_VALUE" + health_value: 0.5 + vampire_multiplicator: 0.5 \ No newline at end of file diff --git a/plugin.yml b/plugin.yml new file mode 100644 index 0000000..a517443 --- /dev/null +++ b/plugin.yml @@ -0,0 +1,5 @@ +name: HealthContainerPlugin +main: com.dhalucario.HealthContainer.Main +version: 1.0 +api-version: 1.13 +commands: \ No newline at end of file diff --git a/src/com/dhalucario/HealthContainer/HCConfigLoader.java b/src/com/dhalucario/HealthContainer/HCConfigLoader.java new file mode 100644 index 0000000..eac114c --- /dev/null +++ b/src/com/dhalucario/HealthContainer/HCConfigLoader.java @@ -0,0 +1,136 @@ +package com.dhalucario.HealthContainer; + +import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.configuration.MemorySection; +import org.bukkit.entity.*; + +public class HCConfigLoader { + + JavaPlugin hcPlugin; + + public HCConfigLoader(JavaPlugin plugin) { + hcPlugin = plugin; + } + + public void loadDefaults() { + + hcPlugin.getConfig().options().copyDefaults(true); + + hcPlugin.getConfig().addDefault("start_health", 3.0); + hcPlugin.getConfig().addDefault("max_health", 30.0); + hcPlugin.getConfig().addDefault("spawning_resets_health", true); + + for (EntityType entType : EntityType.values()) { + + /* + * drop_value: Drops a heart which contains the value specified using health_value + * drop_vampire: Drops a heart which contains the (health of the target * vampire_multiplicator) + * direct_value: Adds the value specified in health_value directly to the max health + * direct_vampire: Adds the value of (health of the target * vampire_multiplicator) directly to the max health + */ + hcPlugin.getConfig().addDefault("drop_config." + entType.name() + ".health_type", "DIRECT_VALUE"); + hcPlugin.getConfig().addDefault("drop_config." + entType.name() + ".health_value", 0.5); + hcPlugin.getConfig().addDefault("drop_config." + entType.name() + ".vampire_multiplicator", 0.5); + } + + hcPlugin.saveDefaultConfig(); + } + + public HCEntityConfig getConfigForEntity(EntityType target) { + + HCEntityConfig entityConfig = new HCEntityConfig(); + MemorySection dropConfig = (MemorySection) hcPlugin.getConfig().get("drop_config"); + MemorySection damageEntityConfig = (MemorySection) dropConfig.get(target.name()); + + if (damageEntityConfig != null) { + if (damageEntityConfig.isString("health_type")) { + + switch (damageEntityConfig.getString("health_type")) { + case "DROP_VALUE": + entityConfig.setHealthType(HealthType.DROP_VALUE); + break; + case "DROP_VAMPIRE": + entityConfig.setHealthType(HealthType.DROP_VAMPIRE); + break; + case "DIRECT_VALUE": + entityConfig.setHealthType(HealthType.DIRECT_VALUE); + break; + case "DIRECT_VAMPIRE": + entityConfig.setHealthType(HealthType.DIRECT_VAMPIRE); + break; + default: + hcPlugin.getLogger().warning("\"health_type\" for entity " + target.name() + " is invalid type"); + hcPlugin.getLogger().warning("Falling back to default (0.5)"); + entityConfig.setHealthType(HealthType.DROP_VALUE); + break; + } + } else { + entityConfig.setVampireMultiplicator(0.5); + hcPlugin.getLogger().warning("\"health_type\" for entity " + target.name() + " is invalid primitive"); + hcPlugin.getLogger().warning("Falling back to default (0.5)"); + } + + if (damageEntityConfig.isDouble("health_value")) { + entityConfig.setHealthValue(damageEntityConfig.getDouble("health_value")); + } else if (damageEntityConfig.isInt("health_value")) { + entityConfig.setHealthValue(damageEntityConfig.getInt("health_value")); + } else { + entityConfig.setHealthValue(damageEntityConfig.getInt("health_value")); + hcPlugin.getLogger().warning("\"health_value\" for entity " + target.name() + " is invalid primitive"); + hcPlugin.getLogger().warning("Falling back to default (0.5)"); + } + + if (damageEntityConfig.isDouble("vampire_multiplicator")) { + entityConfig.setVampireMultiplicator(damageEntityConfig.getDouble("vampire_multiplicator")); + } else if (damageEntityConfig.isInt("vampire_multiplicator")) { + entityConfig.setVampireMultiplicator((double)damageEntityConfig.getInt("vampire_multiplicator")); + } else { + entityConfig.setVampireMultiplicator(0.5); + hcPlugin.getLogger().warning("\"vampire_multiplicator\" for entity " + target.name() + " is invalid primitive"); + hcPlugin.getLogger().warning("Falling back to default (0.5)"); + } + } else { + entityConfig = null; + } + + return entityConfig; + + } + + public boolean playerRespawnResetsHealth() { + boolean respawnResetsHealth = false; + + if (hcPlugin.getConfig().isBoolean("spawning_resets_health")) { + respawnResetsHealth = hcPlugin.getConfig().getBoolean("spawning_resets_health"); + } else { + respawnResetsHealth = true; + hcPlugin.getLogger().warning("\"spawning_resets_health\" is invalid primitive"); + hcPlugin.getLogger().warning("Falling back to default (true)"); + } + + return respawnResetsHealth; + } + + public double getPlayerRespawnHealth() { + + double respawnHealth = 0; + + if (hcPlugin.getConfig().isDouble("start_health")) { + respawnHealth = hcPlugin.getConfig().getDouble("start_health"); + } else if (hcPlugin.getConfig().isInt("start_health")) { + respawnHealth = (double)hcPlugin.getConfig().getInt("start_health"); + } else { + respawnHealth = 30; + hcPlugin.getLogger().warning("\"start_health\" is invalid primitive"); + hcPlugin.getLogger().warning("Falling back to default (true)"); + } + + return respawnHealth; + + } + + public void save() { + hcPlugin.saveDefaultConfig(); + } + +} diff --git a/src/com/dhalucario/HealthContainer/HCController.java b/src/com/dhalucario/HealthContainer/HCController.java new file mode 100644 index 0000000..2bfc6db --- /dev/null +++ b/src/com/dhalucario/HealthContainer/HCController.java @@ -0,0 +1,167 @@ +package com.dhalucario.HealthContainer; + +import java.util.ArrayList; + +import org.bukkit.Material; +import org.bukkit.attribute.Attribute; +import org.bukkit.entity.Entity; +import org.bukkit.entity.LivingEntity; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.event.entity.EntityDamageByEntityEvent; +import org.bukkit.event.entity.EntityPickupItemEvent; +import org.bukkit.event.entity.ItemDespawnEvent; +import org.bukkit.event.player.PlayerRespawnEvent; +import org.bukkit.inventory.ItemStack; + +public class HCController implements Listener { + + JavaPlugin hcPlugin; + HCConfigLoader hcConfigLoader; + ArrayList hcItemList; + + public HCController(JavaPlugin plugin, HCConfigLoader configLoader) { + hcPlugin = plugin; + hcConfigLoader = configLoader; + hcItemList = new ArrayList(); + } + + @EventHandler + public void onPlayerRespawn(PlayerRespawnEvent e) { + if (hcConfigLoader.playerRespawnResetsHealth()) { + e.getPlayer().getAttribute(Attribute.GENERIC_MAX_HEALTH).setBaseValue(hcConfigLoader.getPlayerRespawnHealth()); + } + } + + @EventHandler + public void onDamage(EntityDamageByEntityEvent e) { + + Entity damager = e.getDamager(); + Entity damagee = e.getEntity(); + double damage = e.getDamage(); + + if (damager instanceof Player && damagee instanceof LivingEntity) { + + LivingEntity target = (LivingEntity) damagee; + if ((target.getHealth() - damage) <= 0) { + handleHeartContainer("death", (Player) damager, target); + } + } + } + + @EventHandler + public void onPlayerPickup(EntityPickupItemEvent e) { + //Todo: Check if hearts are picked up + + if (e.getEntity() instanceof Player) { + + Player pickupPlayer = (Player)e.getEntity(); + int eventItemId = e.getItem().getEntityId(); + + for (HCItem hcItem : hcItemList) { + if (eventItemId == hcItem.healthItem.getEntityId()) { + handleHeartContainer("pickup", pickupPlayer, hcItem.healthTarget); + e.getItem().remove(); + e.setCancelled(true); + } + } + + } + } + + public void onItemDespawn(ItemDespawnEvent e) { + + ArrayList tempRemoveList = new ArrayList(); + + for (HCItem hcItem : hcItemList) { + if (hcItem.healthItem.getEntityId() == e.getEntity().getEntityId()) { + tempRemoveList.add(hcItem); + } + } + + for (HCItem tempItem : tempRemoveList) { + hcItemList.remove(tempItem); + } + + } + + void handleHeartContainer(String eventType, Player attacker, LivingEntity target) { + + HCEntityConfig entHealthConfig = hcConfigLoader.getConfigForEntity(target.getType()); + + if (eventType == "death") { + + switch (entHealthConfig.getHealthType()) { + case DROP_VALUE: + handleDamageDrop(attacker, target); + case DROP_VAMPIRE: + handleDamageDrop(attacker, target); + break; + case DIRECT_VALUE: + handleHealthDirectValue(attacker, entHealthConfig); + break; + case DIRECT_VAMPIRE: + handleHealthDirectVampire(attacker, target, entHealthConfig); + break; + default: + break; + } + + } else if (eventType == "pickup") { + switch (entHealthConfig.getHealthType()) { + case DROP_VALUE: + handleDamageDropPickupValue(attacker, entHealthConfig); + break; + case DROP_VAMPIRE: + handleDamageDropPickupVampire(attacker, target, entHealthConfig); + break; + default: + break; + } + } + + } + + + void handleDamageDrop(Player attacker, LivingEntity target) { + ItemStack healthDrop = new ItemStack(Material.RED_ROSE); + + HCItem hcItem = new HCItem(); + hcItem.setHealthTarget(target); + hcItem.setHealthItemStack(target.getWorld().dropItemNaturally(target.getLocation(), healthDrop)); + + hcItemList.add(hcItem); + } + + void handleDamageDropPickupValue(Player attacker, HCEntityConfig healthConfig) { + handleHealthDirectValue(attacker, healthConfig); + } + + void handleDamageDropPickupVampire(Player attacker, LivingEntity target, HCEntityConfig healthConfig) { + handleHealthDirectVampire(attacker, target, healthConfig); + } + + void handleHealthDirectValue(Player attacker, HCEntityConfig healthConfig) { + double attackerHealth = attacker.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue(); + + attacker.getAttribute(Attribute.GENERIC_MAX_HEALTH) + .setBaseValue(attackerHealth + healthConfig.getHealthValue()); + } + + void handleHealthDirectVampire(Player attacker, LivingEntity target, HCEntityConfig entConfig) { + // Todo: Add health directly to the player + + double attackerHealth = attacker.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue(); + HCEntityConfig healthConfig = hcConfigLoader.getConfigForEntity(target.getType()); + attacker.getAttribute(Attribute.GENERIC_MAX_HEALTH) + .setBaseValue(attackerHealth + getTargetVampireHealth(target, healthConfig)); + } + + double getTargetVampireHealth(LivingEntity target, HCEntityConfig healthConfig) { + double targetHealth = target.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue(); + return (targetHealth * healthConfig.getVampireMultiplicator()); + } + +} \ No newline at end of file diff --git a/src/com/dhalucario/HealthContainer/HCEntityConfig.java b/src/com/dhalucario/HealthContainer/HCEntityConfig.java new file mode 100644 index 0000000..9d7682a --- /dev/null +++ b/src/com/dhalucario/HealthContainer/HCEntityConfig.java @@ -0,0 +1,32 @@ +package com.dhalucario.HealthContainer; + +public class HCEntityConfig { + public HealthType healthType; + public double healthValue; + public double vampireMultiplicator; + + public HealthType getHealthType() { + return healthType; + } + + public void setHealthType(HealthType value) { + healthType = value; + } + + public void setHealthValue(double value) { + healthValue = value; + } + + public double getHealthValue() { + return healthValue; + } + + public double getVampireMultiplicator() { + return vampireMultiplicator; + } + + public void setVampireMultiplicator(double value) { + vampireMultiplicator = value; + } + +} diff --git a/src/com/dhalucario/HealthContainer/HCItem.java b/src/com/dhalucario/HealthContainer/HCItem.java new file mode 100644 index 0000000..9497967 --- /dev/null +++ b/src/com/dhalucario/HealthContainer/HCItem.java @@ -0,0 +1,27 @@ +package com.dhalucario.HealthContainer; + +import org.bukkit.entity.Item; +import org.bukkit.entity.LivingEntity; + +public class HCItem { + + public LivingEntity healthTarget; + public Item healthItem; + + public LivingEntity getHealthTarget() { + return healthTarget; + } + + public void setHealthTarget(LivingEntity value) { + healthTarget = value; + } + + public void setHealthItemStack(Item value) { + healthItem = value; + } + + public Item getHealthItemStack() { + return healthItem; + } + +} diff --git a/src/com/dhalucario/HealthContainer/HealthType.java b/src/com/dhalucario/HealthContainer/HealthType.java new file mode 100644 index 0000000..ed9cf89 --- /dev/null +++ b/src/com/dhalucario/HealthContainer/HealthType.java @@ -0,0 +1,15 @@ +package com.dhalucario.HealthContainer; + +/* + * drop_value: Drops a heart which contains the value specified using health_value + * drop_vampire: Drops a heart which contains the (health of the target * vampire_multiplicator) + * direct_value: Adds the value specified in health_value directly to the max health + * direct_vampire: Adds the value of (health of the target * vampire_multiplicator) directly to the max health + */ + +public enum HealthType { + DROP_VALUE, + DROP_VAMPIRE, + DIRECT_VALUE, + DIRECT_VAMPIRE, +} diff --git a/src/com/dhalucario/HealthContainer/Main.java b/src/com/dhalucario/HealthContainer/Main.java new file mode 100644 index 0000000..a164b7b --- /dev/null +++ b/src/com/dhalucario/HealthContainer/Main.java @@ -0,0 +1,25 @@ +package com.dhalucario.HealthContainer; + +import org.bukkit.plugin.java.JavaPlugin; + +public class Main extends JavaPlugin { + + HCConfigLoader hcConfigLoader; + + // Fired when plugin is first enabled + @Override + public void onEnable() { + hcConfigLoader = new HCConfigLoader(this); + hcConfigLoader.loadDefaults(); + getServer().getPluginManager().registerEvents(new HCController(this, hcConfigLoader), this); + } + + // Fired when plugin is disabled + @Override + public void onDisable() { + hcConfigLoader.save(); + hcConfigLoader = null; + } + +} +