From 5be55f547b132daf6fefe48b6c8f6207f92d9ee1 Mon Sep 17 00:00:00 2001 From: Aria Sangarin Date: Thu, 30 Jan 2020 20:45:31 +0100 Subject: [PATCH] Added drop event for custom blocks (API) --- .../event/blocks/CustomBlockDropEvent.java | 47 +++++++++++++++++++ .../mmoitems/command/MMOItemsCommand.java | 5 ++ .../mmoitems/manager/DropTableManager.java | 7 ++- 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/main/java/net/Indyuce/mmoitems/api/event/blocks/CustomBlockDropEvent.java diff --git a/src/main/java/net/Indyuce/mmoitems/api/event/blocks/CustomBlockDropEvent.java b/src/main/java/net/Indyuce/mmoitems/api/event/blocks/CustomBlockDropEvent.java new file mode 100644 index 00000000..3cae424d --- /dev/null +++ b/src/main/java/net/Indyuce/mmoitems/api/event/blocks/CustomBlockDropEvent.java @@ -0,0 +1,47 @@ +package net.Indyuce.mmoitems.api.event.blocks; + +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.HandlerList; +import org.bukkit.event.player.PlayerEvent; +import org.bukkit.inventory.ItemStack; + +import net.Indyuce.mmoitems.api.CustomBlock; + +public class CustomBlockDropEvent extends PlayerEvent implements Cancellable { + private static final HandlerList handlers = new HandlerList(); + private boolean cancelled; + private final CustomBlock custom; + private final ItemStack drop; + + public CustomBlockDropEvent(Player p, CustomBlock c, ItemStack d) { + super(p); custom = c; drop = d; + } + + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(boolean value) { + cancelled = value; + } + + public CustomBlock getCustomBlock() { + return custom; + } + + public ItemStack getDrop() { + return drop; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } +} diff --git a/src/main/java/net/Indyuce/mmoitems/command/MMOItemsCommand.java b/src/main/java/net/Indyuce/mmoitems/command/MMOItemsCommand.java index 45bb330c..78b41137 100644 --- a/src/main/java/net/Indyuce/mmoitems/command/MMOItemsCommand.java +++ b/src/main/java/net/Indyuce/mmoitems/command/MMOItemsCommand.java @@ -430,6 +430,11 @@ public class MMOItemsCommand implements CommandExecutor { if (args[1].equalsIgnoreCase("recipes")) { Bukkit.getScheduler().runTaskAsynchronously(MMOItems.plugin, () -> { MMOItems.plugin.getRecipes().reloadRecipes(); + + if(MMOItems.plugin.getConfig().getBoolean("auto-recipe-book")) + for(Player p : Bukkit.getOnlinePlayers()) + p.discoverRecipes(MMOItems.plugin.getRecipes().getNamespacedKeys()); + sender.sendMessage(MMOItems.plugin.getPrefix() + "Successfully reloaded recipes."); sender.sendMessage(MMOItems.plugin.getPrefix() + "- " + ChatColor.RED + MMOItems.plugin.getRecipes().size() + ChatColor.GRAY + " Recipes"); }); diff --git a/src/main/java/net/Indyuce/mmoitems/manager/DropTableManager.java b/src/main/java/net/Indyuce/mmoitems/manager/DropTableManager.java index b9b173d8..3ee3a663 100644 --- a/src/main/java/net/Indyuce/mmoitems/manager/DropTableManager.java +++ b/src/main/java/net/Indyuce/mmoitems/manager/DropTableManager.java @@ -25,6 +25,7 @@ import net.Indyuce.mmoitems.MMOItems; import net.Indyuce.mmoitems.api.ConfigFile; import net.Indyuce.mmoitems.api.CustomBlock; import net.Indyuce.mmoitems.api.drop.DropTable; +import net.Indyuce.mmoitems.api.event.blocks.CustomBlockDropEvent; import net.Indyuce.mmoitems.listener.CustomBlockListener; import net.mmogroup.mmolib.MMOLib; @@ -97,7 +98,11 @@ public class DropTableManager implements Listener { Bukkit.getScheduler().runTaskLater(MMOItems.plugin, () -> { if(CustomBlockListener.getPickaxePower(player) >= custom.getRequiredPower()) for (ItemStack drop : customBlocks.get(custom.getId()).read(hasSilkTouchTool(player))) { - Item item = block.getWorld().dropItemNaturally(block.getLocation().add(.5, .1, .5), drop); + CustomBlockDropEvent called = new CustomBlockDropEvent(player, custom, drop); + Bukkit.getPluginManager().callEvent(called); + if (called.isCancelled()) + return; + Item item = block.getWorld().dropItemNaturally(block.getLocation().add(.5, .1, .5), called.getDrop()); item.setVelocity(item.getVelocity().multiply(0.5f)); } }, 2);