Skip to content

Commit

Permalink
Locking Cells and Adding Sticky Cards to Digital Singularities (#532)
Browse files Browse the repository at this point in the history
* Added locking behavior to drives and chests when right clicked with wirecutters

* Spotless

* Added check for when the player is holding less than 10 sticky cards

* Spotless

* Added some fixes to avoid unintentionally partitioning other types of extreme cells, as well as random bug fixes, including removing tooltip for artificial universe storage cells

* Spotless

* Made updates to code style, various other changes

- Renamed lockCells and applyStickyToCells methods to be more context-rich
- Made lockDigitalSingularityCells method in both TileDrive and TileChest return a boolean based on the success of the operation
- Made applyStickyToDigitalSingularityCells post a new MENetworkCellArrayUpdate event to the grid
- Cleaned up code in both TileDrive and TileChest as well as outsourced common code to static methods in TileDrive
- Cleaned up onItemUseFirst method in ItemMultiMaterial class to be more concise
- Outsourced player inventory updates to ItemMultiMaterial class
- Updated ItemStack creation for Sticky Card item stack to use AEApi enum
- Updated en_US.lang
- Updated for loops in TileDrive to not use enhanced for loops
- Removed unnecessary inventory size check for cell upgrades
- Updated cellInventory.getStoredItemTypes() check to be in line with the other getTotalTypes check

* Spotless

* More updates via PR review
Checked if GT integration is enabled in both BlockChest and BlockDrive
Unified ItemMultiMaterial#onItemUseFirst to use interface + inherited method and not class methods
Added ItemExtremeStorageCell#checkInvalidForLockingAndStickyCarding to reduce code duplication of the if check in all of the added methods
Added cache invalidation to force cell recalculation on adding sticky cards to cells

---------

Co-authored-by: tth05 <[email protected]>
  • Loading branch information
ReignOfFROZE and tth05 authored Jul 27, 2024
1 parent 21739d5 commit e314aa0
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

package appeng.api.implementations.tiles;

import net.minecraft.item.ItemStack;

import appeng.api.networking.IGridHost;
import appeng.api.storage.ICellContainer;
import appeng.api.util.IOrientable;
Expand Down Expand Up @@ -43,6 +45,10 @@ public interface IChestOrDrive extends ICellContainer, IGridHost, IOrientable {
*/
boolean isPowered();

boolean lockDigitalSingularityCells();

int applyStickyToDigitalSingularityCells(ItemStack cards);

@Deprecated
default boolean isCellBlinking(int slot) {
return false;
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/appeng/block/storage/BlockChest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@
import appeng.core.features.AEFeature;
import appeng.core.localization.PlayerMessages;
import appeng.core.sync.GuiBridge;
import appeng.integration.IntegrationRegistry;
import appeng.integration.IntegrationType;
import appeng.tile.storage.TileChest;
import appeng.util.Platform;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import gregtech.api.GregTech_API;
import gregtech.api.util.GT_Utility;

public class BlockChest extends AEBaseTileBlock {

Expand All @@ -53,6 +57,14 @@ public boolean onActivated(final World w, final int x, final int y, final int z,
return true;
}

if (IntegrationRegistry.INSTANCE.isEnabled(IntegrationType.GT)
&& GT_Utility.isStackInList(p.getHeldItem(), GregTech_API.sWireCutterList)) {
if (tg.lockDigitalSingularityCells()) {
p.addChatMessage(PlayerMessages.ChestLocked.get());
}
return true;
}

if (side != tg.getUp().ordinal()) {
Platform.openGUI(p, tg, ForgeDirection.getOrientation(side), GuiBridge.GUI_CHEST);
} else {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/appeng/block/storage/BlockDrive.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@
import appeng.block.AEBaseTileBlock;
import appeng.client.render.blocks.RenderDrive;
import appeng.core.features.AEFeature;
import appeng.core.localization.PlayerMessages;
import appeng.core.sync.GuiBridge;
import appeng.integration.IntegrationRegistry;
import appeng.integration.IntegrationType;
import appeng.tile.storage.TileDrive;
import appeng.util.Platform;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import gregtech.api.GregTech_API;
import gregtech.api.util.GT_Utility;

public class BlockDrive extends AEBaseTileBlock {

Expand All @@ -50,6 +55,13 @@ public boolean onActivated(final World w, final int x, final int y, final int z,
final TileDrive tg = this.getTileEntity(w, x, y, z);
if (tg != null) {
if (Platform.isServer()) {
if (IntegrationRegistry.INSTANCE.isEnabled(IntegrationType.GT)
&& GT_Utility.isStackInList(p.getHeldItem(), GregTech_API.sWireCutterList)) {
if (tg.lockDigitalSingularityCells()) {
p.addChatMessage(PlayerMessages.DriveLocked.get());
}
return true;
}
Platform.openGUI(p, tg, ForgeDirection.getOrientation(side), GuiBridge.GUI_DRIVE);
}
return true;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/appeng/core/localization/PlayerMessages.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public enum PlayerMessages {
LoadedSettings,
SavedSettings,
MachineNotPowered,

DriveLocked,
ChestLocked,
isNowLocked,
isNowUnlocked,
AmmoDepleted,
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/appeng/items/materials/ItemMultiMaterial.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import appeng.api.implementations.items.IItemGroup;
import appeng.api.implementations.items.IStorageComponent;
import appeng.api.implementations.items.IUpgradeModule;
import appeng.api.implementations.tiles.IChestOrDrive;
import appeng.api.implementations.tiles.ISegmentedInventory;
import appeng.api.parts.IPartHost;
import appeng.api.parts.SelectedPart;
Expand Down Expand Up @@ -273,6 +274,15 @@ public boolean onItemUseFirst(final ItemStack is, final EntityPlayer player, fin

if (player.isSneaking()) {
final TileEntity te = world.getTileEntity(x, y, z);
if (is != null && this.getType(is) == Upgrades.STICKY && Platform.isServer()) {
ItemStack hand = player.getHeldItem();
if (te instanceof IChestOrDrive chestOrDrive) {
hand.stackSize = hand.stackSize - chestOrDrive.applyStickyToDigitalSingularityCells(hand);
player.inventory
.setInventorySlotContents(player.inventory.currentItem, hand.stackSize == 0 ? null : hand);
return true;
}
}
IInventory upgrades = null;

if (te instanceof IPartHost) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import appeng.api.AEApi;
import appeng.api.config.IncludeExclude;
import appeng.api.storage.ICellHandler;
import appeng.api.storage.ICellInventory;
import appeng.api.storage.ICellInventoryHandler;
import appeng.api.storage.IMEInventoryHandler;
Expand Down Expand Up @@ -91,7 +92,7 @@ public void addCheckedInformation(final ItemStack stack, final EntityPlayer play
+ ' '
+ GuiText.Types.getLocal());

if (cellInventory.getStoredItemTypes() != 0) {
if (cellInventory.getTotalItemTypes() == 1 && cellInventory.getStoredItemTypes() != 0) {
ItemStack itemStack = handler.getAvailableItems(new ItemList()).getFirstItem().getItemStack();
lines.add(GuiText.Contains.getLocal() + ": " + itemStack.getDisplayName());
}
Expand Down Expand Up @@ -127,4 +128,10 @@ public void addCheckedInformation(final ItemStack stack, final EntityPlayer play
}
}

public static boolean checkInvalidForLockingAndStickyCarding(ItemStack cell, ICellHandler cellHandler) {
return cellHandler == null || cell == null
|| !(cell.getItem() instanceof ItemExtremeStorageCell)
|| (cell.getItem() instanceof ItemExtremeStorageCell exCell && exCell.getTotalTypes(cell) != 1);
}

}
34 changes: 34 additions & 0 deletions src/main/java/appeng/tile/storage/TileChest.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
import appeng.api.networking.ticking.TickRateModulation;
import appeng.api.networking.ticking.TickingRequest;
import appeng.api.storage.ICellHandler;
import appeng.api.storage.ICellInventoryHandler;
import appeng.api.storage.ICellWorkbenchItem;
import appeng.api.storage.IMEInventory;
import appeng.api.storage.IMEInventoryHandler;
import appeng.api.storage.IMEMonitor;
Expand All @@ -72,6 +74,7 @@
import appeng.api.util.AEColor;
import appeng.api.util.IConfigManager;
import appeng.helpers.IPriorityHost;
import appeng.items.storage.ItemExtremeStorageCell;
import appeng.me.GridAccessException;
import appeng.me.storage.MEInventoryHandler;
import appeng.tile.TileEvent;
Expand Down Expand Up @@ -678,6 +681,37 @@ public void saveChanges(final IMEInventory cellInventory) {
this.worldObj.markTileEntityChunkModified(this.xCoord, this.yCoord, this.zCoord, this);
}

public boolean lockDigitalSingularityCells() {
final ItemStack cell = this.inv.getStackInSlot(1);
if (ItemExtremeStorageCell.checkInvalidForLockingAndStickyCarding(cell, cellHandler)) {
return false;
}
final IMEInventoryHandler<?> inv = cellHandler.getCellInventory(cell, this, StorageChannel.ITEMS);
if (inv instanceof ICellInventoryHandler handler) {
TileDrive.partitionDigitalSingularityCellToItemOnCell(handler);
}
return true;
}

public int applyStickyToDigitalSingularityCells(ItemStack cards) {
ItemStack cell = this.inv.getStackInSlot(1);
if (ItemExtremeStorageCell.checkInvalidForLockingAndStickyCarding(cell, cellHandler) && cards.stackSize != 0) {
return 0;
}
if (cell.getItem() instanceof ICellWorkbenchItem cellItem) {
if (TileDrive.applyStickyCardToDigitalSingularityCell(cellHandler, cell, this, cellItem)) {
if (this.isCached) {
this.isCached = false;
}
try {
this.getProxy().getGrid().postEvent(new MENetworkCellArrayUpdate());
} catch (final GridAccessException ignored) {}
return 1;
}
}
return 0;
}

private static class ChestNoHandler extends Exception {

private static final long serialVersionUID = 7995805326136526631L;
Expand Down
96 changes: 96 additions & 0 deletions src/main/java/appeng/tile/storage/TileDrive.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.util.ForgeDirection;

import com.google.common.base.Optional;

import appeng.api.AEApi;
import appeng.api.config.Upgrades;
import appeng.api.implementations.tiles.IChestOrDrive;
import appeng.api.networking.GridFlags;
import appeng.api.networking.IGridNode;
Expand All @@ -34,13 +37,19 @@
import appeng.api.networking.ticking.TickRateModulation;
import appeng.api.networking.ticking.TickingRequest;
import appeng.api.storage.ICellHandler;
import appeng.api.storage.ICellInventory;
import appeng.api.storage.ICellInventoryHandler;
import appeng.api.storage.ICellWorkbenchItem;
import appeng.api.storage.IMEInventory;
import appeng.api.storage.IMEInventoryHandler;
import appeng.api.storage.ISaveProvider;
import appeng.api.storage.StorageChannel;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.util.AECableType;
import appeng.api.util.DimensionalCoord;
import appeng.helpers.IPriorityHost;
import appeng.items.materials.ItemMultiMaterial;
import appeng.items.storage.ItemExtremeStorageCell;
import appeng.me.GridAccessException;
import appeng.me.storage.MEInventoryHandler;
import appeng.tile.TileEvent;
Expand All @@ -49,6 +58,7 @@
import appeng.tile.inventory.AppEngInternalInventory;
import appeng.tile.inventory.InvOperation;
import appeng.util.Platform;
import appeng.util.item.ItemList;
import io.netty.buffer.ByteBuf;

public class TileDrive extends AENetworkInvTile implements IChestOrDrive, IPriorityHost, IGridTickable {
Expand Down Expand Up @@ -329,4 +339,90 @@ public void setPriority(final int newValue) {
public void saveChanges(final IMEInventory cellInventory) {
this.worldObj.markTileEntityChunkModified(this.xCoord, this.yCoord, this.zCoord, this);
}

public static void partitionDigitalSingularityCellToItemOnCell(ICellInventoryHandler handler) {
ICellInventory cellInventory = handler.getCellInv();
if (cellInventory != null) {
if (cellInventory.getStoredItemTypes() != 0) {
ItemStack partition = handler.getAvailableItems(new ItemList()).getFirstItem().getItemStack().copy();
partition.stackSize = 1;
cellInventory.getConfigInventory().setInventorySlotContents(0, partition);
}
}
}

public static boolean applyStickyCardToDigitalSingularityCell(ICellHandler cellHandler, ItemStack cell,
ISaveProvider host, ICellWorkbenchItem cellItem) {
final IMEInventoryHandler<?> inv = cellHandler.getCellInventory(cell, host, StorageChannel.ITEMS);
if (inv instanceof ICellInventoryHandler handler) {
final ICellInventory cellInventory = handler.getCellInv();
if (cellInventory != null && cellInventory.getStoredItemTypes() == 1) {
IInventory cellUpgrades = cellItem.getUpgradesInventory(cell);
int freeSlot = -1;
for (int i = 0; i < cellUpgrades.getSizeInventory(); i++) {
if (freeSlot == -1 && cellUpgrades.getStackInSlot(i) == null) {
freeSlot = i;
continue;
} else if (cellUpgrades.getStackInSlot(i) == null) {
continue;
}
if (ItemMultiMaterial.instance.getType(cellUpgrades.getStackInSlot(i)) == Upgrades.STICKY) {
freeSlot = -1;
break;
}
}
if (freeSlot != -1) {
Optional<ItemStack> stickyCard = AEApi.instance().definitions().materials().cardSticky()
.maybeStack(1);
if (stickyCard.isPresent()) {
cellUpgrades.setInventorySlotContents(freeSlot, stickyCard.get());
return true;
}
return false;
}
}
}
return false;
}

public boolean lockDigitalSingularityCells() {
boolean res = false;
for (int i = 0; i < this.handlersBySlot.length; i++) {
ICellHandler cellHandler = this.handlersBySlot[i];
final ItemStack cell = this.inv.getStackInSlot(i);
if (ItemExtremeStorageCell.checkInvalidForLockingAndStickyCarding(cell, cellHandler)) {
continue;
}
final IMEInventoryHandler<?> inv = cellHandler.getCellInventory(cell, this, StorageChannel.ITEMS);
if (inv instanceof ICellInventoryHandler handler) {
partitionDigitalSingularityCellToItemOnCell(handler);
res = true;
}
}
return res;
}

public int applyStickyToDigitalSingularityCells(ItemStack cards) {
int res = 0;
for (int i = 0; i < this.handlersBySlot.length; i++) {
ICellHandler cellHandler = this.handlersBySlot[i];
ItemStack cell = this.inv.getStackInSlot(i);
if (ItemExtremeStorageCell.checkInvalidForLockingAndStickyCarding(cell, cellHandler)) {
continue;
}
if (cell.getItem() instanceof ICellWorkbenchItem cellItem && res + 1 <= cards.stackSize) {
if (applyStickyCardToDigitalSingularityCell(cellHandler, cell, this, cellItem)) {
res++;
}
}
}
if (this.isCached) {
this.isCached = false;
this.updateState();
}
try {
this.getProxy().getGrid().postEvent(new MENetworkCellArrayUpdate());
} catch (final GridAccessException ignored) {}
return res;
}
}
2 changes: 2 additions & 0 deletions src/main/resources/assets/appliedenergistics2/lang/en_US.lang
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ chat.appliedenergistics2.FBOUnsupported=OpenGL FBOs not supported, cannot export
chat.appliedenergistics2.PriorityInvalidTarget=Target has no priority
chat.appliedenergistics2.PriorityReadout=Current priority is %d
chat.appliedenergistics2.PriorityConfigured=Updated priority to %d
chat.appliedenergistics2.DriveLocked=Digital Singularity cells in drive were locked to the current item on them
chat.appliedenergistics2.ChestLocked=Digital Singularity cell in chest was locked to the current item on it
# Creative Tabs
itemGroup.appliedenergistics2=Applied Energistics 2
itemGroup.appliedenergistics2.facades=Applied Energistics 2 - Facades
Expand Down

0 comments on commit e314aa0

Please sign in to comment.