Update InventoryView when container layout changed

This commit is contained in:
IzzelAliz 2021-02-18 01:00:46 +08:00
parent 2d6b0d6db2
commit 190726678c
2 changed files with 30 additions and 6 deletions

View File

@ -68,8 +68,9 @@ public abstract class ContainerMixin implements ContainerBridge {
private long bukkitViewHash = 0; private long bukkitViewHash = 0;
public InventoryView getBukkitView() { public InventoryView getBukkitView() {
if (bukkitViewHash != bukkitViewHash()) { if (bukkitView != null && bukkitViewHash != bukkitViewHash()) {
bukkitView = null; ArclightContainer.updateView((Container) (Object) this, bukkitView);
bukkitViewHash = bukkitViewHash();
} }
if (bukkitView == null) { if (bukkitView == null) {
bukkitView = ArclightContainer.createInvView((Container) (Object) this); bukkitView = ArclightContainer.createInvView((Container) (Object) this);

View File

@ -6,6 +6,8 @@ import io.izzel.arclight.common.bridge.inventory.IInventoryBridge;
import io.izzel.arclight.common.bridge.inventory.container.PosContainerBridge; import io.izzel.arclight.common.bridge.inventory.container.PosContainerBridge;
import io.izzel.arclight.common.mod.ArclightMod; import io.izzel.arclight.common.mod.ArclightMod;
import io.izzel.arclight.common.mod.util.ArclightCaptures; import io.izzel.arclight.common.mod.util.ArclightCaptures;
import io.izzel.tools.product.Product;
import io.izzel.tools.product.Product2;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory; import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.IInventory; import net.minecraft.inventory.IInventory;
@ -82,8 +84,26 @@ public class ArclightContainer {
} }
} }
// todo check this
public static InventoryView createInvView(Container container) { public static InventoryView createInvView(Container container) {
Product2<PlayerEntity, Integer> containerInfo = getContainerInfo(container);
Inventory viewing = new CraftInventory(new ContainerInvWrapper(container, containerInfo._2, containerInfo._1));
return new CraftInventoryView(((PlayerEntityBridge) containerInfo._1).bridge$getBukkitEntity(), viewing, container);
}
public static void updateView(Container container, InventoryView inventoryView) {
Inventory topInventory = inventoryView.getTopInventory();
if (topInventory instanceof CraftInventory) {
IInventory inventory = ((CraftInventory) topInventory).getInventory();
if (inventory instanceof ContainerInvWrapper) {
Product2<PlayerEntity, Integer> containerInfo = getContainerInfo(container);
((ContainerInvWrapper) inventory).setOwner(((PlayerEntityBridge) containerInfo._1).bridge$getBukkitEntity());
((ContainerInvWrapper) inventory).setSize(containerInfo._2);
}
}
}
// todo check this
private static Product2<PlayerEntity, Integer> getContainerInfo(Container container) {
PlayerEntity candidate = ArclightCaptures.getContainerOwner(); PlayerEntity candidate = ArclightCaptures.getContainerOwner();
int bottomBegin = -1, bottomEnd = -1; int bottomBegin = -1, bottomEnd = -1;
for (ListIterator<Slot> iterator = container.inventorySlots.listIterator(); iterator.hasNext(); ) { for (ListIterator<Slot> iterator = container.inventorySlots.listIterator(); iterator.hasNext(); ) {
@ -109,14 +129,13 @@ public class ArclightContainer {
if (bottomBegin < bottomEnd || bottomBegin == -1) { if (bottomBegin < bottomEnd || bottomBegin == -1) {
bottomBegin = container.inventorySlots.size(); bottomBegin = container.inventorySlots.size();
} }
Inventory viewing = new CraftInventory(new ContainerInvWrapper(container, bottomBegin, candidate)); return Product.of(candidate, bottomBegin);
return new CraftInventoryView(((PlayerEntityBridge) candidate).bridge$getBukkitEntity(), viewing, container);
} }
private static class ContainerInvWrapper implements IInventory, IInventoryBridge { private static class ContainerInvWrapper implements IInventory, IInventoryBridge {
private final Container container; private final Container container;
private final int size; private int size;
private InventoryHolder owner; private InventoryHolder owner;
private final List<HumanEntity> viewers = new ArrayList<>(); private final List<HumanEntity> viewers = new ArrayList<>();
@ -126,6 +145,10 @@ public class ArclightContainer {
this.owner = ((PlayerEntityBridge) owner).bridge$getBukkitEntity(); this.owner = ((PlayerEntityBridge) owner).bridge$getBukkitEntity();
} }
public void setSize(int size) {
this.size = size;
}
@Override @Override
public int getSizeInventory() { public int getSizeInventory() {
return size; return size;