Compare commits

...
Author SHA1 Message Date
Martin Robertz cb697002d5 update to pre version 2025-12-09 13:12:23 +01:00
Martin Robertz c7b7baa80b sa+update 2025-12-09 13:11:54 +01:00
Martin Robertz cddadf308b Merge remote-tracking branch 'michaeldoylecs/creative-like-pick-block' into dev 2025-12-09 13:04:56 +01:00
Martin Robertz 9b529607c5 Merge remote-tracking branch 'danyadev/certus-quartz-tank-drain-from-above' into dev 2025-12-09 13:03:41 +01:00
Martin Robertz 8450096b04 Merge remote-tracking branch 'lc-1337/gui-reaper' into dev 2025-12-09 13:03:23 +01:00
Kogepan229 3e24f7ba47 Wireless Terminal infinite check 2025-12-08 11:24:45 +09:00
lc-1337 93688313e6 replenisher fixes, 2.8 2.9 (#373) 2025-12-07 14:27:14 +01:00
Michael Doyle 75c62058d4 Remove client-side terminal checks 2025-12-04 20:42:26 -05:00
Michael Doyle f8e40f842b Create dedicated Player Inventory Util class and support WirelessCraftingTerminal 2025-12-04 20:42:26 -05:00
Michael Doyle a106dd7c02 Add JavaDocs 2025-12-04 20:42:21 -05:00
Michael Doyle aea1ce8dd0 Create and use Util function to get any wireless terminal 2025-12-04 20:41:52 -05:00
Michael Doyle 4cf57ee42f Add Keybind to pull looked-at item into inventory from AE2 System 2025-12-04 20:41:38 -05:00
lc-1337 22ac5eee31 Merge remote-tracking branch 'origin/gui-reaper' into gui-reaper 2025-11-24 20:31:15 +02:00
lc-1337 87c8b1ec2a fix client crash on fresh ultra terminal 2025-11-24 20:31:07 +02:00
lc_1337 1d0a1bde9c fix bind 2025-11-24 12:17:20 +02:00
lc-1337 f77014fafc fix inf loop 2025-11-21 21:56:22 +02:00
Martin Robertz 33dba5ae02 Merge branch 'master' into gui-reaper 2025-11-15 21:16:02 +01:00
Martin Robertz c64fe075ad Merge branch 'master' into certus-quartz-tank-drain-from-above 2025-11-15 21:15:53 +01:00
danyadev b95dc4df18 support comparators for Certus Quartz Tank (#349) 2025-11-15 21:15:38 +01:00
Martin Robertz 3098b870f1 Merge branch 'master' into certus-quartz-tank-drain-from-above 2025-11-15 10:43:44 +01:00
danyadev d8242779a5 allow draining tank tower from an empty tank above 2025-11-15 01:30:49 +03:00
17 changed files with 527 additions and 55 deletions

No files matched your search

+9 -9
View File
@@ -34,29 +34,29 @@
* For more details, see https://docs.gradle.org/8.0.1/userguide/java_library_plugin.html#sec:java_library_configurations_graph
*/
dependencies {
api('com.github.GTNewHorizons:NotEnoughItems:2.8.33-GTNH:dev')
api('com.github.GTNewHorizons:Applied-Energistics-2-Unofficial:rv3-beta-732-GTNH:dev')
api('com.github.GTNewHorizons:NotEnoughItems:2.8.40-GTNH:dev')
api('com.github.GTNewHorizons:Applied-Energistics-2-Unofficial:rv3-beta-753-GTNH-pre:dev')
api('curse.maven:cofh-core-69162:2388751')
api('com.github.GTNewHorizons:waila:1.9.7:dev')
api("com.github.GTNewHorizons:GTNHLib:0.8.0:dev")
api('com.github.GTNewHorizons:waila:1.9.15:dev')
api("com.github.GTNewHorizons:GTNHLib:0.8.25:dev")
implementation("com.github.GTNewHorizons:WirelessCraftingTerminal:99:dev") {
implementation("com.github.GTNewHorizons:WirelessCraftingTerminal:1.12.7:dev") {
exclude group: 'com.github.GTNewHorizons', module: 'Applied-Energistics-2-Unofficial'
}
compileOnly('com.github.GTNewHorizons:Baubles-Expanded:2.2.2-GTNH:dev')
compileOnly('com.github.GTNewHorizons:ExtraCells2:2.5.35:dev') { transitive = false }
compileOnly('com.github.GTNewHorizons:ForestryMC:4.11.2:dev')
compileOnly('com.github.GTNewHorizons:EnderIO:2.10.3:dev')
compileOnly('com.github.GTNewHorizons:GT5-Unofficial:5.09.52.99:dev') {
compileOnly('com.github.GTNewHorizons:EnderIO:2.10.6:dev')
compileOnly('com.github.GTNewHorizons:GT5-Unofficial:5.09.52.134:dev') {
exclude group: 'com.github.GTNewHorizons', module: 'AE2FluidCraft-Rework'
exclude group: 'com.github.GTNewHorizons', module: 'Applied-Energistics-2-Unofficial'
}
compileOnly('thaumcraft:Thaumcraft:1.7.10-4.2.3.5:dev')
compileOnly('com.gregoriust.gregtech:gregtech_1.7.10:6.14.23:dev') { transitive = false }
compileOnly('com.github.GTNewHorizons:OpenComputers:1.12.4-GTNH:dev') { transitive = false }
compileOnly('com.github.GTNewHorizons:ThaumicEnergistics:1.7.14-GTNH:dev') { transitive = false }
compileOnly('com.github.GTNewHorizons:OpenComputers:1.12.6-GTNH:dev') { transitive = false }
compileOnly('com.github.GTNewHorizons:ThaumicEnergistics:1.7.16-GTNH:dev') { transitive = false }
runtimeOnlyNonPublishable("com.github.GTNewHorizons:DuraDisplay:1.4.0:dev")
}
@@ -16,6 +16,7 @@ import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.FluidContainerRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTankInfo;
import net.minecraftforge.fluids.IFluidContainerItem;
import com.glodblock.github.common.item.ItemCertusQuartzTank;
@@ -242,4 +243,22 @@ public class BlockCertusQuartzTank extends BaseBlockContainer implements IRegist
setCreativeTab(FluidCraftingTabs.INSTANCE);
return this;
}
@Override
public boolean hasComparatorInputOverride() {
return true;
}
@Override
public int getComparatorInputOverride(World worldIn, int x, int y, int z, int side) {
TileEntity tileEntity = worldIn.getTileEntity(x, y, z);
if (tileEntity instanceof TileCertusQuartzTank tileCertusQuartzTank) {
FluidTankInfo info = tileCertusQuartzTank.getTankInfo(false)[0];
if (info.fluid != null) {
int nonEmptyBump = info.fluid.amount > 0 ? 1 : 0;
return (int) (14.0 * info.fluid.amount / info.capacity) + nonEmptyBump;
}
}
return 0;
}
}
@@ -12,6 +12,7 @@ import net.minecraft.client.gui.GuiScreen;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
@@ -100,6 +101,7 @@ public class ItemBaseWirelessTerminal extends ToolWirelessTerminal implements II
public static UltraTerminalModes getMode(ItemStack is) {
Item item = is.getItem();
if (item instanceof ItemWirelessUltraTerminal) {
if (!is.hasTagCompound()) is.setTagCompound(new NBTTagCompound());
return UltraTerminalModes.values()[is.getTagCompound().getInteger(MODE)];
} else if (item instanceof ItemWirelessPatternTerminal) {
return UltraTerminalModes.PATTERN;
@@ -144,4 +146,16 @@ public class ItemBaseWirelessTerminal extends ToolWirelessTerminal implements II
if (wh == null) return null;
return new WirelessTerminalGuiObject(wh, is, p, world, x, y, z);
}
@Override
public boolean hasInfinityPower(ItemStack is) {
NBTTagCompound data = is.getTagCompound();
return data != null && data.hasKey(infinityEnergyCard) && data.getBoolean(infinityEnergyCard);
}
@Override
public boolean hasInfinityRange(ItemStack is) {
NBTTagCompound data = is.getTagCompound();
return data != null && data.hasKey(infinityBoosterCard) && data.getBoolean(infinityBoosterCard);
}
}
@@ -41,6 +41,6 @@ public class ItemWirelessInterfaceTerminal extends ItemBaseWirelessTerminal
public IGuiItemObject getGuiObject(ItemStack is, World world, EntityPlayer p, int x, int y, int z) {
final IWirelessTermHandler wh = AEApi.instance().registries().wireless().getWirelessTerminalHandler(is);
if (wh == null) return null;
return new WirelessInterfaceTerminalGuiObject(wh, is);
return new WirelessInterfaceTerminalGuiObject(wh, is, x);
}
}
@@ -129,7 +129,9 @@ public class ItemWirelessUltraTerminal extends ItemBaseWirelessTerminal
if (terminal == null) return;
if (mode != null) setMode(terminal, mode);
else mode = getMode(terminal);
openGui(terminal, player.worldObj, player, mode, term.getLeft());
if (AEApi.instance().registries().wireless().performCheck(terminal, player))
openGui(terminal, player.worldObj, player, mode, term.getLeft());
}
}
@@ -217,7 +219,7 @@ public class ItemWirelessUltraTerminal extends ItemBaseWirelessTerminal
return switch (y != Integer.MIN_VALUE ? UltraTerminalModes.values()[y] : getMode(is)) {
case CRAFTING -> new WirelessCraftingTerminalGuiObject(wh, is, p, world, x, y, z);
case PATTERN, PATTERN_EX -> new WirelessPatternTerminalGuiObject(wh, is, p, world, x, y, z);
case INTERFACE -> new WirelessInterfaceTerminalGuiObject(wh, is);
case INTERFACE -> new WirelessInterfaceTerminalGuiObject(wh, is, x);
default -> super.getGuiObject(is, world, p, x, y, z);
};
}
@@ -95,10 +95,10 @@ public class PartFluidImportBus extends PartBaseImportBus<IAEFluidStack> {
final IMEMonitor<IAEFluidStack> inv, final IEnergySource energy, final FuzzyMode fzMode) {
if (myTarget instanceof IFluidHandler fh) {
FluidTankInfo[] tanksInfo = fh.getTankInfo(this.getSide().getOpposite());
if (tanksInfo == null) return false;
if (tanksInfo == null) return true;
int maxDrain = this.calculateAmountToSend();
boolean drained = false;
boolean doBreak = true;
for (FluidTankInfo tankInfo : tanksInfo) {
if (tankInfo.fluid == null) continue;
@@ -117,14 +117,14 @@ public class PartFluidImportBus extends PartBaseImportBus<IAEFluidStack> {
fh.drain(this.getSide().getOpposite(), aeFluidStack.getFluidStack(), true);
maxDrain -= aeFluidStack.getFluidStack().amount;
drained = true;
doBreak = false;
}
}
return drained;
return doBreak;
}
return false;
return true;
}
@Override
@@ -33,42 +33,57 @@ public class TileCertusQuartzTank extends TileEntity implements IFluidHandler {
return tankFluid == null || tankFluid.amount == 0 ? 0 : tankFluid.getFluid().getLuminosity(tankFluid);
}
public FluidStack drain(FluidStack fluid, boolean doDrain, boolean findMainTank) {
if (fluid == null || this.getFluid() == null || this.getFluid() != fluid.getFluid()) {
public FluidStack drain(Fluid fluid, int amount, boolean doDrain, boolean findMainTank) {
if (fluid != null && this.getFluid() != fluid) {
return null;
}
if (findMainTank) {
// Main tank is the highest tank in the column with desired fluid
TileCertusQuartzTank mainTank = this;
TileCertusQuartzTank tankAbove;
while ((tankAbove = mainTank.getTankAbove()) != null) {
// We can't drain tank with different or null fluid
if (fluid.getFluid() != tankAbove.getFluid()) {
break;
if (this.tank.getFluid() != null) {
// Current tank has a fluid, searching for the highest tank with this fluid
TileCertusQuartzTank tankAbove;
while ((tankAbove = mainTank.getTankAbove()) != null) {
// We can't drain tank with different or null fluid
if (tankAbove.getFluid() == null || mainTank.getFluid() != tankAbove.getFluid()) {
break;
}
mainTank = tankAbove;
}
} else {
// Current tank doesn't have a fluid, searching for the first non-empty tank below
TileCertusQuartzTank tankBelow;
while ((tankBelow = mainTank.getTankBelow()) != null) {
// We can't drain tank with different fluid
if (fluid != null && tankBelow.getFluid() != fluid) {
break;
}
mainTank = tankBelow;
if (tankBelow.getFluid() != null) {
break;
}
}
mainTank = tankAbove;
}
return mainTank.drain(fluid, doDrain, false);
return mainTank.drain(fluid, amount, doDrain, false);
}
FluidStack drainedFluid = this.tank.drain(fluid.amount, doDrain);
FluidStack drainedFluid = this.tank.drain(amount, doDrain);
int drained = drainedFluid != null ? drainedFluid.amount : 0;
if (drained > 0) {
update();
}
if (drained < fluid.amount) {
if (drained < amount) {
TileCertusQuartzTank tankBelow = this.getTankBelow();
if (tankBelow != null) {
FluidStack fluidToDrain = new FluidStack(fluid.getFluid(), fluid.amount - drained);
FluidStack externallyDrained = tankBelow.drain(fluidToDrain, doDrain, false);
FluidStack externallyDrained = tankBelow.drain(fluid, amount - drained, doDrain, false);
if (externallyDrained != null) {
return new FluidStack(fluid.getFluid(), drained + externallyDrained.amount);
return new FluidStack(fluid, drained + externallyDrained.amount);
}
}
}
@@ -78,14 +93,14 @@ public class TileCertusQuartzTank extends TileEntity implements IFluidHandler {
@Override
public FluidStack drain(ForgeDirection from, FluidStack resource, boolean doDrain) {
return drain(resource, doDrain, true);
if (resource == null) return null;
return drain(resource.getFluid(), resource.amount, doDrain, true);
}
@Override
public FluidStack drain(ForgeDirection from, int maxDrain, boolean doDrain) {
if (this.tank.getFluid() == null) return null;
return drain(from, new FluidStack(this.tank.getFluid(), maxDrain), doDrain);
return drain(null, maxDrain, true, true);
}
public int fill(FluidStack fluid, boolean doFill, boolean findMainTank) {
@@ -174,14 +189,14 @@ public class TileCertusQuartzTank extends TileEntity implements IFluidHandler {
}
final FluidStack tankFluidStack = this.tank.getFluid();
final Fluid mainFluid = tankFluidStack != null ? tankFluidStack.getFluid() : null;
Fluid mainFluid = tankFluidStack != null ? tankFluidStack.getFluid() : null;
int amount = tankFluidStack == null ? 0 : tankFluidStack.amount;
int capacity = this.tank.getCapacity();
TileCertusQuartzTank tankAbove = this;
while ((tankAbove = tankAbove.getTankAbove()) != null) {
if (tankAbove.getFluid() == null || mainFluid != tankAbove.getFluid()) {
if (tankAbove.getFluid() != null && mainFluid != tankAbove.getFluid()) {
break;
}
@@ -192,7 +207,10 @@ public class TileCertusQuartzTank extends TileEntity implements IFluidHandler {
TileCertusQuartzTank tankBelow = this;
while ((tankBelow = tankBelow.getTankBelow()) != null) {
if (mainFluid == null || mainFluid != tankBelow.getFluid()) {
if (mainFluid == null && tankBelow.getFluid() != null) {
mainFluid = tankBelow.getFluid();
}
if (mainFluid != tankBelow.getFluid()) {
break;
}
@@ -255,24 +255,24 @@ public class TileSuperStockReplenisher extends AENetworkInvTile
public void onChangeInventory(IInventory inv, int slot, InvOperation mc, ItemStack removed, ItemStack added) {
switch (mc) {
case setInventorySlotContents -> {
if (added != null) {
if (inv == cell) {
if (inv == cell) {
if (added != null) {
if (added.getItem() instanceof ItemBasicStorageCell ibsc) {
totalBytes = ibsc.getBytesLong(added);
} else if (added.getItem() instanceof FCBaseItemCell fcbic) {
totalBytes = fcbic.getBytes(added);
}
getProxy().setIdlePowerUsage(Math.sqrt(Math.pow(totalBytes, 0.576D)));
} else if (inv == invItems) {
storedItemCount += added.stackSize;
}
}
if (removed != null) {
if (inv == cell) {
} else if (removed != null) {
totalBytes = 0;
getProxy().setIdlePowerUsage(4d);
} else if (inv == invItems) {
}
} else if (inv == invItems) {
if (added != null) {
storedItemCount += added.stackSize;
}
if (removed != null) {
storedItemCount -= removed.stackSize;
}
}
@@ -282,11 +282,12 @@ public class TileSuperStockReplenisher extends AENetworkInvTile
totalBytes = 0;
getProxy().setIdlePowerUsage(4d);
}
if (inv == invItems) {
storedItemCount -= removed.stackSize;
}
}
case markDirty -> {}
case markDirty -> markDirty();
}
try {
@@ -20,9 +20,11 @@ public class CertusQuartzTankWailaDataProvider extends BaseWailaDataProvider {
TileEntity tileEntity = accessor.getTileEntity();
if (tileEntity instanceof TileCertusQuartzTank quartzTank) {
FluidTankInfo info = quartzTank.getTankInfo(true)[0];
if (info.fluid != null) {
currentToolTip
.add(Tooltip.fluidFormat(info.fluid.getLocalizedName(), info.fluid.amount, info.capacity));
if (info.fluid == null) {
currentToolTip.add(Tooltip.tankEmptyFormat(info.capacity));
} else {
currentToolTip.add(
Tooltip.tankWithFluidFormat(info.fluid.getLocalizedName(), info.fluid.amount, info.capacity));
}
}
return currentToolTip;
@@ -15,14 +15,18 @@ public class Tooltip {
return String.format("%s: %s mB", name, NumberFormat.getInstance().format(amount));
}
public static String fluidFormat(String name, long amount, long capacity) {
public static String tankWithFluidFormat(String fluidName, long amount, long capacity) {
return String.format(
"%s: %s mB / %s mB",
name,
fluidName,
NumberFormat.getInstance().format(amount),
NumberFormat.getInstance().format(capacity));
}
public static String tankEmptyFormat(long capacity) {
return String.format("0 mB / %s mB", NumberFormat.getInstance().format(capacity));
}
public static String partFluidBusFormat(int amount) {
return String
.format("%s: %s mB/t", I18n.format(NameConst.WAILA_SPEED), NumberFormat.getInstance().format(amount));
@@ -6,6 +6,7 @@ import com.glodblock.github.network.CPacketDumpTank;
import com.glodblock.github.network.CPacketFluidPatternTermBtns;
import com.glodblock.github.network.CPacketLevelMaintainer;
import com.glodblock.github.network.CPacketLevelTerminalCommands;
import com.glodblock.github.network.CPacketPickBlockWithdraw;
import com.glodblock.github.network.CPacketSwitchGuis;
import com.glodblock.github.network.CPacketValueConfig;
import com.glodblock.github.network.SPacketCustomButtonUpdate;
@@ -57,6 +58,11 @@ public class ChannelLoader implements Runnable {
SPacketLevelMaintainerGuiUpdate.class,
id++,
Side.CLIENT);
netHandler.registerMessage(
new CPacketPickBlockWithdraw.Handler(),
CPacketPickBlockWithdraw.class,
id++,
Side.SERVER);
netHandler.registerMessage(
new SPacketCustomButtonUpdate.Handler(),
SPacketCustomButtonUpdate.class,
@@ -4,11 +4,13 @@ import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityClientPlayerMP;
import net.minecraft.client.settings.KeyBinding;
import net.minecraft.item.ItemStack;
import net.minecraft.util.MovingObjectPosition;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.lwjgl.input.Keyboard;
import com.glodblock.github.FluidCraft;
import com.glodblock.github.network.CPacketPickBlockWithdraw;
import com.glodblock.github.network.CPacketSwitchGuis;
import com.glodblock.github.network.CPacketValueConfig;
import com.glodblock.github.util.Util;
@@ -25,13 +27,16 @@ public class KeybindLoader implements Runnable {
public static KeyBinding openTerminal;
public static KeyBinding restock;
public static KeyBinding pickBlock;
@Override
public void run() {
openTerminal = new KeyBinding(FluidCraft.MODID + ".key.OpenTerminal", Keyboard.CHAR_NONE, "itemGroup.ae2fc");
restock = new KeyBinding(FluidCraft.MODID + ".key.Restock", Keyboard.CHAR_NONE, "itemGroup.ae2fc");
pickBlock = new KeyBinding(FluidCraft.MODID + ".key.PickBlock", Keyboard.CHAR_NONE, "itemGroup.ae2fc");
ClientRegistry.registerKeyBinding(openTerminal);
ClientRegistry.registerKeyBinding(restock);
ClientRegistry.registerKeyBinding(pickBlock);
FMLCommonHandler.instance().bus().register(this);
}
@@ -42,6 +47,8 @@ public class KeybindLoader implements Runnable {
handleOpenTerminalKey();
} else if (restock.isPressed()) {
handleRestockKey();
} else if (pickBlock.isPressed()) {
handlePickBlockKey();
}
}
@@ -52,6 +59,8 @@ public class KeybindLoader implements Runnable {
handleOpenTerminalKey();
} else if (restock.isPressed()) {
handleRestockKey();
} else if (pickBlock.isPressed()) {
handlePickBlockKey();
}
}
@@ -75,4 +84,22 @@ public class KeybindLoader implements Runnable {
FluidCraft.proxy.netHandler.sendToServer(new CPacketValueConfig(0, 1));
}
}
private void handlePickBlockKey() {
Minecraft minecraft = Minecraft.getMinecraft();
if (minecraft.currentScreen != null) return; // Don't act if a GUI is open
EntityClientPlayerMP player = minecraft.thePlayer;
if (player == null) return;
// Get the block the player is currently looking at
MovingObjectPosition movingObject = minecraft.objectMouseOver;
if (movingObject == null || movingObject.typeOfHit != MovingObjectPosition.MovingObjectType.BLOCK) {
return;
}
// Send packet to server with block coordinates
FluidCraft.proxy.netHandler.sendToServer(
new CPacketPickBlockWithdraw(movingObject.blockX, movingObject.blockY, movingObject.blockZ));
}
}
@@ -0,0 +1,247 @@
package com.glodblock.github.network;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ChatComponentText;
import net.minecraft.world.World;
import net.p455w0rd.wirelesscraftingterminal.common.container.ContainerWirelessCraftingTerminal;
import net.p455w0rd.wirelesscraftingterminal.common.utils.RandomUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import com.glodblock.github.util.PlayerInventoryUtil;
import com.glodblock.github.util.Util;
import appeng.api.AEApi;
import appeng.api.config.Actionable;
import appeng.api.networking.security.PlayerSource;
import appeng.api.storage.IMEInventoryHandler;
import appeng.api.storage.StorageChannel;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IAEStack;
import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
import io.netty.buffer.ByteBuf;
/**
* Network packet sent from client to server when a player looks at a block and requests to withdraw that block type
* from their wireless terminal's ME network.
*
* <p>
* This packet handles the entire withdrawal flow including:
* <ul>
* <li>Scanning the player's inventory for existing stacks of the target block</li>
* <li>Consolidating partial stacks to maximize inventory efficiency</li>
* <li>Extracting the appropriate amount from the ME network to fill or create a full stack</li>
* <li>Setting the target stack as the player's active hotbar slot for immediate use</li>
* </ul>
*
* <p>
* The packet carries the coordinates of the block the player is looking at, which determines what item type to withdraw
* from the ME network.
*/
public class CPacketPickBlockWithdraw implements IMessage {
private int blockX;
private int blockY;
private int blockZ;
public CPacketPickBlockWithdraw() {
// Required for FML
}
public CPacketPickBlockWithdraw(int x, int y, int z) {
this.blockX = x;
this.blockY = y;
this.blockZ = z;
}
@Override
public void fromBytes(ByteBuf buf) {
this.blockX = buf.readInt();
this.blockY = buf.readInt();
this.blockZ = buf.readInt();
}
@Override
public void toBytes(ByteBuf buf) {
buf.writeInt(this.blockX);
buf.writeInt(this.blockY);
buf.writeInt(this.blockZ);
}
public static class Handler implements IMessageHandler<CPacketPickBlockWithdraw, IMessage> {
@Override
public IMessage onMessage(CPacketPickBlockWithdraw message, MessageContext ctx) {
EntityPlayerMP player = ctx.getServerHandler().playerEntity;
if (player == null || player.inventory == null) {
return null;
}
World world = player.worldObj;
// Ensure the player has a wireless terminal
IMEInventoryHandler<IAEItemStack> terminalInventory = getWirelessInventory(player);
if (terminalInventory == null) {
player.addChatMessage(new ChatComponentText("Could not access AE2 network."));
return null;
}
// Get the target block
Block targetBlock = world.getBlock(message.blockX, message.blockY, message.blockZ);
if (targetBlock == Blocks.air) {
return null; // Don't try to withdraw air
}
Item targetItem = Item.getItemFromBlock(targetBlock);
if (targetItem == null) {
return null; // Should not happen for non-air blocks
}
// Check player inventory for existing stacks to determine how much to withdraw.
ItemStack itemToFind = new ItemStack(
targetItem,
1,
world.getBlockMetadata(message.blockX, message.blockY, message.blockZ));
// 1. Scan through the player's main inventory to categorize existing stacks of the target block:
// - If a full stack (stackSize >= maxStackSize) is found, record its slot and stop searching.
// This indicates the player already has the maximum possible stack, so no withdrawal is needed.
// - Otherwise, collect all partial stack slots (stacks that match the item but aren't full).
// Partial stacks will be consolidated in a later step.
int fullStackSlot = -1;
List<Integer> partialStackSlotsList = new ArrayList<>();
for (int i = 0; i < player.inventory.mainInventory.length; i++) {
ItemStack stackInSlot = player.inventory.mainInventory[i];
if (stackInSlot != null && stackInSlot.isItemEqual(itemToFind)
&& ItemStack.areItemStackTagsEqual(stackInSlot, itemToFind)) {
if (stackInSlot.stackSize >= stackInSlot.getMaxStackSize()) {
fullStackSlot = i;
break; // Found a full stack, no need to do anything.
}
partialStackSlotsList.add(i);
}
}
// 2. If a full stack already exists, put in active slot and return.
if (fullStackSlot >= 0) {
PlayerInventoryUtil.setSlotAsActiveSlot(player, fullStackSlot);
return null;
}
// 3. If there are no partial stacks and the player's inventory is full,
// then return since we cannot add a retrieved stack to a full inventory
int nextEmptySlot = PlayerInventoryUtil.getFirstEmptyStackReverse(player.inventory);
if (partialStackSlotsList.isEmpty() && nextEmptySlot == -1) {
return null;
}
// 4. Consolidate all partial stacks of target block into 1 ItemStack.
// If a full stack is obtained, set it as the active slot and return.
ItemStack consolidatedStack = null;
int consolidatedStackSlot = -1;
for (Integer partialStackSlot : partialStackSlotsList) {
if (consolidatedStack == null) {
consolidatedStack = player.inventory.getStackInSlot(partialStackSlot);
consolidatedStackSlot = partialStackSlot;
} else {
PlayerInventoryUtil
.consolidateItemStacks(player.inventory, partialStackSlot, consolidatedStackSlot);
}
// Check if we created a full stack of items
if (consolidatedStack.stackSize == consolidatedStack.getMaxStackSize()) {
PlayerInventoryUtil.setSlotAsActiveSlot(player, consolidatedStackSlot);
return null;
}
}
// 5. Calculate withdrawal amount
int amountToWithdraw = consolidatedStack == null ? itemToFind.getMaxStackSize()
: itemToFind.getMaxStackSize() - consolidatedStack.stackSize;
if (amountToWithdraw <= 0) {
return null;
}
// Create an IAEItemStack for the target block with the calculated amount
ItemStack targetItemStack = itemToFind.copy();
targetItemStack.stackSize = amountToWithdraw;
IAEItemStack targetAeItemStack = AEApi.instance().storage().createItemStack(targetItemStack);
if (targetAeItemStack == null) {
return null;
}
// 6. Extract items from the network
PlayerSource source = new PlayerSource(player, null);
IAEStack<?> extractedStack = terminalInventory.extractItems(targetAeItemStack, Actionable.MODULATE, source);
if (extractedStack instanceof IAEItemStack extractedAeItemStack && extractedStack.getStackSize() > 0) {
ItemStack itemsToGive = extractedAeItemStack.getItemStack();
// Update the player's inventory with the withdrawn items
if (itemsToGive != null && itemsToGive.stackSize > 0) {
if (consolidatedStack == null) {
player.inventory.setInventorySlotContents(nextEmptySlot, itemsToGive);
} else {
consolidatedStack.stackSize += itemsToGive.stackSize;
}
player.inventory.markDirty();
}
}
// If the target stack is already in the player's hotbar, set that as the active slot.
// Otherwise, move the target stack to the active slot.
// The slot to swap will have either been a consolidated stack of partial ItemStacks,
// or it will have been a newly created ItemStack in the next empty slot.
int slotToSwap = consolidatedStack == null ? nextEmptySlot : consolidatedStackSlot;
PlayerInventoryUtil.setSlotAsActiveSlot(player, slotToSwap);
return null; // No reply packet needed
}
@SuppressWarnings("unchecked")
private IMEInventoryHandler<IAEItemStack> getWirelessInventory(EntityPlayerMP player) {
ImmutablePair<Integer, ItemStack> terminalAndInventorySlot = Util.getWirelessTerminal(player);
ItemStack wctTerminal = null;
if (terminalAndInventorySlot == null) {
// Secondary check, does the player have a WirelessCraftingTerminal terminal
// Why is this mod still in GTNH ;-;
wctTerminal = RandomUtils.getWirelessTerm(player.inventory);
if (wctTerminal == null) {
return null;
}
}
IMEInventoryHandler<IAEItemStack> inventoryHandler;
if (wctTerminal != null) {
inventoryHandler = ContainerWirelessCraftingTerminal.getGuiObject(
wctTerminal,
player,
player.worldObj,
(int) player.posX,
(int) player.posY,
(int) player.posZ).getItemInventory();
} else {
ItemStack terminalItemStack = terminalAndInventorySlot.getRight();
if (terminalItemStack == null) {
return null;
}
// Obtain the inventory handler for the AE2 wireless terminal.
//
// We can safely suppress this warning, as we are the one providing the storage type in
// the function call.
inventoryHandler = (IMEInventoryHandler<IAEItemStack>) Util
.getWirelessInv(terminalItemStack, player, StorageChannel.ITEMS);
}
return inventoryHandler;
}
}
}
@@ -28,7 +28,7 @@ public class FluidPrimaryGui extends PrimaryGui {
p,
p.worldObj,
new BlockPos(
p.inventory.currentItem,
this.slotIndex == Integer.MIN_VALUE ? p.inventory.currentItem : this.slotIndex,
Util.GuiHelper.encodeType(0, Util.GuiHelper.GuiType.ITEM),
0),
Objects.requireNonNull(side),
@@ -0,0 +1,105 @@
package com.glodblock.github.util;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.network.play.server.S09PacketHeldItemChange;
public class PlayerInventoryUtil {
/**
* Finds the first empty slot in the player's inventory, searching in reverse order (from the last slot to the
* first). This prioritizes filling the inventory from the end, which helps keep commonly accessed hotbar slots
* free.
*
* @param inventory The player's inventory to search.
* @return The index of the first empty slot found (searching backwards), or -1 if the inventory is full.
*/
public static int getFirstEmptyStackReverse(InventoryPlayer inventory) {
for (int i = inventory.mainInventory.length - 1; i >= 0; --i) {
if (inventory.mainInventory[i] == null) {
return i;
}
}
return -1;
}
/**
* Sets the specified inventory slot as the player's active (held) slot. If the target slot is in the hotbar (slots
* 0-8), it directly selects that hotbar slot. If the target slot is outside the hotbar, it swaps the contents of
* that slot with the current active slot.
*
* @param player The player whose active slot should be changed.
* @param slot The inventory slot index to make active (0-8 for hotbar, 9+ for main inventory).
*/
public static void setSlotAsActiveSlot(EntityPlayerMP player, int slot) {
if (slot >= 0 && slot <= 8) {
player.inventory.currentItem = slot;
player.playerNetServerHandler.sendPacket(new S09PacketHeldItemChange(slot));
} else {
swapInventorySlots(player.inventory, player.inventory.currentItem, slot);
}
player.inventory.markDirty();
}
/**
* Moves an ItemStack from a source slot to a destination slot in the player's inventory. If the destination slot is
* occupied, the items are swapped.
*
* @param inventory The player's inventory.
* @param slot1 The index of the first ItemStack to move.
* @param slot2 The index of the second ItemStack to move.
*/
public static void swapInventorySlots(InventoryPlayer inventory, int slot1, int slot2) {
if (slot1 == slot2) {
return;
}
// Get the stacks from both slots
ItemStack sourceStack = inventory.getStackInSlot(slot1);
ItemStack destinationStack = inventory.getStackInSlot(slot2);
// Set the destination slot with the source stack (even if it's null)
inventory.setInventorySlotContents(slot2, sourceStack);
// Set the source slot with the original destination stack
inventory.setInventorySlotContents(slot1, destinationStack);
// Mark the inventory as dirty to ensure changes are saved and synced
inventory.markDirty();
}
/**
* Consolidate ItemStacks from a source slot to a destination slot in the player's inventory.
*
* @param inventory The player's inventory.
* @param sourceSlot The index of the slot to move the item from.
* @param destinationSlot The index of the slot to move the item to.
*/
public static void consolidateItemStacks(InventoryPlayer inventory, int sourceSlot, int destinationSlot) {
ItemStack sourceStack = inventory.getStackInSlot(sourceSlot);
ItemStack destinationStack = inventory.getStackInSlot(destinationSlot);
if (!sourceStack.isItemEqual(destinationStack)
|| !ItemStack.areItemStackTagsEqual(sourceStack, destinationStack)) {
return;
}
int missingQuantity = destinationStack.getMaxStackSize() - destinationStack.stackSize;
if (missingQuantity >= sourceStack.stackSize) {
destinationStack.stackSize = destinationStack.stackSize + sourceStack.stackSize;
sourceStack = null;
} else {
sourceStack.stackSize -= missingQuantity;
destinationStack.stackSize += missingQuantity;
}
// Update the inventory stacks
inventory.setInventorySlotContents(destinationSlot, destinationStack);
inventory.setInventorySlotContents(sourceSlot, sourceStack);
// Mark the inventory as dirty to ensure changes are saved and synced
inventory.markDirty();
}
}
@@ -202,6 +202,32 @@ public final class Util {
}
}
public static ImmutablePair<Integer, ItemStack> getWirelessTerminal(EntityPlayer player) {
// Check player inventory for the wireless terminal.
int inventorySize = player.inventory.getSizeInventory();
for (int i = 0; i < inventorySize; ++i) {
ItemStack is = player.inventory.getStackInSlot(i);
if (is != null && is.getItem() instanceof ToolWirelessTerminal) {
return new ImmutablePair<>(GuiHelper.encodeType(i, GuiHelper.InvType.PLAYER_BAUBLES), is);
}
}
if (isBaublesLoaded) {
IInventory handler = BaublesApi.getBaubles(player);
if (handler != null) {
int baubleSlotCount = handler.getSizeInventory();
for (int i = 0; i < baubleSlotCount; ++i) {
ItemStack is = handler.getStackInSlot(i);
if (is != null && is.getItem() instanceof ToolWirelessTerminal) {
return new ImmutablePair<>(GuiHelper.encodeType(i, GuiHelper.InvType.PLAYER_BAUBLES), is);
}
}
}
}
return null;
}
public static ImmutablePair<Integer, ItemStack> getUltraWirelessTerm(EntityPlayer player) {
int invSize = player.inventory.getSizeInventory();
@@ -222,6 +222,7 @@ error.unknown=Unknown
ae2fc.key.OpenTerminal=Open Wireless Universal Terminal
ae2fc.key.Restock=Toggle Restock
ae2fc.key.PickBlock=AE2 Pick Block
itemGroup.ae2fc=Fluid Craft For AE2