From db428892e91e9565e50ce4123ed2afbb70eac68e Mon Sep 17 00:00:00 2001
From: Sn0wStorm <milan-alb@hotmail.de>
Date: Sun, 10 Jul 2022 14:22:23 +0200
Subject: [PATCH] Fix Slimefun item eat at cauldron

---
 resources/plugin.yml                          |  2 +-
 src/com/dre/brewery/P.java                    |  5 +-
 src/com/dre/brewery/filedata/BConfig.java     |  3 +-
 .../brewery/integration/SlimefunListener.java | 56 +++++++++++++++++++
 .../integration/item/SlimefunPluginItem.java  |  3 -
 5 files changed, 63 insertions(+), 6 deletions(-)
 create mode 100644 src/com/dre/brewery/integration/SlimefunListener.java

diff --git a/resources/plugin.yml b/resources/plugin.yml
index d07c7a56..b224c5b0 100644
--- a/resources/plugin.yml
+++ b/resources/plugin.yml
@@ -1,7 +1,7 @@
 name: Brewery
 version: 3.1
 main: com.dre.brewery.P
-softdepend: [LWC, LogBlock, WorldGuard, GriefPrevention, Vault, ChestShop, Shopkeepers, Towny, BlockLocker]
+softdepend: [LWC, LogBlock, WorldGuard, GriefPrevention, Vault, ChestShop, Shopkeepers, Towny, BlockLocker, Slimefun]
 authors: [Milan Albrecht, Frank Baumann, ProgrammerDan, Daniel Saukel]
 api-version: 1.13
 commands:
diff --git a/src/com/dre/brewery/P.java b/src/com/dre/brewery/P.java
index e3495a20..6037361e 100644
--- a/src/com/dre/brewery/P.java
+++ b/src/com/dre/brewery/P.java
@@ -32,6 +32,7 @@
 import com.dre.brewery.integration.ChestShopListener;
 import com.dre.brewery.integration.IntegrationListener;
 import com.dre.brewery.integration.ShopKeepersListener;
+import com.dre.brewery.integration.SlimefunListener;
 import com.dre.brewery.integration.barrel.BlocklockerBarrel;
 import com.dre.brewery.integration.barrel.LogBlockBarrel;
 import com.dre.brewery.listeners.*;
@@ -161,6 +162,9 @@ public void onEnable() {
 		if (BConfig.hasShopKeepers) {
 			p.getServer().getPluginManager().registerEvents(new ShopKeepersListener(), p);
 		}
+		if (BConfig.hasSlimefun && use1_14) {
+			p.getServer().getPluginManager().registerEvents(new SlimefunListener(), p);
+		}
 
 		// Heartbeat
 		p.getServer().getScheduler().runTaskTimer(p, new BreweryRunnable(), 650, 1200);
@@ -267,7 +271,6 @@ private void clearConfigData() {
 		BCauldronRecipe.getConfigRecipes().clear();
 		BCauldronRecipe.numConfigRecipes = 0;
 		BConfig.customItems.clear();
-		BConfig.hasSlimefun = null;
 		BConfig.hasMMOItems = null;
 		DistortChat.commands = null;
 		BConfig.drainItems.clear();
diff --git a/src/com/dre/brewery/filedata/BConfig.java b/src/com/dre/brewery/filedata/BConfig.java
index b6db9958..e45ac187 100644
--- a/src/com/dre/brewery/filedata/BConfig.java
+++ b/src/com/dre/brewery/filedata/BConfig.java
@@ -55,7 +55,7 @@ public class BConfig {
 	public static boolean hasVault; // Vault
 	public static boolean useCitadel; // CivCraft/DevotedMC Citadel
 	public static boolean useGMInventories; // GamemodeInventories
-	public static Boolean hasSlimefun = null; // Slimefun ; Null if not checked
+	public static boolean hasSlimefun; // Slimefun
 	public static Boolean hasMMOItems = null; // MMOItems ; Null if not checked
 	public static boolean hasChestShop;
 	public static boolean hasShopKeepers;
@@ -216,6 +216,7 @@ public static void readConfig(FileConfiguration config) {
 			&& Integer.parseInt(plMan.getPlugin("Vault").getDescription().getVersion().split("\\.")[1]) <= 6;
 		hasChestShop = plMan.isPluginEnabled("ChestShop");
 		hasShopKeepers = plMan.isPluginEnabled("Shopkeepers");
+		hasSlimefun = plMan.isPluginEnabled("Slimefun");
 
 		// various Settings
 		DataSave.autosave = config.getInt("autosave", 3);
diff --git a/src/com/dre/brewery/integration/SlimefunListener.java b/src/com/dre/brewery/integration/SlimefunListener.java
new file mode 100644
index 00000000..07d04541
--- /dev/null
+++ b/src/com/dre/brewery/integration/SlimefunListener.java
@@ -0,0 +1,56 @@
+package com.dre.brewery.integration;
+
+import com.Acrobot.ChestShop.Events.ShopCreatedEvent;
+import com.dre.brewery.Brew;
+import com.dre.brewery.P;
+import com.dre.brewery.filedata.BConfig;
+import com.dre.brewery.integration.item.SlimefunPluginItem;
+import com.dre.brewery.recipe.BCauldronRecipe;
+import com.dre.brewery.recipe.RecipeItem;
+import com.dre.brewery.utility.LegacyUtil;
+import io.github.thebusybiscuit.slimefun4.api.events.PlayerRightClickEvent;
+import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem;
+import org.bukkit.Material;
+import org.bukkit.block.Block;
+import org.bukkit.block.Container;
+import org.bukkit.event.EventHandler;
+import org.bukkit.event.HandlerList;
+import org.bukkit.event.Listener;
+import org.bukkit.event.block.Action;
+import org.bukkit.inventory.EquipmentSlot;
+import org.bukkit.inventory.ItemStack;
+
+import java.util.Optional;
+
+public class SlimefunListener implements Listener {
+
+	/**
+	 * Catch the Slimefun Right Click event, to cancel it if the right click was on a Cauldron.
+	 * This prevents item consumption while adding to the cauldron
+	 */
+	@EventHandler
+	public void onCauldronClickSlimefun(PlayerRightClickEvent event) {
+		try {
+			if (event.getClickedBlock().isPresent() && event.getHand() == EquipmentSlot.HAND) {
+				if (LegacyUtil.isWaterCauldron(event.getClickedBlock().get().getType())) {
+					Optional<SlimefunItem> slimefunItem = event.getSlimefunItem();
+					if (slimefunItem.isPresent()) {
+						for (RecipeItem rItem : BCauldronRecipe.acceptedCustom) {
+							if (rItem instanceof SlimefunPluginItem) {
+								if (slimefunItem.get().getId().equalsIgnoreCase(((SlimefunPluginItem) rItem).getItemId())) {
+									event.cancel();
+									P.p.playerListener.onPlayerInteract(event.getInteractEvent());
+									return;
+								}
+							}
+						}
+					}
+				}
+			}
+		} catch (Throwable e) {
+			HandlerList.unregisterAll(this);
+			P.p.errorLog("Slimefun check failed");
+			e.printStackTrace();
+		}
+	}
+}
diff --git a/src/com/dre/brewery/integration/item/SlimefunPluginItem.java b/src/com/dre/brewery/integration/item/SlimefunPluginItem.java
index 99ed3e38..88f6df69 100644
--- a/src/com/dre/brewery/integration/item/SlimefunPluginItem.java
+++ b/src/com/dre/brewery/integration/item/SlimefunPluginItem.java
@@ -15,9 +15,6 @@ public class SlimefunPluginItem extends PluginItem {
 
 	@Override
 	public boolean matches(ItemStack item) {
-		if (BConfig.hasSlimefun == null) {
-			BConfig.hasSlimefun = P.p.getServer().getPluginManager().isPluginEnabled("Slimefun");
-		}
 		if (!BConfig.hasSlimefun) return false;
 
 		try {