forked from xsun2001/Applied-Energistics-2-Unofficial
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [major] implemented NEI API for bookmark pulling * [minor] added functionality to the wireless terminal * [minor] added functionality to the skystone chests * added remaining guis -better method for inventory checking * [minor] Another rework on fitStack * add missing imports add forgotten import * [minor] -sa, removed redundant code --------- Co-authored-by: Firenoo <49818773+firenoo@users.noreply.github.com> Co-authored-by: Martin Robertz <dream-master@gmx.net>
- Loading branch information
1 parent
a423baa
commit 1564929
Showing
7 changed files
with
264 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
src/main/java/appeng/core/sync/packets/PacketNEIBookmark.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package appeng.core.sync.packets; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.DataOutputStream; | ||
import java.io.IOException; | ||
|
||
import net.minecraft.entity.player.EntityPlayer; | ||
import net.minecraft.entity.player.EntityPlayerMP; | ||
import net.minecraft.inventory.Container; | ||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.nbt.CompressedStreamTools; | ||
import net.minecraft.nbt.NBTTagCompound; | ||
import net.minecraftforge.common.util.ForgeDirection; | ||
|
||
import appeng.api.networking.energy.IEnergySource; | ||
import appeng.api.networking.security.BaseActionSource; | ||
import appeng.api.storage.IMEMonitor; | ||
import appeng.api.storage.data.IAEItemStack; | ||
import appeng.container.implementations.ContainerMEMonitorable; | ||
import appeng.core.sync.AppEngPacket; | ||
import appeng.core.sync.network.INetworkInfo; | ||
import appeng.util.InventoryAdaptor; | ||
import appeng.util.Platform; | ||
import appeng.util.item.AEItemStack; | ||
import io.netty.buffer.ByteBuf; | ||
import io.netty.buffer.Unpooled; | ||
|
||
public class PacketNEIBookmark extends AppEngPacket { | ||
|
||
private ItemStack bookmarkItem; | ||
|
||
// automatic | ||
public PacketNEIBookmark(final ByteBuf stream) throws IOException { | ||
final ByteArrayInputStream bytes = new ByteArrayInputStream(stream.array()); | ||
bytes.skip(stream.readerIndex()); | ||
final NBTTagCompound comp = CompressedStreamTools.readCompressed(bytes); | ||
if (comp != null) { | ||
this.bookmarkItem = ItemStack.loadItemStackFromNBT(comp); | ||
} | ||
} | ||
|
||
// api | ||
public PacketNEIBookmark(final NBTTagCompound bookmarkItemComp) throws IOException { | ||
final ByteBuf data = Unpooled.buffer(); | ||
|
||
final ByteArrayOutputStream bytes = new ByteArrayOutputStream(); | ||
final DataOutputStream outputStream = new DataOutputStream(bytes); | ||
|
||
data.writeInt(this.getPacketID()); | ||
|
||
CompressedStreamTools.writeCompressed(bookmarkItemComp, outputStream); | ||
data.writeBytes(bytes.toByteArray()); | ||
|
||
this.configureWrite(data); | ||
} | ||
|
||
@Override | ||
public void serverPacketData(final INetworkInfo manager, final AppEngPacket packet, final EntityPlayer player) { | ||
bookmarkItem.stackSize = fitStack(player, bookmarkItem); | ||
final EntityPlayerMP pmp = (EntityPlayerMP) player; | ||
final Container con = pmp.openContainer; | ||
|
||
if (con instanceof ContainerMEMonitorable monitorable) { | ||
final IMEMonitor<IAEItemStack> monitor = monitorable.getMonitor(); | ||
if (monitor != null) { | ||
final IEnergySource energy = monitorable.getPowerSource(); | ||
final BaseActionSource actionSource = monitorable.getActionSource(); | ||
|
||
final AEItemStack request = AEItemStack.create(bookmarkItem); | ||
final IAEItemStack out = Platform.poweredExtraction(energy, monitor, request, actionSource); | ||
if (out != null) { | ||
final InventoryAdaptor adp = InventoryAdaptor.getAdaptor(player, ForgeDirection.UNKNOWN); | ||
ItemStack outItem = out.getItemStack(); | ||
adp.addItems(outItem); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private int fitStack(final EntityPlayer player, ItemStack itemStack) { | ||
// Check if the bookmark item fits fully or partially into the inventory | ||
// itemStack will always be <= maxStackSize because of how the packets are received | ||
ItemStack[] inv = player.inventory.mainInventory; | ||
int remainingStackSize = itemStack.stackSize; // We want to fit this | ||
for (ItemStack slotStack : inv) { | ||
if (slotStack == null) { // Empty slot, stack fits completely | ||
return itemStack.stackSize; | ||
} else if (slotStack.isItemEqual(itemStack)) { | ||
remainingStackSize -= itemStack.getMaxStackSize() - slotStack.stackSize; | ||
if (remainingStackSize < 0) { | ||
return itemStack.stackSize; | ||
} | ||
} | ||
} | ||
if (remainingStackSize == itemStack.getMaxStackSize()) { | ||
return 0; // Stack didn't fit at all | ||
} | ||
return itemStack.stackSize - remainingStackSize; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/main/java/appeng/integration/modules/NEIHelpers/NEIAEBookmarkContainerHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package appeng.integration.modules.NEIHelpers; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import net.minecraft.client.Minecraft; | ||
import net.minecraft.client.gui.inventory.GuiContainer; | ||
import net.minecraft.inventory.Container; | ||
import net.minecraft.item.ItemStack; | ||
|
||
import codechicken.nei.api.IBookmarkContainerHandler; | ||
|
||
public class NEIAEBookmarkContainerHandler implements IBookmarkContainerHandler { | ||
|
||
protected static Minecraft mc = Minecraft.getMinecraft(); | ||
|
||
@Override | ||
public void pullBookmarkItemsFromContainer(GuiContainer guiContainer, ArrayList<ItemStack> bookmarkItems) { | ||
List<ItemStack> containerStacks = guiContainer.inventorySlots.getInventory(); | ||
for (ItemStack bookmarkItem : bookmarkItems) { | ||
|
||
int bookmarkSizeBackup = bookmarkItem.stackSize; | ||
|
||
for (int i = 0; i < containerStacks.size() - 4 * 9; i++) { | ||
ItemStack containerItem = containerStacks.get(i); | ||
|
||
if (containerItem == null) { | ||
continue; | ||
} | ||
|
||
if (bookmarkItem.isItemEqual(containerItem)) { | ||
if (bookmarkItem.stackSize <= 0) { | ||
break; | ||
} | ||
|
||
int transferAmount = Math.min(bookmarkItem.stackSize, containerItem.stackSize); | ||
|
||
moveItems(guiContainer, i, transferAmount); | ||
bookmarkItem.stackSize -= transferAmount; | ||
|
||
if (bookmarkItem.stackSize == 0) { | ||
break; | ||
} | ||
} | ||
} | ||
bookmarkItem.stackSize = bookmarkSizeBackup; | ||
} | ||
} | ||
|
||
private void moveItems(GuiContainer container, int fromSlot, int transferAmount) { | ||
for (int i = 0; i < transferAmount; i++) { | ||
int toSlot = findValidPlayerInventoryDestination(container.inventorySlots, fromSlot); | ||
if (toSlot == -1) { | ||
return; | ||
} | ||
clickSlot(container, fromSlot, 0); | ||
clickSlot(container, toSlot, 1); | ||
clickSlot(container, fromSlot, 0); | ||
} | ||
} | ||
|
||
private void clickSlot(GuiContainer container, int slotIdx, int button) { | ||
mc.playerController.windowClick(container.inventorySlots.windowId, slotIdx, button, 0, mc.thePlayer); | ||
} | ||
|
||
private int findValidPlayerInventoryDestination(Container container, int fromSlot) { | ||
ItemStack stackToMove = container.getSlot(fromSlot).getStack(); | ||
for (int i = container.getInventory().size() - 4 * 9; i < container.getInventory().size(); i++) { | ||
if (container.getInventory().get(i) == null) { | ||
return i; | ||
} | ||
int diff = stackToMove.getMaxStackSize() - container.getInventory().get(i).stackSize; | ||
if (container.getInventory().get(i).isItemEqual(stackToMove) && diff > 0) { | ||
return i; | ||
} | ||
} | ||
return -1; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...ain/java/appeng/integration/modules/NEIHelpers/NEIAETerminalBookmarkContainerHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package appeng.integration.modules.NEIHelpers; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
|
||
import net.minecraft.client.gui.inventory.GuiContainer; | ||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.nbt.NBTTagCompound; | ||
|
||
import appeng.core.sync.network.NetworkHandler; | ||
import appeng.core.sync.packets.PacketNEIBookmark; | ||
import codechicken.nei.api.IBookmarkContainerHandler; | ||
|
||
public class NEIAETerminalBookmarkContainerHandler implements IBookmarkContainerHandler { | ||
|
||
@Override | ||
public void pullBookmarkItemsFromContainer(GuiContainer guiContainer, ArrayList<ItemStack> bookmarkItems) { | ||
for (ItemStack bookmarkItem : bookmarkItems) { | ||
int backupStackSize = bookmarkItem.stackSize; | ||
int bookmarkStackSize = bookmarkItem.stackSize; | ||
int maxStackSize = bookmarkItem.getMaxStackSize(); | ||
try { | ||
while (bookmarkStackSize > 0) { | ||
if (bookmarkStackSize <= maxStackSize) { | ||
NetworkHandler.instance.sendToServer(new PacketNEIBookmark(packBookmarkItem(bookmarkItem))); | ||
bookmarkStackSize = 0; | ||
} else { | ||
ItemStack splitStack = bookmarkItem.splitStack(maxStackSize); | ||
NetworkHandler.instance.sendToServer(new PacketNEIBookmark(packBookmarkItem(splitStack))); | ||
bookmarkStackSize -= maxStackSize; | ||
} | ||
} | ||
} catch (final Exception | Error ignored) {} | ||
bookmarkItem.stackSize = backupStackSize; | ||
} | ||
} | ||
|
||
private NBTTagCompound packBookmarkItem(ItemStack bookmarkItem) throws IOException { | ||
final NBTTagCompound comp = new NBTTagCompound(); | ||
bookmarkItem.writeToNBT(comp); | ||
return comp; | ||
} | ||
} |