From dc94fedee18612c589aa0f2abb93fbce647739f7 Mon Sep 17 00:00:00 2001 From: nossr50 Date: Tue, 20 Jul 2021 18:47:25 -0700 Subject: [PATCH] Added member getPlayer to McMMOItemSpawnEvent Fixes #4572 --- .../events/items/McMMOItemSpawnEvent.java | 15 +++++++++- .../nossr50/listeners/BlockListener.java | 2 +- .../gmail/nossr50/skills/archery/Archery.java | 2 +- .../skills/excavation/ExcavationManager.java | 2 +- .../skills/fishing/FishingManager.java | 4 +-- .../skills/herbalism/HerbalismManager.java | 2 +- .../nossr50/skills/mining/MiningManager.java | 6 ++-- .../skills/salvage/SalvageManager.java | 4 +-- .../skills/unarmed/UnarmedManager.java | 2 +- .../woodcutting/WoodcuttingManager.java | 10 +++---- .../java/com/gmail/nossr50/util/Misc.java | 28 +++++++++---------- 11 files changed, 45 insertions(+), 32 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/events/items/McMMOItemSpawnEvent.java b/src/main/java/com/gmail/nossr50/events/items/McMMOItemSpawnEvent.java index a626ebf00..1249476d4 100644 --- a/src/main/java/com/gmail/nossr50/events/items/McMMOItemSpawnEvent.java +++ b/src/main/java/com/gmail/nossr50/events/items/McMMOItemSpawnEvent.java @@ -2,11 +2,13 @@ package com.gmail.nossr50.events.items; import com.gmail.nossr50.api.ItemSpawnReason; import org.bukkit.Location; +import org.bukkit.entity.Player; import org.bukkit.event.Cancellable; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; /** * Called when mcMMO is preparing to drop an item. @@ -16,14 +18,25 @@ public class McMMOItemSpawnEvent extends Event implements Cancellable { private ItemStack itemStack; private boolean cancelled; private final ItemSpawnReason itemSpawnReason; + private final Player player; - public McMMOItemSpawnEvent(@NotNull Location location, @NotNull ItemStack itemStack, @NotNull ItemSpawnReason itemSpawnReason) { + public McMMOItemSpawnEvent(@NotNull Location location, @NotNull ItemStack itemStack, @NotNull ItemSpawnReason itemSpawnReason, @Nullable Player player) { this.location = location; this.itemStack = itemStack; this.itemSpawnReason = itemSpawnReason; + this.player = player; this.cancelled = false; } + /** + * Get the associated player + * This can be null + * @return the associated player if one exists null otherwise + */ + public @Nullable Player getPlayer() { + return player; + } + /** * The reason an item is being spawned by mcMMO * @see ItemSpawnReason diff --git a/src/main/java/com/gmail/nossr50/listeners/BlockListener.java b/src/main/java/com/gmail/nossr50/listeners/BlockListener.java index 658793842..0953f00c3 100644 --- a/src/main/java/com/gmail/nossr50/listeners/BlockListener.java +++ b/src/main/java/com/gmail/nossr50/listeners/BlockListener.java @@ -96,7 +96,7 @@ public class BlockListener implements Listener { int bonusCount = bonusDropMeta.asInt(); for (int i = 0; i < bonusCount; i++) { - Misc.spawnItemNaturally(event.getBlockState().getLocation(), is, ItemSpawnReason.BONUS_DROPS); + Misc.spawnItemNaturally(event.getPlayer(), event.getBlockState().getLocation(), is, ItemSpawnReason.BONUS_DROPS); } } } diff --git a/src/main/java/com/gmail/nossr50/skills/archery/Archery.java b/src/main/java/com/gmail/nossr50/skills/archery/Archery.java index b980a6f34..3ad317287 100644 --- a/src/main/java/com/gmail/nossr50/skills/archery/Archery.java +++ b/src/main/java/com/gmail/nossr50/skills/archery/Archery.java @@ -57,7 +57,7 @@ public class Archery { TrackedEntity trackedEntity = entityIterator.next(); if (trackedEntity.getID() == livingEntity.getUniqueId()) { - Misc.spawnItems(livingEntity.getLocation(), new ItemStack(Material.ARROW), trackedEntity.getArrowCount(), ItemSpawnReason.ARROW_RETRIEVAL_ACTIVATED); + Misc.spawnItems(null, livingEntity.getLocation(), new ItemStack(Material.ARROW), trackedEntity.getArrowCount(), ItemSpawnReason.ARROW_RETRIEVAL_ACTIVATED); entityIterator.remove(); return; } diff --git a/src/main/java/com/gmail/nossr50/skills/excavation/ExcavationManager.java b/src/main/java/com/gmail/nossr50/skills/excavation/ExcavationManager.java index 5412c9ef8..876634b46 100644 --- a/src/main/java/com/gmail/nossr50/skills/excavation/ExcavationManager.java +++ b/src/main/java/com/gmail/nossr50/skills/excavation/ExcavationManager.java @@ -49,7 +49,7 @@ public class ExcavationManager extends SkillManager { } xp += treasure.getXp(); - Misc.spawnItem(location, treasure.getDrop(), ItemSpawnReason.EXCAVATION_TREASURE); + Misc.spawnItem(getPlayer(), location, treasure.getDrop(), ItemSpawnReason.EXCAVATION_TREASURE); } } } diff --git a/src/main/java/com/gmail/nossr50/skills/fishing/FishingManager.java b/src/main/java/com/gmail/nossr50/skills/fishing/FishingManager.java index d4a4d266c..9a78dd499 100644 --- a/src/main/java/com/gmail/nossr50/skills/fishing/FishingManager.java +++ b/src/main/java/com/gmail/nossr50/skills/fishing/FishingManager.java @@ -446,7 +446,7 @@ public class FishingManager extends SkillManager { if(fishingSucceeds) { if (mcMMO.p.getGeneralConfig().getFishingExtraFish()) { - Misc.spawnItem(player.getEyeLocation(), fishingCatch.getItemStack(), ItemSpawnReason.FISHING_EXTRA_FISH); + Misc.spawnItem(getPlayer(), player.getEyeLocation(), fishingCatch.getItemStack(), ItemSpawnReason.FISHING_EXTRA_FISH); } fishingCatch.setItemStack(treasureDrop); @@ -553,7 +553,7 @@ public class FishingManager extends SkillManager { return; } - Misc.spawnItem(target.getLocation(), drop, ItemSpawnReason.FISHING_SHAKE_TREASURE); + Misc.spawnItem(getPlayer(), target.getLocation(), drop, ItemSpawnReason.FISHING_SHAKE_TREASURE); CombatUtils.dealDamage(target, Math.min(Math.max(target.getMaxHealth() / 4, 1), 10), EntityDamageEvent.DamageCause.CUSTOM, getPlayer()); // Make it so you can shake a mob no more than 4 times. applyXpGain(ExperienceConfig.getInstance().getFishingShakeXP(), XPGainReason.PVE); } diff --git a/src/main/java/com/gmail/nossr50/skills/herbalism/HerbalismManager.java b/src/main/java/com/gmail/nossr50/skills/herbalism/HerbalismManager.java index 7cf4ce1c4..6200d9eec 100644 --- a/src/main/java/com/gmail/nossr50/skills/herbalism/HerbalismManager.java +++ b/src/main/java/com/gmail/nossr50/skills/herbalism/HerbalismManager.java @@ -704,7 +704,7 @@ public class HerbalismManager extends SkillManager { return false; } blockState.setType(Material.AIR); - Misc.spawnItem(location, treasure.getDrop(), ItemSpawnReason.HYLIAN_LUCK_TREASURE); + Misc.spawnItem(getPlayer(), location, treasure.getDrop(), ItemSpawnReason.HYLIAN_LUCK_TREASURE); NotificationManager.sendPlayerInformation(player, NotificationType.SUBSKILL_MESSAGE, "Herbalism.HylianLuck"); return true; } diff --git a/src/main/java/com/gmail/nossr50/skills/mining/MiningManager.java b/src/main/java/com/gmail/nossr50/skills/mining/MiningManager.java index 4ab9f6a17..2a9890cd9 100644 --- a/src/main/java/com/gmail/nossr50/skills/mining/MiningManager.java +++ b/src/main/java/com/gmail/nossr50/skills/mining/MiningManager.java @@ -189,7 +189,7 @@ public class MiningManager extends SkillManager { //Drop "debris" based on skill modifiers for(BlockState blockState : notOres) { if(RandomUtils.nextFloat() < debrisYield) { - Misc.spawnItem(Misc.getBlockCenter(blockState), new ItemStack(blockState.getType()), ItemSpawnReason.BLAST_MINING_DEBRIS_NON_ORES); // Initial block that would have been dropped + Misc.spawnItem(getPlayer(), Misc.getBlockCenter(blockState), new ItemStack(blockState.getType()), ItemSpawnReason.BLAST_MINING_DEBRIS_NON_ORES); // Initial block that would have been dropped } } @@ -197,12 +197,12 @@ public class MiningManager extends SkillManager { if (RandomUtils.nextFloat() < (yield + oreBonus)) { xp += Mining.getBlockXp(blockState); - Misc.spawnItem(Misc.getBlockCenter(blockState), new ItemStack(blockState.getType()), ItemSpawnReason.BLAST_MINING_ORES); // Initial block that would have been dropped + Misc.spawnItem(getPlayer(), Misc.getBlockCenter(blockState), new ItemStack(blockState.getType()), ItemSpawnReason.BLAST_MINING_ORES); // Initial block that would have been dropped if (!mcMMO.getPlaceStore().isTrue(blockState)) { for (int i = 1; i < dropMultiplier; i++) { // Bukkit.broadcastMessage("Bonus Drop on Ore: "+blockState.getType().toString()); - Misc.spawnItem(Misc.getBlockCenter(blockState), new ItemStack(blockState.getType()), ItemSpawnReason.BLAST_MINING_ORES_BONUS_DROP); // Initial block that would have been dropped + Misc.spawnItem(getPlayer(), Misc.getBlockCenter(blockState), new ItemStack(blockState.getType()), ItemSpawnReason.BLAST_MINING_ORES_BONUS_DROP); // Initial block that would have been dropped } } } diff --git a/src/main/java/com/gmail/nossr50/skills/salvage/SalvageManager.java b/src/main/java/com/gmail/nossr50/skills/salvage/SalvageManager.java index 28afb1192..4ffb1baae 100644 --- a/src/main/java/com/gmail/nossr50/skills/salvage/SalvageManager.java +++ b/src/main/java/com/gmail/nossr50/skills/salvage/SalvageManager.java @@ -160,10 +160,10 @@ public class SalvageManager extends SkillManager { anvilLoc.add(0, .1, 0); if (enchantBook != null) { - Misc.spawnItemTowardsLocation(anvilLoc.clone(), playerLoc.clone(), enchantBook, vectorSpeed, ItemSpawnReason.SALVAGE_ENCHANTMENT_BOOK); + Misc.spawnItemTowardsLocation(getPlayer(), anvilLoc.clone(), playerLoc.clone(), enchantBook, vectorSpeed, ItemSpawnReason.SALVAGE_ENCHANTMENT_BOOK); } - Misc.spawnItemTowardsLocation(anvilLoc.clone(), playerLoc.clone(), salvageResults, vectorSpeed, ItemSpawnReason.SALVAGE_MATERIALS); + Misc.spawnItemTowardsLocation(getPlayer(), anvilLoc.clone(), playerLoc.clone(), salvageResults, vectorSpeed, ItemSpawnReason.SALVAGE_MATERIALS); // BWONG BWONG BWONG - CLUNK! if (mcMMO.p.getGeneralConfig().getSalvageAnvilUseSoundsEnabled()) { diff --git a/src/main/java/com/gmail/nossr50/skills/unarmed/UnarmedManager.java b/src/main/java/com/gmail/nossr50/skills/unarmed/UnarmedManager.java index 668eae038..4ad595a8e 100644 --- a/src/main/java/com/gmail/nossr50/skills/unarmed/UnarmedManager.java +++ b/src/main/java/com/gmail/nossr50/skills/unarmed/UnarmedManager.java @@ -110,7 +110,7 @@ public class UnarmedManager extends SkillManager { if(UserManager.getPlayer(defender) == null) return; - Item item = Misc.spawnItem(defender.getLocation(), defender.getInventory().getItemInMainHand(), ItemSpawnReason.UNARMED_DISARMED_ITEM); + Item item = Misc.spawnItem(getPlayer(), defender.getLocation(), defender.getInventory().getItemInMainHand(), ItemSpawnReason.UNARMED_DISARMED_ITEM); if (item != null && mcMMO.p.getAdvancedConfig().getDisarmProtected()) { item.setMetadata(mcMMO.disarmedItemKey, UserManager.getPlayer(defender).getPlayerMetadata()); diff --git a/src/main/java/com/gmail/nossr50/skills/woodcutting/WoodcuttingManager.java b/src/main/java/com/gmail/nossr50/skills/woodcutting/WoodcuttingManager.java index 3fd8f2ab9..d08cad2da 100644 --- a/src/main/java/com/gmail/nossr50/skills/woodcutting/WoodcuttingManager.java +++ b/src/main/java/com/gmail/nossr50/skills/woodcutting/WoodcuttingManager.java @@ -299,7 +299,7 @@ public class WoodcuttingManager extends SkillManager { xp += processTreeFellerXPGains(blockState, processedLogCount); //Drop displaced block - Misc.spawnItemsFromCollection(Misc.getBlockCenter(blockState), block.getDrops(), ItemSpawnReason.TREE_FELLER_DISPLACED_BLOCK); + Misc.spawnItemsFromCollection(getPlayer(), Misc.getBlockCenter(blockState), block.getDrops(), ItemSpawnReason.TREE_FELLER_DISPLACED_BLOCK); //Bonus Drops / Harvest lumber checks processHarvestLumber(blockState); @@ -307,7 +307,7 @@ public class WoodcuttingManager extends SkillManager { //Drop displaced non-woodcutting XP blocks if(RankUtils.hasUnlockedSubskill(player, SubSkillType.WOODCUTTING_KNOCK_ON_WOOD)) { - Misc.spawnItemsFromCollection(Misc.getBlockCenter(blockState), block.getDrops(), ItemSpawnReason.TREE_FELLER_DISPLACED_BLOCK); + Misc.spawnItemsFromCollection(getPlayer(), Misc.getBlockCenter(blockState), block.getDrops(), ItemSpawnReason.TREE_FELLER_DISPLACED_BLOCK); if(RankUtils.hasReachedRank(2, player, SubSkillType.WOODCUTTING_KNOCK_ON_WOOD)) { if(mcMMO.p.getAdvancedConfig().isKnockOnWoodXPOrbEnabled()) { @@ -319,7 +319,7 @@ public class WoodcuttingManager extends SkillManager { } } else { - Misc.spawnItemsFromCollection(Misc.getBlockCenter(blockState), block.getDrops(), ItemSpawnReason.TREE_FELLER_DISPLACED_BLOCK, 1); + Misc.spawnItemsFromCollection(getPlayer(), Misc.getBlockCenter(blockState), block.getDrops(), ItemSpawnReason.TREE_FELLER_DISPLACED_BLOCK, 1); } } @@ -384,7 +384,7 @@ public class WoodcuttingManager extends SkillManager { * * @param blockState Block being broken */ - protected static void spawnHarvestLumberBonusDrops(@NotNull BlockState blockState) { - Misc.spawnItemsFromCollection(Misc.getBlockCenter(blockState), blockState.getBlock().getDrops(), ItemSpawnReason.BONUS_DROPS); + protected void spawnHarvestLumberBonusDrops(@NotNull BlockState blockState) { + Misc.spawnItemsFromCollection(getPlayer(), Misc.getBlockCenter(blockState), blockState.getBlock().getDrops(), ItemSpawnReason.BONUS_DROPS); } } diff --git a/src/main/java/com/gmail/nossr50/util/Misc.java b/src/main/java/com/gmail/nossr50/util/Misc.java index 9b74f110c..03821dccd 100644 --- a/src/main/java/com/gmail/nossr50/util/Misc.java +++ b/src/main/java/com/gmail/nossr50/util/Misc.java @@ -107,9 +107,9 @@ public final class Misc { return blockState.getLocation().add(0.5, 0.5, 0.5); } - public static void spawnItemsFromCollection(@NotNull Location location, @NotNull Collection drops, @NotNull ItemSpawnReason itemSpawnReason) { + public static void spawnItemsFromCollection(@NotNull Player player, @NotNull Location location, @NotNull Collection drops, @NotNull ItemSpawnReason itemSpawnReason) { for (ItemStack drop : drops) { - spawnItem(location, drop, itemSpawnReason); + spawnItem(player, location, drop, itemSpawnReason); } } @@ -121,11 +121,11 @@ public final class Misc { * @param drops collection to iterate over * @param sizeLimit the number of drops to process */ - public static void spawnItemsFromCollection(@NotNull Location location, @NotNull Collection drops, @NotNull ItemSpawnReason itemSpawnReason, int sizeLimit) { + public static void spawnItemsFromCollection(@Nullable Player player, @NotNull Location location, @NotNull Collection drops, @NotNull ItemSpawnReason itemSpawnReason, int sizeLimit) { ItemStack[] arrayDrops = drops.toArray(new ItemStack[0]); for(int i = 0; i < sizeLimit-1; i++) { - spawnItem(location, arrayDrops[i], itemSpawnReason); + spawnItem(player, location, arrayDrops[i], itemSpawnReason); } } @@ -136,9 +136,9 @@ public final class Misc { * @param is The items to drop * @param quantity The amount of items to drop */ - public static void spawnItems(@NotNull Location location, @NotNull ItemStack is, int quantity, @NotNull ItemSpawnReason itemSpawnReason) { + public static void spawnItems(@Nullable Player player, @NotNull Location location, @NotNull ItemStack is, int quantity, @NotNull ItemSpawnReason itemSpawnReason) { for (int i = 0; i < quantity; i++) { - spawnItem(location, is, itemSpawnReason); + spawnItem(player, location, is, itemSpawnReason); } } @@ -150,13 +150,13 @@ public final class Misc { * @param itemSpawnReason the reason for the item drop * @return Dropped Item entity or null if invalid or cancelled */ - public static @Nullable Item spawnItem(@NotNull Location location, @NotNull ItemStack itemStack, @NotNull ItemSpawnReason itemSpawnReason) { + public static @Nullable Item spawnItem(@Nullable Player player, @NotNull Location location, @NotNull ItemStack itemStack, @NotNull ItemSpawnReason itemSpawnReason) { if (itemStack.getType() == Material.AIR || location.getWorld() == null) { return null; } // We can't get the item until we spawn it and we want to make it cancellable, so we have a custom event. - McMMOItemSpawnEvent event = new McMMOItemSpawnEvent(location, itemStack, itemSpawnReason); + McMMOItemSpawnEvent event = new McMMOItemSpawnEvent(location, itemStack, itemSpawnReason, player); mcMMO.p.getServer().getPluginManager().callEvent(event); if (event.isCancelled()) { @@ -174,13 +174,13 @@ public final class Misc { * @param itemSpawnReason the reason for the item drop * @return Dropped Item entity or null if invalid or cancelled */ - public static @Nullable Item spawnItemNaturally(@NotNull Location location, @NotNull ItemStack itemStack, @NotNull ItemSpawnReason itemSpawnReason) { + public static @Nullable Item spawnItemNaturally(@Nullable Player player, @NotNull Location location, @NotNull ItemStack itemStack, @NotNull ItemSpawnReason itemSpawnReason) { if (itemStack.getType() == Material.AIR || location.getWorld() == null) { return null; } // We can't get the item until we spawn it and we want to make it cancellable, so we have a custom event. - McMMOItemSpawnEvent event = new McMMOItemSpawnEvent(location, itemStack, itemSpawnReason); + McMMOItemSpawnEvent event = new McMMOItemSpawnEvent(location, itemStack, itemSpawnReason, player); mcMMO.p.getServer().getPluginManager().callEvent(event); if (event.isCancelled()) { @@ -198,9 +198,9 @@ public final class Misc { * @param speed the speed that the item should travel * @param quantity The amount of items to drop */ - public static void spawnItemsTowardsLocation(@NotNull Location fromLocation, @NotNull Location toLocation, @NotNull ItemStack is, int quantity, double speed, @NotNull ItemSpawnReason itemSpawnReason) { + public static void spawnItemsTowardsLocation(@Nullable Player player, @NotNull Location fromLocation, @NotNull Location toLocation, @NotNull ItemStack is, int quantity, double speed, @NotNull ItemSpawnReason itemSpawnReason) { for (int i = 0; i < quantity; i++) { - spawnItemTowardsLocation(fromLocation, toLocation, is, speed, itemSpawnReason); + spawnItemTowardsLocation(player, fromLocation, toLocation, is, speed, itemSpawnReason); } } @@ -214,7 +214,7 @@ public final class Misc { * @param speed the speed that the item should travel * @return Dropped Item entity or null if invalid or cancelled */ - public static @Nullable Item spawnItemTowardsLocation(@NotNull Location fromLocation, @NotNull Location toLocation, @NotNull ItemStack itemToSpawn, double speed, @NotNull ItemSpawnReason itemSpawnReason) { + public static @Nullable Item spawnItemTowardsLocation(@Nullable Player player, @NotNull Location fromLocation, @NotNull Location toLocation, @NotNull ItemStack itemToSpawn, double speed, @NotNull ItemSpawnReason itemSpawnReason) { if (itemToSpawn.getType() == Material.AIR) { return null; } @@ -228,7 +228,7 @@ public final class Misc { return null; // We can't get the item until we spawn it and we want to make it cancellable, so we have a custom event. - McMMOItemSpawnEvent event = new McMMOItemSpawnEvent(spawnLocation, clonedItem, itemSpawnReason); + McMMOItemSpawnEvent event = new McMMOItemSpawnEvent(spawnLocation, clonedItem, itemSpawnReason, player); mcMMO.p.getServer().getPluginManager().callEvent(event); //Something cancelled the event so back out