Skip to content

Commit

Permalink
Fix sand portal not triggering comparator updates
Browse files Browse the repository at this point in the history
  • Loading branch information
dlee13 committed Sep 14, 2024
1 parent 57f02ae commit f30a9f4
Showing 1 changed file with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.klin.holoItems.collections.en1.watsonCollection.items;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
Expand Down Expand Up @@ -41,8 +45,22 @@ public class SandPortal extends Item implements Dispensable, Placeable {
private static final boolean shiny = false;
public static final int cost = 19200;

private final MethodHandle getInventoryHandle;
private final MethodHandle setChangedHandle;

public SandPortal() {
super(name, material, quantity, lore, durability, stackable, shiny, cost);
final var lookup = MethodHandles.lookup();
try {
final var craftInventoryClass = lookup.findClass("org.bukkit.craftbukkit.inventory.CraftInventory");
final var containerClass = lookup.findClass("net.minecraft.world.Container");
final var getInventoryType = MethodType.methodType(containerClass);
this.getInventoryHandle = lookup.findVirtual(craftInventoryClass, "getInventory", getInventoryType);
final var setChangedType = MethodType.methodType(void.class);
this.setChangedHandle = lookup.findVirtual(containerClass, "setChanged", setChangedType);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Override
Expand Down Expand Up @@ -84,7 +102,13 @@ public void ability(BlockDispenseEvent event) {
if (facedBlock.getType().isAir()) {
facedBlock.setType(type);
} else if (facedBlock.getState() instanceof Container container) {
container.getInventory().addItem(new ItemStack(type));
final var inventory = container.getInventory();
inventory.addItem(new ItemStack(type));
try {
final var nmsContainer = getInventoryHandle.invoke(inventory);
setChangedHandle.invoke(nmsContainer);
} catch (Throwable t) {
}
}
}

Expand Down

0 comments on commit f30a9f4

Please sign in to comment.