From 958c0e66ce654df0a0b634322fedd65d4fcff537 Mon Sep 17 00:00:00 2001 From: Lilac Date: Fri, 30 Aug 2019 20:40:34 +0100 Subject: [PATCH] Added realistic weapon damage. --- .gitlab-ci.yml | 2 +- .../listeners/DeathListeners.java | 52 +++++++++++++++++++ .../utils/settings/Setting.java | 3 ++ 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 9f08153..7d92eea 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -4,7 +4,7 @@ stages: variables: name: "UltimateStacker" path: "/builds/$CI_PROJECT_PATH" - version: "1.9.7" + version: "1.9.8" build: stage: build diff --git a/src/main/java/com/songoda/ultimatestacker/listeners/DeathListeners.java b/src/main/java/com/songoda/ultimatestacker/listeners/DeathListeners.java index 32ab8c6..b31fc4e 100644 --- a/src/main/java/com/songoda/ultimatestacker/listeners/DeathListeners.java +++ b/src/main/java/com/songoda/ultimatestacker/listeners/DeathListeners.java @@ -2,29 +2,37 @@ package com.songoda.ultimatestacker.listeners; import com.songoda.lootables.loot.Drop; import com.songoda.ultimatestacker.UltimateStacker; +import com.songoda.ultimatestacker.entity.EntityStack; import com.songoda.ultimatestacker.utils.DropUtils; import com.songoda.ultimatestacker.utils.ServerVersion; import com.songoda.ultimatestacker.utils.settings.Setting; import org.bukkit.Material; +import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.ChestedHorse; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; +import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.entity.EntityDeathEvent; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.Damageable; +import org.bukkit.inventory.meta.ItemMeta; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Random; public class DeathListeners implements Listener { private final UltimateStacker instance; + private Random random; public DeathListeners(UltimateStacker instance) { this.instance = instance; + random = new Random(); } @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) @@ -96,4 +104,48 @@ public class DeathListeners implements Listener { return false; } } + + @EventHandler + public void onEntityHit(EntityDamageByEntityEvent event) { + if (!(event.getDamager() instanceof Player)) return; + if (!instance.getEntityStackManager().isStacked(event.getEntity())) return; + EntityStack stack = instance.getEntityStackManager().getStack(event.getEntity()); + + if (Setting.KILL_WHOLE_STACK_ON_DEATH.getBoolean() && Setting.REALISTIC_DAMAGE.getBoolean()) { + Player player = (Player) event.getDamager(); + ItemStack tool = player.getInventory().getItemInMainHand(); + if (tool.getType().getMaxDurability() < 1 || (tool.getItemMeta() != null && (tool.getItemMeta().isUnbreakable() + || tool.getItemMeta().spigot().isUnbreakable()))) + return; + + int unbreakingLevel = tool.getEnchantmentLevel(Enchantment.DURABILITY); + Damageable damageable = (Damageable) tool.getItemMeta(); + + int actualDamage = 0; + for (int i = 0; i < stack.getAmount(); i++) + if (checkUnbreakingChance(unbreakingLevel)) + actualDamage++; + + damageable.setDamage(damageable.getDamage() + actualDamage-1); + tool.setItemMeta((ItemMeta) damageable); + + if (!this.hasEnoughDurability(tool, 1)) + player.getInventory().setItemInMainHand(null); + + } + } + + public boolean hasEnoughDurability(ItemStack tool, int requiredAmount) { + if (!tool.hasItemMeta() || !(tool.getItemMeta() instanceof Damageable) || tool.getType().getMaxDurability() < 1) + return true; + + Damageable damageable = (Damageable) tool.getItemMeta(); + int durabilityRemaining = tool.getType().getMaxDurability() - damageable.getDamage(); + return durabilityRemaining > requiredAmount; + } + + public boolean checkUnbreakingChance(int level) { + return (1.0 / (level + 1)) > random.nextDouble(); + } + } diff --git a/src/main/java/com/songoda/ultimatestacker/utils/settings/Setting.java b/src/main/java/com/songoda/ultimatestacker/utils/settings/Setting.java index 2ea4a08..aebd820 100644 --- a/src/main/java/com/songoda/ultimatestacker/utils/settings/Setting.java +++ b/src/main/java/com/songoda/ultimatestacker/utils/settings/Setting.java @@ -148,6 +148,9 @@ public enum Setting { "Y axis. This is important for grinders so that flying entities don't continuously", "stack upwards to a higher up entity."), + REALISTIC_DAMAGE("Entities.Use Realistic Weapon Damage", true, + "Should weapons tale damage based on the amount of entites in the stack?"), + STACK_ITEMS("Items.Enabled", true, "Should items be stacked?"),