From e809c688dfd271ff155d58260e911f62fa0a76a1 Mon Sep 17 00:00:00 2001 From: fscan Date: Sat, 14 Jul 2018 16:53:49 +0200 Subject: [PATCH] Performance improvements for saveChanges() and ICellInventory (#3586) API break! Fixes #3553 --- .../java/appeng/api/storage/ICellHandler.java | 4 +- .../appeng/api/storage/ICellInventory.java | 5 + .../api/storage/ICellInventoryHandler.java | 2 +- .../appeng/api/storage/ICellRegistry.java | 4 +- .../appeng/api/storage/ISaveProvider.java | 7 +- .../registries/cell/BasicItemCellHandler.java | 4 +- .../registries/cell/CellRegistry.java | 4 +- .../registries/cell/CreativeCellHandler.java | 3 +- .../fluids/helper/DualityFluidInterface.java | 2 +- .../fluids/helper/IFluidInterfaceHost.java | 2 + .../registries/BasicFluidCellHandler.java | 4 +- .../fluids/storage/FluidCellInventory.java | 16 +-- .../java/appeng/helpers/DualityInterface.java | 15 +-- .../items/tools/powered/ToolMatterCannon.java | 31 +++-- .../implementations/CraftingCPUCluster.java | 4 +- .../me/storage/AbstractCellInventory.java | 86 +++++++------ .../me/storage/CreativeCellInventory.java | 3 +- .../java/appeng/me/storage/DriveWatcher.java | 9 +- .../appeng/me/storage/ItemCellInventory.java | 18 +-- .../PartAbstractFormationPlane.java | 3 +- .../parts/automation/PartFormationPlane.java | 1 + .../parts/misc/PartSharedStorageBus.java | 4 +- .../appeng/parts/misc/PartStorageBus.java | 3 +- src/main/java/appeng/tile/AEBaseTile.java | 19 ++- .../crafting/TileCraftingMonitorTile.java | 2 +- .../tile/crafting/TileMolecularAssembler.java | 10 +- .../tile/inventory/AppEngCellInventory.java | 119 ++++++++++++++++++ .../appeng/tile/misc/TileCellWorkbench.java | 2 +- .../java/appeng/tile/misc/TileInscriber.java | 2 +- .../java/appeng/tile/misc/TileInterface.java | 8 +- src/main/java/appeng/tile/misc/TilePaint.java | 4 +- .../appeng/tile/misc/TileSecurityStation.java | 2 +- .../tile/misc/TileVibrationChamber.java | 2 +- .../appeng/tile/networking/TileCableBus.java | 2 +- .../appeng/tile/networking/TileWireless.java | 3 +- .../java/appeng/tile/storage/TileChest.java | 22 ++-- .../java/appeng/tile/storage/TileDrive.java | 27 ++-- 37 files changed, 292 insertions(+), 166 deletions(-) create mode 100644 src/main/java/appeng/tile/inventory/AppEngCellInventory.java diff --git a/src/api/java/appeng/api/storage/ICellHandler.java b/src/api/java/appeng/api/storage/ICellHandler.java index 5171f7295..cfde25bdf 100644 --- a/src/api/java/appeng/api/storage/ICellHandler.java +++ b/src/api/java/appeng/api/storage/ICellHandler.java @@ -52,12 +52,12 @@ public interface ICellHandler * * @param is a storage cell item. * @param host anytime the contents of your storage cell changes it should use this to request a save, please - * note, this value can be null. + * note, this value can be null. If provided, the host is responsible for persisting the cell content. * @param channel the storage channel requested. * * @return a new IMEHandler for the provided item */ - > IMEInventoryHandler getCellInventory( ItemStack is, ISaveProvider host, IStorageChannel channel ); + > ICellInventoryHandler getCellInventory( ItemStack is, ISaveProvider host, IStorageChannel channel ); /** * Called when the storage cell is planed in an ME Chest and the user tries to open the terminal side, if your item diff --git a/src/api/java/appeng/api/storage/ICellInventory.java b/src/api/java/appeng/api/storage/ICellInventory.java index 36e1cef22..fe7ed7438 100644 --- a/src/api/java/appeng/api/storage/ICellInventory.java +++ b/src/api/java/appeng/api/storage/ICellInventory.java @@ -118,4 +118,9 @@ public interface ICellInventory> extends IMEInventory * @return the status number for this drive. */ int getStatusForCell(); + + /** + * Tells the cell to persist to NBT + */ + void persist(); } diff --git a/src/api/java/appeng/api/storage/ICellInventoryHandler.java b/src/api/java/appeng/api/storage/ICellInventoryHandler.java index 45716fd9b..2a80438f0 100644 --- a/src/api/java/appeng/api/storage/ICellInventoryHandler.java +++ b/src/api/java/appeng/api/storage/ICellInventoryHandler.java @@ -34,7 +34,7 @@ public interface ICellInventoryHandler> extends IMEInvento /** * @return get access to the Cell Inventory. */ - ICellInventory getCellInv(); + ICellInventory getCellInv(); boolean isPreformatted(); diff --git a/src/api/java/appeng/api/storage/ICellRegistry.java b/src/api/java/appeng/api/storage/ICellRegistry.java index ad3563bc0..a56451203 100644 --- a/src/api/java/appeng/api/storage/ICellRegistry.java +++ b/src/api/java/appeng/api/storage/ICellRegistry.java @@ -77,11 +77,11 @@ public interface ICellRegistry * returns an IMEInventoryHandler for the provided item. * * @param is item with inventory handler - * @param host can be null, or the hosting tile / part. + * @param host can be null. If provided, the host is responsible for persisting the cell content. * @param chan the storage channel to request the handler for. * * @return new IMEInventoryHandler, or null if there isn't one. */ @Nullable - > IMEInventoryHandler getCellInventory( ItemStack is, ISaveProvider host, IStorageChannel chan ); + > ICellInventoryHandler getCellInventory( ItemStack is, ISaveProvider host, IStorageChannel chan ); } \ No newline at end of file diff --git a/src/api/java/appeng/api/storage/ISaveProvider.java b/src/api/java/appeng/api/storage/ISaveProvider.java index 78ce5907c..5a224f8a6 100644 --- a/src/api/java/appeng/api/storage/ISaveProvider.java +++ b/src/api/java/appeng/api/storage/ISaveProvider.java @@ -24,8 +24,11 @@ package appeng.api.storage; +/** + * Tells the cell provider that changes have been made an the cell must be persisted + * + */ public interface ISaveProvider { - - void saveChanges( IMEInventory cellInventory ); + void saveChanges( ICellInventory cellInventory ); } diff --git a/src/main/java/appeng/core/features/registries/cell/BasicItemCellHandler.java b/src/main/java/appeng/core/features/registries/cell/BasicItemCellHandler.java index fe9a3a737..0f9ea352d 100644 --- a/src/main/java/appeng/core/features/registries/cell/BasicItemCellHandler.java +++ b/src/main/java/appeng/core/features/registries/cell/BasicItemCellHandler.java @@ -51,11 +51,11 @@ public class BasicItemCellHandler implements ICellHandler } @Override - public > IMEInventoryHandler getCellInventory( final ItemStack is, final ISaveProvider container, final IStorageChannel channel ) + public > ICellInventoryHandler getCellInventory( final ItemStack is, final ISaveProvider container, final IStorageChannel channel ) { if( channel == AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ) ) { - return ItemCellInventory.getCell( is, container ); + return (ICellInventoryHandler) ItemCellInventory.getCell( is, container ); } return null; diff --git a/src/main/java/appeng/core/features/registries/cell/CellRegistry.java b/src/main/java/appeng/core/features/registries/cell/CellRegistry.java index bb4c0322f..11c29b552 100644 --- a/src/main/java/appeng/core/features/registries/cell/CellRegistry.java +++ b/src/main/java/appeng/core/features/registries/cell/CellRegistry.java @@ -28,8 +28,8 @@ import com.google.common.base.Verify; import net.minecraft.item.ItemStack; import appeng.api.storage.ICellHandler; +import appeng.api.storage.ICellInventoryHandler; import appeng.api.storage.ICellRegistry; -import appeng.api.storage.IMEInventoryHandler; import appeng.api.storage.ISaveProvider; import appeng.api.storage.IStorageChannel; import appeng.api.storage.data.IAEStack; @@ -92,7 +92,7 @@ public class CellRegistry implements ICellRegistry } @Override - public > IMEInventoryHandler getCellInventory( final ItemStack is, final ISaveProvider container, final IStorageChannel chan ) + public > ICellInventoryHandler getCellInventory( final ItemStack is, final ISaveProvider container, final IStorageChannel chan ) { if( is.isEmpty() ) { diff --git a/src/main/java/appeng/core/features/registries/cell/CreativeCellHandler.java b/src/main/java/appeng/core/features/registries/cell/CreativeCellHandler.java index 8c4b4168b..632734969 100644 --- a/src/main/java/appeng/core/features/registries/cell/CreativeCellHandler.java +++ b/src/main/java/appeng/core/features/registries/cell/CreativeCellHandler.java @@ -26,6 +26,7 @@ import net.minecraft.tileentity.TileEntity; import appeng.api.AEApi; import appeng.api.implementations.tiles.IChestOrDrive; import appeng.api.storage.ICellHandler; +import appeng.api.storage.ICellInventoryHandler; import appeng.api.storage.IMEInventory; import appeng.api.storage.IMEInventoryHandler; import appeng.api.storage.ISaveProvider; @@ -48,7 +49,7 @@ public final class CreativeCellHandler implements ICellHandler } @Override - public IMEInventoryHandler getCellInventory( final ItemStack is, final ISaveProvider container, final IStorageChannel channel ) + public ICellInventoryHandler getCellInventory( final ItemStack is, final ISaveProvider container, final IStorageChannel channel ) { if( channel == AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ) && !is.isEmpty() && is .getItem() instanceof ItemCreativeStorageCell ) diff --git a/src/main/java/appeng/fluids/helper/DualityFluidInterface.java b/src/main/java/appeng/fluids/helper/DualityFluidInterface.java index 6999251c7..c4ffab87a 100644 --- a/src/main/java/appeng/fluids/helper/DualityFluidInterface.java +++ b/src/main/java/appeng/fluids/helper/DualityFluidInterface.java @@ -577,7 +577,7 @@ public class DualityFluidInterface implements IGridTickable, IStorageMonitorable public void saveChanges() { - this.iHost.getTileEntity().markDirty(); + this.iHost.saveChanges(); } @Override diff --git a/src/main/java/appeng/fluids/helper/IFluidInterfaceHost.java b/src/main/java/appeng/fluids/helper/IFluidInterfaceHost.java index 231af0ab8..bbba850fc 100644 --- a/src/main/java/appeng/fluids/helper/IFluidInterfaceHost.java +++ b/src/main/java/appeng/fluids/helper/IFluidInterfaceHost.java @@ -36,4 +36,6 @@ public interface IFluidInterfaceHost extends IActionHost, IGridProxyable, IUpgra EnumSet getTargets(); TileEntity getTileEntity(); + + void saveChanges(); } diff --git a/src/main/java/appeng/fluids/registries/BasicFluidCellHandler.java b/src/main/java/appeng/fluids/registries/BasicFluidCellHandler.java index f50941c73..7b460b460 100644 --- a/src/main/java/appeng/fluids/registries/BasicFluidCellHandler.java +++ b/src/main/java/appeng/fluids/registries/BasicFluidCellHandler.java @@ -51,11 +51,11 @@ public class BasicFluidCellHandler implements ICellHandler } @Override - public > IMEInventoryHandler getCellInventory( final ItemStack is, final ISaveProvider container, final IStorageChannel channel ) + public > ICellInventoryHandler getCellInventory( final ItemStack is, final ISaveProvider container, final IStorageChannel channel ) { if( channel == AEApi.instance().storage().getStorageChannel( IFluidStorageChannel.class ) ) { - return (IMEInventoryHandler) FluidCellInventory.getCell( is, container ); + return (ICellInventoryHandler) FluidCellInventory.getCell( is, container ); } return null; diff --git a/src/main/java/appeng/fluids/storage/FluidCellInventory.java b/src/main/java/appeng/fluids/storage/FluidCellInventory.java index 13b520f95..c46279123 100644 --- a/src/main/java/appeng/fluids/storage/FluidCellInventory.java +++ b/src/main/java/appeng/fluids/storage/FluidCellInventory.java @@ -29,7 +29,7 @@ import appeng.api.config.Actionable; import appeng.api.exceptions.AppEngException; import appeng.api.implementations.items.IStorageCell; import appeng.api.networking.security.IActionSource; -import appeng.api.storage.IMEInventoryHandler; +import appeng.api.storage.ICellInventoryHandler; import appeng.api.storage.ISaveProvider; import appeng.api.storage.IStorageChannel; import appeng.api.storage.channels.IFluidStorageChannel; @@ -47,17 +47,12 @@ import appeng.me.storage.AbstractCellInventory; */ public class FluidCellInventory extends AbstractCellInventory { - protected FluidCellInventory( final NBTTagCompound data, final ISaveProvider container ) - { - super( data, container, 8000 ); - } - private FluidCellInventory( final ItemStack o, final ISaveProvider container ) throws AppEngException { super( o, container, 8000 ); } - public static IMEInventoryHandler getCell( final ItemStack o, final ISaveProvider container2 ) + public static ICellInventoryHandler getCell( final ItemStack o, final ISaveProvider container2 ) { try { @@ -123,7 +118,6 @@ public class FluidCellInventory extends AbstractCellInventory if( mode == Actionable.MODULATE ) { l.setStackSize( l.getStackSize() + remainingItemSlots ); - this.updateItemCount( remainingItemSlots ); this.saveChanges(); } return r; @@ -133,7 +127,6 @@ public class FluidCellInventory extends AbstractCellInventory if( mode == Actionable.MODULATE ) { l.setStackSize( l.getStackSize() + input.getStackSize() ); - this.updateItemCount( input.getStackSize() ); this.saveChanges(); } return null; @@ -155,8 +148,6 @@ public class FluidCellInventory extends AbstractCellInventory toWrite.amount = remainingItemCount; this.cellItems.add( AEFluidStack.fromFluidStack( toWrite ) ); - this.updateItemCount( toWrite.amount ); - this.saveChanges(); } return AEFluidStack.fromFluidStack( toReturn ); @@ -164,7 +155,6 @@ public class FluidCellInventory extends AbstractCellInventory if( mode == Actionable.MODULATE ) { - this.updateItemCount( input.getStackSize() ); this.cellItems.add( input ); this.saveChanges(); } @@ -198,7 +188,6 @@ public class FluidCellInventory extends AbstractCellInventory results.setStackSize( l.getStackSize() ); if( mode == Actionable.MODULATE ) { - this.updateItemCount( -l.getStackSize() ); l.setStackSize( 0 ); this.saveChanges(); } @@ -209,7 +198,6 @@ public class FluidCellInventory extends AbstractCellInventory if( mode == Actionable.MODULATE ) { l.setStackSize( l.getStackSize() - size ); - this.updateItemCount( -size ); this.saveChanges(); } } diff --git a/src/main/java/appeng/helpers/DualityInterface.java b/src/main/java/appeng/helpers/DualityInterface.java index 09def5981..ce5fd369b 100644 --- a/src/main/java/appeng/helpers/DualityInterface.java +++ b/src/main/java/appeng/helpers/DualityInterface.java @@ -561,14 +561,6 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn return this.storage; } - public void markDirty() - { - for( int slot = 0; slot < this.storage.getSlots(); slot++ ) - { - this.storage.markDirty( slot ); - } - } - @Override public TickingRequest getTickingRequest( final IGridNode node ) { @@ -869,8 +861,7 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn { this.cancelCrafting(); } - - this.markDirty(); + this.iHost.saveChanges(); } private void cancelCrafting() @@ -1251,8 +1242,8 @@ public class DualityInterface implements IGridTickable, IStorageMonitorable, IIn public void setPriority( final int newValue ) { this.priority = newValue; - this.markDirty(); - + this.iHost.saveChanges(); + try { this.gridProxy.getGrid().postEvent( new MENetworkCraftingPatternChange( this, this.gridProxy.getNode() ) ); diff --git a/src/main/java/appeng/items/tools/powered/ToolMatterCannon.java b/src/main/java/appeng/items/tools/powered/ToolMatterCannon.java index dcb76ad61..a6bf45689 100644 --- a/src/main/java/appeng/items/tools/powered/ToolMatterCannon.java +++ b/src/main/java/appeng/items/tools/powered/ToolMatterCannon.java @@ -54,11 +54,9 @@ import appeng.api.config.Upgrades; import appeng.api.implementations.items.IStorageCell; import appeng.api.storage.ICellInventory; import appeng.api.storage.ICellInventoryHandler; -import appeng.api.storage.IMEInventory; import appeng.api.storage.IStorageChannel; import appeng.api.storage.channels.IItemStorageChannel; import appeng.api.storage.data.IAEItemStack; -import appeng.api.storage.data.IAEStack; import appeng.api.storage.data.IItemList; import appeng.api.util.AEColor; import appeng.api.util.DimensionalCoord; @@ -77,7 +75,6 @@ import appeng.items.contents.CellUpgrades; import appeng.items.misc.ItemPaintBall; import appeng.items.tools.powered.powersink.AEBasePoweredItem; import appeng.me.helpers.PlayerSource; -import appeng.me.storage.ItemCellInventoryHandler; import appeng.tile.misc.TilePaint; import appeng.util.LookDirection; import appeng.util.Platform; @@ -97,12 +94,15 @@ public class ToolMatterCannon extends AEBasePoweredItem implements IStorageCell< { super.addCheckedInformation( stack, world, lines, advancedTooltips ); - final IMEInventory cdi = AEApi.instance().registries().cell().getCellInventory( stack, null, - AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ) ); + final ICellInventoryHandler cdi = AEApi.instance() + .registries() + .cell() + .getCellInventory( stack, null, + AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ) ); - if( cdi instanceof ItemCellInventoryHandler ) + if( cdi != null ) { - final ICellInventory cd = ( (ICellInventoryHandler) cdi ).getCellInv(); + final ICellInventory cd = cdi.getCellInv(); if( cd != null ) { lines.add( cd.getUsedBytes() + " " + GuiText.Of.getLocal() + ' ' + cd.getTotalBytes() + ' ' + GuiText.BytesUsed.getLocal() ); @@ -124,17 +124,22 @@ public class ToolMatterCannon extends AEBasePoweredItem implements IStorageCell< shots += cu.getInstalledUpgrades( Upgrades.SPEED ); } - final IMEInventory inv = AEApi.instance().registries().cell().getCellInventory( p.getHeldItem( hand ), null, - AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ) ); + final ICellInventoryHandler inv = AEApi.instance() + .registries() + .cell() + .getCellInventory( p.getHeldItem( hand ), null, + AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ) ); if( inv != null ) { - final IItemList itemList = inv.getAvailableItems( AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ).createList() ); - IAEStack aeAmmo = itemList.getFirstItem(); - if( aeAmmo instanceof IAEItemStack ) + final IItemList itemList = inv + .getAvailableItems( AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ).createList() ); + IAEItemStack req = itemList.getFirstItem(); + if( req instanceof IAEItemStack ) { - shots = Math.min( shots, (int) aeAmmo.getStackSize() ); + shots = Math.min( shots, (int) req.getStackSize() ); for( int sh = 0; sh < shots; sh++ ) { + IAEItemStack aeAmmo = req.copy(); this.extractAEPower( p.getHeldItem( hand ), 1600, Actionable.MODULATE ); if( Platform.isClient() ) diff --git a/src/main/java/appeng/me/cluster/implementations/CraftingCPUCluster.java b/src/main/java/appeng/me/cluster/implementations/CraftingCPUCluster.java index 0820ba588..b298967a0 100644 --- a/src/main/java/appeng/me/cluster/implementations/CraftingCPUCluster.java +++ b/src/main/java/appeng/me/cluster/implementations/CraftingCPUCluster.java @@ -206,7 +206,7 @@ public final class CraftingCPUCluster implements IAECluster, ICraftingCPU } te.setCoreBlock( false ); - te.markDirty(); + te.saveChanges(); this.tiles.add( 0, te ); if( te.isStorage() ) @@ -395,7 +395,7 @@ public final class CraftingCPUCluster implements IAECluster, ICraftingCPU private void markDirty() { - this.getCore().markDirty(); + this.getCore().saveChanges(); } private void postCraftingStatusChange( final IAEItemStack diff ) diff --git a/src/main/java/appeng/me/storage/AbstractCellInventory.java b/src/main/java/appeng/me/storage/AbstractCellInventory.java index c254ca5bb..a991f5177 100644 --- a/src/main/java/appeng/me/storage/AbstractCellInventory.java +++ b/src/main/java/appeng/me/storage/AbstractCellInventory.java @@ -42,7 +42,7 @@ import appeng.util.Platform; */ public abstract class AbstractCellInventory> implements ICellInventory { - + private static final int MAX_ITEM_TYPES = 63; private static final String ITEM_TYPE_TAG = "it"; private static final String ITEM_COUNT_TAG = "ic"; private static final String ITEM_SLOT = "#"; @@ -51,46 +51,37 @@ public abstract class AbstractCellInventory> implements IC protected static final String ITEM_PRE_FORMATTED_SLOT = "PF#"; protected static final String ITEM_PRE_FORMATTED_NAME = "PN"; protected static final String ITEM_PRE_FORMATTED_FUZZY = "FP"; - private static String[] itemSlots; - private static String[] itemSlotCount; + private static final String[] ITEM_SLOT_KEYS = new String[MAX_ITEM_TYPES]; + private static final String[] ITEM_SLOT_COUNT_KEYS = new String[MAX_ITEM_TYPES]; private final NBTTagCompound tagCompound; protected final ISaveProvider container; - private int maxItemTypes = 63; + private int maxItemTypes = MAX_ITEM_TYPES; private short storedItems = 0; private int storedItemCount = 0; protected IItemList cellItems; - protected ItemStack i; - protected IStorageCell cellType; + protected final ItemStack i; + protected final IStorageCell cellType; protected final int itemsPerByte; + private boolean isPersisted = true; - protected AbstractCellInventory( final NBTTagCompound data, final ISaveProvider container, final int itemsPerByte ) + static { - this.tagCompound = data; - this.container = container; - this.itemsPerByte = itemsPerByte; + for( int x = 0; x < MAX_ITEM_TYPES; x++ ) + { + ITEM_SLOT_KEYS[x] = ITEM_SLOT + x; + ITEM_SLOT_COUNT_KEYS[x] = ITEM_SLOT_COUNT + x; + } } protected AbstractCellInventory( final ItemStack o, final ISaveProvider container, final int itemsPerByte ) throws AppEngException { this.itemsPerByte = itemsPerByte; - if( itemSlots == null ) - { - itemSlots = new String[this.maxItemTypes]; - itemSlotCount = new String[this.maxItemTypes]; - - for( int x = 0; x < this.maxItemTypes; x++ ) - { - itemSlots[x] = ITEM_SLOT + x; - itemSlotCount[x] = ITEM_SLOT_COUNT + x; - } - } if( o == null ) { throw new AppEngException( "ItemStack was used as a cell, but was not a cell!" ); } - this.cellType = null; this.i = o; final Item type = this.i.getItem(); @@ -99,8 +90,7 @@ public abstract class AbstractCellInventory> implements IC this.cellType = (IStorageCell) this.i.getItem(); this.maxItemTypes = this.cellType.getTotalTypes( this.i ); } - - if( this.cellType == null ) + else { throw new AppEngException( "ItemStack was used as a cell, but was not a cell!" ); } @@ -110,9 +100,9 @@ public abstract class AbstractCellInventory> implements IC throw new AppEngException( "ItemStack was used as a cell, but was not a cell!" ); } - if( this.maxItemTypes > 63 ) + if( this.maxItemTypes > MAX_ITEM_TYPES ) { - this.maxItemTypes = 63; + this.maxItemTypes = MAX_ITEM_TYPES; } if( this.maxItemTypes < 1 ) { @@ -142,14 +132,14 @@ public abstract class AbstractCellInventory> implements IC return this.cellItems; } - protected void updateItemCount( final long delta ) + @Override + public void persist() { - this.storedItemCount += delta; - this.tagCompound.setInteger( ITEM_COUNT_TAG, this.storedItemCount ); - } + if( this.isPersisted ) + { + return; + } - protected void saveChanges() - { int itemCount = 0; // add new pretty stuff... @@ -160,9 +150,8 @@ public abstract class AbstractCellInventory> implements IC final NBTTagCompound g = new NBTTagCompound(); v.writeToNBT( g ); - this.tagCompound.setTag( itemSlots[x], g ); - - this.tagCompound.setInteger( itemSlotCount[x], (int) v.getStackSize() ); + this.tagCompound.setTag( ITEM_SLOT_KEYS[x], g ); + this.tagCompound.setInteger( ITEM_SLOT_COUNT_KEYS[x], (int) v.getStackSize() ); x++; } @@ -192,14 +181,33 @@ public abstract class AbstractCellInventory> implements IC // clean any old crusty stuff... for( ; x < oldStoredItems && x < this.maxItemTypes; x++ ) { - this.tagCompound.removeTag( itemSlots[x] ); - this.tagCompound.removeTag( itemSlotCount[x] ); + this.tagCompound.removeTag( ITEM_SLOT_KEYS[x] ); + this.tagCompound.removeTag( ITEM_SLOT_COUNT_KEYS[x] ); } + this.isPersisted = true; + } + + protected void saveChanges() + { + // recalculate values + this.storedItems = (short) this.cellItems.size(); + this.storedItemCount = 0; + for( final T v : this.cellItems ) + { + this.storedItemCount += v.getStackSize(); + } + + this.isPersisted = false; if( this.container != null ) { this.container.saveChanges( this ); } + else + { + // if there is no ISaveProvider, store to NBT immediately + this.persist(); + } } private void loadCellItems() @@ -215,8 +223,8 @@ public abstract class AbstractCellInventory> implements IC for( int slot = 0; slot < types; slot++ ) { - NBTTagCompound compoundTag = this.tagCompound.getCompoundTag( itemSlots[slot] ); - int stackSize = this.tagCompound.getInteger( itemSlotCount[slot] ); + NBTTagCompound compoundTag = this.tagCompound.getCompoundTag( ITEM_SLOT_KEYS[slot] ); + int stackSize = this.tagCompound.getInteger( ITEM_SLOT_COUNT_KEYS[slot] ); this.loadCellItem( compoundTag, stackSize ); } } diff --git a/src/main/java/appeng/me/storage/CreativeCellInventory.java b/src/main/java/appeng/me/storage/CreativeCellInventory.java index 8d59c60e9..faf616001 100644 --- a/src/main/java/appeng/me/storage/CreativeCellInventory.java +++ b/src/main/java/appeng/me/storage/CreativeCellInventory.java @@ -25,6 +25,7 @@ import appeng.api.AEApi; import appeng.api.config.AccessRestriction; import appeng.api.config.Actionable; import appeng.api.networking.security.IActionSource; +import appeng.api.storage.ICellInventoryHandler; import appeng.api.storage.IMEInventoryHandler; import appeng.api.storage.IStorageChannel; import appeng.api.storage.channels.IItemStorageChannel; @@ -53,7 +54,7 @@ public class CreativeCellInventory implements IMEInventoryHandler } } - public static IMEInventoryHandler getCell( final ItemStack o ) + public static ICellInventoryHandler getCell( final ItemStack o ) { return new ItemCellInventoryHandler( new CreativeCellInventory( o ) ); } diff --git a/src/main/java/appeng/me/storage/DriveWatcher.java b/src/main/java/appeng/me/storage/DriveWatcher.java index 43147b4fa..ba2c0515b 100644 --- a/src/main/java/appeng/me/storage/DriveWatcher.java +++ b/src/main/java/appeng/me/storage/DriveWatcher.java @@ -45,6 +45,11 @@ public class DriveWatcher> extends MEInventoryHandler this.cord = cod; } + public int getStatus() + { + return this.handler.getStatusForCell( this.is, this.getInternal() ); + } + @Override public T injectItems( final T input, final Actionable type, final IActionSource src ) { @@ -54,7 +59,7 @@ public class DriveWatcher> extends MEInventoryHandler if( type == Actionable.MODULATE && ( a == null || a.getStackSize() != size ) ) { - final int newStatus = this.handler.getStatusForCell( this.is, this.getInternal() ); + final int newStatus = this.getStatus(); if( newStatus != this.oldStatus ) { @@ -73,7 +78,7 @@ public class DriveWatcher> extends MEInventoryHandler if( type == Actionable.MODULATE && a != null ) { - final int newStatus = this.handler.getStatusForCell( this.is, this.getInternal() ); + final int newStatus = this.getStatus(); if( newStatus != this.oldStatus ) { diff --git a/src/main/java/appeng/me/storage/ItemCellInventory.java b/src/main/java/appeng/me/storage/ItemCellInventory.java index edafedc69..3ad8d7cbb 100644 --- a/src/main/java/appeng/me/storage/ItemCellInventory.java +++ b/src/main/java/appeng/me/storage/ItemCellInventory.java @@ -28,8 +28,8 @@ import appeng.api.config.Actionable; import appeng.api.exceptions.AppEngException; import appeng.api.implementations.items.IStorageCell; import appeng.api.networking.security.IActionSource; +import appeng.api.storage.ICellInventoryHandler; import appeng.api.storage.IMEInventory; -import appeng.api.storage.IMEInventoryHandler; import appeng.api.storage.ISaveProvider; import appeng.api.storage.IStorageChannel; import appeng.api.storage.channels.IItemStorageChannel; @@ -40,17 +40,12 @@ import appeng.core.AELog; public class ItemCellInventory extends AbstractCellInventory { - protected ItemCellInventory( final NBTTagCompound data, final ISaveProvider container ) - { - super( data, container, 8 ); - } - private ItemCellInventory( final ItemStack o, final ISaveProvider container ) throws AppEngException { super( o, container, 8 ); } - public static IMEInventoryHandler getCell( final ItemStack o, final ISaveProvider container2 ) + public static ICellInventoryHandler getCell( final ItemStack o, final ISaveProvider container2 ) { try { @@ -95,7 +90,7 @@ public class ItemCellInventory extends AbstractCellInventory final Item type = i.getItem(); if( type instanceof IStorageCell ) { - if ( ( (IStorageCell) type ).getChannel() == AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ) ) + if( ( (IStorageCell) type ).getChannel() == AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ) ) { return ( (IStorageCell) type ).isStorageCell( i ); } @@ -148,7 +143,6 @@ public class ItemCellInventory extends AbstractCellInventory if( mode == Actionable.MODULATE ) { l.setStackSize( l.getStackSize() + remainingItemCount ); - this.updateItemCount( remainingItemCount ); this.saveChanges(); } return r; @@ -158,7 +152,6 @@ public class ItemCellInventory extends AbstractCellInventory if( mode == Actionable.MODULATE ) { l.setStackSize( l.getStackSize() + input.getStackSize() ); - this.updateItemCount( input.getStackSize() ); this.saveChanges(); } return null; @@ -180,8 +173,6 @@ public class ItemCellInventory extends AbstractCellInventory toWrite.setStackSize( remainingItemCount ); this.cellItems.add( toWrite ); - this.updateItemCount( toWrite.getStackSize() ); - this.saveChanges(); } return toReturn; @@ -189,7 +180,6 @@ public class ItemCellInventory extends AbstractCellInventory if( mode == Actionable.MODULATE ) { - this.updateItemCount( input.getStackSize() ); this.cellItems.add( input ); this.saveChanges(); } @@ -223,7 +213,6 @@ public class ItemCellInventory extends AbstractCellInventory Results.setStackSize( l.getStackSize() ); if( mode == Actionable.MODULATE ) { - this.updateItemCount( -l.getStackSize() ); l.setStackSize( 0 ); this.saveChanges(); } @@ -234,7 +223,6 @@ public class ItemCellInventory extends AbstractCellInventory if( mode == Actionable.MODULATE ) { l.setStackSize( l.getStackSize() - size ); - this.updateItemCount( -size ); this.saveChanges(); } } diff --git a/src/main/java/appeng/parts/automation/PartAbstractFormationPlane.java b/src/main/java/appeng/parts/automation/PartAbstractFormationPlane.java index 2bb7c6313..496efc3a2 100644 --- a/src/main/java/appeng/parts/automation/PartAbstractFormationPlane.java +++ b/src/main/java/appeng/parts/automation/PartAbstractFormationPlane.java @@ -15,6 +15,7 @@ import appeng.api.parts.IPart; import appeng.api.parts.IPartCollisionHelper; import appeng.api.parts.IPartHost; import appeng.api.storage.ICellContainer; +import appeng.api.storage.ICellInventory; import appeng.api.storage.IMEInventory; import appeng.api.storage.data.IAEStack; import appeng.api.storage.data.IItemList; @@ -257,7 +258,7 @@ public abstract class PartAbstractFormationPlane> extends } @Override - public void saveChanges( final IMEInventory cell ) + public void saveChanges( final ICellInventory cell ) { // nope! } diff --git a/src/main/java/appeng/parts/automation/PartFormationPlane.java b/src/main/java/appeng/parts/automation/PartFormationPlane.java index d48096176..f378b29bc 100644 --- a/src/main/java/appeng/parts/automation/PartFormationPlane.java +++ b/src/main/java/appeng/parts/automation/PartFormationPlane.java @@ -61,6 +61,7 @@ import appeng.api.networking.events.MENetworkPowerStatusChange; import appeng.api.networking.security.IActionSource; import appeng.api.parts.IPartItem; import appeng.api.parts.IPartModel; +import appeng.api.storage.ICellInventory; import appeng.api.storage.IMEInventoryHandler; import appeng.api.storage.IStorageChannel; import appeng.api.storage.channels.IItemStorageChannel; diff --git a/src/main/java/appeng/parts/misc/PartSharedStorageBus.java b/src/main/java/appeng/parts/misc/PartSharedStorageBus.java index 9db4888f6..02d1cdefa 100644 --- a/src/main/java/appeng/parts/misc/PartSharedStorageBus.java +++ b/src/main/java/appeng/parts/misc/PartSharedStorageBus.java @@ -39,7 +39,7 @@ import appeng.api.networking.ticking.IGridTickable; import appeng.api.parts.IPartCollisionHelper; import appeng.api.parts.IPartModel; import appeng.api.storage.ICellContainer; -import appeng.api.storage.IMEInventory; +import appeng.api.storage.ICellInventory; import appeng.api.storage.IMEInventoryHandler; import appeng.api.storage.IStorageChannel; import appeng.api.storage.channels.IItemStorageChannel; @@ -126,7 +126,7 @@ public abstract class PartSharedStorageBus extends PartUpgradeable implements IG } @Override - public void saveChanges( IMEInventory cellInventory ) + public void saveChanges( ICellInventory cellInventory ) { } diff --git a/src/main/java/appeng/parts/misc/PartStorageBus.java b/src/main/java/appeng/parts/misc/PartStorageBus.java index f3719f12e..55ee72948 100644 --- a/src/main/java/appeng/parts/misc/PartStorageBus.java +++ b/src/main/java/appeng/parts/misc/PartStorageBus.java @@ -58,6 +58,7 @@ import appeng.api.parts.IPartCollisionHelper; import appeng.api.parts.IPartHost; import appeng.api.parts.IPartModel; import appeng.api.storage.ICellContainer; +import appeng.api.storage.ICellInventory; import appeng.api.storage.IMEInventory; import appeng.api.storage.IMEInventoryHandler; import appeng.api.storage.IMEMonitorHandlerReceiver; @@ -599,7 +600,7 @@ public class PartStorageBus extends PartUpgradeable implements IGridTickable, IC * } */ @Override - public void saveChanges( final IMEInventory cellInventory ) + public void saveChanges( final ICellInventory cellInventory ) { // nope! } diff --git a/src/main/java/appeng/tile/AEBaseTile.java b/src/main/java/appeng/tile/AEBaseTile.java index 7db81fbff..fb0412834 100644 --- a/src/main/java/appeng/tile/AEBaseTile.java +++ b/src/main/java/appeng/tile/AEBaseTile.java @@ -52,6 +52,7 @@ import appeng.core.AELog; import appeng.core.features.IStackSrc; import appeng.helpers.ICustomNameObject; import appeng.helpers.IPriorityHost; +import appeng.hooks.TickHandler; import appeng.tile.inventory.AppEngInternalAEInventory; import appeng.util.Platform; import appeng.util.SettingsFrom; @@ -68,6 +69,7 @@ public class AEBaseTile extends TileEntity implements IOrientable, ICommonTile, private EnumFacing forward = null; private EnumFacing up = null; private IBlockState state; + private boolean markDirtyQueued = false; @Override public boolean shouldRefresh( final World world, final BlockPos pos, final IBlockState oldState, final IBlockState newSate ) @@ -488,7 +490,22 @@ public class AEBaseTile extends TileEntity implements IOrientable, ICommonTile, public void saveChanges() { - markDirty(); + if( this.world != null ) + { + this.world.markChunkDirty( this.pos, this ); + if( !this.markDirtyQueued ) + { + TickHandler.INSTANCE.addCallable( null, this::markDirtyAtEndOfTick ); + this.markDirtyQueued = true; + } + } + } + + private Object markDirtyAtEndOfTick( final World w ) + { + this.markDirty(); + this.markDirtyQueued = false; + return null; } public boolean requiresTESR() diff --git a/src/main/java/appeng/tile/crafting/TileCraftingMonitorTile.java b/src/main/java/appeng/tile/crafting/TileCraftingMonitorTile.java index afa5510e4..085f97eb1 100644 --- a/src/main/java/appeng/tile/crafting/TileCraftingMonitorTile.java +++ b/src/main/java/appeng/tile/crafting/TileCraftingMonitorTile.java @@ -159,7 +159,7 @@ public class TileCraftingMonitorTile extends TileCraftingTile implements IColora } this.paintedColor = newPaintedColor; - this.markDirty(); + this.saveChanges(); this.markForUpdate(); return true; } diff --git a/src/main/java/appeng/tile/crafting/TileMolecularAssembler.java b/src/main/java/appeng/tile/crafting/TileMolecularAssembler.java index 6a16b07a4..74921a9ae 100644 --- a/src/main/java/appeng/tile/crafting/TileMolecularAssembler.java +++ b/src/main/java/appeng/tile/crafting/TileMolecularAssembler.java @@ -133,7 +133,7 @@ public class TileMolecularAssembler extends AENetworkInvTile implements IUpgrade } this.updateSleepiness(); - this.markDirty(); + this.saveChanges(); return true; } } @@ -398,7 +398,7 @@ public class TileMolecularAssembler extends AENetworkInvTile implements IUpgrade // did it eject? if( this.gridInv.getStackInSlot( 9 ).isEmpty() ) { - this.markDirty(); + this.saveChanges(); } this.ejectHeldItems(); @@ -487,7 +487,7 @@ public class TileMolecularAssembler extends AENetworkInvTile implements IUpgrade // ;P } - this.markDirty(); + this.saveChanges(); this.updateSleepiness(); return this.isAwake ? TickRateModulation.IDLE : TickRateModulation.SLEEP; } @@ -509,7 +509,7 @@ public class TileMolecularAssembler extends AENetworkInvTile implements IUpgrade { this.gridInv.setStackInSlot( 9, is ); this.gridInv.setStackInSlot( x, ItemStack.EMPTY ); - this.markDirty(); + this.saveChanges(); return; } } @@ -580,7 +580,7 @@ public class TileMolecularAssembler extends AENetworkInvTile implements IUpgrade if( size != newSize ) { - this.markDirty(); + this.saveChanges(); } return output; diff --git a/src/main/java/appeng/tile/inventory/AppEngCellInventory.java b/src/main/java/appeng/tile/inventory/AppEngCellInventory.java new file mode 100644 index 000000000..60dc0eb4c --- /dev/null +++ b/src/main/java/appeng/tile/inventory/AppEngCellInventory.java @@ -0,0 +1,119 @@ + +package appeng.tile.inventory; + + +import net.minecraft.item.ItemStack; + +import appeng.api.storage.ICellInventoryHandler; +import appeng.util.inv.IAEAppEngInventory; +import appeng.util.inv.IInternalItemHandler; +import appeng.util.inv.filter.IAEItemFilter; + + +public class AppEngCellInventory implements IInternalItemHandler +{ + private final AppEngInternalInventory inv; + private final ICellInventoryHandler handlerForSlot[]; + + public AppEngCellInventory( final IAEAppEngInventory host, final int slots ) + { + this.inv = new AppEngInternalInventory( host, slots, 1 ); + this.handlerForSlot = new ICellInventoryHandler[slots]; + } + + public void setHandler( final int slot, final ICellInventoryHandler handler ) + { + this.handlerForSlot[slot] = handler; + } + + public void setFilter( IAEItemFilter filter ) + { + this.inv.setFilter( filter ); + } + + @Override + public void setStackInSlot( int slot, ItemStack stack ) + { + this.persist( slot ); + this.inv.setStackInSlot( slot, stack ); + this.cleanup( slot ); + } + + @Override + public int getSlots() + { + return this.inv.getSlots(); + } + + @Override + public ItemStack getStackInSlot( int slot ) + { + this.persist( slot ); + return this.inv.getStackInSlot( slot ); + } + + @Override + public ItemStack insertItem( int slot, ItemStack stack, boolean simulate ) + { + this.persist( slot ); + final ItemStack ret = inv.insertItem( slot, stack, simulate ); + this.cleanup( slot ); + return ret; + } + + @Override + public ItemStack extractItem( int slot, int amount, boolean simulate ) + { + this.persist( slot ); + final ItemStack ret = inv.extractItem( slot, amount, simulate ); + this.cleanup( slot ); + return ret; + } + + @Override + public int getSlotLimit( int slot ) + { + return inv.getSlotLimit( slot ); + } + + @Override + public boolean isItemValidForSlot( int slot, ItemStack stack ) + { + return inv.isItemValidForSlot( slot, stack ); + } + + @Override + public void markDirty( int slot ) + { + this.persist( slot ); + this.inv.markDirty( slot ); + this.cleanup( slot ); + } + + public void persist() + { + for( int i = 0; i < this.getSlots(); ++i ) + { + this.persist( i ); + } + } + + private void persist( int slot ) + { + if( this.handlerForSlot[slot] != null ) + { + this.handlerForSlot[slot].getCellInv().persist(); + } + } + + private void cleanup( int slot ) + { + if( this.handlerForSlot[slot] != null ) + { + if( this.handlerForSlot[slot].getCellInv().getItemStack() != this.inv.getStackInSlot( slot ) ) + { + this.handlerForSlot[slot] = null; + } + } + } +} diff --git a/src/main/java/appeng/tile/misc/TileCellWorkbench.java b/src/main/java/appeng/tile/misc/TileCellWorkbench.java index 80c14d516..9a06094c4 100644 --- a/src/main/java/appeng/tile/misc/TileCellWorkbench.java +++ b/src/main/java/appeng/tile/misc/TileCellWorkbench.java @@ -185,7 +185,7 @@ public class TileCellWorkbench extends AEBaseTile implements IUpgradeableHost, I this.config.setStackInSlot( x, ItemStack.EMPTY ); } - this.markDirty(); + this.saveChanges(); } this.locked = false; diff --git a/src/main/java/appeng/tile/misc/TileInscriber.java b/src/main/java/appeng/tile/misc/TileInscriber.java index bd8d27613..7ed1ae4f7 100644 --- a/src/main/java/appeng/tile/misc/TileInscriber.java +++ b/src/main/java/appeng/tile/misc/TileInscriber.java @@ -380,7 +380,7 @@ public class TileInscriber extends AENetworkPowerTile implements IGridTickable, this.sideItemHandler.setStackInSlot( 0, ItemStack.EMPTY ); } } - this.markDirty(); + this.saveChanges(); } else if( this.finalStep == 16 ) { diff --git a/src/main/java/appeng/tile/misc/TileInterface.java b/src/main/java/appeng/tile/misc/TileInterface.java index ea7e95616..5aa20114e 100644 --- a/src/main/java/appeng/tile/misc/TileInterface.java +++ b/src/main/java/appeng/tile/misc/TileInterface.java @@ -130,7 +130,7 @@ public class TileInterface extends AENetworkInvTile implements IGridTickable, II this.configureNodeSides(); this.markForUpdate(); - this.markDirty(); + this.saveChanges(); } private void configureNodeSides() @@ -145,12 +145,6 @@ public class TileInterface extends AENetworkInvTile implements IGridTickable, II } } - @Override - public void markDirty() - { - this.duality.markDirty(); - } - @Override public void getDrops( final World w, final BlockPos pos, final List drops ) { diff --git a/src/main/java/appeng/tile/misc/TilePaint.java b/src/main/java/appeng/tile/misc/TilePaint.java index 637148531..3ea68a889 100644 --- a/src/main/java/appeng/tile/misc/TilePaint.java +++ b/src/main/java/appeng/tile/misc/TilePaint.java @@ -191,7 +191,7 @@ public class TilePaint extends AEBaseTile } this.markForUpdate(); - this.markDirty(); + this.saveChanges(); } private void updateData() @@ -265,7 +265,7 @@ public class TilePaint extends AEBaseTile this.maxLit(); this.markForUpdate(); - this.markDirty(); + this.saveChanges(); } } diff --git a/src/main/java/appeng/tile/misc/TileSecurityStation.java b/src/main/java/appeng/tile/misc/TileSecurityStation.java index abc309e34..eac5479c1 100644 --- a/src/main/java/appeng/tile/misc/TileSecurityStation.java +++ b/src/main/java/appeng/tile/misc/TileSecurityStation.java @@ -363,7 +363,7 @@ public class TileSecurityStation extends AENetworkTile implements ITerminalHost, } this.paintedColor = newPaintedColor; - this.markDirty(); + this.saveChanges(); this.markForUpdate(); return true; } diff --git a/src/main/java/appeng/tile/misc/TileVibrationChamber.java b/src/main/java/appeng/tile/misc/TileVibrationChamber.java index 1ad93bee3..d101cd5e5 100644 --- a/src/main/java/appeng/tile/misc/TileVibrationChamber.java +++ b/src/main/java/appeng/tile/misc/TileVibrationChamber.java @@ -261,7 +261,7 @@ public class TileVibrationChamber extends AENetworkInvTile implements IGridTicka this.inv.setStackInSlot( 0, is ); } - this.markDirty(); + this.saveChanges(); } } diff --git a/src/main/java/appeng/tile/networking/TileCableBus.java b/src/main/java/appeng/tile/networking/TileCableBus.java index 81dce258f..0f53291cd 100644 --- a/src/main/java/appeng/tile/networking/TileCableBus.java +++ b/src/main/java/appeng/tile/networking/TileCableBus.java @@ -312,7 +312,7 @@ public class TileCableBus extends AEBaseTile implements AEMultiTile, ICustomColl @Override public void markForSave() { - super.markDirty(); + this.saveChanges(); } @Override diff --git a/src/main/java/appeng/tile/networking/TileWireless.java b/src/main/java/appeng/tile/networking/TileWireless.java index 1a102bda7..3c9d401c9 100644 --- a/src/main/java/appeng/tile/networking/TileWireless.java +++ b/src/main/java/appeng/tile/networking/TileWireless.java @@ -163,9 +163,10 @@ public class TileWireless extends AENetworkInvTile implements IWirelessAccessPoi } @Override - public void markDirty() + public void saveChanges() { this.updatePower(); + super.saveChanges(); } @Override diff --git a/src/main/java/appeng/tile/storage/TileChest.java b/src/main/java/appeng/tile/storage/TileChest.java index b8f60092c..4c593a1d3 100644 --- a/src/main/java/appeng/tile/storage/TileChest.java +++ b/src/main/java/appeng/tile/storage/TileChest.java @@ -69,6 +69,8 @@ import appeng.api.networking.security.ISecurityGrid; import appeng.api.networking.storage.IBaseMonitor; import appeng.api.networking.storage.IStorageGrid; import appeng.api.storage.ICellHandler; +import appeng.api.storage.ICellInventory; +import appeng.api.storage.ICellInventoryHandler; import appeng.api.storage.IMEInventory; import appeng.api.storage.IMEInventoryHandler; import appeng.api.storage.IMEMonitor; @@ -121,7 +123,6 @@ public class TileChest extends AENetworkPowerTile implements IMEChest, ITerminal private boolean wasActive = false; private AEColor paintedColor = AEColor.TRANSPARENT; private boolean isCached = false; - private ICellHandler cellHandler; private MEMonitorHandler itemCell; private MEMonitorHandler fluidCell; private Accessor accessor; @@ -226,23 +227,23 @@ public class TileChest extends AENetworkPowerTile implements IMEChest, ITerminal if( !is.isEmpty() ) { this.isCached = true; - this.cellHandler = AEApi.instance().registries().cell().getHandler( is ); - if( this.cellHandler != null ) + ICellHandler cellHandler = AEApi.instance().registries().cell().getHandler( is ); + if( cellHandler != null ) { double power = 1.0; - final IMEInventoryHandler itemCell = this.cellHandler.getCellInventory( is, this, + final ICellInventoryHandler itemCell = cellHandler.getCellInventory( is, this, AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ) ); - final IMEInventoryHandler fluidCell = this.cellHandler.getCellInventory( is, this, + final ICellInventoryHandler fluidCell = cellHandler.getCellInventory( is, this, AEApi.instance().storage().getStorageChannel( IFluidStorageChannel.class ) ); if( itemCell != null ) { - power += this.cellHandler.cellIdleDrain( is, itemCell ); + power += cellHandler.cellIdleDrain( is, itemCell ); } else if( fluidCell != null ) { - power += this.cellHandler.cellIdleDrain( is, fluidCell ); + power += cellHandler.cellIdleDrain( is, fluidCell ); } this.getProxy().setIdlePowerUsage( power ); @@ -766,15 +767,16 @@ public class TileChest extends AENetworkPowerTile implements IMEChest, ITerminal } this.paintedColor = newPaintedColor; - this.markDirty(); + this.saveChanges(); this.markForUpdate(); return true; } @Override - public void saveChanges( final IMEInventory cellInventory ) + public void saveChanges( final ICellInventory cellInventory ) { - this.markDirty(); + cellInventory.persist(); + this.world.markChunkDirty( this.pos, this ); } private static class ChestNoHandler extends Exception diff --git a/src/main/java/appeng/tile/storage/TileDrive.java b/src/main/java/appeng/tile/storage/TileDrive.java index e312dc2b7..c06cf8328 100644 --- a/src/main/java/appeng/tile/storage/TileDrive.java +++ b/src/main/java/appeng/tile/storage/TileDrive.java @@ -43,7 +43,8 @@ import appeng.api.networking.events.MENetworkPowerStatusChange; import appeng.api.networking.security.IActionSource; import appeng.api.networking.storage.IStorageGrid; import appeng.api.storage.ICellHandler; -import appeng.api.storage.IMEInventory; +import appeng.api.storage.ICellInventory; +import appeng.api.storage.ICellInventoryHandler; import appeng.api.storage.IMEInventoryHandler; import appeng.api.storage.IStorageChannel; import appeng.api.storage.data.IAEItemStack; @@ -56,9 +57,8 @@ import appeng.helpers.IPriorityHost; import appeng.me.GridAccessException; import appeng.me.helpers.MachineSource; import appeng.me.storage.DriveWatcher; -import appeng.me.storage.MEInventoryHandler; import appeng.tile.grid.AENetworkInvTile; -import appeng.tile.inventory.AppEngInternalInventory; +import appeng.tile.inventory.AppEngCellInventory; import appeng.util.Platform; import appeng.util.inv.InvOperation; import appeng.util.inv.filter.IAEItemFilter; @@ -71,7 +71,7 @@ public class TileDrive extends AENetworkInvTile implements IChestOrDrive, IPrior private static final int BIT_BLINK_MASK = 0x24924924; private static final int BIT_STATE_MASK = 0xDB6DB6DB; - private final AppEngInternalInventory inv = new AppEngInternalInventory( this, 10, 1 ); + private final AppEngCellInventory inv = new AppEngCellInventory( this, 10 ); private final ICellHandler[] handlersBySlot = new ICellHandler[10]; private final DriveWatcher[] invBySlot = new DriveWatcher[10]; private final IActionSource mySrc; @@ -145,21 +145,13 @@ public class TileDrive extends AENetworkInvTile implements IChestOrDrive, IPrior return ( this.state >> ( slot * 3 ) ) & 3; } - final ItemStack cell = this.inv.getStackInSlot( 2 ); - final ICellHandler ch = this.handlersBySlot[slot]; - - final MEInventoryHandler handler = this.invBySlot[slot]; + final DriveWatcher handler = this.invBySlot[slot]; if( handler == null ) { return 0; } - if( ch != null ) - { - return ch.getStatusForCell( cell, handler.getInternal() ); - } - - return 0; + return handler.getStatus(); } @Override @@ -307,10 +299,11 @@ public class TileDrive extends AENetworkInvTile implements IChestOrDrive, IPrior for( IStorageChannel> channel : storageChannels ) { - IMEInventoryHandler cell = this.handlersBySlot[x].getCellInventory( is, this, channel ); + ICellInventoryHandler cell = this.handlersBySlot[x].getCellInventory( is, this, channel ); if( cell != null ) { + this.inv.setHandler( x, cell ); power += this.handlersBySlot[x].cellIdleDrain( is, cell ); final DriveWatcher ih = new DriveWatcher( cell, is, this.handlersBySlot[x], this ); @@ -360,7 +353,7 @@ public class TileDrive extends AENetworkInvTile implements IChestOrDrive, IPrior public void setPriority( final int newValue ) { this.priority = newValue; - this.markDirty(); + this.saveChanges(); this.isCached = false; // recalculate the storage cell. this.updateState(); @@ -384,7 +377,7 @@ public class TileDrive extends AENetworkInvTile implements IChestOrDrive, IPrior } @Override - public void saveChanges( final IMEInventory cellInventory ) + public void saveChanges( final ICellInventory cellInventory ) { this.world.markChunkDirty( this.pos, this ); }