This commit is contained in:
mfnalex 2021-09-30 07:12:30 +02:00
parent 8d122d19a2
commit 695869f404
4 changed files with 43 additions and 12 deletions

View File

@ -1,5 +1,8 @@
# Changelog
## 11.5.4
- Fixed CratesReloaded preview GUI not opening when leftclicking chests
## 11.5.3
- Fixed "disabled-worlds" not working for "left-click hotkey"

View File

@ -9,7 +9,7 @@
<name>ChestSort</name>
<url>https://www.chestsort.de</url>
<description>Allows automatic chest sorting!</description>
<version>11.5.3</version>
<version>11.5.4</version>
<packaging>jar</packaging>
<properties>

View File

@ -1,23 +1,50 @@
package de.jeff_media.chestsort.hooks;
import de.jeff_media.chestsort.ChestSortPlugin;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.inventory.Inventory;
import org.jetbrains.annotations.NotNull;
import java.lang.reflect.Method;
import java.util.Locale;
/**
* Who doesn't make their API available in a public maven repository
* deserves to be touched using Reflection only.
*/
public class CrateReloadedHook {
private final ChestSortPlugin main;
private static final ChestSortPlugin main = ChestSortPlugin.getInstance();
private static Object blockCrateRegistrarObject;
private static Method isCrateMethod;
public CrateReloadedHook(ChestSortPlugin main) {
this.main=main;
static {
try {
Class<?> crateApiClazz = Class.forName("com.hazebyte.crate.api.CrateAPI");
Method getBlockCrateRegistrarMethod = crateApiClazz.getMethod("getBlockCrateRegistrar");
Class<?> blockCrateRegistrarClazz = Class.forName("com.hazebyte.crate.api.crate.BlockCrateRegistrar");
blockCrateRegistrarObject = getBlockCrateRegistrarMethod.invoke(null);
isCrateMethod = blockCrateRegistrarClazz.getMethod("hasCrates", Location.class);
} catch (Throwable ignored) {
isCrateMethod = null;
}
}
// CrateReloaded inventories seem to have a holder called cratereloaded.bo
// Maybe this changes? We just check if the String starts with cratereloaded
public boolean isCrate(Inventory inv) {
public static boolean isCrate(@NotNull final Block block) {
try {
if(isCrateMethod != null) {
return (boolean) isCrateMethod.invoke(blockCrateRegistrarObject, block.getLocation());
}
} catch (Throwable ignored) { }
return false;
}
public static boolean isCrate(@NotNull final Inventory inv) {
if(inv==null) return false;
if(inv.getHolder()==null) return false;
if(!main.getConfig().getBoolean("hook-cratereloaded",true)) return false;
return inv.getHolder().getClass().getName().startsWith("cratereloaded");
return inv.getHolder().getClass().getName().toLowerCase(Locale.ROOT).contains("cratereloaded");
}
}

View File

@ -41,7 +41,6 @@ public class Listener implements org.bukkit.event.Listener {
final ChestSortPlugin plugin;
public final MinepacksHook minepacksHook;
final HeadDatabaseHook headDatabaseHook;
final CrateReloadedHook crateReloadedHook;
final GoldenCratesHook goldenCratesHook;
final AdvancedChestsHook advancedChestsHook;
@ -49,7 +48,6 @@ public class Listener implements org.bukkit.event.Listener {
this.plugin = plugin;
this.minepacksHook = new MinepacksHook(plugin);
this.headDatabaseHook = new HeadDatabaseHook(plugin);
this.crateReloadedHook = new CrateReloadedHook(plugin);
this.goldenCratesHook = new GoldenCratesHook(plugin);
this.advancedChestsHook = new AdvancedChestsHook(plugin);
}
@ -67,6 +65,9 @@ public class Listener implements org.bukkit.event.Listener {
if(!plugin.getConfig().getBoolean("allow-left-click-to-sort")) return;
Block clickedBlock = event.getClickedBlock();
if(!(clickedBlock.getState() instanceof Container)) return;
if(CrateReloadedHook.isCrate(clickedBlock)) {
return;
}
if(!ProtectionUtils.canInteract(event.getPlayer(), clickedBlock)) {
//System.out.println("ChestSort: cannot interact!");
return;
@ -637,8 +638,8 @@ public class Listener implements org.bukkit.event.Listener {
}
// CrateReloaded hook
if(crateReloadedHook.isCrate(e.getClickedInventory())
|| crateReloadedHook.isCrate(e.getInventory())) {
if(CrateReloadedHook.isCrate(e.getClickedInventory())
|| CrateReloadedHook.isCrate(e.getInventory())) {
//if(plugin.debug) plugin.getLogger().info("Aborting hotkey because this is a CrateReloaded crate");
return;
}