Batch Actions and Defer Registration of Slots (#72)

This commit is contained in:
Julia authored and GitHub committed 2025-09-24 23:25:39 +02:00
1 parent 2e36fbcfcc
commit a2a57d1656
22 files changed
+286 -119

No files matched your search

@@ -12,6 +12,7 @@ import net.minecraft.init.Items;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import binnie.Binnie;
import binnie.core.AbstractMod;
@@ -33,6 +34,7 @@ import binnie.core.craftgui.resource.minecraft.CraftGUITexture;
import binnie.core.craftgui.resource.minecraft.PaddedTexture;
import binnie.core.craftgui.resource.minecraft.StandardTexture;
import binnie.core.machines.inventory.SlotValidator;
import binnie.core.network.packet.MessageCraftGUI;
import binnie.core.util.I18N;
import binnie.extrabees.core.ExtraBeeTexture;
import binnie.extrabees.gui.ExtraBeeGUITexture;
@@ -125,8 +127,14 @@ public class WindowFieldKit extends Window {
handGlass.x(),
handGlass.y(),
new StandardTexture(0, 160, 96, 96, ExtraBeeTexture.GUIPunnett));
new ControlSlot(this, handGlass.x() + 54.0f, handGlass.y() + 26.0f).assign(InventoryType.Window, 0);
new ControlSlot(this, 208.0f, 8.0f).assign(InventoryType.Window, 1);
final NBTTagList actions = new NBTTagList();
new ControlSlot(this, handGlass.x() + 54.0f, handGlass.y() + 26.0f).assign(actions, InventoryType.Window, 0);
new ControlSlot(this, 208.0f, 8.0f).assign(actions, InventoryType.Window, 1);
MessageCraftGUI.sendToServer(actions);
(text = new ControlText(this, new IPoint(232.0f, 13.0f), I18N.localise("binniecore.gui.tooltip.paper")))
.setColor(0x222222);
(text = new ControlText(this, new IArea(0.0f, 120.0f, w(), 24.0f), "", TextJustification.MIDDLE_CENTER))
@@ -1,5 +1,7 @@
package binnie.core.craftgui.minecraft;
import static net.minecraftforge.common.util.Constants.NBT.TAG_COMPOUND;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -14,6 +16,7 @@ import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import com.mojang.authlib.GameProfile;
@@ -38,31 +41,40 @@ import cpw.mods.fml.relauncher.Side;
public class ContainerCraftGUI extends Container {
// Action type is one of the action constants below
private final static String ACTION_TYPE = "type";
public final static String ACTION_TYPE = "type";
// Action constants
private final static String TANK_CLICK = "tank-click";
private final static String SLOT_REG = "slot-reg";
private final static String TANK_UPDATE = "tank-update-";
private final static String POWER_UPDATE = "power-update";
private final static String PROCESS_UPDATE = "process-update";
private final static String ERROR_UPDATE = "error-update";
private final static String MOUSE_OVER_SLOT = "mouse-over-slot";
private final static String SHIFT_CLICK_INFO = "shift-click-info";
public final static String ACTION_BATCH = "batch";
public final static String SLOT_REG = "slot-reg";
public final static String TANK_CLICK = "tank-click";
public final static String TANK_UPDATE = "tank-update-";
public final static String POWER_UPDATE = "power-update";
public final static String PROCESS_UPDATE = "process-update";
public final static String ERROR_UPDATE = "error-update";
public final static String MOUSE_OVER_SLOT = "mouse-over-slot";
public final static String SHIFT_CLICK_INFO = "shift-click-info";
// Error type is one of the error constants below
private final static String ERROR_TYPE = "type";
public final static String ERROR_TYPE = "type";
// Error constants
private static final byte CANNOT_WORK = (byte) 0;
private static final byte CANNOT_PROGRESS = (byte) 1;
public static final byte CANNOT_WORK = (byte) 0;
public static final byte CANNOT_PROGRESS = (byte) 1;
// Slot constants
private static final String SLOT_TYPE = "t";
private static final String SLOT_INDEX = "i";
private static final String SLOT_NUMBER = "n";
public static final String SLOT_TYPE = "t";
public static final String SLOT_INDEX = "i";
public static final String SLOT_NUMBER = "n";
// Uncategorized constants
public static final String ACTIONS = "actions";
private static final String USERNAME = "username";
private static final String SLOT_ID = "id";
private static final String SLOT = "slot";
private static final String SLOTS = "slots";
private static final String TANK = "tank";
private static final String ORIGIN = "origin";
private static final String NAME = "name";
private final Window window;
private final Map<String, NBTTagCompound> syncedNBT = new HashMap<>();
@@ -111,17 +123,15 @@ public class ContainerCraftGUI extends Container {
@Override
public void putStackInSlot(int index, ItemStack stack) {
if (getSlot(index) != null) {
getSlot(index).putStack(stack);
}
final Slot slot = getSlot(index);
if (slot != null) slot.putStack(stack);
}
@Override
public void putStacksInSlots(ItemStack[] par1ArrayOfItemStack) {
for (int i = 0; i < par1ArrayOfItemStack.length; ++i) {
if (getSlot(i) != null) {
getSlot(i).putStack(par1ArrayOfItemStack[i]);
}
final Slot slot = getSlot(i);
if (slot != null) slot.putStack(par1ArrayOfItemStack[i]);
}
}
@@ -172,20 +182,17 @@ public class ContainerCraftGUI extends Container {
@Override
public ItemStack transferStackInSlot(EntityPlayer player, int slotID) {
return shiftClick(player, slotID);
shiftClick(player, slotID);
return null;
}
private ItemStack shiftClick(EntityPlayer player, int index) {
private void shiftClick(EntityPlayer player, int index) {
TransferRequest request = getShiftClickRequest(player, index);
if (request == null) {
return null;
}
if (request == null) return;
ItemStack stack = request.transfer(true);
Slot shiftClickedSlot = inventorySlots.get(index);
shiftClickedSlot.putStack(stack);
shiftClickedSlot.onSlotChanged();
return null;
}
private TransferRequest getShiftClickRequest(EntityPlayer player, int index) {
@@ -238,14 +245,24 @@ public class ContainerCraftGUI extends Container {
return heldItem;
}
public void handleNBT(Side side, EntityPlayer player, String name, NBTTagCompound action) {
public void handleNBT(Side side, EntityPlayer player, NBTTagCompound action) {
final String actionType = action.getString(ACTION_TYPE);
if (actionType.equals(ACTION_BATCH)) {
NBTTagList actionsBatch = action.getTagList(ACTIONS, TAG_COMPOUND);
for (int i = 0; i < actionsBatch.tagCount(); i++) {
handleNBT(side, player, actionsBatch.getCompoundTagAt(i));
}
}
if (side == Side.SERVER) {
switch (name) {
case TANK_CLICK -> tankClick(player, action.getByte("id"));
switch (actionType) {
case TANK_CLICK -> tankClick(player, action.getByte(SLOT_ID));
case SLOT_REG -> {
int slotType = action.getByte(SLOT_TYPE);
int slotIndex = action.getShort(SLOT_INDEX);
int slotNumber = action.getShort(SLOT_NUMBER);
final int slotType = action.getByte(SLOT_TYPE);
final int slotIndex = action.getShort(SLOT_INDEX);
final int slotNumber = action.getShort(SLOT_NUMBER);
createSlot(InventoryType.values()[slotType % 4], slotIndex, slotNumber);
for (ICrafting crafterObject : crafters) {
@@ -255,14 +272,14 @@ public class ContainerCraftGUI extends Container {
}
}
switch (name) {
switch (actionType) {
case POWER_UPDATE -> onPowerUpdate(action);
case PROCESS_UPDATE -> onProcessUpdate(action);
case ERROR_UPDATE -> onErrorUpdate(action);
case MOUSE_OVER_SLOT -> onMouseOverSlot(player, action);
case SHIFT_CLICK_INFO -> onReceiveShiftClickHighlights(player, action);
case SHIFT_CLICK_INFO -> onReceiveShiftClickHighlights(action);
default -> {
if (name.contains(TANK_UPDATE)) onTankUpdate(action);
if (actionType.contains(TANK_UPDATE)) onTankUpdate(action);
}
}
}
@@ -298,23 +315,30 @@ public class ContainerCraftGUI extends Container {
if (machineSync != null) machineSync.sendGuiNBT(syncedNBT);
Map<String, NBTTagCompound> sentThisTime = new HashMap<>();
for (Map.Entry<String, NBTTagCompound> nbt : syncedNBT.entrySet()) {
final NBTTagCompound nbtValue = nbt.getValue();
final String nbtKey = nbt.getKey();
final NBTTagCompound nbtValuePrev = sentNBT.get(nbtKey);
NBTTagList actions = new NBTTagList();
nbtValue.setString(ACTION_TYPE, nbtKey);
for (Map.Entry<String, NBTTagCompound> entry : syncedNBT.entrySet()) {
final String actionType = entry.getKey();
final NBTTagCompound action = entry.getValue();
final NBTTagCompound actionPrev = sentNBT.get(actionType);
if (nbtValue.equals(nbtValuePrev)) continue;
action.setString(ACTION_TYPE, actionType);
for (ICrafting crafter : crafters) {
if (crafter instanceof EntityPlayerMP playerMP) {
final MessageContainerUpdate packet = new MessageContainerUpdate(nbtValue);
BinnieCore.proxy.sendToPlayer(packet, playerMP);
}
if (action.equals(actionPrev)) continue;
actions.appendTag(action);
sentThisTime.put(actionType, action);
}
NBTTagCompound actionsBatch = new NBTTagCompound();
actionsBatch.setString(ACTION_TYPE, ACTION_BATCH);
actionsBatch.setTag(ACTIONS, actions);
for (ICrafting crafter : crafters) {
if (crafter instanceof EntityPlayerMP playerMP) {
final MessageContainerUpdate packet = new MessageContainerUpdate(actionsBatch);
BinnieCore.proxy.sendToPlayer(packet, playerMP);
}
sentThisTime.put(nbtKey, nbtValue);
}
sentNBT.putAll(sentThisTime);
@@ -353,12 +377,12 @@ public class ContainerCraftGUI extends Container {
public NBTTagCompound createTankNBT(int tank, TankInfo tankInfo) {
NBTTagCompound nbt = new NBTTagCompound();
tankInfo.writeToNBT(nbt);
nbt.setByte("tank", (byte) tank);
nbt.setByte(TANK, (byte) tank);
return nbt;
}
public void onTankUpdate(NBTTagCompound nbt) {
int tankID = nbt.getByte("tank");
int tankID = nbt.getByte(TANK);
TankInfo tank = new TankInfo();
tank.readFromNBT(nbt);
syncedTanks.put(tankID, tank);
@@ -388,7 +412,7 @@ public class ContainerCraftGUI extends Container {
public void onErrorUpdate(NBTTagCompound nbt) {
errorType = nbt.getByte(ERROR_TYPE);
if (nbt.hasKey("name")) {
if (nbt.hasKey(NAME)) {
error = new ErrorState("", "");
error.readFromNBT(nbt);
} else {
@@ -420,13 +444,13 @@ public class ContainerCraftGUI extends Container {
mousedOverSlotNumber = slot.slotNumber;
ControlSlot.highlighting.get(EnumHighlighting.SHIFT_CLICK).clear();
NBTTagCompound nbt = new NBTTagCompound();
nbt.setShort("slot", (short) slot.slotNumber);
nbt.setShort(SLOT, (short) slot.slotNumber);
window.sendClientAction(MOUSE_OVER_SLOT, nbt);
}
private void onMouseOverSlot(EntityPlayer player, NBTTagCompound data) {
int slotnumber = data.getShort("slot");
TransferRequest request = getShiftClickRequest(player, slotnumber);
int slotNumber = data.getShort(SLOT);
TransferRequest request = getShiftClickRequest(player, slotNumber);
if (request == null) {
return;
}
@@ -434,8 +458,8 @@ public class ContainerCraftGUI extends Container {
request.transfer(false);
NBTTagCompound nbt = new NBTTagCompound();
List<Integer> slots = new ArrayList<>();
for (TransferRequest.TransferSlot tslot : request.getInsertedSlots()) {
Slot slot = getSlot(tslot.inventory, tslot.id);
for (TransferRequest.TransferSlot transferSlot : request.getInsertedSlots()) {
Slot slot = getSlot(transferSlot.inventory, transferSlot.id);
if (slot != null) {
slots.add(slot.slotNumber);
}
@@ -446,14 +470,14 @@ public class ContainerCraftGUI extends Container {
array[i] = slots.get(i);
}
nbt.setIntArray("slots", array);
nbt.setShort("origin", (short) slotnumber);
nbt.setIntArray(SLOTS, array);
nbt.setShort(ORIGIN, (short) slotNumber);
syncedNBT.put(SHIFT_CLICK_INFO, nbt);
}
private void onReceiveShiftClickHighlights(EntityPlayer player, NBTTagCompound data) {
private void onReceiveShiftClickHighlights(NBTTagCompound data) {
ControlSlot.highlighting.get(EnumHighlighting.SHIFT_CLICK).clear();
for (int slotIndex : data.getIntArray("slots")) {
for (int slotIndex : data.getIntArray(SLOTS)) {
ControlSlot.highlighting.get(EnumHighlighting.SHIFT_CLICK).add(slotIndex);
}
}
@@ -469,27 +493,23 @@ public class ContainerCraftGUI extends Container {
}
public void receiveNBT(Side side, EntityPlayer player, NBTTagCompound action) {
String name = action.getString(ACTION_TYPE);
handleNBT(side, player, name, action);
window.receiveGuiNBT(getSide(), player, name, action);
String actionType = action.getString(ACTION_TYPE);
handleNBT(side, player, action);
window.receiveGuiNBT(getSide(), player, actionType, action);
INetwork.ReceiveGuiNBT machine = Machine.getInterface(INetwork.ReceiveGuiNBT.class, window.getInventory());
if (machine == null) return;
machine.receiveGuiNBT(getSide(), player, name, action);
machine.receiveGuiNBT(getSide(), player, actionType, action);
}
public Slot getOrCreateSlot(InventoryType type, int slotIndex) {
IInventory inventory = getInventory(type);
public Slot getOrCreateSlot(InventoryType inventoryType, int slotIndex) {
final IInventory inventory = getInventory(inventoryType);
Slot slot = getSlot(inventory, slotIndex);
if (slot == null) {
slot = new CustomSlot(inventory, slotIndex);
addSlotToContainer(slot);
}
NBTTagCompound nbt = new NBTTagCompound();
nbt.setByte(SLOT_TYPE, (byte) type.ordinal());
nbt.setShort(SLOT_INDEX, (short) slotIndex);
nbt.setShort(SLOT_NUMBER, (short) slot.slotNumber);
window.sendClientAction(SLOT_REG, nbt);
return slot;
}
@@ -3,9 +3,12 @@ package binnie.core.craftgui.minecraft.control;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.nbt.NBTTagList;
import binnie.core.craftgui.IWidget;
import binnie.core.craftgui.controls.core.Control;
import binnie.core.craftgui.minecraft.InventoryType;
import binnie.core.network.packet.MessageCraftGUI;
public class ControlPlayerInventory extends Control {
@@ -57,17 +60,21 @@ public class ControlPlayerInventory extends Control {
}
public void create() {
final NBTTagList actions = new NBTTagList();
for (int row = 0; row < 3; ++row) {
for (int column = 0; column < 9; ++column) {
ControlSlot slot = slots.get(column + row * 9);
slot.assign(InventoryType.Player, 9 + column + row * 9);
slot.assign(actions, InventoryType.Player, 9 + column + row * 9);
}
}
for (int i1 = 0; i1 < 9; ++i1) {
ControlSlot slot2 = slots.get(27 + i1);
slot2.assign(InventoryType.Player, i1);
slot2.assign(actions, InventoryType.Player, i1);
}
MessageCraftGUI.sendToServer(actions);
}
public ControlSlot getSlot(int i) {
@@ -1,5 +1,11 @@
package binnie.core.craftgui.minecraft.control;
import static binnie.core.craftgui.minecraft.ContainerCraftGUI.ACTION_TYPE;
import static binnie.core.craftgui.minecraft.ContainerCraftGUI.SLOT_INDEX;
import static binnie.core.craftgui.minecraft.ContainerCraftGUI.SLOT_NUMBER;
import static binnie.core.craftgui.minecraft.ContainerCraftGUI.SLOT_REG;
import static binnie.core.craftgui.minecraft.ContainerCraftGUI.SLOT_TYPE;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -10,6 +16,8 @@ import net.minecraft.client.multiplayer.PlayerControllerMP;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.IIcon;
import binnie.core.craftgui.CraftGUI;
@@ -26,6 +34,7 @@ import binnie.core.craftgui.resource.minecraft.CraftGUITexture;
import binnie.core.machines.inventory.InventorySlot;
import binnie.core.machines.inventory.MachineSide;
import binnie.core.machines.inventory.SlotValidator;
import binnie.core.network.packet.MessageCraftGUI;
import binnie.core.util.I18N;
public class ControlSlot extends ControlSlotBase {
@@ -179,14 +188,28 @@ public class ControlSlot extends ControlSlotBase {
return null;
}
public ControlSlot assign(int index) {
return assign(InventoryType.Machine, index);
/// Assign a slot and register it immediately. Should be used when no other actions need to be sent.
@SuppressWarnings("UnusedReturnValue")
public ControlSlot assignAndRegister(InventoryType inventoryType, int slotIndex) {
final NBTTagList actions = new NBTTagList();
assign(actions, inventoryType, slotIndex);
MessageCraftGUI.sendToServer(actions);
return this;
}
public ControlSlot assign(InventoryType inventory, int index) {
if (slot == null) {
slot = ((Window) getSuperParent()).getContainer().getOrCreateSlot(inventory, index);
}
/// Assign a slot and defer registration using an `NBTTagList`.
/// Use in combination with {@link MessageCraftGUI#sendToServer(NBTTagList)}.
public ControlSlot assign(NBTTagList actions, InventoryType inventoryType, int slotIndex) {
if (slot == null) slot = ((Window) getSuperParent()).getContainer().getOrCreateSlot(inventoryType, slotIndex);
NBTTagCompound slotReg = new NBTTagCompound();
slotReg.setString(ACTION_TYPE, SLOT_REG);
slotReg.setByte(SLOT_TYPE, (byte) inventoryType.ordinal());
slotReg.setShort(SLOT_INDEX, (short) slotIndex);
slotReg.setShort(SLOT_NUMBER, (short) slot.slotNumber);
actions.appendTag(slotReg);
return this;
}
@@ -4,9 +4,12 @@ import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import net.minecraft.nbt.NBTTagList;
import binnie.core.craftgui.IWidget;
import binnie.core.craftgui.controls.core.Control;
import binnie.core.craftgui.minecraft.InventoryType;
import binnie.core.network.packet.MessageCraftGUI;
public class ControlSlotArray extends Control implements Iterable<ControlSlot> {
@@ -30,11 +33,17 @@ public class ControlSlotArray extends Control implements Iterable<ControlSlot> {
return create(InventoryType.Machine, index);
}
public ControlSlotArray create(InventoryType type, int[] index) {
public ControlSlotArray create(InventoryType inventoryType, int[] index) {
final NBTTagList actions = new NBTTagList();
int i = 0;
for (ControlSlot slot : slots) {
slot.assign(type, index[i++]);
for (ControlSlot controlSlot : slots) {
final int slotIndex = index[i];
controlSlot.assign(actions, inventoryType, slotIndex);
i++;
}
MessageCraftGUI.sendToServer(actions);
return this;
}
@@ -15,6 +15,7 @@ import net.minecraft.init.Items;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import binnie.core.AbstractMod;
import binnie.core.BinnieCore;
@@ -42,6 +43,7 @@ import binnie.core.craftgui.geometry.Position;
import binnie.core.craftgui.minecraft.Dialog;
import binnie.core.craftgui.minecraft.EnumColor;
import binnie.core.craftgui.minecraft.IWindowAffectsShiftClick;
import binnie.core.craftgui.minecraft.InventoryType;
import binnie.core.craftgui.minecraft.MinecraftGUI;
import binnie.core.craftgui.minecraft.Window;
import binnie.core.craftgui.minecraft.control.ControlItemDisplay;
@@ -55,6 +57,7 @@ import binnie.core.craftgui.resource.minecraft.CraftGUITexture;
import binnie.core.craftgui.window.Panel;
import binnie.core.machines.Machine;
import binnie.core.machines.transfer.TransferRequest;
import binnie.core.network.packet.MessageCraftGUI;
import binnie.core.util.I18N;
import binnie.genetics.craftgui.WindowMachine;
import cpw.mods.fml.relauncher.Side;
@@ -459,8 +462,10 @@ public class WindowCompartment extends WindowMachine implements IWindowAffectsSh
slotGrid.deleteAllChildren();
slotGrid.setSize(new IPoint(width, height));
final NBTTagList actions = new NBTTagList();
for (int k : slotIds.keySet()) {
new ControlSlot(slotGrid, x, y).assign(k);
new ControlSlot(slotGrid, x, y).assign(actions, InventoryType.Machine, k);
x += 18;
if (x >= 108) {
x = 0;
@@ -468,6 +473,8 @@ public class WindowCompartment extends WindowMachine implements IWindowAffectsSh
}
}
MessageCraftGUI.sendToServer(actions);
while (y < 108 || x != 0) {
new ControlSlot(slotGrid, x, y);
x += 18;
@@ -1,7 +1,13 @@
package binnie.core.network.packet;
import net.minecraft.nbt.NBTTagCompound;
import static binnie.core.craftgui.minecraft.ContainerCraftGUI.ACTIONS;
import static binnie.core.craftgui.minecraft.ContainerCraftGUI.ACTION_BATCH;
import static binnie.core.craftgui.minecraft.ContainerCraftGUI.ACTION_TYPE;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import binnie.core.BinnieCore;
import binnie.core.network.BinnieCorePacketID;
public class MessageCraftGUI extends MessageNBT {
@@ -13,4 +19,12 @@ public class MessageCraftGUI extends MessageNBT {
public MessageCraftGUI(NBTTagCompound action) {
super(BinnieCorePacketID.CraftGUIAction.ordinal(), action);
}
public static void sendToServer(NBTTagList actions) {
NBTTagCompound actionsBatch = new NBTTagCompound();
actionsBatch.setString(ACTION_TYPE, ACTION_BATCH);
actionsBatch.setTag(ACTIONS, actions);
final MessageCraftGUI packet = new MessageCraftGUI(actionsBatch);
BinnieCore.proxy.sendToServer(packet);
}
}