From 13355c1afe1afb36106108f04929e9c1949acd98 Mon Sep 17 00:00:00 2001 From: TheBusyBiscuit Date: Mon, 22 Mar 2021 10:04:57 +0100 Subject: [PATCH] Fixes #2896 --- CHANGELOG.md | 1 + .../implementation/SlimefunPlugin.java | 28 ++++++-- .../auto_crafters/AbstractAutoCrafter.java | 64 ++++++++++--------- .../auto_crafters/EnhancedAutoCrafter.java | 2 + .../listeners/AncientAltarListener.java | 1 + .../listeners/AutoCrafterListener.java | 62 ++++++++++++++++++ 6 files changed, 123 insertions(+), 35 deletions(-) create mode 100644 src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/AutoCrafterListener.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 1645f32ce..1b8b3a9a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,7 @@ * Fixed #2887 * Fixed items getting deleted when breaking enhanced furnaces * Fixed #2895 +* Fixed #2896 ## Release Candidate 21 (14 Mar 2021) https://thebusybiscuit.github.io/builds/TheBusyBiscuit/Slimefun4/stable/#21 diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/SlimefunPlugin.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/SlimefunPlugin.java index 420ffe0de..e0cf42aef 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/SlimefunPlugin.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/SlimefunPlugin.java @@ -18,6 +18,7 @@ import javax.annotation.ParametersAreNonnullByDefault; import org.apache.commons.lang.Validate; import org.bukkit.Bukkit; import org.bukkit.Server; +import org.bukkit.World; import org.bukkit.command.Command; import org.bukkit.entity.Player; import org.bukkit.event.Listener; @@ -61,6 +62,7 @@ import io.github.thebusybiscuit.slimefun4.implementation.items.tools.GrapplingHo import io.github.thebusybiscuit.slimefun4.implementation.items.weapons.SeismicAxe; import io.github.thebusybiscuit.slimefun4.implementation.items.weapons.VampireBlade; import io.github.thebusybiscuit.slimefun4.implementation.listeners.AncientAltarListener; +import io.github.thebusybiscuit.slimefun4.implementation.listeners.AutoCrafterListener; import io.github.thebusybiscuit.slimefun4.implementation.listeners.BackpackListener; import io.github.thebusybiscuit.slimefun4.implementation.listeners.BeeWingsListener; import io.github.thebusybiscuit.slimefun4.implementation.listeners.BlockListener; @@ -118,6 +120,7 @@ import io.github.thebusybiscuit.slimefun4.utils.NumberUtils; import io.github.thebusybiscuit.slimefun4.utils.tags.SlimefunTag; import io.papermc.lib.PaperLib; import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.MenuListener; +import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.abstractItems.AContainer; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.abstractItems.AGenerator; import me.mrCookieSlime.Slimefun.api.BlockStorage; @@ -642,6 +645,7 @@ public final class SlimefunPlugin extends JavaPlugin implements SlimefunAddon { new HopperListener(this); new TalismanListener(this); new SoulboundListener(this); + new AutoCrafterListener(this); // Bees were added in 1.15 if (minecraftVersion.isAtLeast(MinecraftVersion.MINECRAFT_1_15)) { @@ -834,12 +838,26 @@ public final class SlimefunPlugin extends JavaPlugin implements SlimefunAddon { return instance.blockDataService; } + /** + * This method returns out world settings service. + * That service is responsible for managing item settings per + * {@link World}, such as disabling a {@link SlimefunItem} in a + * specific {@link World}. + * + * @return Our instance of {@link PerWorldSettingsService} + */ @Nonnull public static PerWorldSettingsService getWorldSettingsService() { validateInstance(); return instance.worldSettingsService; } + /** + * This returns our {@link HologramsService} which handles the creation and + * cleanup of any holograms. + * + * @return Our instance of {@link HologramsService} + */ @Nonnull public static HologramsService getHologramsService() { validateInstance(); @@ -997,15 +1015,13 @@ public final class SlimefunPlugin extends JavaPlugin implements SlimefunAddon { @Nonnull public static Set getInstalledAddons() { validateInstance(); - String pluginName = instance.getName(); // @formatter:off - return Arrays.stream(instance.getServer().getPluginManager().getPlugins()) - .filter(plugin -> { - PluginDescriptionFile description = plugin.getDescription(); - return description.getDepend().contains(pluginName) || description.getSoftDepend().contains(pluginName); - }).collect(Collectors.toSet()); + return Arrays.stream(instance.getServer().getPluginManager().getPlugins()).filter(plugin -> { + PluginDescriptionFile description = plugin.getDescription(); + return description.getDepend().contains(pluginName) || description.getSoftDepend().contains(pluginName); + }).collect(Collectors.toSet()); // @formatter:on } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/auto_crafters/AbstractAutoCrafter.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/auto_crafters/AbstractAutoCrafter.java index 851c9b04c..05a080489 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/auto_crafters/AbstractAutoCrafter.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/auto_crafters/AbstractAutoCrafter.java @@ -31,9 +31,9 @@ import io.github.thebusybiscuit.cscorelib2.protection.ProtectableAction; import io.github.thebusybiscuit.slimefun4.api.SlimefunAddon; import io.github.thebusybiscuit.slimefun4.api.items.ItemState; import io.github.thebusybiscuit.slimefun4.core.attributes.EnergyNetComponent; -import io.github.thebusybiscuit.slimefun4.core.handlers.BlockUseHandler; import io.github.thebusybiscuit.slimefun4.core.networks.energy.EnergyNetComponentType; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; +import io.github.thebusybiscuit.slimefun4.implementation.listeners.AutoCrafterListener; import io.github.thebusybiscuit.slimefun4.implementation.tasks.AsyncRecipeChoiceTask; import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils; import io.papermc.lib.PaperLib; @@ -88,7 +88,6 @@ public abstract class AbstractAutoCrafter extends SlimefunItem implements Energy recipeStorageKey = new NamespacedKey(SlimefunPlugin.instance(), "recipe_key"); - addItemHandler(onRightClick()); addItemHandler(new BlockTicker() { @Override @@ -103,36 +102,43 @@ public abstract class AbstractAutoCrafter extends SlimefunItem implements Energy }); } - @Nonnull - private BlockUseHandler onRightClick() { - return e -> e.getClickedBlock().ifPresent(b -> { - Player p = e.getPlayer(); + /** + * This method handles our right-clicking behaviour. + *

+ * Do not call this method directly, see our {@link AutoCrafterListener} for the intended + * use case. + * + * @param b + * The {@link Block} that was clicked + * @param p + * The {@link Player} who clicked + */ + @ParametersAreNonnullByDefault + public void onRightClick(Block b, Player p) { + Validate.notNull(b, "The Block must not be null!"); + Validate.notNull(p, "The Player cannot be null!"); - // Prevent blocks from being placed, food from being eaten, etc... - e.cancel(); - - // Check if we have a valid chest below - if (!isValidChest(b.getRelative(BlockFace.DOWN))) { - SlimefunPlugin.getLocalization().sendMessage(p, "messages.auto-crafting.missing-chest"); - } else if (SlimefunPlugin.getProtectionManager().hasPermission(p, b, ProtectableAction.INTERACT_BLOCK)) { - if (p.isSneaking()) { - // Select a new recipe - updateRecipe(b, p); - } else { - AbstractRecipe recipe = getSelectedRecipe(b); - - if (recipe == null) { - // Prompt the User to crouch - SlimefunPlugin.getLocalization().sendMessage(p, "messages.auto-crafting.select-a-recipe"); - } else { - // Show the current recipe - showRecipe(p, b, recipe); - } - } + // Check if we have a valid chest below + if (!isValidChest(b.getRelative(BlockFace.DOWN))) { + SlimefunPlugin.getLocalization().sendMessage(p, "messages.auto-crafting.missing-chest"); + } else if (SlimefunPlugin.getProtectionManager().hasPermission(p, b, ProtectableAction.INTERACT_BLOCK)) { + if (p.isSneaking()) { + // Select a new recipe + updateRecipe(b, p); } else { - SlimefunPlugin.getLocalization().sendMessage(p, "inventory.no-access"); + AbstractRecipe recipe = getSelectedRecipe(b); + + if (recipe == null) { + // Prompt the User to crouch + SlimefunPlugin.getLocalization().sendMessage(p, "messages.auto-crafting.select-a-recipe"); + } else { + // Show the current recipe + showRecipe(p, b, recipe); + } } - }); + } else { + SlimefunPlugin.getLocalization().sendMessage(p, "inventory.no-access"); + } } protected void tick(@Nonnull Block b, @Nonnull Config data) { diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/auto_crafters/EnhancedAutoCrafter.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/auto_crafters/EnhancedAutoCrafter.java index c9ca4c72e..08914f427 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/auto_crafters/EnhancedAutoCrafter.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/auto_crafters/EnhancedAutoCrafter.java @@ -5,6 +5,7 @@ import javax.annotation.ParametersAreNonnullByDefault; import org.bukkit.inventory.ItemStack; import io.github.thebusybiscuit.slimefun4.implementation.items.multiblocks.EnhancedCraftingTable; +import io.github.thebusybiscuit.slimefun4.implementation.listeners.AutoCrafterListener; import me.mrCookieSlime.Slimefun.Lists.RecipeType; import me.mrCookieSlime.Slimefun.Objects.Category; import me.mrCookieSlime.Slimefun.api.SlimefunItemStack; @@ -18,6 +19,7 @@ import me.mrCookieSlime.Slimefun.api.SlimefunItemStack; * @see AbstractAutoCrafter * @see VanillaAutoCrafter * @see SlimefunItemRecipe + * @see AutoCrafterListener * */ public class EnhancedAutoCrafter extends SlimefunAutoCrafter { diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/AncientAltarListener.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/AncientAltarListener.java index 58c7d1a3c..d2dfd5534 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/AncientAltarListener.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/AncientAltarListener.java @@ -60,6 +60,7 @@ public class AncientAltarListener implements Listener { private final List altars = new ArrayList<>(); private final Set removedItems = new HashSet<>(); + @ParametersAreNonnullByDefault public AncientAltarListener(SlimefunPlugin plugin, AncientAltar altar, AncientPedestal pedestal) { plugin.getServer().getPluginManager().registerEvents(this, plugin); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/AutoCrafterListener.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/AutoCrafterListener.java new file mode 100644 index 000000000..5fd4aa0f9 --- /dev/null +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/listeners/AutoCrafterListener.java @@ -0,0 +1,62 @@ +package io.github.thebusybiscuit.slimefun4.implementation.listeners; + +import java.util.Optional; + +import javax.annotation.ParametersAreNonnullByDefault; + +import org.bukkit.block.Block; +import org.bukkit.event.Event.Result; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.inventory.EquipmentSlot; + +import io.github.thebusybiscuit.slimefun4.api.events.PlayerRightClickEvent; +import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; +import io.github.thebusybiscuit.slimefun4.implementation.items.electric.machines.auto_crafters.AbstractAutoCrafter; +import io.github.thebusybiscuit.slimefun4.implementation.items.electric.machines.auto_crafters.EnhancedAutoCrafter; +import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; + +/** + * This {@link Listener} is responsible for providing interactions to the auto crafters. + * See Issue #2896 with the {@link EnhancedAutoCrafter}, any {@link SlimefunItem} which + * overrides the right click functonality would be ignored. + * This {@link Listener} resolves that issue. + * + * @author TheBusyBiscuit + * + * @see EnhancedAutoCrafter + * + */ +public class AutoCrafterListener implements Listener { + + @ParametersAreNonnullByDefault + public AutoCrafterListener(SlimefunPlugin plugin) { + plugin.getServer().getPluginManager().registerEvents(this, plugin); + } + + @EventHandler + public void onInteract(PlayerRightClickEvent e) { + Optional clickedBlock = e.getClickedBlock(); + + // We want to make sure we used the main hand, the interaction was not cancelled and a Block was clicked. + if (e.getHand() == EquipmentSlot.HAND && e.useBlock() != Result.DENY && clickedBlock.isPresent()) { + Optional slimefunBlock = e.getSlimefunBlock(); + + // Check if the clicked Block is a Slimefun block. + if (!slimefunBlock.isPresent()) { + return; + } + + SlimefunItem block = slimefunBlock.get(); + + if (block instanceof AbstractAutoCrafter) { + // Prevent blocks from being placed, food from being eaten, etc... + e.cancel(); + + // Fixes 2896 - Forward the interaction before items get handled. + ((AbstractAutoCrafter) block).onRightClick(clickedBlock.get(), e.getPlayer()); + } + } + } + +}