From 70b0b1bd2d90b90f41235712c275e38bf09ae4e9 Mon Sep 17 00:00:00 2001 From: Tsumiki Miniwa <37684627+cpaca@users.noreply.github.com> Date: Fri, 28 Jun 2024 22:00:09 -0700 Subject: [PATCH 1/6] SSK Sword First Creation Untested. --- .../enchantment/EnchantmentAbility.java | 6 +- .../enchantment/EnchantmentListener.java | 20 ++++ .../xyz/holocons/mc/holoitemsrevamp/Util.java | 26 ++++-- .../collection/CollectionManager.java | 2 +- .../holoitemsrevamp/enchantment/SSKSword.java | 91 +++++++++++++++++++ .../mc/holoitemsrevamp/item/SSKSwordBook.java | 67 ++++++++++++++ 6 files changed, 204 insertions(+), 8 deletions(-) create mode 100644 src/main/java/xyz/holocons/mc/holoitemsrevamp/enchantment/SSKSword.java create mode 100644 src/main/java/xyz/holocons/mc/holoitemsrevamp/item/SSKSwordBook.java diff --git a/src/main/java/com/strangeone101/holoitemsapi/enchantment/EnchantmentAbility.java b/src/main/java/com/strangeone101/holoitemsapi/enchantment/EnchantmentAbility.java index fcc8beb5..904aa161 100644 --- a/src/main/java/com/strangeone101/holoitemsapi/enchantment/EnchantmentAbility.java +++ b/src/main/java/com/strangeone101/holoitemsapi/enchantment/EnchantmentAbility.java @@ -3,6 +3,7 @@ import org.bukkit.Keyed; import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockPlaceEvent; +import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.event.entity.ProjectileLaunchEvent; import org.bukkit.event.player.PlayerInteractEvent; @@ -24,8 +25,11 @@ default void onProjectileLaunch(ProjectileLaunchEvent event, ItemStack itemStack } default void onPlayerInteract(PlayerInteractEvent event, ItemStack itemStack) { - } + } default void onPlayerToggleSneak(PlayerToggleSneakEvent event, ItemStack itemStack) { } + + default void onPlayerAttack(EntityDamageByEntityEvent event, ItemStack itemStack) { + } } diff --git a/src/main/java/com/strangeone101/holoitemsapi/enchantment/EnchantmentListener.java b/src/main/java/com/strangeone101/holoitemsapi/enchantment/EnchantmentListener.java index f80cd914..91130baf 100644 --- a/src/main/java/com/strangeone101/holoitemsapi/enchantment/EnchantmentListener.java +++ b/src/main/java/com/strangeone101/holoitemsapi/enchantment/EnchantmentListener.java @@ -1,11 +1,13 @@ package com.strangeone101.holoitemsapi.enchantment; import org.bukkit.Material; +import org.bukkit.entity.Player; import org.bukkit.entity.ThrowableProjectile; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockPlaceEvent; +import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.event.entity.ProjectileLaunchEvent; import org.bukkit.event.player.PlayerInteractEvent; @@ -99,4 +101,22 @@ public void onPlayerToggleSneak(PlayerToggleSneakEvent event) { }); } } + + @EventHandler(ignoreCancelled = true) + public void onPlayerAttack(EntityDamageByEntityEvent event) { + final var attacker = event.getDamager(); + final var target = event.getEntity(); + + if(!(attacker instanceof Player attackingPlayer)) { + return; + } + + // Now that I think about it, can you not attack enemies with your offhand weapon? + final var weapon = attackingPlayer.getInventory().getItemInMainHand(); + weapon.getEnchantments().keySet().forEach(enchantment -> { + if(enchantment instanceof EnchantmentAbility ability) { + ability.onPlayerAttack(event, weapon); + } + }); + } } diff --git a/src/main/java/xyz/holocons/mc/holoitemsrevamp/Util.java b/src/main/java/xyz/holocons/mc/holoitemsrevamp/Util.java index 210d2a3e..24797332 100644 --- a/src/main/java/xyz/holocons/mc/holoitemsrevamp/Util.java +++ b/src/main/java/xyz/holocons/mc/holoitemsrevamp/Util.java @@ -7,6 +7,8 @@ import org.bukkit.Bukkit; import org.bukkit.Material; +import org.bukkit.attribute.Attribute; +import org.bukkit.entity.LivingEntity; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.SkullMeta; import org.jetbrains.annotations.NotNull; @@ -29,7 +31,7 @@ private Util() { /** * Convenience method to call the deprecated {@code UnsafeValues#nextEntityId()}. - * + * * @return The next EntityId available */ @SuppressWarnings("deprecation") @@ -42,7 +44,7 @@ public static int nextEntityId() { * secure random UUID, but for our use cases, we don't need it to be * cryptographically secure. We can generate our UUIDs a little more cheaply using * {@code ThreadLocalRandom} instead. Should not be called from any async threads. - * + * * @return A pseudo randomly generated UUID */ public static UUID randomUUID() { @@ -51,7 +53,7 @@ public static UUID randomUUID() { /** * Returns a player head with the base64 texture. Mostly used for GUI. - * + * * @param base64 A base 64 string that contains ONLY the texture * @return The ItemStack player head */ @@ -74,7 +76,7 @@ public static ItemStack getPlayerHeadFromSkinUrl(String url) { * it begins counting from 0 when the server starts. Instead, we'll use the system * time as an epoch and add the current tick to it to efficiently get an absolute * current time. - * + * * @return The current time represented in terms of game ticks, assuming 20 TPS */ public static long currentTimeTicks() { @@ -88,7 +90,7 @@ public static long currentTimeTicks() { /** * Returns the ore rarity of a tool material. - * + * * @param material The material to check * @return An ingot material that corresponds to the provided material, or air if there is none. */ @@ -118,7 +120,7 @@ public static Material getOreLevel(Material material) { /** * Returns the roman numeral equivalent of a number. This is only useful for numbers 1 through 10. * Mainly used for enchantments. - * + * * @param number A number from 1 through 10 * @return A TranslatableComponent, or empty Component if it is outside the available range. */ @@ -126,4 +128,16 @@ public static Component toRoman(int number) { return (number > 0 && number <= 10) ? Component.translatable("enchantment.level." + Integer.toString(number)) : Component.empty(); } + + public static void healEntity(final LivingEntity entity, double amountToHeal) { + final var maxHealth = entity.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue(); + var health = entity.getHealth(); + + if(health + amountToHeal > maxHealth) { + entity.setHealth(maxHealth); + } + else { + entity.setHealth(health + amountToHeal); + } + } } diff --git a/src/main/java/xyz/holocons/mc/holoitemsrevamp/collection/CollectionManager.java b/src/main/java/xyz/holocons/mc/holoitemsrevamp/collection/CollectionManager.java index f757de01..15954283 100644 --- a/src/main/java/xyz/holocons/mc/holoitemsrevamp/collection/CollectionManager.java +++ b/src/main/java/xyz/holocons/mc/holoitemsrevamp/collection/CollectionManager.java @@ -1741,7 +1741,7 @@ public List getLore() { } private static Idol buildTemma(HoloItemsRevamp plugin) { - return new Idol() { + return new Idol(new SSKSwordBook(plugin)) { @Override public @NotNull String getSkinUrl() { diff --git a/src/main/java/xyz/holocons/mc/holoitemsrevamp/enchantment/SSKSword.java b/src/main/java/xyz/holocons/mc/holoitemsrevamp/enchantment/SSKSword.java new file mode 100644 index 00000000..e323c56c --- /dev/null +++ b/src/main/java/xyz/holocons/mc/holoitemsrevamp/enchantment/SSKSword.java @@ -0,0 +1,91 @@ +package xyz.holocons.mc.holoitemsrevamp.enchantment; + +import com.destroystokyo.paper.MaterialTags; +import com.strangeone101.holoitemsapi.enchantment.CustomEnchantment; +import com.strangeone101.holoitemsapi.enchantment.EnchantmentAbility; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; +import net.kyori.adventure.text.format.TextDecoration; +import org.bukkit.Location; +import org.bukkit.Particle; +import org.bukkit.Sound; +import org.bukkit.World; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.entity.LivingEntity; +import org.bukkit.event.entity.EntityDamageByEntityEvent; +import org.bukkit.inventory.ItemStack; +import org.bukkit.potion.PotionEffect; +import org.bukkit.potion.PotionEffectType; +import org.jetbrains.annotations.NotNull; +import xyz.holocons.mc.holoitemsrevamp.HoloItemsRevamp; +import xyz.holocons.mc.holoitemsrevamp.Util; + +import java.util.List; + +public class SSKSword extends CustomEnchantment implements EnchantmentAbility { + private final HoloItemsRevamp plugin; + + public SSKSword(HoloItemsRevamp plugin) { + super(plugin, "ssk_sword"); + this.plugin = plugin; + } + + @Override + public int getMaxLevel() { + return 1; + } + + @Override + public boolean conflictsWith(@NotNull Enchantment other) { + return false; + } + + @Override + public boolean canEnchantItem(@NotNull ItemStack itemStack) { + return MaterialTags.AXES.isTagged(itemStack); + } + + @Override + public @NotNull Component displayName(int level) { + return Component.text() + .color(NamedTextColor.GRAY) + .decoration(TextDecoration.ITALIC, false) + .append(Component.text("SSK Sword")) + .build(); + } + + @Override + public int getCostMultiplier() { + return 12; + } + + @Override + public void onPlayerAttack(EntityDamageByEntityEvent event, ItemStack itemStack) { + if(!(event.getEntity() instanceof LivingEntity target)) { + return; + } + + // This is what Klin used, and it seems to be equivalent to damage after-sharpness after-plugins before-prot4 + final var amountToHeal = event.getDamage(); + Util.healEntity(target, amountToHeal); + event.setDamage(0); + + // If there's fire aspect, add some sort of heal-over-time (since fire is DoT) + // Code mostly copied from klin. + int fireAspectLevel = itemStack.getEnchantmentLevel(FIRE_ASPECT); + if(fireAspectLevel > 0) { + target.addPotionEffects(List.of( + new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 80*fireAspectLevel, 1), + new PotionEffect(PotionEffectType.REGENERATION, 80*fireAspectLevel, 1), + new PotionEffect(PotionEffectType.SPEED, 80*fireAspectLevel, 1) + )); + } + + // Add some cool particle effects? Code copied straight from klin, + // unchanged besides "target" instead of "entity". + Location loc = target.getLocation().add(0, target.getHeight()/2, 0); + World world = target.getWorld(); + world.spawnParticle(Particle.HEART, loc, 3, 0.5, 0.5, 0.5); + world.playSound(loc, Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 0.1f, 1f); + } +} diff --git a/src/main/java/xyz/holocons/mc/holoitemsrevamp/item/SSKSwordBook.java b/src/main/java/xyz/holocons/mc/holoitemsrevamp/item/SSKSwordBook.java new file mode 100644 index 00000000..8e4f43d6 --- /dev/null +++ b/src/main/java/xyz/holocons/mc/holoitemsrevamp/item/SSKSwordBook.java @@ -0,0 +1,67 @@ +package xyz.holocons.mc.holoitemsrevamp.item; + +import com.strangeone101.holoitemsapi.enchantment.CustomEnchantment; +import com.strangeone101.holoitemsapi.enchantment.EnchantManager; +import com.strangeone101.holoitemsapi.enchantment.Enchantable; +import com.strangeone101.holoitemsapi.item.CustomItem; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; +import org.bukkit.Material; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.Recipe; +import org.bukkit.inventory.ShapedRecipe; +import org.bukkit.inventory.meta.EnchantmentStorageMeta; +import xyz.holocons.mc.holoitemsrevamp.HoloItemsRevamp; + +import java.util.List; + +public class SSKSwordBook extends CustomItem implements Enchantable { + + private static final String name = "ssk_sword"; + private static final Material material = Material.ENCHANTED_BOOK; + private static final Component displayName = Component.text("SSK Sword", NamedTextColor.RED); + private static final List lore = List.of( + Component.text("Heal-on-hit when at full health!", NamedTextColor.DARK_PURPLE) + ); + + private final EnchantManager enchantManager; + + public SSKSwordBook(HoloItemsRevamp plugin) { + super(plugin, name, material, displayName, lore); + this.enchantManager = plugin.getEnchantManager(); + this.register(); + } + + @Override + protected Recipe getRecipe() { + // Recipe 0 from OldHoloItems + ShapedRecipe recipe = new ShapedRecipe(getKey(), buildStack(null)); + recipe.shape("*&#","*&#","*/#"); + recipe.setIngredient('*', Material.BLAZE_POWDER); + recipe.setIngredient('&', Material.GHAST_TEAR); + recipe.setIngredient('#', Material.SUGAR); + recipe.setIngredient('/', Material.NETHER_STAR); + return recipe; + } + + @Override + public Enchantment getEnchantment() { + return CustomEnchantment.getByKey(getKey()); + } + + @Override + public ItemStack applyEnchantment(ItemStack itemStack) { + var enchantedStack = itemStack.clone(); + var enchantedMeta = (EnchantmentStorageMeta) enchantedStack.getItemMeta(); + + if (enchantedMeta.addStoredEnchant(getEnchantment(), 1, false)) { + enchantedStack.setItemMeta(enchantedMeta); + enchantManager.removeCustomEnchantmentLore(enchantedStack); + enchantManager.applyCustomEnchantmentLore(enchantedStack); + return enchantedStack; + } else { + return null; + } + } +} From e3bf3f0891f85b9a6c2f10414ddb9bf4111756e7 Mon Sep 17 00:00:00 2001 From: Tsumiki Miniwa <37684627+cpaca@users.noreply.github.com> Date: Sat, 6 Jul 2024 10:20:42 -0700 Subject: [PATCH 2/6] Rewrite SSK to be a diamond sword No longer an e-book. --- .../holoitemsapi/enchantment/EnchantManager.java | 3 ++- .../holoitemsrevamp/collection/CollectionManager.java | 2 +- .../mc/holoitemsrevamp/enchantment/SSKSword.java | 10 ++-------- .../item/{SSKSwordBook.java => SSKSwordItem.java} | 6 +++--- 4 files changed, 8 insertions(+), 13 deletions(-) rename src/main/java/xyz/holocons/mc/holoitemsrevamp/item/{SSKSwordBook.java => SSKSwordItem.java} (92%) diff --git a/src/main/java/com/strangeone101/holoitemsapi/enchantment/EnchantManager.java b/src/main/java/com/strangeone101/holoitemsapi/enchantment/EnchantManager.java index 5f524024..1262d20a 100644 --- a/src/main/java/com/strangeone101/holoitemsapi/enchantment/EnchantManager.java +++ b/src/main/java/com/strangeone101/holoitemsapi/enchantment/EnchantManager.java @@ -110,7 +110,8 @@ private static Set buildCustomEnchantments(HoloItemsRevamp pl new Memento(plugin), new TideRider(plugin), new Backdash(plugin), - new Plow(plugin) + new Plow(plugin), + new SSKSword(plugin) ); } } diff --git a/src/main/java/xyz/holocons/mc/holoitemsrevamp/collection/CollectionManager.java b/src/main/java/xyz/holocons/mc/holoitemsrevamp/collection/CollectionManager.java index 2a4a890e..02f5aba5 100644 --- a/src/main/java/xyz/holocons/mc/holoitemsrevamp/collection/CollectionManager.java +++ b/src/main/java/xyz/holocons/mc/holoitemsrevamp/collection/CollectionManager.java @@ -1741,7 +1741,7 @@ public List getLore() { } private static Idol buildTemma(HoloItemsRevamp plugin) { - return new Idol(new SSKSwordBook(plugin)) { + return new Idol(new SSKSwordItem(plugin)) { @Override public @NotNull String getSkinUrl() { diff --git a/src/main/java/xyz/holocons/mc/holoitemsrevamp/enchantment/SSKSword.java b/src/main/java/xyz/holocons/mc/holoitemsrevamp/enchantment/SSKSword.java index e323c56c..0081a2ed 100644 --- a/src/main/java/xyz/holocons/mc/holoitemsrevamp/enchantment/SSKSword.java +++ b/src/main/java/xyz/holocons/mc/holoitemsrevamp/enchantment/SSKSword.java @@ -6,10 +6,7 @@ import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.TextDecoration; -import org.bukkit.Location; -import org.bukkit.Particle; -import org.bukkit.Sound; -import org.bukkit.World; +import org.bukkit.*; import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.LivingEntity; import org.bukkit.event.entity.EntityDamageByEntityEvent; @@ -23,11 +20,8 @@ import java.util.List; public class SSKSword extends CustomEnchantment implements EnchantmentAbility { - private final HoloItemsRevamp plugin; - public SSKSword(HoloItemsRevamp plugin) { super(plugin, "ssk_sword"); - this.plugin = plugin; } @Override @@ -42,7 +36,7 @@ public boolean conflictsWith(@NotNull Enchantment other) { @Override public boolean canEnchantItem(@NotNull ItemStack itemStack) { - return MaterialTags.AXES.isTagged(itemStack); + return itemStack.getType() == Material.DIAMOND_SWORD; } @Override diff --git a/src/main/java/xyz/holocons/mc/holoitemsrevamp/item/SSKSwordBook.java b/src/main/java/xyz/holocons/mc/holoitemsrevamp/item/SSKSwordItem.java similarity index 92% rename from src/main/java/xyz/holocons/mc/holoitemsrevamp/item/SSKSwordBook.java rename to src/main/java/xyz/holocons/mc/holoitemsrevamp/item/SSKSwordItem.java index 8e4f43d6..d6353dd7 100644 --- a/src/main/java/xyz/holocons/mc/holoitemsrevamp/item/SSKSwordBook.java +++ b/src/main/java/xyz/holocons/mc/holoitemsrevamp/item/SSKSwordItem.java @@ -16,10 +16,10 @@ import java.util.List; -public class SSKSwordBook extends CustomItem implements Enchantable { +public class SSKSwordItem extends CustomItem implements Enchantable { private static final String name = "ssk_sword"; - private static final Material material = Material.ENCHANTED_BOOK; + private static final Material material = Material.DIAMOND_SWORD; private static final Component displayName = Component.text("SSK Sword", NamedTextColor.RED); private static final List lore = List.of( Component.text("Heal-on-hit when at full health!", NamedTextColor.DARK_PURPLE) @@ -27,7 +27,7 @@ public class SSKSwordBook extends CustomItem implements Enchantable { private final EnchantManager enchantManager; - public SSKSwordBook(HoloItemsRevamp plugin) { + public SSKSwordItem(HoloItemsRevamp plugin) { super(plugin, name, material, displayName, lore); this.enchantManager = plugin.getEnchantManager(); this.register(); From ef18bbd3840ff0468b3e2302da7b47ec41165620 Mon Sep 17 00:00:00 2001 From: Tsumiki Miniwa <37684627+cpaca@users.noreply.github.com> Date: Sat, 6 Jul 2024 10:28:58 -0700 Subject: [PATCH 3/6] Fix SSKSword applyEnchantment --- .../xyz/holocons/mc/holoitemsrevamp/item/SSKSwordItem.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/xyz/holocons/mc/holoitemsrevamp/item/SSKSwordItem.java b/src/main/java/xyz/holocons/mc/holoitemsrevamp/item/SSKSwordItem.java index d6353dd7..04c49258 100644 --- a/src/main/java/xyz/holocons/mc/holoitemsrevamp/item/SSKSwordItem.java +++ b/src/main/java/xyz/holocons/mc/holoitemsrevamp/item/SSKSwordItem.java @@ -6,6 +6,7 @@ import com.strangeone101.holoitemsapi.item.CustomItem; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; +import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.ItemStack; @@ -53,9 +54,9 @@ public Enchantment getEnchantment() { @Override public ItemStack applyEnchantment(ItemStack itemStack) { var enchantedStack = itemStack.clone(); - var enchantedMeta = (EnchantmentStorageMeta) enchantedStack.getItemMeta(); + var enchantedMeta = enchantedStack.hasItemMeta() ? enchantedStack.getItemMeta() : Bukkit.getItemFactory().getItemMeta(enchantedStack.getType()); - if (enchantedMeta.addStoredEnchant(getEnchantment(), 1, false)) { + if (enchantedMeta.addEnchant(getEnchantment(), 1, false)) { enchantedStack.setItemMeta(enchantedMeta); enchantManager.removeCustomEnchantmentLore(enchantedStack); enchantManager.applyCustomEnchantmentLore(enchantedStack); From be9bb8a1addff24bb36123c3adac78c9b908c22d Mon Sep 17 00:00:00 2001 From: Tsumiki Miniwa <37684627+cpaca@users.noreply.github.com> Date: Sat, 6 Jul 2024 10:34:45 -0700 Subject: [PATCH 4/6] Negative damage does actually heal --- .../xyz/holocons/mc/holoitemsrevamp/enchantment/SSKSword.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/xyz/holocons/mc/holoitemsrevamp/enchantment/SSKSword.java b/src/main/java/xyz/holocons/mc/holoitemsrevamp/enchantment/SSKSword.java index 0081a2ed..08ad74b2 100644 --- a/src/main/java/xyz/holocons/mc/holoitemsrevamp/enchantment/SSKSword.java +++ b/src/main/java/xyz/holocons/mc/holoitemsrevamp/enchantment/SSKSword.java @@ -61,8 +61,7 @@ public void onPlayerAttack(EntityDamageByEntityEvent event, ItemStack itemStack) // This is what Klin used, and it seems to be equivalent to damage after-sharpness after-plugins before-prot4 final var amountToHeal = event.getDamage(); - Util.healEntity(target, amountToHeal); - event.setDamage(0); + event.setDamage(-amountToHeal); // If there's fire aspect, add some sort of heal-over-time (since fire is DoT) // Code mostly copied from klin. From 8479e0deece9ae72247224aeef3741ccac46e9fb Mon Sep 17 00:00:00 2001 From: Derek Lee Date: Mon, 8 Jul 2024 03:00:48 -0700 Subject: [PATCH 5/6] Refactor --- .../enchantment/EnchantmentAbility.java | 6 +- .../enchantment/EnchantmentListener.java | 27 ++---- .../xyz/holocons/mc/holoitemsrevamp/Util.java | 14 --- .../holoitemsrevamp/enchantment/SSKSword.java | 85 ++++++++++++------- .../mc/holoitemsrevamp/item/SSKSwordItem.java | 4 +- 5 files changed, 65 insertions(+), 71 deletions(-) diff --git a/src/main/java/com/strangeone101/holoitemsapi/enchantment/EnchantmentAbility.java b/src/main/java/com/strangeone101/holoitemsapi/enchantment/EnchantmentAbility.java index 904aa161..658ee417 100644 --- a/src/main/java/com/strangeone101/holoitemsapi/enchantment/EnchantmentAbility.java +++ b/src/main/java/com/strangeone101/holoitemsapi/enchantment/EnchantmentAbility.java @@ -18,6 +18,9 @@ default void onBlockBreak(BlockBreakEvent event, ItemStack itemStack) { default void onBlockPlace(BlockPlaceEvent event, ItemStack itemStack) { } + default void onEntityDamageByEntity(EntityDamageByEntityEvent event, ItemStack itemStack) { + } + default void onPlayerDeath(PlayerDeathEvent event, ItemStack itemStack) { } @@ -29,7 +32,4 @@ default void onPlayerInteract(PlayerInteractEvent event, ItemStack itemStack) { default void onPlayerToggleSneak(PlayerToggleSneakEvent event, ItemStack itemStack) { } - - default void onPlayerAttack(EntityDamageByEntityEvent event, ItemStack itemStack) { - } } diff --git a/src/main/java/com/strangeone101/holoitemsapi/enchantment/EnchantmentListener.java b/src/main/java/com/strangeone101/holoitemsapi/enchantment/EnchantmentListener.java index 3d55b654..58770b8d 100644 --- a/src/main/java/com/strangeone101/holoitemsapi/enchantment/EnchantmentListener.java +++ b/src/main/java/com/strangeone101/holoitemsapi/enchantment/EnchantmentListener.java @@ -39,6 +39,15 @@ public void onBlockPlace(BlockPlaceEvent event) { forEachEnchantment(itemStack, ability -> ability.onBlockPlace(event, itemStack)); } + @EventHandler(ignoreCancelled = true) + public void onEntityDamageByEntity(EntityDamageByEntityEvent event) { + if (!(event.getDamager() instanceof Player player)) { + return; + } + final var itemStack = player.getInventory().getItemInMainHand(); + forEachEnchantment(itemStack, ability -> ability.onEntityDamageByEntity(event, itemStack)); + } + @EventHandler(ignoreCancelled = true) public void onPlayerDeath(PlayerDeathEvent event) { final var storageContents = event.getPlayer().getInventory().getStorageContents(); @@ -82,22 +91,4 @@ public void onPlayerToggleSneak(PlayerToggleSneakEvent event) { forEachEnchantment(itemStack, ability -> ability.onPlayerToggleSneak(event, itemStack)); } } - - @EventHandler(ignoreCancelled = true) - public void onPlayerAttack(EntityDamageByEntityEvent event) { - final var attacker = event.getDamager(); - final var target = event.getEntity(); - - if(!(attacker instanceof Player attackingPlayer)) { - return; - } - - // Now that I think about it, can you not attack enemies with your offhand weapon? - final var weapon = attackingPlayer.getInventory().getItemInMainHand(); - weapon.getEnchantments().keySet().forEach(enchantment -> { - if(enchantment instanceof EnchantmentAbility ability) { - ability.onPlayerAttack(event, weapon); - } - }); - } } diff --git a/src/main/java/xyz/holocons/mc/holoitemsrevamp/Util.java b/src/main/java/xyz/holocons/mc/holoitemsrevamp/Util.java index 2533994d..be34f24d 100644 --- a/src/main/java/xyz/holocons/mc/holoitemsrevamp/Util.java +++ b/src/main/java/xyz/holocons/mc/holoitemsrevamp/Util.java @@ -9,8 +9,6 @@ import org.bukkit.Bukkit; import org.bukkit.Material; -import org.bukkit.attribute.Attribute; -import org.bukkit.entity.LivingEntity; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.SkullMeta; @@ -113,16 +111,4 @@ public static long toTicks(long amount, TemporalUnit unit) { public static long toTicks(Duration duration) { return Tick.tick().fromDuration(duration); } - - public static void healEntity(final LivingEntity entity, double amountToHeal) { - final var maxHealth = entity.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue(); - var health = entity.getHealth(); - - if(health + amountToHeal > maxHealth) { - entity.setHealth(maxHealth); - } - else { - entity.setHealth(health + amountToHeal); - } - } } diff --git a/src/main/java/xyz/holocons/mc/holoitemsrevamp/enchantment/SSKSword.java b/src/main/java/xyz/holocons/mc/holoitemsrevamp/enchantment/SSKSword.java index 08ad74b2..e45e95b1 100644 --- a/src/main/java/xyz/holocons/mc/holoitemsrevamp/enchantment/SSKSword.java +++ b/src/main/java/xyz/holocons/mc/holoitemsrevamp/enchantment/SSKSword.java @@ -1,25 +1,33 @@ package xyz.holocons.mc.holoitemsrevamp.enchantment; -import com.destroystokyo.paper.MaterialTags; -import com.strangeone101.holoitemsapi.enchantment.CustomEnchantment; -import com.strangeone101.holoitemsapi.enchantment.EnchantmentAbility; -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.format.NamedTextColor; -import net.kyori.adventure.text.format.TextDecoration; -import org.bukkit.*; +import java.util.List; + +import org.bukkit.Material; +import org.bukkit.Particle; +import org.bukkit.Sound; +import org.bukkit.attribute.Attribute; import org.bukkit.enchantments.Enchantment; +import org.bukkit.entity.Entity; import org.bukkit.entity.LivingEntity; import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; import org.jetbrains.annotations.NotNull; -import xyz.holocons.mc.holoitemsrevamp.HoloItemsRevamp; -import xyz.holocons.mc.holoitemsrevamp.Util; +import org.joml.Math; -import java.util.List; +import com.strangeone101.holoitemsapi.enchantment.CustomEnchantment; +import com.strangeone101.holoitemsapi.enchantment.EnchantmentAbility; + +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; +import net.kyori.adventure.text.format.TextDecoration; +import xyz.holocons.mc.holoitemsrevamp.HoloItemsRevamp; public class SSKSword extends CustomEnchantment implements EnchantmentAbility { + + private static final int ticksPerLevel = 80; + public SSKSword(HoloItemsRevamp plugin) { super(plugin, "ssk_sword"); } @@ -42,10 +50,10 @@ public boolean canEnchantItem(@NotNull ItemStack itemStack) { @Override public @NotNull Component displayName(int level) { return Component.text() - .color(NamedTextColor.GRAY) - .decoration(TextDecoration.ITALIC, false) - .append(Component.text("SSK Sword")) - .build(); + .color(NamedTextColor.GRAY) + .decoration(TextDecoration.ITALIC, false) + .append(Component.text("SSK Sword")) + .build(); } @Override @@ -54,31 +62,42 @@ public int getCostMultiplier() { } @Override - public void onPlayerAttack(EntityDamageByEntityEvent event, ItemStack itemStack) { - if(!(event.getEntity() instanceof LivingEntity target)) { + public void onEntityDamageByEntity(EntityDamageByEntityEvent event, ItemStack itemStack) { + if (isMissingHealth(event.getDamager())) { return; } - // This is what Klin used, and it seems to be equivalent to damage after-sharpness after-plugins before-prot4 - final var amountToHeal = event.getDamage(); - event.setDamage(-amountToHeal); + final var rawDamage = event.getDamage(); + if (rawDamage <= 0d || !(event.getEntity() instanceof LivingEntity target)) { + return; + } - // If there's fire aspect, add some sort of heal-over-time (since fire is DoT) - // Code mostly copied from klin. - int fireAspectLevel = itemStack.getEnchantmentLevel(FIRE_ASPECT); - if(fireAspectLevel > 0) { + final var clampedDamage = Math.clamp(0d, getMissingHealth(target), rawDamage); + event.setDamage(-clampedDamage); + + final var fireAspectLevel = itemStack.getEnchantmentLevel(FIRE_ASPECT); + if (fireAspectLevel > 0) { target.addPotionEffects(List.of( - new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 80*fireAspectLevel, 1), - new PotionEffect(PotionEffectType.REGENERATION, 80*fireAspectLevel, 1), - new PotionEffect(PotionEffectType.SPEED, 80*fireAspectLevel, 1) - )); + new PotionEffect(PotionEffectType.FIRE_RESISTANCE, ticksPerLevel * fireAspectLevel, 1), + new PotionEffect(PotionEffectType.REGENERATION, ticksPerLevel * fireAspectLevel, 1), + new PotionEffect(PotionEffectType.SPEED, ticksPerLevel * fireAspectLevel, 1))); } - // Add some cool particle effects? Code copied straight from klin, - // unchanged besides "target" instead of "entity". - Location loc = target.getLocation().add(0, target.getHeight()/2, 0); - World world = target.getWorld(); - world.spawnParticle(Particle.HEART, loc, 3, 0.5, 0.5, 0.5); - world.playSound(loc, Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 0.1f, 1f); + final var location = target.getLocation().add(0, target.getHeight() / 2, 0); + final var world = location.getWorld(); + world.spawnParticle(Particle.HEART, location, 3, 0.5, 0.5, 0.5); + world.playSound(location, Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 0.1f, 1f); + } + + private static boolean isMissingHealth(final Entity entity) { + return entity instanceof LivingEntity livingEntity + && livingEntity.getHealth() < livingEntity.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue(); + } + + private static double getMissingHealth(final Entity entity) { + if (entity instanceof LivingEntity livingEntity) { + return livingEntity.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue() - livingEntity.getHealth(); + } + return 0d; } } diff --git a/src/main/java/xyz/holocons/mc/holoitemsrevamp/item/SSKSwordItem.java b/src/main/java/xyz/holocons/mc/holoitemsrevamp/item/SSKSwordItem.java index 04c49258..bc7ce797 100644 --- a/src/main/java/xyz/holocons/mc/holoitemsrevamp/item/SSKSwordItem.java +++ b/src/main/java/xyz/holocons/mc/holoitemsrevamp/item/SSKSwordItem.java @@ -12,7 +12,6 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.Recipe; import org.bukkit.inventory.ShapedRecipe; -import org.bukkit.inventory.meta.EnchantmentStorageMeta; import xyz.holocons.mc.holoitemsrevamp.HoloItemsRevamp; import java.util.List; @@ -36,8 +35,7 @@ public SSKSwordItem(HoloItemsRevamp plugin) { @Override protected Recipe getRecipe() { - // Recipe 0 from OldHoloItems - ShapedRecipe recipe = new ShapedRecipe(getKey(), buildStack(null)); + final var recipe = new ShapedRecipe(getKey(), buildStack(null)); recipe.shape("*&#","*&#","*/#"); recipe.setIngredient('*', Material.BLAZE_POWDER); recipe.setIngredient('&', Material.GHAST_TEAR); From 7cbbfccdafa9d7e25f1c0009a8ee43a7b5fedf06 Mon Sep 17 00:00:00 2001 From: Derek Lee Date: Mon, 8 Jul 2024 16:29:03 -0700 Subject: [PATCH 6/6] Refactor --- .../holoitemsrevamp/enchantment/SSKSword.java | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/main/java/xyz/holocons/mc/holoitemsrevamp/enchantment/SSKSword.java b/src/main/java/xyz/holocons/mc/holoitemsrevamp/enchantment/SSKSword.java index e45e95b1..fc1ed9c2 100644 --- a/src/main/java/xyz/holocons/mc/holoitemsrevamp/enchantment/SSKSword.java +++ b/src/main/java/xyz/holocons/mc/holoitemsrevamp/enchantment/SSKSword.java @@ -8,13 +8,13 @@ import org.bukkit.attribute.Attribute; import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Entity; +import org.bukkit.entity.EntityCategory; import org.bukkit.entity.LivingEntity; import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; import org.jetbrains.annotations.NotNull; -import org.joml.Math; import com.strangeone101.holoitemsapi.enchantment.CustomEnchantment; import com.strangeone101.holoitemsapi.enchantment.EnchantmentAbility; @@ -72,14 +72,16 @@ public void onEntityDamageByEntity(EntityDamageByEntityEvent event, ItemStack it return; } - final var clampedDamage = Math.clamp(0d, getMissingHealth(target), rawDamage); - event.setDamage(-clampedDamage); + event.setDamage(0); + + final var targetIsUndead = target.getCategory() == EntityCategory.UNDEAD; + final var effectType = targetIsUndead ? PotionEffectType.HARM : PotionEffectType.HEAL; + target.addPotionEffect(new PotionEffect(effectType, 1, 1)); final var fireAspectLevel = itemStack.getEnchantmentLevel(FIRE_ASPECT); if (fireAspectLevel > 0) { target.addPotionEffects(List.of( new PotionEffect(PotionEffectType.FIRE_RESISTANCE, ticksPerLevel * fireAspectLevel, 1), - new PotionEffect(PotionEffectType.REGENERATION, ticksPerLevel * fireAspectLevel, 1), new PotionEffect(PotionEffectType.SPEED, ticksPerLevel * fireAspectLevel, 1))); } @@ -93,11 +95,4 @@ private static boolean isMissingHealth(final Entity entity) { return entity instanceof LivingEntity livingEntity && livingEntity.getHealth() < livingEntity.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue(); } - - private static double getMissingHealth(final Entity entity) { - if (entity instanceof LivingEntity livingEntity) { - return livingEntity.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue() - livingEntity.getHealth(); - } - return 0d; - } }