From 23b8e0a28cb9bcf8dd2e5ce06f5757b0ed4cce1a Mon Sep 17 00:00:00 2001 From: nossr50 Date: Wed, 20 Dec 2023 16:53:20 -0800 Subject: [PATCH] Add BowType metadata --- Changelog.txt | 1 + .../nossr50/listeners/EntityListener.java | 12 +++++++-- .../java/com/gmail/nossr50/util/BowType.java | 6 +++++ .../gmail/nossr50/util/MetadataConstants.java | 1 + .../nossr50/util/skills/CombatUtils.java | 27 +++++++++++++++---- .../skills/woodcutting/WoodcuttingTest.java | 2 +- 6 files changed, 41 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/gmail/nossr50/util/BowType.java diff --git a/Changelog.txt b/Changelog.txt index e4a34a095..2d98e7f99 100644 --- a/Changelog.txt +++ b/Changelog.txt @@ -1,4 +1,5 @@ Version 2.2.000 + TODO: com/gmail/nossr50/database/FlatFileDatabaseManager.java:109 reporting data entries that need correction on each launch TODO: Add Xbows/Tridents to salvage/repair TODO: Add unit test for combat XP values TODO: Add unit test to determine crossbow or bow skill diff --git a/src/main/java/com/gmail/nossr50/listeners/EntityListener.java b/src/main/java/com/gmail/nossr50/listeners/EntityListener.java index 7da526414..7f71c13f7 100644 --- a/src/main/java/com/gmail/nossr50/listeners/EntityListener.java +++ b/src/main/java/com/gmail/nossr50/listeners/EntityListener.java @@ -128,13 +128,21 @@ public class EntityListener implements Listener { ItemStack bow = event.getBow(); - if (bow != null - && bow.containsEnchantment(Enchantment.ARROW_INFINITE)) { + if (bow == null) + return; + + // determine if bow or crossbow + BowType bowType = ItemUtils.isCrossbow(bow) ? BowType.CROSSBOW : BowType.BOW; + + if (bow.containsEnchantment(Enchantment.ARROW_INFINITE)) { projectile.setMetadata(MetadataConstants.METADATA_KEY_INF_ARROW, MetadataConstants.MCMMO_METADATA_VALUE); } + // Set BowType, Force, and Distance metadata + projectile.setMetadata(MetadataConstants.METADATA_KEY_BOW_TYPE, new FixedMetadataValue(pluginRef, bowType)); projectile.setMetadata(MetadataConstants.METADATA_KEY_BOW_FORCE, new FixedMetadataValue(pluginRef, Math.min(event.getForce() * mcMMO.p.getAdvancedConfig().getForceMultiplier(), 1.0))); projectile.setMetadata(MetadataConstants.METADATA_KEY_ARROW_DISTANCE, new FixedMetadataValue(pluginRef, projectile.getLocation())); + //Cleanup metadata in 1 minute in case normal collection falls through CombatUtils.delayArrowMetaCleanup((Projectile) projectile); } diff --git a/src/main/java/com/gmail/nossr50/util/BowType.java b/src/main/java/com/gmail/nossr50/util/BowType.java new file mode 100644 index 000000000..5e4d78274 --- /dev/null +++ b/src/main/java/com/gmail/nossr50/util/BowType.java @@ -0,0 +1,6 @@ +package com.gmail.nossr50.util; + +public enum BowType { + BOW, + CROSSBOW +} diff --git a/src/main/java/com/gmail/nossr50/util/MetadataConstants.java b/src/main/java/com/gmail/nossr50/util/MetadataConstants.java index 090e04d17..8bec61365 100644 --- a/src/main/java/com/gmail/nossr50/util/MetadataConstants.java +++ b/src/main/java/com/gmail/nossr50/util/MetadataConstants.java @@ -14,6 +14,7 @@ public class MetadataConstants { * Take great care if you ever modify the value of these keys */ public static final @NotNull String METADATA_KEY_REPLANT = "mcMMO: Recently Replanted"; + public static final @NotNull String METADATA_KEY_BOW_TYPE = "mcMMO: Bow Type"; public static final @NotNull String METADATA_KEY_EXPLOSION_FROM_RUPTURE = "mcMMO: Rupture Explosion"; public static final @NotNull String METADATA_KEY_FISH_HOOK_REF = "mcMMO: Fish Hook Tracker"; public static final @NotNull String METADATA_KEY_DODGE_TRACKER = "mcMMO: Dodge Tracker"; diff --git a/src/main/java/com/gmail/nossr50/util/skills/CombatUtils.java b/src/main/java/com/gmail/nossr50/util/skills/CombatUtils.java index 7654d4ac9..e6397d954 100644 --- a/src/main/java/com/gmail/nossr50/util/skills/CombatUtils.java +++ b/src/main/java/com/gmail/nossr50/util/skills/CombatUtils.java @@ -481,17 +481,23 @@ public final class CombatUtils { Projectile arrow = (Projectile) painSource; ProjectileSource projectileSource = arrow.getShooter(); - if (projectileSource instanceof Player player && mcMMO.p.getSkillTools().canCombatSkillsTrigger(PrimarySkillType.ARCHERY, target)) { - // TODO: Add metadata to projectiles to determine source weapon to process combat skills + if (projectileSource instanceof Player player) { + BowType bowType = getBowTypeFromMetadata(arrow); - if (!Misc.isNPCEntityExcludingVillagers(player) && mcMMO.p.getSkillTools().doesPlayerHaveSkillPermission(player, PrimarySkillType.ARCHERY)) { - processArcheryCombat(target, player, event, arrow); + if (!Misc.isNPCEntityExcludingVillagers(player)) { + if(bowType == BowType.BOW && mcMMO.p.getSkillTools().canCombatSkillsTrigger(PrimarySkillType.ARCHERY, target)) { + processArcheryCombat(target, player, event, arrow); + } else if(bowType == BowType.CROSSBOW && mcMMO.p.getSkillTools().canCombatSkillsTrigger(PrimarySkillType.CROSSBOWS, target)) { + processCrossbowsCombat(target, player, event, arrow); + } } else { //Cleanup Arrow cleanupArrowMetadata(arrow); } - if (target.getType() != EntityType.CREEPER && !Misc.isNPCEntityExcludingVillagers(player) && mcMMO.p.getSkillTools().doesPlayerHaveSkillPermission(player, PrimarySkillType.TAMING)) { + if (target.getType() != EntityType.CREEPER + && !Misc.isNPCEntityExcludingVillagers(player) + && mcMMO.p.getSkillTools().doesPlayerHaveSkillPermission(player, PrimarySkillType.TAMING)) { McMMOPlayer mcMMOPlayer = UserManager.getPlayer(player); if(mcMMOPlayer == null) @@ -502,7 +508,18 @@ public final class CombatUtils { } } } + } + private static BowType getBowTypeFromMetadata(Projectile projectile) { + // Return the BowType from the metadata, or default to BOW + if (projectile.hasMetadata(MetadataConstants.METADATA_KEY_BOW_TYPE)) { + List metadataValue = projectile.getMetadata(MetadataConstants.METADATA_KEY_BOW_TYPE); + + if (!metadataValue.isEmpty()) { + return (BowType) metadataValue.get(0).value(); + } + } + throw new IllegalStateException("BowType metadata is empty"); } /** diff --git a/src/test/java/com/gmail/nossr50/skills/woodcutting/WoodcuttingTest.java b/src/test/java/com/gmail/nossr50/skills/woodcutting/WoodcuttingTest.java index 1de25cc71..8960ed821 100644 --- a/src/test/java/com/gmail/nossr50/skills/woodcutting/WoodcuttingTest.java +++ b/src/test/java/com/gmail/nossr50/skills/woodcutting/WoodcuttingTest.java @@ -72,10 +72,10 @@ class WoodcuttingTest extends MMOTestEnvironment { woodcuttingManager.processBonusDropCheck(blockState); // verify bonus drops were spawned + // TODO: Can fail if triple drops happen, need to update test Mockito.verify(woodcuttingManager, Mockito.times(1)).spawnHarvestLumberBonusDrops(blockState); } - @Test void harvestLumberShouldNotDoubleDrop() { mmoPlayer.modifySkill(PrimarySkillType.WOODCUTTING, 0);