Skip to content

Commit

Permalink
support chest guis
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Jul 10, 2024
1 parent de6d6c6 commit 5285f5b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,19 @@ public abstract class IllegalItemModule extends AEFModule implements Listener {
protected final AEFPermission bypassPermission;
protected final IllegalHandling handling;
protected final Set<Listener> optionalListeners;

private final boolean guiPluginsSupported;
private final Cache<Class<? extends Event>, ExpiringSet<Object>> listenerCooldowns;
private final Function<Class<? extends Event>, @PolyNull ExpiringSet<Object>> createIfAbsent;

public IllegalItemModule(String configPath, AEFPermission bypassPermission) {
super(configPath);
this.bypassPermission = bypassPermission;
this.optionalListeners = new HashSet<>();

this.guiPluginsSupported = config.getBoolean(configPath + ".gui-plugins-supported", false, """
Enable this if you have problems with the plugin removing items from chest guis.
Check if the inventory is connected to a location in the game.
If it is not, its very likely created by custom gui plugin.
""");
String configuredHandling = config.getString(configPath + ".handling", IllegalHandling.PREVENT_USE_ONLY.name(),
"Available options:\n" + Arrays.stream(IllegalHandling.values())
.map(option -> option.name() + " - " + option.description())
Expand All @@ -76,11 +80,18 @@ public IllegalItemModule(String configPath, AEFPermission bypassPermission) {
}
this.handling = handling;

final boolean guiPluginsSupported = config.getBoolean(configPath + ".gui-plugins-supported", false, """
Enable this if you have problems with the plugin removing items from chest guis.
Check if the inventory is connected to a location in the game.
If it is not, its very likely created by custom gui plugin.""");
if (this.handling == IllegalHandling.STRICT) {
optionalListeners.add(new Listener() {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onInventoryOpen(InventoryOpenEvent event) {
if (!CachingPermTool.hasPermission(bypassPermission, event.getPlayer())) {
if (CachingPermTool.hasPermission(bypassPermission, event.getPlayer())) return;
// Check if the inventory is connected to a location in the game. If it is not,
// its very likely created by custom gui plugin
if (!guiPluginsSupported || event.getInventory().getLocation() != null) {
for (ItemStack invItem : event.getInventory()) {
handleItem(invItem, legalityOf(invItem));
}
Expand All @@ -105,12 +116,12 @@ private void onItemGoesThroughHopper(InventoryMoveItemEvent event) {
});
}

boolean checkOnChunkload = config.getBoolean(configPath + ".check-on-chunkload.enable", false, """
final boolean checkOnChunkload = config.getBoolean(configPath + ".check-on-chunkload.enable", false, """
WARNING: CHECKING ON CHUNKLOAD IS NOT RECOMMENDED AS IT IS VERY RESOURCE INTENSE.\s
BE VERY SURE YOU ACTUALLY NEED THIS.\s
Iterates over all blocks in a chunk when it is loaded and checks any inventories\s
for illegals. If a container with illegals is found, it will be REMOVED.""");
boolean remove = config.getBoolean(configPath + ".check-on-chunkload.remove-container", false, """
final boolean removeContainers = config.getBoolean(configPath + ".check-on-chunkload.remove-container", false, """
If set to true, immediately replaces the container with air. Otherwise, will try\s
to handle items separately.""");
if (checkOnChunkload) {
Expand All @@ -129,7 +140,7 @@ private void onChunkLoad(ChunkLoadEvent event) {
Block block = chunk.getBlock(x, y, z);
if (!MaterialUtil.INVENTORY_HOLDER_ITEMS.contains(block.getType())) continue;

if (remove) {
if (removeContainers) {
if (legalityOf(((InventoryHolder) block.getState()).getInventory()) != ItemLegality.LEGAL)
block.setType(Material.AIR, false);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,13 @@ public abstract class IllegalItemModule extends AEFModule implements Disableable
protected final AEFPermission bypassPermission;
protected final IllegalHandling handling;
protected final Set<Listener> optionalListeners;

private final Cache<Class<? extends Event>, ExpiringSet<Object>> listenerCooldowns;
private final Function<Class<? extends Event>, @PolyNull ExpiringSet<Object>> createIfAbsent;

public IllegalItemModule(String configPath, AEFPermission bypassPermission) {
super(configPath);
this.bypassPermission = bypassPermission;
this.optionalListeners = new HashSet<>();

String configuredHandling = config.getString(configPath + ".handling", IllegalHandling.PREVENT_USE_ONLY.name(),
"Available options:\n" + Arrays.stream(IllegalHandling.values())
.map(option -> option.name() + " - " + option.description())
Expand All @@ -77,14 +75,21 @@ public IllegalItemModule(String configPath, AEFPermission bypassPermission) {
}
this.handling = handling;

final boolean guiPluginsSupported = config.getBoolean(configPath + ".gui-plugins-supported", false,
"Enable this if you have problems with the plugin removing items from chest guis\n."+
"Check if the inventory is connected to a location in the game\n."+
"If it is not, its very likely created by custom gui plugin.");
if (this.handling == IllegalHandling.STRICT) {
optionalListeners.add(new Listener() {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onInventoryOpen(InventoryOpenEvent event) {
if (CachingPermTool.hasPermission(bypassPermission, event.getPlayer())) return;

for (ItemStack invItem : event.getInventory()) {
handleItem(invItem, legalityOf(invItem));
// Check if the inventory is connected to a location in the game. If it is not,
// its very likely created by custom gui plugin
if (!guiPluginsSupported || event.getInventory().getLocation() != null) {
for (ItemStack invItem : event.getInventory()) {
handleItem(invItem, legalityOf(invItem));
}
}
}
});
Expand All @@ -106,12 +111,12 @@ private void onItemGoesThroughHopper(InventoryMoveItemEvent event) {
});
}

boolean checkOnChunkload = config.getBoolean(configPath + ".check-on-chunkload.enable", false,
final boolean checkOnChunkload = config.getBoolean(configPath + ".check-on-chunkload.enable", false,
"WARNING: CHECKING ON CHUNKLOAD IS NOT RECOMMENDED AS IT IS VERY RESOURCE INTENSE.\n" +
"BE VERY SURE YOU ACTUALLY NEED THIS.\n" +
"Iterates over all blocks in a chunk when it is loaded and checks any inventories\n" +
"for illegals. If a container with illegals is found, it will be REMOVED.");
boolean remove = config.getBoolean(configPath + ".check-on-chunkload.remove-container", false,
final boolean removeContainers = config.getBoolean(configPath + ".check-on-chunkload.remove-container", false,
"If set to true, immediately replaces the container with air. Otherwise, will try\n" +
"to handle items separately.");
if (checkOnChunkload) {
Expand All @@ -130,7 +135,7 @@ private void onChunkLoad(ChunkLoadEvent event) {
Block block = chunk.getBlock(x, y, z);
if (!MaterialUtil.INVENTORY_HOLDER_ITEMS.contains(block.getType())) continue;

if (remove) {
if (removeContainers) {
if (legalityOf(((InventoryHolder) block.getState(false)).getInventory()) != ItemLegality.LEGAL)
block.setType(Material.AIR, false);
} else {
Expand Down

0 comments on commit 5285f5b

Please sign in to comment.