diff --git a/src/api/java/appeng/api/IAppEngApi.java b/src/api/java/appeng/api/IAppEngApi.java index 69e39519a..3b9203b16 100644 --- a/src/api/java/appeng/api/IAppEngApi.java +++ b/src/api/java/appeng/api/IAppEngApi.java @@ -30,6 +30,7 @@ import appeng.api.networking.IGridHelper; import appeng.api.networking.IGridNode; import appeng.api.parts.IPartHelper; import appeng.api.storage.IStorageHelper; +import appeng.api.util.IClientHelper; @AEInjectable @@ -60,4 +61,9 @@ public interface IAppEngApi */ IDefinitions definitions(); + /** + * @return Utility methods primarily useful for client side stuff + */ + IClientHelper client(); + } \ No newline at end of file diff --git a/src/api/java/appeng/api/storage/ICellGuiHandler.java b/src/api/java/appeng/api/storage/ICellGuiHandler.java new file mode 100644 index 000000000..5870a9606 --- /dev/null +++ b/src/api/java/appeng/api/storage/ICellGuiHandler.java @@ -0,0 +1,47 @@ + +package appeng.api.storage; + + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; + +import appeng.api.implementations.tiles.IChestOrDrive; +import appeng.api.storage.data.IAEStack; + + +public interface ICellGuiHandler +{ + /** + * Return true if this handler can show GUI for this channel. + * + * @param channel Storage channel + * @return True if handled, else false. + */ + > boolean isHandlerFor( IStorageChannel channel ); + + /** + * Return true to prioritize this handler for the provided {@link ItemStack}. + * + * @param is Cell ItemStack + * @return True, if specialized else false. + */ + default boolean isSpecializedFor( ItemStack is ) + { + return false; + } + + /** + * Called when the storage cell is placed in an ME Chest and the user tries to open the terminal side, if your item + * is not available via ME Chests simply tell the user they can't use it, or something, other wise you should open + * your gui and display the cell to the user. + * + * @param player player opening chest gui + * @param chest to be opened chest + * @param cellHandler cell handler + * @param inv inventory handler + * @param is item + * @param chan storage channel + */ + > void openChestGui( EntityPlayer player, IChestOrDrive chest, ICellHandler cellHandler, IMEInventoryHandler inv, ItemStack is, IStorageChannel chan ); + +} diff --git a/src/api/java/appeng/api/storage/ICellHandler.java b/src/api/java/appeng/api/storage/ICellHandler.java index cfde25bdf..3b626d467 100644 --- a/src/api/java/appeng/api/storage/ICellHandler.java +++ b/src/api/java/appeng/api/storage/ICellHandler.java @@ -24,10 +24,8 @@ package appeng.api.storage; -import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; -import appeng.api.implementations.tiles.IChestOrDrive; import appeng.api.storage.data.IAEStack; @@ -59,20 +57,6 @@ public interface ICellHandler */ > 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 - * is not available via ME Chests simply tell the user they can't use it, or something, other wise you should open - * your gui and display the cell to the user. - * - * @param player player opening chest gui - * @param chest to be opened chest - * @param cellHandler cell handler - * @param inv inventory handler - * @param is item - * @param chan storage channel - */ - > void openChestGui( EntityPlayer player, IChestOrDrive chest, ICellHandler cellHandler, IMEInventoryHandler inv, ItemStack is, IStorageChannel chan ); - /** * 0 - cell is missing. * @@ -87,10 +71,31 @@ public interface ICellHandler * * @return get the status of the cell based on its contents. */ - > int getStatusForCell( ItemStack is, IMEInventory handler ); + default > int getStatusForCell( ItemStack is, ICellInventoryHandler handler ) + { + if( handler.getCellInv() != null ) + { + int val = handler.getCellInv().getStatusForCell(); + + if( val == 1 && handler.isPreformatted() ) + { + val = 2; + } + + return val; + } + return 0; + } /** * @return the ae/t to drain for this storage cell inside a chest/drive. */ - > double cellIdleDrain( ItemStack is, IMEInventory handler ); + default > double cellIdleDrain( ItemStack is, ICellInventoryHandler handler ) + { + if( handler.getCellInv() != null ) + { + return handler.getCellInv().getIdleDrain(); + } + return 1.0; + } } \ No newline at end of file diff --git a/src/api/java/appeng/api/storage/ICellInventory.java b/src/api/java/appeng/api/storage/ICellInventory.java index fe7ed7438..fa37b4c3d 100644 --- a/src/api/java/appeng/api/storage/ICellInventory.java +++ b/src/api/java/appeng/api/storage/ICellInventory.java @@ -40,7 +40,7 @@ public interface ICellInventory> extends IMEInventory ItemStack getItemStack(); /** - * @return idle cost for this Storage Cell + * @return the ae/t to drain for this storage cell inside a chest/drive. */ double getIdleDrain(); @@ -115,7 +115,15 @@ public interface ICellInventory> extends IMEInventory int getUnusedItemCount(); /** - * @return the status number for this drive. + * 0 - cell is missing. + * + * 1 - green, ( usually means available room for types or items. ) + * + * 2 - orange, ( usually means available room for items, but not types. ) + * + * 3 - red, ( usually means the cell is 100% full ) + * + * @return get the status of the cell based on its contents. */ int getStatusForCell(); diff --git a/src/api/java/appeng/api/storage/ICellInventoryHandler.java b/src/api/java/appeng/api/storage/ICellInventoryHandler.java index 2a80438f0..a29228cf8 100644 --- a/src/api/java/appeng/api/storage/ICellInventoryHandler.java +++ b/src/api/java/appeng/api/storage/ICellInventoryHandler.java @@ -24,6 +24,8 @@ package appeng.api.storage; +import javax.annotation.Nullable; + import appeng.api.config.IncludeExclude; import appeng.api.storage.data.IAEStack; @@ -32,8 +34,11 @@ public interface ICellInventoryHandler> extends IMEInvento { /** + * Get access to the ICellInventory. Can be null for custom cells. + * * @return get access to the Cell Inventory. */ + @Nullable 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 a56451203..4a0744fab 100644 --- a/src/api/java/appeng/api/storage/ICellRegistry.java +++ b/src/api/java/appeng/api/storage/ICellRegistry.java @@ -53,6 +53,13 @@ public interface ICellRegistry */ void addCellHandler( @Nonnull ICellHandler handler ); + /** + * Register a new handler + * + * @param handler cell gui handler + */ + void addCellGuiHandler( @Nonnull ICellGuiHandler handler ); + /** * return true, if you can get a InventoryHandler for the item passed. * @@ -64,7 +71,7 @@ public interface ICellRegistry boolean isCellHandled( ItemStack is ); /** - * get the handler, for the requested type. + * get the handler, for the requested item. * * @param is to be checked item * @@ -74,13 +81,23 @@ public interface ICellRegistry ICellHandler getHandler( ItemStack is ); /** - * returns an IMEInventoryHandler for the provided item. + * get the handler, for the requested channel. + * + * @param channel requested channel + * @param Cell ItemStack + * @return the handler registered for this channel. + */ + @Nullable + > ICellGuiHandler getGuiHandler( IStorageChannel channel, ItemStack is ); + + /** + * returns an ICellInventoryHandler for the provided item by querying all registered handlers. * * @param is item with inventory handler * @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. + * @return new ICellInventoryHandler, or null if there isn't one. */ @Nullable > ICellInventoryHandler getCellInventory( ItemStack is, ISaveProvider host, IStorageChannel chan ); diff --git a/src/api/java/appeng/api/storage/ISaveProvider.java b/src/api/java/appeng/api/storage/ISaveProvider.java index 5a224f8a6..c6e8f0afa 100644 --- a/src/api/java/appeng/api/storage/ISaveProvider.java +++ b/src/api/java/appeng/api/storage/ISaveProvider.java @@ -24,11 +24,19 @@ package appeng.api.storage; +import javax.annotation.Nullable; + + /** * Tells the cell provider that changes have been made an the cell must be persisted * */ public interface ISaveProvider { - void saveChanges( ICellInventory cellInventory ); + /** + * Cell has changed and needs to be changed. + * + * @param cellInventory can be null for custom cells. + */ + void saveChanges( @Nullable ICellInventory cellInventory ); } diff --git a/src/api/java/appeng/api/storage/IStorageChannel.java b/src/api/java/appeng/api/storage/IStorageChannel.java index f8c6f049a..122dcf6f3 100644 --- a/src/api/java/appeng/api/storage/IStorageChannel.java +++ b/src/api/java/appeng/api/storage/IStorageChannel.java @@ -32,10 +32,9 @@ import javax.annotation.Nullable; import io.netty.buffer.ByteBuf; import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraftforge.fluids.FluidStack; -import appeng.api.networking.energy.IEnergySource; -import appeng.api.networking.security.IActionSource; -import appeng.api.storage.data.IAEItemStack; import appeng.api.storage.data.IAEStack; import appeng.api.storage.data.IItemList; @@ -56,6 +55,17 @@ public interface IStorageChannel> return 1; } + /** + * The number of units (eg item count, or millibuckets) that can be stored per byte in a storage cell. + * Standard value for items is 8, and for fluids it's 8000 + * + * @return number of units + */ + default int getUnitsPerByte() + { + return 8; + } + /** * Create a new {@link IItemList} of the specific type. * @@ -68,9 +78,12 @@ public interface IStorageChannel> * Create a new {@link IAEStack} subtype of the specific object. * * The parameter is unbound to allow a slightly more flexible approach. - * But the general intention is about converting an {@link ItemStack} into the corresponding {@link IAEItemStack}. + * But the general intention is about converting an {@link ItemStack} or {@link FluidStack} into the corresponding + * {@link IAEStack}. * Another valid case might be to use it instead of {@link IAEStack#copy()}, but this might not be supported by all * types. + * IAEStacks that use custom items for {@link IAEStack#asItemStackRepresentation()} must also be able to convert + * these. * * @param input The object to turn into an {@link IAEStack} * @return The converted stack or null @@ -88,29 +101,11 @@ public interface IStorageChannel> T readFromPacket( @Nonnull ByteBuf input ) throws IOException; /** - * use energy from energy, to remove request items from cell, at the request of src. - * - * @param energy to be drained energy source - * @param cell cell of requested items - * @param request requested items - * @param src action source - * - * @return items that successfully extracted. + * create from nbt data + * + * @param nbt + * @return */ @Nullable - T poweredExtraction( @Nonnull IEnergySource energy, @Nonnull IMEInventory cell, @Nonnull T request, @Nonnull IActionSource src ); - - /** - * use energy from energy, to inject input items into cell, at the request of src - * - * @param energy to be added energy source - * @param cell injected cell - * @param input to be injected items - * @param src action source - * - * @return items that failed to insert. - */ - @Nullable - T poweredInsert( @Nonnull IEnergySource energy, @Nonnull IMEInventory cell, @Nonnull T input, @Nonnull IActionSource src ); - + T createFromNBT( @Nonnull NBTTagCompound nbt ); } diff --git a/src/api/java/appeng/api/storage/IStorageHelper.java b/src/api/java/appeng/api/storage/IStorageHelper.java index 29d0707f2..91328c7f9 100644 --- a/src/api/java/appeng/api/storage/IStorageHelper.java +++ b/src/api/java/appeng/api/storage/IStorageHelper.java @@ -30,8 +30,11 @@ import javax.annotation.Nonnull; import net.minecraft.nbt.NBTTagCompound; +import appeng.api.config.Actionable; import appeng.api.networking.crafting.ICraftingLink; import appeng.api.networking.crafting.ICraftingRequester; +import appeng.api.networking.energy.IEnergySource; +import appeng.api.networking.security.IActionSource; import appeng.api.storage.data.IAEFluidStack; import appeng.api.storage.data.IAEItemStack; import appeng.api.storage.data.IAEStack; @@ -90,4 +93,27 @@ public interface IStorageHelper */ ICraftingLink loadCraftingLink( NBTTagCompound data, ICraftingRequester req ); + /** + * Extracts items from a {@link IMEInventory} respecting power requirements. + * + * @param energy Energy source. + * @param inv Inventory to extract from. + * @param request Requested item and count. + * @param src Action source. + * @param mode Simulate or modulate + * @return extracted items or {@code null} of nothing was extracted. + */ + > T poweredExtraction( final IEnergySource energy, final IMEInventory inv, final T request, final IActionSource src, final Actionable mode ); + + /** + * Inserts items into a {@link IMEInventory} respecting power requirements. + * + * @param energy Energy source. + * @param inv Inventory to insert into. + * @param request Items to insert. + * @param src Action source. + * @param mode Simulate or modulate + * @return items not inserted or {@code null} if everything was inserted. + */ + > T poweredInsert( final IEnergySource energy, final IMEInventory inv, final T input, final IActionSource src, final Actionable mode ); } diff --git a/src/api/java/appeng/api/util/IClientHelper.java b/src/api/java/appeng/api/util/IClientHelper.java new file mode 100644 index 000000000..4209a4f6e --- /dev/null +++ b/src/api/java/appeng/api/util/IClientHelper.java @@ -0,0 +1,21 @@ + +package appeng.api.util; + + +import java.util.List; + +import appeng.api.storage.ICellInventoryHandler; +import appeng.api.storage.data.IAEStack; + + +public interface IClientHelper +{ + /** + * Add cell information to the provided list. Used for tooltip content. + * + * @param handler Cell handler. + * @param lines List of lines to add to. + */ + > void addCellInformation( ICellInventoryHandler handler, List lines ); + +} diff --git a/src/main/java/appeng/block/storage/BlockChest.java b/src/main/java/appeng/block/storage/BlockChest.java index f38ef7b1a..8ce89a037 100644 --- a/src/main/java/appeng/block/storage/BlockChest.java +++ b/src/main/java/appeng/block/storage/BlockChest.java @@ -34,8 +34,6 @@ import net.minecraft.util.math.BlockPos; import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; -import appeng.api.AEApi; -import appeng.api.storage.ICellHandler; import appeng.api.util.AEPartLocation; import appeng.block.AEBaseTileBlock; import appeng.core.localization.PlayerMessages; @@ -108,14 +106,7 @@ public class BlockChest extends AEBaseTileBlock } else { - final ItemStack cell = tg.getCell(); - if( !cell.isEmpty() ) - { - final ICellHandler ch = AEApi.instance().registries().cell().getHandler( cell ); - - tg.openGui( p, ch, cell, side ); - } - else + if( !tg.openGui( p ) ) { p.sendMessage( PlayerMessages.ChestCannotReadStorageCell.get() ); } diff --git a/src/main/java/appeng/core/Api.java b/src/main/java/appeng/core/Api.java index d35a46590..53d7b274d 100644 --- a/src/main/java/appeng/core/Api.java +++ b/src/main/java/appeng/core/Api.java @@ -23,6 +23,8 @@ import appeng.api.IAppEngApi; import appeng.api.features.IRegistryContainer; import appeng.api.networking.IGridHelper; import appeng.api.storage.IStorageHelper; +import appeng.api.util.IClientHelper; +import appeng.core.api.ApiClientHelper; import appeng.core.api.ApiGrid; import appeng.core.api.ApiPart; import appeng.core.api.ApiStorage; @@ -41,6 +43,7 @@ public final class Api implements IAppEngApi private final IStorageHelper storageHelper; private final IGridHelper networkHelper; private final ApiDefinitions definitions; + private final IClientHelper client; private Api() { @@ -49,6 +52,7 @@ public final class Api implements IAppEngApi this.registryContainer = new RegistryContainer(); this.partHelper = new ApiPart(); this.definitions = new ApiDefinitions( (PartModels) this.registryContainer.partModels() ); + this.client = new ApiClientHelper(); } public PartModels getPartModels() @@ -85,4 +89,10 @@ public final class Api implements IAppEngApi { return this.definitions; } + + @Override + public IClientHelper client() + { + return this.client; + } } diff --git a/src/main/java/appeng/core/Registration.java b/src/main/java/appeng/core/Registration.java index a42b3c5a1..b7439e858 100644 --- a/src/main/java/appeng/core/Registration.java +++ b/src/main/java/appeng/core/Registration.java @@ -91,7 +91,8 @@ import appeng.bootstrap.components.IRecipeRegistrationComponent; import appeng.capabilities.Capabilities; import appeng.core.features.AEFeature; import appeng.core.features.registries.P2PTunnelRegistry; -import appeng.core.features.registries.cell.BasicItemCellHandler; +import appeng.core.features.registries.cell.BasicCellHandler; +import appeng.core.features.registries.cell.BasicItemCellGuiHandler; import appeng.core.features.registries.cell.CreativeCellHandler; import appeng.core.localization.GuiText; import appeng.core.localization.PlayerMessages; @@ -99,7 +100,7 @@ import appeng.core.stats.AdvancementTriggers; import appeng.core.stats.PartItemPredicate; import appeng.core.stats.Stats; import appeng.core.worlddata.SpatialDimensionManager; -import appeng.fluids.registries.BasicFluidCellHandler; +import appeng.fluids.registries.BasicFluidCellGuiHandler; import appeng.hooks.TickHandler; import appeng.items.materials.ItemMaterial; import appeng.items.parts.ItemFacade; @@ -236,9 +237,10 @@ final class Registration gcr.registerGridCache( ISecurityGrid.class, SecurityCache.class ); gcr.registerGridCache( ICraftingGrid.class, CraftingGridCache.class ); - registries.cell().addCellHandler( new BasicItemCellHandler() ); + registries.cell().addCellHandler( new BasicCellHandler() ); registries.cell().addCellHandler( new CreativeCellHandler() ); - registries.cell().addCellHandler( new BasicFluidCellHandler() ); + registries.cell().addCellGuiHandler( new BasicItemCellGuiHandler() ); + registries.cell().addCellGuiHandler( new BasicFluidCellGuiHandler() ); api.definitions().materials().matterBall().maybeStack( 1 ).ifPresent( ammoStack -> { @@ -286,7 +288,7 @@ final class Registration final Side side = FMLCommonHandler.instance().getEffectiveSide(); definitions.getRegistry().getBootstrapComponents( IItemRegistrationComponent.class ).forEachRemaining( b -> b.itemRegistration( side, registry ) ); // register oredicts - definitions.getRegistry().getBootstrapComponents( IOreDictComponent.class ).forEachRemaining( b -> b.oreRegistration( side ) ); + definitions.getRegistry().getBootstrapComponents( IOreDictComponent.class ).forEachRemaining( b -> b.oreRegistration( side ) ); ItemMaterial.instance.registerOredicts(); ItemPart.instance.registerOreDicts(); } @@ -320,15 +322,15 @@ final class Registration final AERecipeLoader ldr = new AERecipeLoader(); ldr.loadProcessingRecipes(); } - + @SubscribeEvent - public void registerEntities( RegistryEvent.Register event) + public void registerEntities( RegistryEvent.Register event ) { final IForgeRegistry registry = event.getRegistry(); final ApiDefinitions definitions = Api.INSTANCE.definitions(); definitions.getRegistry().getBootstrapComponents( IEntityRegistrationComponent.class ).forEachRemaining( b -> b.entityRegistration( registry ) ); } - + @SubscribeEvent public void attachSpatialDimensionManager( AttachCapabilitiesEvent event ) { diff --git a/src/main/java/appeng/core/api/ApiClientHelper.java b/src/main/java/appeng/core/api/ApiClientHelper.java new file mode 100644 index 000000000..d9db7bc9f --- /dev/null +++ b/src/main/java/appeng/core/api/ApiClientHelper.java @@ -0,0 +1,51 @@ + +package appeng.core.api; + + +import java.util.List; + +import appeng.api.config.IncludeExclude; +import appeng.api.storage.ICellInventory; +import appeng.api.storage.ICellInventoryHandler; +import appeng.api.storage.data.IAEStack; +import appeng.api.util.IClientHelper; +import appeng.core.localization.GuiText; + + +public class ApiClientHelper implements IClientHelper +{ + @Override + public > void addCellInformation( ICellInventoryHandler handler, List lines ) + { + if( handler == null ) + { + return; + } + + final ICellInventory cellInventory = handler.getCellInv(); + + if( cellInventory != null ) + { + lines.add( cellInventory.getUsedBytes() + " " + GuiText.Of.getLocal() + ' ' + cellInventory.getTotalBytes() + ' ' + GuiText.BytesUsed.getLocal() ); + + lines.add( cellInventory.getStoredItemTypes() + " " + GuiText.Of.getLocal() + ' ' + cellInventory.getTotalItemTypes() + ' ' + GuiText.Types + .getLocal() ); + } + + if( handler.isPreformatted() ) + { + final String list = ( handler.getIncludeExcludeMode() == IncludeExclude.WHITELIST ? GuiText.Included : GuiText.Excluded ).getLocal(); + + if( handler.isFuzzy() ) + { + lines.add( GuiText.Partitioned.getLocal() + " - " + list + ' ' + GuiText.Fuzzy.getLocal() ); + } + else + { + lines.add( GuiText.Partitioned.getLocal() + " - " + list + ' ' + GuiText.Precise.getLocal() ); + } + } + + } + +} diff --git a/src/main/java/appeng/core/api/ApiStorage.java b/src/main/java/appeng/core/api/ApiStorage.java index f8c4e410d..083d71e28 100644 --- a/src/main/java/appeng/core/api/ApiStorage.java +++ b/src/main/java/appeng/core/api/ApiStorage.java @@ -32,7 +32,9 @@ import io.netty.buffer.ByteBuf; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraftforge.fluids.FluidStack; +import net.minecraftforge.fluids.FluidUtil; +import appeng.api.config.Actionable; import appeng.api.networking.crafting.ICraftingLink; import appeng.api.networking.crafting.ICraftingRequester; import appeng.api.networking.energy.IEnergySource; @@ -47,6 +49,7 @@ import appeng.api.storage.data.IAEItemStack; import appeng.api.storage.data.IAEStack; import appeng.api.storage.data.IItemList; import appeng.crafting.CraftingLink; +import appeng.fluids.items.FluidDummyItem; import appeng.fluids.util.AEFluidStack; import appeng.fluids.util.FluidList; import appeng.util.Platform; @@ -104,6 +107,18 @@ public class ApiStorage implements IStorageHelper return new CraftingLink( data, req ); } + @Override + public > T poweredInsert( IEnergySource energy, IMEInventory inv, T input, IActionSource src, Actionable mode ) + { + return Platform.poweredInsert( energy, inv, input, src, mode ); + } + + @Override + public > T poweredExtraction( IEnergySource energy, IMEInventory inv, T request, IActionSource src, Actionable mode ) + { + return Platform.poweredExtraction( energy, inv, request, src, mode ); + } + private static final class ItemStorageChannel implements IItemStorageChannel { @@ -126,6 +141,13 @@ public class ApiStorage implements IStorageHelper return null; } + @Override + public IAEItemStack createFromNBT( NBTTagCompound nbt ) + { + Preconditions.checkNotNull( nbt ); + return AEItemStack.fromNBT( nbt ); + } + @Override public IAEItemStack readFromPacket( ByteBuf input ) throws IOException { @@ -133,28 +155,6 @@ public class ApiStorage implements IStorageHelper return AEItemStack.fromPacket( input ); } - - @Override - public IAEItemStack poweredExtraction( IEnergySource energy, IMEInventory cell, IAEItemStack request, IActionSource src ) - { - Preconditions.checkNotNull( energy ); - Preconditions.checkNotNull( cell ); - Preconditions.checkNotNull( request ); - Preconditions.checkNotNull( src ); - - return Platform.poweredExtraction( energy, cell, request, src ); - } - - @Override - public IAEItemStack poweredInsert( IEnergySource energy, IMEInventory cell, IAEItemStack input, IActionSource src ) - { - Preconditions.checkNotNull( energy ); - Preconditions.checkNotNull( cell ); - Preconditions.checkNotNull( input ); - Preconditions.checkNotNull( src ); - - return Platform.poweredInsert( energy, cell, input, src ); - } } private static final class FluidStorageChannel implements IFluidStorageChannel @@ -166,6 +166,12 @@ public class ApiStorage implements IStorageHelper return 125; } + @Override + public int getUnitsPerByte() + { + return 8000; + } + @Override public IItemList createList() { @@ -181,6 +187,18 @@ public class ApiStorage implements IStorageHelper { return AEFluidStack.fromFluidStack( (FluidStack) input ); } + if( input instanceof ItemStack ) + { + final ItemStack is = (ItemStack) input; + if( is.getItem() instanceof FluidDummyItem ) + { + return AEFluidStack.fromFluidStack( ( (FluidDummyItem) is.getItem() ).getFluidStack( is ) ); + } + else + { + return AEFluidStack.fromFluidStack( FluidUtil.getFluidContained( is ) ); + } + } return null; } @@ -194,25 +212,10 @@ public class ApiStorage implements IStorageHelper } @Override - public IAEFluidStack poweredExtraction( IEnergySource energy, IMEInventory cell, IAEFluidStack request, IActionSource src ) + public IAEFluidStack createFromNBT( NBTTagCompound nbt ) { - Preconditions.checkNotNull( energy ); - Preconditions.checkNotNull( cell ); - Preconditions.checkNotNull( request ); - Preconditions.checkNotNull( src ); - - return Platform.poweredExtraction( energy, cell, request, src ); - } - - @Override - public IAEFluidStack poweredInsert( IEnergySource energy, IMEInventory cell, IAEFluidStack input, IActionSource src ) - { - Preconditions.checkNotNull( energy ); - Preconditions.checkNotNull( cell ); - Preconditions.checkNotNull( input ); - Preconditions.checkNotNull( src ); - - return Platform.poweredInsert( energy, cell, input, src ); + Preconditions.checkNotNull( nbt ); + return AEFluidStack.fromNBT( nbt ); } } diff --git a/src/main/java/appeng/core/features/registries/cell/BasicCellHandler.java b/src/main/java/appeng/core/features/registries/cell/BasicCellHandler.java new file mode 100644 index 000000000..c4fba4323 --- /dev/null +++ b/src/main/java/appeng/core/features/registries/cell/BasicCellHandler.java @@ -0,0 +1,54 @@ +/* + * This file is part of Applied Energistics 2. + * Copyright (c) 2013 - 2015, AlgorithmX2, All rights reserved. + * + * Applied Energistics 2 is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Applied Energistics 2 is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Applied Energistics 2. If not, see . + */ + +package appeng.core.features.registries.cell; + + +import net.minecraft.item.ItemStack; + +import appeng.api.storage.ICellHandler; +import appeng.api.storage.ICellInventory; +import appeng.api.storage.ICellInventoryHandler; +import appeng.api.storage.ISaveProvider; +import appeng.api.storage.IStorageChannel; +import appeng.api.storage.data.IAEStack; +import appeng.me.storage.BasicCellInventory; +import appeng.me.storage.BasicCellInventoryHandler; + + +public class BasicCellHandler implements ICellHandler +{ + + @Override + public boolean isCell( final ItemStack is ) + { + return BasicCellInventory.isCell( is ); + } + + @Override + public > ICellInventoryHandler getCellInventory( final ItemStack is, final ISaveProvider container, final IStorageChannel channel ) + { + final ICellInventory inv = BasicCellInventory.createInventory( is, container ); + if( inv == null || inv.getChannel() != channel ) + { + return null; + } + return new BasicCellInventoryHandler<>( inv, channel ); + } + +} diff --git a/src/main/java/appeng/core/features/registries/cell/BasicItemCellGuiHandler.java b/src/main/java/appeng/core/features/registries/cell/BasicItemCellGuiHandler.java new file mode 100644 index 000000000..bdb7e595c --- /dev/null +++ b/src/main/java/appeng/core/features/registries/cell/BasicItemCellGuiHandler.java @@ -0,0 +1,35 @@ + +package appeng.core.features.registries.cell; + + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; +import net.minecraft.tileentity.TileEntity; + +import appeng.api.AEApi; +import appeng.api.implementations.tiles.IChestOrDrive; +import appeng.api.storage.ICellGuiHandler; +import appeng.api.storage.ICellHandler; +import appeng.api.storage.IMEInventoryHandler; +import appeng.api.storage.IStorageChannel; +import appeng.api.storage.channels.IItemStorageChannel; +import appeng.api.storage.data.IAEStack; +import appeng.api.util.AEPartLocation; +import appeng.core.sync.GuiBridge; +import appeng.util.Platform; + + +public class BasicItemCellGuiHandler implements ICellGuiHandler +{ + @Override + public > boolean isHandlerFor( final IStorageChannel channel ) + { + return channel == AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ); + } + + @Override + public void openChestGui( final EntityPlayer player, final IChestOrDrive chest, final ICellHandler cellHandler, final IMEInventoryHandler inv, final ItemStack is, final IStorageChannel chan ) + { + Platform.openGUI( player, (TileEntity) chest, AEPartLocation.fromFacing( chest.getUp() ), GuiBridge.GUI_ME ); + } +} diff --git a/src/main/java/appeng/core/features/registries/cell/BasicItemCellHandler.java b/src/main/java/appeng/core/features/registries/cell/BasicItemCellHandler.java deleted file mode 100644 index 0f9ea352d..000000000 --- a/src/main/java/appeng/core/features/registries/cell/BasicItemCellHandler.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * This file is part of Applied Energistics 2. - * Copyright (c) 2013 - 2015, AlgorithmX2, All rights reserved. - * - * Applied Energistics 2 is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Applied Energistics 2 is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with Applied Energistics 2. If not, see . - */ - -package appeng.core.features.registries.cell; - - -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.item.ItemStack; -import net.minecraft.tileentity.TileEntity; - -import appeng.api.AEApi; -import appeng.api.implementations.tiles.IChestOrDrive; -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.ISaveProvider; -import appeng.api.storage.IStorageChannel; -import appeng.api.storage.channels.IItemStorageChannel; -import appeng.api.storage.data.IAEStack; -import appeng.api.util.AEPartLocation; -import appeng.core.sync.GuiBridge; -import appeng.me.storage.ItemCellInventory; -import appeng.me.storage.ItemCellInventoryHandler; -import appeng.util.Platform; - - -public class BasicItemCellHandler implements ICellHandler -{ - - @Override - public boolean isCell( final ItemStack is ) - { - return ItemCellInventory.isCell( is ); - } - - @Override - public > ICellInventoryHandler getCellInventory( final ItemStack is, final ISaveProvider container, final IStorageChannel channel ) - { - if( channel == AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ) ) - { - return (ICellInventoryHandler) ItemCellInventory.getCell( is, container ); - } - - return null; - } - - @Override - public void openChestGui( final EntityPlayer player, final IChestOrDrive chest, final ICellHandler cellHandler, final IMEInventoryHandler inv, final ItemStack is, final IStorageChannel chan ) - { - if( chan == AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ) ) - { - Platform.openGUI( player, (TileEntity) chest, AEPartLocation.fromFacing( chest.getUp() ), GuiBridge.GUI_ME ); - } - } - - @Override - public int getStatusForCell( final ItemStack is, final IMEInventory handler ) - { - if( handler instanceof ItemCellInventoryHandler ) - { - final ItemCellInventoryHandler ci = (ItemCellInventoryHandler) handler; - return ci.getStatusForCell(); - } - return 0; - } - - @Override - public double cellIdleDrain( final ItemStack is, final IMEInventory handler ) - { - final ICellInventory inv = ( (ICellInventoryHandler) handler ).getCellInv(); - return inv.getIdleDrain(); - } -} 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 11c29b552..11619891e 100644 --- a/src/main/java/appeng/core/features/registries/cell/CellRegistry.java +++ b/src/main/java/appeng/core/features/registries/cell/CellRegistry.java @@ -27,6 +27,7 @@ import com.google.common.base.Verify; import net.minecraft.item.ItemStack; +import appeng.api.storage.ICellGuiHandler; import appeng.api.storage.ICellHandler; import appeng.api.storage.ICellInventoryHandler; import appeng.api.storage.ICellRegistry; @@ -39,10 +40,12 @@ public class CellRegistry implements ICellRegistry { private final List handlers; + private final List guiHandlers; public CellRegistry() { this.handlers = new ArrayList<>(); + this.guiHandlers = new ArrayList<>(); } @Override @@ -54,7 +57,7 @@ public class CellRegistry implements ICellRegistry this.handlers.add( handler ); // Verify that the first entry is always our own handler. - Verify.verify( this.handlers.get( 0 ) instanceof BasicItemCellHandler ); + Verify.verify( this.handlers.get( 0 ) instanceof BasicCellHandler ); } @Override @@ -107,4 +110,33 @@ public class CellRegistry implements ICellRegistry } return null; } + + @Override + public void addCellGuiHandler( ICellGuiHandler handler ) + { + this.guiHandlers.add( handler ); + } + + @Override + public > ICellGuiHandler getGuiHandler( final IStorageChannel channel, final ItemStack is ) + { + ICellGuiHandler fallBack = null; + + for( final ICellGuiHandler ch : this.guiHandlers ) + { + if( ch.isHandlerFor( channel ) ) + { + if( ch.isSpecializedFor( is ) ) + { + return ch; + } + + if( fallBack == null ) + { + fallBack = ch; + } + } + } + return fallBack; + } } 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 632734969..4a9f3e81c 100644 --- a/src/main/java/appeng/core/features/registries/cell/CreativeCellHandler.java +++ b/src/main/java/appeng/core/features/registries/cell/CreativeCellHandler.java @@ -19,24 +19,16 @@ package appeng.core.features.registries.cell; -import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; -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; import appeng.api.storage.IStorageChannel; import appeng.api.storage.channels.IItemStorageChannel; -import appeng.api.util.AEPartLocation; -import appeng.core.sync.GuiBridge; import appeng.items.storage.ItemCreativeStorageCell; import appeng.me.storage.CreativeCellInventory; -import appeng.util.Platform; public final class CreativeCellHandler implements ICellHandler @@ -60,19 +52,13 @@ public final class CreativeCellHandler implements ICellHandler } @Override - public void openChestGui( final EntityPlayer player, final IChestOrDrive chest, final ICellHandler cellHandler, final IMEInventoryHandler inv, final ItemStack is, final IStorageChannel chan ) - { - Platform.openGUI( player, (TileEntity) chest, AEPartLocation.fromFacing( chest.getUp() ), GuiBridge.GUI_ME ); - } - - @Override - public int getStatusForCell( final ItemStack is, final IMEInventory handler ) + public int getStatusForCell( final ItemStack is, final ICellInventoryHandler handler ) { return 2; } @Override - public double cellIdleDrain( final ItemStack is, final IMEInventory handler ) + public double cellIdleDrain( final ItemStack is, final ICellInventoryHandler handler ) { return 0; } diff --git a/src/main/java/appeng/fluids/items/BasicFluidStorageCell.java b/src/main/java/appeng/fluids/items/BasicFluidStorageCell.java index 034edf5bd..2b7444284 100644 --- a/src/main/java/appeng/fluids/items/BasicFluidStorageCell.java +++ b/src/main/java/appeng/fluids/items/BasicFluidStorageCell.java @@ -105,7 +105,8 @@ public final class BasicFluidStorageCell extends AbstractStorageCell { + AEApi.instance().definitions().materials().emptyStorageCell().maybeStack( 1 ).ifPresent( is -> + { final ItemStack extraA = ia.addItems( is ); if( !extraA.isEmpty() ) { diff --git a/src/main/java/appeng/fluids/registries/BasicFluidCellHandler.java b/src/main/java/appeng/fluids/registries/BasicFluidCellGuiHandler.java similarity index 51% rename from src/main/java/appeng/fluids/registries/BasicFluidCellHandler.java rename to src/main/java/appeng/fluids/registries/BasicFluidCellGuiHandler.java index 7b460b460..d0148bf38 100644 --- a/src/main/java/appeng/fluids/registries/BasicFluidCellHandler.java +++ b/src/main/java/appeng/fluids/registries/BasicFluidCellGuiHandler.java @@ -25,66 +25,29 @@ import net.minecraft.tileentity.TileEntity; import appeng.api.AEApi; import appeng.api.implementations.tiles.IChestOrDrive; +import appeng.api.storage.ICellGuiHandler; 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.ISaveProvider; import appeng.api.storage.IStorageChannel; import appeng.api.storage.channels.IFluidStorageChannel; import appeng.api.storage.data.IAEStack; import appeng.api.util.AEPartLocation; import appeng.core.sync.GuiBridge; -import appeng.fluids.storage.FluidCellInventory; -import appeng.fluids.storage.FluidCellInventoryHandler; import appeng.util.Platform; -public class BasicFluidCellHandler implements ICellHandler +public class BasicFluidCellGuiHandler implements ICellGuiHandler { @Override - public boolean isCell( final ItemStack is ) + public > boolean isHandlerFor( final IStorageChannel channel ) { - return FluidCellInventory.isCell( is ); - } - - @Override - public > ICellInventoryHandler getCellInventory( final ItemStack is, final ISaveProvider container, final IStorageChannel channel ) - { - if( channel == AEApi.instance().storage().getStorageChannel( IFluidStorageChannel.class ) ) - { - return (ICellInventoryHandler) FluidCellInventory.getCell( is, container ); - } - - return null; + return channel == AEApi.instance().storage().getStorageChannel( IFluidStorageChannel.class ); } @Override public void openChestGui( final EntityPlayer player, final IChestOrDrive chest, final ICellHandler cellHandler, final IMEInventoryHandler inv, final ItemStack is, final IStorageChannel chan ) { - if( chan == AEApi.instance().storage().getStorageChannel( IFluidStorageChannel.class ) ) - { - Platform.openGUI( player, (TileEntity) chest, AEPartLocation.fromFacing( chest.getUp() ), GuiBridge.GUI_FLUID_TERMINAL ); - } - } - - @Override - public int getStatusForCell( final ItemStack is, final IMEInventory handler ) - { - if( handler instanceof FluidCellInventoryHandler ) - { - final FluidCellInventoryHandler ci = (FluidCellInventoryHandler) handler; - return ci.getStatusForCell(); - } - return 0; - } - - @Override - public double cellIdleDrain( final ItemStack is, final IMEInventory handler ) - { - final ICellInventory inv = ( (ICellInventoryHandler) handler ).getCellInv(); - return inv.getIdleDrain(); + Platform.openGUI( player, (TileEntity) chest, AEPartLocation.fromFacing( chest.getUp() ), GuiBridge.GUI_FLUID_TERMINAL ); } } diff --git a/src/main/java/appeng/fluids/storage/FluidCellInventory.java b/src/main/java/appeng/fluids/storage/FluidCellInventory.java deleted file mode 100644 index c46279123..000000000 --- a/src/main/java/appeng/fluids/storage/FluidCellInventory.java +++ /dev/null @@ -1,258 +0,0 @@ -/* - * This file is part of Applied Energistics 2. - * Copyright (c) 2013 - 2018, AlgorithmX2, All rights reserved. - * - * Applied Energistics 2 is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Applied Energistics 2 is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with Applied Energistics 2. If not, see . - */ - -package appeng.fluids.storage; - - -import net.minecraft.item.Item; -import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NBTTagCompound; -import net.minecraftforge.fluids.FluidStack; - -import appeng.api.AEApi; -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.ISaveProvider; -import appeng.api.storage.IStorageChannel; -import appeng.api.storage.channels.IFluidStorageChannel; -import appeng.api.storage.data.IAEFluidStack; -import appeng.core.AEConfig; -import appeng.core.AELog; -import appeng.fluids.util.AEFluidStack; -import appeng.me.storage.AbstractCellInventory; - - -/** - * @author DrummerMC - * @version rv6 - 2018-01-16 - * @since rv6 2018-01-16 - */ -public class FluidCellInventory extends AbstractCellInventory -{ - private FluidCellInventory( final ItemStack o, final ISaveProvider container ) throws AppEngException - { - super( o, container, 8000 ); - } - - public static ICellInventoryHandler getCell( final ItemStack o, final ISaveProvider container2 ) - { - try - { - return new FluidCellInventoryHandler( new FluidCellInventory( o, container2 ) ); - } - catch( final AppEngException e ) - { - return null; - } - } - - public static boolean isCell( final ItemStack i ) - { - if( i == null ) - { - return false; - } - - final Item type = i.getItem(); - if( type instanceof IStorageCell ) - { - if( ( (IStorageCell) type ).getChannel() == AEApi.instance().storage().getStorageChannel( IFluidStorageChannel.class ) ) - { - return ( (IStorageCell) type ).isStorageCell( i ); - } - } - - return false; - } - - @Override - public IAEFluidStack injectItems( final IAEFluidStack input, final Actionable mode, final IActionSource src ) - { - if( input == null ) - { - return null; - } - if( input.getStackSize() == 0 ) - { - return null; - } - - if( this.cellType.isBlackListed( this.i, input ) ) - { - return input; - } - - final FluidStack sharedFluidStack = input.getFluidStack(); - - final IAEFluidStack l = this.getCellItems().findPrecise( input ); - if( l != null ) - { - final long remainingItemSlots = this.getRemainingItemCount(); - if( remainingItemSlots <= 0 ) - { - return input; - } - - if( input.getStackSize() > remainingItemSlots ) - { - final IAEFluidStack r = input.copy(); - r.setStackSize( r.getStackSize() - remainingItemSlots ); - if( mode == Actionable.MODULATE ) - { - l.setStackSize( l.getStackSize() + remainingItemSlots ); - this.saveChanges(); - } - return r; - } - else - { - if( mode == Actionable.MODULATE ) - { - l.setStackSize( l.getStackSize() + input.getStackSize() ); - this.saveChanges(); - } - return null; - } - } - - if( this.canHoldNewItem() ) // room for new type, and for at least one item! - { - final int remainingItemCount = (int) this.getRemainingItemCount() - this.getBytesPerType() * itemsPerByte; - if( remainingItemCount > 0 ) - { - if( input.getStackSize() > remainingItemCount ) - { - final FluidStack toReturn = sharedFluidStack.copy(); - toReturn.amount = sharedFluidStack.amount - remainingItemCount; - if( mode == Actionable.MODULATE ) - { - final FluidStack toWrite = sharedFluidStack.copy(); - toWrite.amount = remainingItemCount; - - this.cellItems.add( AEFluidStack.fromFluidStack( toWrite ) ); - this.saveChanges(); - } - return AEFluidStack.fromFluidStack( toReturn ); - } - - if( mode == Actionable.MODULATE ) - { - this.cellItems.add( input ); - this.saveChanges(); - } - - return null; - } - } - - return input; - } - - @Override - public IAEFluidStack extractItems( final IAEFluidStack request, final Actionable mode, final IActionSource src ) - { - if( request == null ) - { - return null; - } - - final long size = Math.min( Integer.MAX_VALUE, request.getStackSize() ); - - IAEFluidStack results = null; - - final IAEFluidStack l = this.getCellItems().findPrecise( request ); - if( l != null ) - { - results = l.copy(); - - if( l.getStackSize() <= size ) - { - results.setStackSize( l.getStackSize() ); - if( mode == Actionable.MODULATE ) - { - l.setStackSize( 0 ); - this.saveChanges(); - } - } - else - { - results.setStackSize( size ); - if( mode == Actionable.MODULATE ) - { - l.setStackSize( l.getStackSize() - size ); - this.saveChanges(); - } - } - } - - return results; - } - - @Override - public IStorageChannel getChannel() - { - return AEApi.instance().storage().getStorageChannel( IFluidStorageChannel.class ); - } - - protected void loadCellItem( NBTTagCompound compoundTag, int stackSize ) - { - - // Now load the fluid stack - final FluidStack t; - try - { - t = FluidStack.loadFluidStackFromNBT( compoundTag ); - if( t == null ) - { - AELog.warn( "Removing item " + compoundTag + " from storage cell because the associated item type couldn't be found." ); - return; - } - } - catch( Throwable ex ) - { - if( AEConfig.instance().isRemoveCrashingItemsOnLoad() ) - { - AELog.warn( ex, "Removing item " + compoundTag + " from storage cell because loading the ItemStack crashed." ); - return; - } - throw ex; - } - - t.amount = stackSize; - - if( t.amount > 0 ) - { - try - { - this.cellItems.add( AEApi.instance().storage().getStorageChannel( IFluidStorageChannel.class ).createStack( t ) ); - } - catch( Throwable ex ) - { - if( AEConfig.instance().isRemoveCrashingItemsOnLoad() ) - { - AELog.warn( ex, "Removing item " + t + " from storage cell because processing the loaded item crashed." ); - return; - } - throw ex; - } - } - } -} diff --git a/src/main/java/appeng/fluids/storage/FluidCellInventoryHandler.java b/src/main/java/appeng/fluids/storage/FluidCellInventoryHandler.java deleted file mode 100644 index 49d6092a3..000000000 --- a/src/main/java/appeng/fluids/storage/FluidCellInventoryHandler.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * This file is part of Applied Energistics 2. - * Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved. - * - * Applied Energistics 2 is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Applied Energistics 2 is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with Applied Energistics 2. If not, see . - */ - -package appeng.fluids.storage; - - -import net.minecraft.item.ItemStack; -import net.minecraftforge.fluids.FluidStack; - -import appeng.api.AEApi; -import appeng.api.storage.IMEInventory; -import appeng.api.storage.channels.IFluidStorageChannel; -import appeng.api.storage.data.IAEFluidStack; -import appeng.fluids.items.FluidDummyItem; -import appeng.fluids.util.AEFluidStack; -import appeng.me.storage.AbstractCellInventoryHandler; - - -public class FluidCellInventoryHandler extends AbstractCellInventoryHandler -{ - - public FluidCellInventoryHandler( IMEInventory c ) - { - super( c, AEApi.instance().storage().getStorageChannel( IFluidStorageChannel.class ) ); - } - - @Override - protected IAEFluidStack createConfigStackFromItem( ItemStack is ) - { - if( is.getItem() instanceof FluidDummyItem ) - { - FluidStack fs = ( (FluidDummyItem) is.getItem() ).getFluidStack( is ); - return fs != null ? AEFluidStack.fromFluidStack( fs ) : null; - } - return null; - } -} diff --git a/src/main/java/appeng/items/contents/PortableCellViewer.java b/src/main/java/appeng/items/contents/PortableCellViewer.java index 07d21a309..e85f9589f 100644 --- a/src/main/java/appeng/items/contents/PortableCellViewer.java +++ b/src/main/java/appeng/items/contents/PortableCellViewer.java @@ -39,7 +39,6 @@ import appeng.api.storage.data.IAEStack; import appeng.api.util.IConfigManager; import appeng.container.interfaces.IInventorySlotAware; import appeng.me.helpers.MEMonitorHandler; -import appeng.me.storage.ItemCellInventory; import appeng.util.ConfigManager; import appeng.util.IConfigManagerHost; import appeng.util.Platform; @@ -54,7 +53,7 @@ public class PortableCellViewer extends MEMonitorHandler implement public PortableCellViewer( final ItemStack is, final int slot ) { - super( ItemCellInventory.getCell( is, null ) ); + super( AEApi.instance().registries().cell().getCellInventory( is, null, AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ) ) ); this.ips = (IAEItemPowerStorage) is.getItem(); this.target = is; this.inventorySlot = slot; diff --git a/src/main/java/appeng/items/storage/AbstractStorageCell.java b/src/main/java/appeng/items/storage/AbstractStorageCell.java index ba5144792..de33a57fd 100644 --- a/src/main/java/appeng/items/storage/AbstractStorageCell.java +++ b/src/main/java/appeng/items/storage/AbstractStorageCell.java @@ -38,13 +38,10 @@ import net.minecraftforge.items.IItemHandler; import appeng.api.AEApi; import appeng.api.config.FuzzyMode; -import appeng.api.config.IncludeExclude; import appeng.api.exceptions.MissingDefinitionException; import appeng.api.implementations.items.IItemGroup; import appeng.api.implementations.items.IStorageCell; import appeng.api.implementations.items.IUpgradeModule; -import appeng.api.storage.ICellInventory; -import appeng.api.storage.ICellInventoryHandler; import appeng.api.storage.IMEInventoryHandler; import appeng.api.storage.data.IAEItemStack; import appeng.api.storage.data.IAEStack; @@ -81,34 +78,9 @@ public abstract class AbstractStorageCell> extends AEBaseI @Override public void addCheckedInformation( final ItemStack stack, final World world, final List lines, final ITooltipFlag advancedTooltips ) { - final IMEInventoryHandler inventory = AEApi.instance().registries().cell().getCellInventory( stack, null, getChannel() ); - - if( inventory instanceof ICellInventoryHandler ) - { - final ICellInventoryHandler handler = (ICellInventoryHandler) inventory; - final ICellInventory cellInventory = handler.getCellInv(); - - if( cellInventory != null ) - { - lines.add( cellInventory.getUsedBytes() + " " + GuiText.Of.getLocal() + ' ' + cellInventory.getTotalBytes() + ' ' + GuiText.BytesUsed.getLocal() ); - - lines.add( cellInventory.getStoredItemTypes() + " " + GuiText.Of.getLocal() + ' ' + cellInventory.getTotalItemTypes() + ' ' + GuiText.Types.getLocal() ); - - if( handler.isPreformatted() ) - { - final String list = ( handler.getIncludeExcludeMode() == IncludeExclude.WHITELIST ? GuiText.Included : GuiText.Excluded ).getLocal(); - - if( handler.isFuzzy() ) - { - lines.add( GuiText.Partitioned.getLocal() + " - " + list + ' ' + GuiText.Fuzzy.getLocal() ); - } - else - { - lines.add( GuiText.Partitioned.getLocal() + " - " + list + ' ' + GuiText.Precise.getLocal() ); - } - } - } - } + AEApi.instance() + .client() + .addCellInformation( AEApi.instance().registries().cell().getCellInventory( stack, null, this.getChannel() ), lines ); } @Override @@ -256,7 +228,12 @@ public abstract class AbstractStorageCell> extends AEBaseI @Override public ItemStack getContainerItem( final ItemStack itemStack ) { - return AEApi.instance().definitions().materials().emptyStorageCell().maybeStack( 1 ).orElseThrow( () -> new MissingDefinitionException( "Tried to use empty storage cells while basic storage cells are defined." ) ); + return AEApi.instance() + .definitions() + .materials() + .emptyStorageCell() + .maybeStack( 1 ) + .orElseThrow( () -> new MissingDefinitionException( "Tried to use empty storage cells while basic storage cells are defined." ) ); } @Override diff --git a/src/main/java/appeng/items/storage/BasicItemStorageCell.java b/src/main/java/appeng/items/storage/BasicItemStorageCell.java index cdf5492b4..5c6de9811 100644 --- a/src/main/java/appeng/items/storage/BasicItemStorageCell.java +++ b/src/main/java/appeng/items/storage/BasicItemStorageCell.java @@ -33,12 +33,12 @@ import appeng.util.InventoryAdaptor; public final class BasicItemStorageCell extends AbstractStorageCell { - protected final int perType; protected final double idleDrain; + public BasicItemStorageCell( final MaterialType whichCell, final int kilobytes ) { - super(whichCell, kilobytes); + super( whichCell, kilobytes ); switch( whichCell ) { case CELL1K_PART: @@ -85,7 +85,8 @@ public final class BasicItemStorageCell extends AbstractStorageCell { + AEApi.instance().definitions().materials().emptyStorageCell().maybeStack( 1 ).ifPresent( is -> + { final ItemStack extraA = ia.addItems( is ); if( !extraA.isEmpty() ) { diff --git a/src/main/java/appeng/items/tools/powered/ToolColorApplicator.java b/src/main/java/appeng/items/tools/powered/ToolColorApplicator.java index 49a6e0f70..d85120770 100644 --- a/src/main/java/appeng/items/tools/powered/ToolColorApplicator.java +++ b/src/main/java/appeng/items/tools/powered/ToolColorApplicator.java @@ -58,7 +58,6 @@ import appeng.api.config.FuzzyMode; import appeng.api.implementations.items.IItemGroup; import appeng.api.implementations.items.IStorageCell; import appeng.api.implementations.tiles.IColorableTile; -import appeng.api.storage.ICellInventory; import appeng.api.storage.ICellInventoryHandler; import appeng.api.storage.IMEInventory; import appeng.api.storage.IStorageChannel; @@ -78,7 +77,6 @@ import appeng.items.contents.CellUpgrades; import appeng.items.misc.ItemPaintBall; import appeng.items.tools.powered.powersink.AEBasePoweredItem; import appeng.me.helpers.BaseActionSource; -import appeng.me.storage.ItemCellInventoryHandler; import appeng.tile.misc.TilePaint; import appeng.util.Platform; import appeng.util.item.AEItemStack; @@ -119,8 +117,11 @@ public class ToolColorApplicator extends AEBasePoweredItem implements IStorageCe ItemStack paintBall = this.getColor( is ); - final IMEInventory inv = AEApi.instance().registries().cell().getCellInventory( is, null, - AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ) ); + final IMEInventory inv = AEApi.instance() + .registries() + .cell() + .getCellInventory( is, null, + AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ) ); if( inv != null ) { final IAEItemStack option = inv.extractItems( AEItemStack.fromItemStack( paintBall ), Actionable.SIMULATE, new BaseActionSource() ); @@ -266,8 +267,11 @@ public class ToolColorApplicator extends AEBasePoweredItem implements IStorageCe { ItemStack newColor = ItemStack.EMPTY; - final IMEInventory inv = AEApi.instance().registries().cell().getCellInventory( is, null, - AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ) ); + final IMEInventory inv = AEApi.instance() + .registries() + .cell() + .getCellInventory( is, null, + AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ) ); if( inv != null ) { final IItemList itemList = inv @@ -432,18 +436,13 @@ public class ToolColorApplicator extends AEBasePoweredItem implements IStorageCe { 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 ) - { - final ICellInventory cd = ( (ICellInventoryHandler) cdi ).getCellInv(); - if( cd != null ) - { - lines.add( cd.getUsedBytes() + " " + GuiText.Of.getLocal() + ' ' + cd.getTotalBytes() + ' ' + GuiText.BytesUsed.getLocal() ); - lines.add( cd.getStoredItemTypes() + " " + GuiText.Of.getLocal() + ' ' + cd.getTotalItemTypes() + ' ' + GuiText.Types.getLocal() ); - } - } + AEApi.instance().client().addCellInformation( cdi, lines ); } @Override diff --git a/src/main/java/appeng/items/tools/powered/ToolMatterCannon.java b/src/main/java/appeng/items/tools/powered/ToolMatterCannon.java index a6bf45689..3d60529a3 100644 --- a/src/main/java/appeng/items/tools/powered/ToolMatterCannon.java +++ b/src/main/java/appeng/items/tools/powered/ToolMatterCannon.java @@ -52,7 +52,6 @@ import appeng.api.config.Actionable; import appeng.api.config.FuzzyMode; 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.IStorageChannel; import appeng.api.storage.channels.IItemStorageChannel; @@ -64,7 +63,6 @@ import appeng.core.AEConfig; import appeng.core.AELog; import appeng.core.AppEng; import appeng.core.features.AEFeature; -import appeng.core.localization.GuiText; import appeng.core.localization.PlayerMessages; import appeng.core.sync.network.NetworkHandler; import appeng.core.sync.packets.PacketMatterCannon; @@ -100,15 +98,7 @@ public class ToolMatterCannon extends AEBasePoweredItem implements IStorageCell< .getCellInventory( stack, null, AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ) ); - if( cdi != null ) - { - final ICellInventory cd = cdi.getCellInv(); - if( cd != null ) - { - lines.add( cd.getUsedBytes() + " " + GuiText.Of.getLocal() + ' ' + cd.getTotalBytes() + ' ' + GuiText.BytesUsed.getLocal() ); - lines.add( cd.getStoredItemTypes() + " " + GuiText.Of.getLocal() + ' ' + cd.getTotalItemTypes() + ' ' + GuiText.Types.getLocal() ); - } - } + AEApi.instance().client().addCellInformation( cdi, lines ); } @Override diff --git a/src/main/java/appeng/items/tools/powered/ToolPortableCell.java b/src/main/java/appeng/items/tools/powered/ToolPortableCell.java index 261e8cf96..1adbe42ef 100644 --- a/src/main/java/appeng/items/tools/powered/ToolPortableCell.java +++ b/src/main/java/appeng/items/tools/powered/ToolPortableCell.java @@ -40,9 +40,7 @@ import appeng.api.implementations.guiobjects.IGuiItem; import appeng.api.implementations.guiobjects.IGuiItemObject; import appeng.api.implementations.items.IItemGroup; 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; @@ -54,7 +52,6 @@ import appeng.items.contents.CellConfig; import appeng.items.contents.CellUpgrades; import appeng.items.contents.PortableCellViewer; import appeng.items.tools.powered.powersink.AEBasePoweredItem; -import appeng.me.storage.ItemCellInventoryHandler; import appeng.util.Platform; @@ -85,18 +82,13 @@ public class ToolPortableCell 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 ) - { - final ICellInventory cd = ( (ICellInventoryHandler) cdi ).getCellInv(); - if( cd != null ) - { - lines.add( cd.getUsedBytes() + " " + GuiText.Of.getLocal() + ' ' + cd.getTotalBytes() + ' ' + GuiText.BytesUsed.getLocal() ); - lines.add( cd.getStoredItemTypes() + " " + GuiText.Of.getLocal() + ' ' + cd.getTotalItemTypes() + ' ' + GuiText.Types.getLocal() ); - } - } + AEApi.instance().client().addCellInformation( cdi, lines ); } @Override diff --git a/src/main/java/appeng/me/storage/AbstractCellInventory.java b/src/main/java/appeng/me/storage/AbstractCellInventory.java index a991f5177..5f69d850d 100644 --- a/src/main/java/appeng/me/storage/AbstractCellInventory.java +++ b/src/main/java/appeng/me/storage/AbstractCellInventory.java @@ -19,16 +19,13 @@ package appeng.me.storage; -import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraftforge.items.IItemHandler; import appeng.api.config.FuzzyMode; -import appeng.api.exceptions.AppEngException; import appeng.api.implementations.items.IStorageCell; import appeng.api.storage.ICellInventory; -import appeng.api.storage.IMEInventory; import appeng.api.storage.ISaveProvider; import appeng.api.storage.data.IAEStack; import appeng.api.storage.data.IItemList; @@ -59,7 +56,7 @@ public abstract class AbstractCellInventory> implements IC private short storedItems = 0; private int storedItemCount = 0; protected IItemList cellItems; - protected final ItemStack i; + private final ItemStack i; protected final IStorageCell cellType; protected final int itemsPerByte; private boolean isPersisted = true; @@ -73,32 +70,12 @@ public abstract class AbstractCellInventory> implements IC } } - protected AbstractCellInventory( final ItemStack o, final ISaveProvider container, final int itemsPerByte ) throws AppEngException + protected AbstractCellInventory( final IStorageCell cellType, final ItemStack o, final ISaveProvider container ) { - this.itemsPerByte = itemsPerByte; - - if( o == null ) - { - throw new AppEngException( "ItemStack was used as a cell, but was not a cell!" ); - } - this.i = o; - - final Item type = this.i.getItem(); - if( type instanceof IStorageCell ) - { - this.cellType = (IStorageCell) this.i.getItem(); - this.maxItemTypes = this.cellType.getTotalTypes( this.i ); - } - else - { - throw new AppEngException( "ItemStack was used as a cell, but was not a cell!" ); - } - - if( !this.cellType.isStorageCell( this.i ) ) - { - throw new AppEngException( "ItemStack was used as a cell, but was not a cell!" ); - } + this.cellType = cellType; + this.itemsPerByte = this.cellType.getChannel().getUnitsPerByte(); + this.maxItemTypes = this.cellType.getTotalTypes( this.i ); if( this.maxItemTypes > MAX_ITEM_TYPES ) { @@ -116,11 +93,6 @@ public abstract class AbstractCellInventory> implements IC this.cellItems = null; } - protected boolean isEmpty( final IMEInventory meInventory ) - { - return meInventory.getAvailableItems( getChannel().createList() ).isEmpty(); - } - protected IItemList getCellItems() { if( this.cellItems == null ) diff --git a/src/main/java/appeng/me/storage/ItemCellInventory.java b/src/main/java/appeng/me/storage/BasicCellInventory.java similarity index 54% rename from src/main/java/appeng/me/storage/ItemCellInventory.java rename to src/main/java/appeng/me/storage/BasicCellInventory.java index 3ad8d7cbb..25c500d63 100644 --- a/src/main/java/appeng/me/storage/ItemCellInventory.java +++ b/src/main/java/appeng/me/storage/BasicCellInventory.java @@ -1,20 +1,3 @@ -/* - * This file is part of Applied Energistics 2. - * Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved. - * - * Applied Energistics 2 is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Applied Energistics 2 is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with Applied Energistics 2. If not, see . - */ package appeng.me.storage; @@ -23,50 +6,90 @@ import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; -import appeng.api.AEApi; 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.ICellInventory; import appeng.api.storage.ISaveProvider; 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.core.AEConfig; import appeng.core.AELog; +import appeng.util.item.AEStack; -public class ItemCellInventory extends AbstractCellInventory +public class BasicCellInventory> extends AbstractCellInventory { - private ItemCellInventory( final ItemStack o, final ISaveProvider container ) throws AppEngException + private final IStorageChannel channel; + + private BasicCellInventory( final IStorageCell cellType, final ItemStack o, final ISaveProvider container ) { - super( o, container, 8 ); + super( cellType, o, container ); + this.channel = cellType.getChannel(); } - public static ICellInventoryHandler getCell( final ItemStack o, final ISaveProvider container2 ) + public static > ICellInventory createInventory( final ItemStack o, final ISaveProvider container ) { try { - return new ItemCellInventoryHandler( new ItemCellInventory( o, container2 ) ); + if( o == null ) + { + throw new AppEngException( "ItemStack was used as a cell, but was not a cell!" ); + } + + final Item type = o.getItem(); + final IStorageCell cellType; + if( type instanceof IStorageCell ) + { + cellType = (IStorageCell) type; + } + else + { + throw new AppEngException( "ItemStack was used as a cell, but was not a cell!" ); + } + + if( !cellType.isStorageCell( o ) ) + { + throw new AppEngException( "ItemStack was used as a cell, but was not a cell!" ); + } + + return new BasicCellInventory( cellType, o, container ); } catch( final AppEngException e ) { + AELog.error( e ); return null; } } - private static boolean isStorageCell( final ItemStack i ) + public static > boolean isCellOfType( final ItemStack input, IStorageChannel channel ) { - if( i == null ) + if( input == null ) + { + return false; + } + + final Item type = input.getItem(); + if( type instanceof IStorageCell ) + { + return ( (IStorageCell) type ).getChannel() == channel; + } + + return false; + } + + public static boolean isCell( final ItemStack input ) + { + if( input == null ) { return false; } try { - final Item type = i.getItem(); + final Item type = input.getItem(); if( type instanceof IStorageCell ) { return !( (IStorageCell) type ).storableInStorageCell(); @@ -80,27 +103,27 @@ public class ItemCellInventory extends AbstractCellInventory return false; } - public static boolean isCell( final ItemStack i ) + private boolean isStorageCell( final T input ) { - if( i == null ) + if( !input.isItem() ) { return false; } + return isCell( ( (IAEItemStack) input ).getDefinition() ); + } - final Item type = i.getItem(); - if( type instanceof IStorageCell ) + @SuppressWarnings( { "rawtypes", "unchecked" } ) + private static boolean isCellEmpty( ICellInventory inv ) + { + if( inv != null ) { - if( ( (IStorageCell) type ).getChannel() == AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ) ) - { - return ( (IStorageCell) type ).isStorageCell( i ); - } + return inv.getAvailableItems( inv.getChannel().createList() ).isEmpty(); } - - return false; + return true; } @Override - public IAEItemStack injectItems( final IAEItemStack input, final Actionable mode, final IActionSource src ) + public T injectItems( T input, Actionable mode, IActionSource src ) { if( input == null ) { @@ -111,23 +134,23 @@ public class ItemCellInventory extends AbstractCellInventory return null; } - if( this.cellType.isBlackListed( this.i, input ) ) + if( this.cellType.isBlackListed( this.getItemStack(), input ) ) { return input; } // This is slightly hacky as it expects a read-only access, but fine for now. // TODO: Guarantee a read-only access. E.g. provide an isEmpty() method and ensure CellInventory does not write // any NBT data for empty cells instead of relying on an empty IItemContainer - if( ItemCellInventory.isStorageCell( input.getDefinition() ) ) + if( this.isStorageCell( input ) ) { - final IMEInventory meInventory = getCell( input.createItemStack(), null ); - if( meInventory != null && !this.isEmpty( meInventory ) ) + final ICellInventory meInventory = createInventory( ( (IAEItemStack) input ).createItemStack(), null ); + if( !isCellEmpty( meInventory ) ) { return input; } } - final IAEItemStack l = this.getCellItems().findPrecise( input ); + final T l = this.getCellItems().findPrecise( input ); if( l != null ) { final long remainingItemCount = this.getRemainingItemCount(); @@ -138,7 +161,7 @@ public class ItemCellInventory extends AbstractCellInventory if( input.getStackSize() > remainingItemCount ) { - final IAEItemStack r = input.copy(); + final T r = input.copy(); r.setStackSize( r.getStackSize() - remainingItemCount ); if( mode == Actionable.MODULATE ) { @@ -165,11 +188,11 @@ public class ItemCellInventory extends AbstractCellInventory { if( input.getStackSize() > remainingItemCount ) { - final IAEItemStack toReturn = input.copy(); + final T toReturn = input.copy(); toReturn.setStackSize( input.getStackSize() - remainingItemCount ); if( mode == Actionable.MODULATE ) { - final IAEItemStack toWrite = input.copy(); + final T toWrite = input.copy(); toWrite.setStackSize( remainingItemCount ); this.cellItems.add( toWrite ); @@ -192,7 +215,7 @@ public class ItemCellInventory extends AbstractCellInventory } @Override - public IAEItemStack extractItems( final IAEItemStack request, final Actionable mode, final IActionSource src ) + public T extractItems( T request, Actionable mode, IActionSource src ) { if( request == null ) { @@ -201,9 +224,9 @@ public class ItemCellInventory extends AbstractCellInventory final long size = Math.min( Integer.MAX_VALUE, request.getStackSize() ); - IAEItemStack Results = null; + T Results = null; - final IAEItemStack l = this.getCellItems().findPrecise( request ); + final T l = this.getCellItems().findPrecise( request ); if( l != null ) { Results = l.copy(); @@ -232,20 +255,20 @@ public class ItemCellInventory extends AbstractCellInventory } @Override - public IStorageChannel getChannel() + public IStorageChannel getChannel() { - return AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ); + return this.channel; } + @Override protected void loadCellItem( NBTTagCompound compoundTag, int stackSize ) { - // Now load the item stack - final ItemStack t; + final T t; try { - t = new ItemStack( compoundTag ); - if( t.isEmpty() ) + t = getChannel().createFromNBT( compoundTag ); + if( t == null ) { AELog.warn( "Removing item " + compoundTag + " from storage cell because the associated item type couldn't be found." ); return; @@ -261,23 +284,11 @@ public class ItemCellInventory extends AbstractCellInventory throw ex; } - t.setCount( stackSize ); + t.setStackSize( stackSize ); - if( t.getCount() > 0 ) + if( stackSize > 0 ) { - try - { - this.cellItems.add( AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ).createStack( t ) ); - } - catch( Throwable ex ) - { - if( AEConfig.instance().isRemoveCrashingItemsOnLoad() ) - { - AELog.warn( ex, "Removing item " + t + " from storage cell because processing the loaded item crashed." ); - return; - } - throw ex; - } + this.cellItems.add( t ); } } } diff --git a/src/main/java/appeng/me/storage/AbstractCellInventoryHandler.java b/src/main/java/appeng/me/storage/BasicCellInventoryHandler.java similarity index 87% rename from src/main/java/appeng/me/storage/AbstractCellInventoryHandler.java rename to src/main/java/appeng/me/storage/BasicCellInventoryHandler.java index c4baf5407..5a0bce287 100644 --- a/src/main/java/appeng/me/storage/AbstractCellInventoryHandler.java +++ b/src/main/java/appeng/me/storage/BasicCellInventoryHandler.java @@ -43,9 +43,9 @@ import appeng.util.prioritylist.PrecisePriorityList; * @version rv6 - 2018-01-23 * @since rv6 2018-01-23 */ -public abstract class AbstractCellInventoryHandler> extends MEInventoryHandler implements ICellInventoryHandler +public class BasicCellInventoryHandler> extends MEInventoryHandler implements ICellInventoryHandler { - public AbstractCellInventoryHandler( final IMEInventory c, final IStorageChannel channel ) + public BasicCellInventoryHandler( final IMEInventory c, final IStorageChannel channel ) { super( c, channel ); @@ -88,7 +88,11 @@ public abstract class AbstractCellInventoryHandler> extend final ItemStack is = config.getStackInSlot( x ); if( !is.isEmpty() ) { - priorityList.add( createConfigStackFromItem( is ) ); + final T configItem = channel.createStack( is ); + if( configItem != null ) + { + priorityList.add( configItem ); + } } } @@ -108,8 +112,6 @@ public abstract class AbstractCellInventoryHandler> extend } } - protected abstract T createConfigStackFromItem( ItemStack is ); - @Override public ICellInventory getCellInv() { @@ -145,16 +147,4 @@ public abstract class AbstractCellInventoryHandler> extend { return Platform.openNbtData( this.getCellInv().getItemStack() ); } - - public int getStatusForCell() - { - int val = this.getCellInv().getStatusForCell(); - - if( val == 1 && this.isPreformatted() ) - { - val = 2; - } - - return val; - } } diff --git a/src/main/java/appeng/me/storage/CreativeCellInventory.java b/src/main/java/appeng/me/storage/CreativeCellInventory.java index faf616001..53031553c 100644 --- a/src/main/java/appeng/me/storage/CreativeCellInventory.java +++ b/src/main/java/appeng/me/storage/CreativeCellInventory.java @@ -56,7 +56,7 @@ public class CreativeCellInventory implements IMEInventoryHandler public static ICellInventoryHandler getCell( final ItemStack o ) { - return new ItemCellInventoryHandler( new CreativeCellInventory( o ) ); + return new BasicCellInventoryHandler( new CreativeCellInventory( o ), AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ) ); } @Override diff --git a/src/main/java/appeng/me/storage/DriveWatcher.java b/src/main/java/appeng/me/storage/DriveWatcher.java index ba2c0515b..073b9fb19 100644 --- a/src/main/java/appeng/me/storage/DriveWatcher.java +++ b/src/main/java/appeng/me/storage/DriveWatcher.java @@ -25,7 +25,7 @@ import appeng.api.config.Actionable; import appeng.api.implementations.tiles.IChestOrDrive; import appeng.api.networking.security.IActionSource; import appeng.api.storage.ICellHandler; -import appeng.api.storage.IMEInventory; +import appeng.api.storage.ICellInventoryHandler; import appeng.api.storage.data.IAEStack; @@ -37,7 +37,7 @@ public class DriveWatcher> extends MEInventoryHandler private final ICellHandler handler; private final IChestOrDrive cord; - public DriveWatcher( final IMEInventory i, final ItemStack is, final ICellHandler han, final IChestOrDrive cod ) + public DriveWatcher( final ICellInventoryHandler i, final ItemStack is, final ICellHandler han, final IChestOrDrive cod ) { super( i, i.getChannel() ); this.is = is; @@ -47,7 +47,7 @@ public class DriveWatcher> extends MEInventoryHandler public int getStatus() { - return this.handler.getStatusForCell( this.is, this.getInternal() ); + return this.handler.getStatusForCell( this.is, (ICellInventoryHandler) this.getInternal() ); } @Override diff --git a/src/main/java/appeng/me/storage/ItemCellInventoryHandler.java b/src/main/java/appeng/me/storage/ItemCellInventoryHandler.java deleted file mode 100644 index c1de58ade..000000000 --- a/src/main/java/appeng/me/storage/ItemCellInventoryHandler.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * This file is part of Applied Energistics 2. - * Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved. - * - * Applied Energistics 2 is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Applied Energistics 2 is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with Applied Energistics 2. If not, see . - */ - -package appeng.me.storage; - - -import net.minecraft.item.ItemStack; - -import appeng.api.AEApi; -import appeng.api.storage.IMEInventory; -import appeng.api.storage.channels.IItemStorageChannel; -import appeng.api.storage.data.IAEItemStack; -import appeng.util.item.AEItemStack; - - -public class ItemCellInventoryHandler extends AbstractCellInventoryHandler -{ - - public ItemCellInventoryHandler( IMEInventory c ) - { - super( c, AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ) ); - } - - @Override - protected IAEItemStack createConfigStackFromItem( ItemStack is ) - { - return AEItemStack.fromItemStack( is ); - } -} diff --git a/src/main/java/appeng/me/storage/MEInventoryHandler.java b/src/main/java/appeng/me/storage/MEInventoryHandler.java index 853026944..9f3a2c84b 100644 --- a/src/main/java/appeng/me/storage/MEInventoryHandler.java +++ b/src/main/java/appeng/me/storage/MEInventoryHandler.java @@ -129,7 +129,7 @@ public class MEInventoryHandler> implements IMEInventoryHa } @Override - public IStorageChannel getChannel() + public IStorageChannel getChannel() { return this.internal.getChannel(); } diff --git a/src/main/java/appeng/tile/inventory/AppEngCellInventory.java b/src/main/java/appeng/tile/inventory/AppEngCellInventory.java index 60dc0eb4c..287f0313c 100644 --- a/src/main/java/appeng/tile/inventory/AppEngCellInventory.java +++ b/src/main/java/appeng/tile/inventory/AppEngCellInventory.java @@ -4,6 +4,7 @@ package appeng.tile.inventory; import net.minecraft.item.ItemStack; +import appeng.api.storage.ICellInventory; import appeng.api.storage.ICellInventoryHandler; import appeng.util.inv.IAEAppEngInventory; import appeng.util.inv.IInternalItemHandler; @@ -102,7 +103,11 @@ public class AppEngCellInventory implements IInternalItemHandler { if( this.handlerForSlot[slot] != null ) { - this.handlerForSlot[slot].getCellInv().persist(); + final ICellInventory ci = this.handlerForSlot[slot].getCellInv(); + if( ci != null ) + { + ci.persist(); + } } } @@ -110,7 +115,9 @@ public class AppEngCellInventory implements IInternalItemHandler { if( this.handlerForSlot[slot] != null ) { - if( this.handlerForSlot[slot].getCellInv().getItemStack() != this.inv.getStackInSlot( slot ) ) + final ICellInventory ci = this.handlerForSlot[slot].getCellInv(); + + if( ci == null || ci.getItemStack() != this.inv.getStackInSlot( slot ) ) { this.handlerForSlot[slot] = null; } diff --git a/src/main/java/appeng/tile/storage/TileChest.java b/src/main/java/appeng/tile/storage/TileChest.java index 4c593a1d3..02e461f2d 100644 --- a/src/main/java/appeng/tile/storage/TileChest.java +++ b/src/main/java/appeng/tile/storage/TileChest.java @@ -68,6 +68,7 @@ import appeng.api.networking.security.IActionSource; import appeng.api.networking.security.ISecurityGrid; import appeng.api.networking.storage.IBaseMonitor; import appeng.api.networking.storage.IStorageGrid; +import appeng.api.storage.ICellGuiHandler; import appeng.api.storage.ICellHandler; import appeng.api.storage.ICellInventory; import appeng.api.storage.ICellInventoryHandler; @@ -123,8 +124,8 @@ public class TileChest extends AENetworkPowerTile implements IMEChest, ITerminal private boolean wasActive = false; private AEColor paintedColor = AEColor.TRANSPARENT; private boolean isCached = false; - private MEMonitorHandler itemCell; - private MEMonitorHandler fluidCell; + private ChestMonitorHandler itemCell; + private ChestMonitorHandler fluidCell; private Accessor accessor; private IFluidHandler fluidHandler; @@ -282,7 +283,7 @@ public class TileChest extends AENetworkPowerTile implements IMEChest, ITerminal return null; } - private > MEMonitorHandler wrap( final IMEInventoryHandler h ) + private > ChestMonitorHandler wrap( final IMEInventoryHandler h ) { if( h == null ) { @@ -292,7 +293,7 @@ public class TileChest extends AENetworkPowerTile implements IMEChest, ITerminal final MEInventoryHandler ih = new MEInventoryHandler( h, h.getChannel() ); ih.setPriority( this.priority ); - final MEMonitorHandler g = new ChestMonitorHandler( ih ); + final ChestMonitorHandler g = new ChestMonitorHandler( ih ); g.addListener( new ChestNetNotifier( h.getChannel() ), g ); return g; @@ -719,15 +720,24 @@ public class TileChest extends AENetworkPowerTile implements IMEChest, ITerminal } - public boolean openGui( final EntityPlayer p, final ICellHandler ch, final ItemStack cell, final EnumFacing side ) + public boolean openGui( final EntityPlayer p ) { + final ICellHandler ch = AEApi.instance().registries().cell().getHandler( this.getCell() ); + try { final IMEInventoryHandler invHandler = this.getHandler( AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ) ); if( ch != null && invHandler != null ) { - ch.openChestGui( p, this, ch, invHandler, cell, AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ) ); - return true; + final ICellGuiHandler chg = AEApi.instance() + .registries() + .cell() + .getGuiHandler( AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ), this.getCell() ); + if( chg != null ) + { + chg.openChestGui( p, this, ch, invHandler, this.getCell(), AEApi.instance().storage().getStorageChannel( IItemStorageChannel.class ) ); + return true; + } } } catch( final ChestNoHandler e ) @@ -740,8 +750,15 @@ public class TileChest extends AENetworkPowerTile implements IMEChest, ITerminal final IMEInventoryHandler invHandler = this.getHandler( AEApi.instance().storage().getStorageChannel( IFluidStorageChannel.class ) ); if( ch != null && invHandler != null ) { - ch.openChestGui( p, this, ch, invHandler, cell, AEApi.instance().storage().getStorageChannel( IFluidStorageChannel.class ) ); - return true; + final ICellGuiHandler chg = AEApi.instance() + .registries() + .cell() + .getGuiHandler( AEApi.instance().storage().getStorageChannel( IFluidStorageChannel.class ), this.getCell() ); + if( chg != null ) + { + chg.openChestGui( p, this, ch, invHandler, this.getCell(), AEApi.instance().storage().getStorageChannel( IFluidStorageChannel.class ) ); + return true; + } } } catch( final ChestNoHandler e ) @@ -775,7 +792,10 @@ public class TileChest extends AENetworkPowerTile implements IMEChest, ITerminal @Override public void saveChanges( final ICellInventory cellInventory ) { - cellInventory.persist(); + if( cellInventory != null ) + { + cellInventory.persist(); + } this.world.markChunkDirty( this.pos, this ); } @@ -844,14 +864,14 @@ public class TileChest extends AENetworkPowerTile implements IMEChest, ITerminal super( t ); } - private IMEInventoryHandler getInternalHandler() + private ICellInventoryHandler getInternalHandler() { final IMEInventoryHandler h = this.getHandler(); if( h instanceof MEInventoryHandler ) { - return (IMEInventoryHandler) ( (MEInventoryHandler) h ).getInternal(); + return (ICellInventoryHandler) ( (MEInventoryHandler) h ).getInternal(); } - return this.getHandler(); + return (ICellInventoryHandler) this.getHandler(); } @Override diff --git a/src/main/java/appeng/tile/storage/TileIOPort.java b/src/main/java/appeng/tile/storage/TileIOPort.java index aec4a4a22..55cca495d 100644 --- a/src/main/java/appeng/tile/storage/TileIOPort.java +++ b/src/main/java/appeng/tile/storage/TileIOPort.java @@ -66,6 +66,7 @@ import appeng.tile.inventory.AppEngInternalInventory; import appeng.util.ConfigManager; import appeng.util.IConfigManagerHost; import appeng.util.InventoryAdaptor; +import appeng.util.Platform; import appeng.util.helpers.ItemHandlerUtil; import appeng.util.inv.AdaptorItemHandler; import appeng.util.inv.InvOperation; @@ -424,7 +425,7 @@ public class TileIOPort extends AENetworkInvTile implements IUpgradeableHost, IC if( extracted != null ) { possible = extracted.getStackSize(); - final IAEStack failed = chan.poweredInsert( energy, destination, extracted, this.mySrc ); + final IAEStack failed = Platform.poweredInsert( energy, destination, extracted, this.mySrc ); if( failed != null ) { diff --git a/src/main/java/appeng/util/Platform.java b/src/main/java/appeng/util/Platform.java index 8f642bbde..128791805 100644 --- a/src/main/java/appeng/util/Platform.java +++ b/src/main/java/appeng/util/Platform.java @@ -32,6 +32,7 @@ import java.util.WeakHashMap; import javax.annotation.Nonnull; import javax.annotation.Nullable; +import com.google.common.base.Preconditions; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; @@ -1165,13 +1166,6 @@ public class Platform return pos; } - public static long nanoTime() - { - // if ( Configuration.INSTANCE.enableNetworkProfiler ) - // return System.nanoTime(); - return 0; - } - public static > T poweredExtraction( final IEnergySource energy, final IMEInventory cell, final T request, final IActionSource src ) { return poweredExtraction( energy, cell, request, src, Actionable.MODULATE ); @@ -1179,6 +1173,12 @@ public class Platform public static > T poweredExtraction( final IEnergySource energy, final IMEInventory cell, final T request, final IActionSource src, final Actionable mode ) { + Preconditions.checkNotNull( energy ); + Preconditions.checkNotNull( cell ); + Preconditions.checkNotNull( request ); + Preconditions.checkNotNull( src ); + Preconditions.checkNotNull( mode ); + final T possible = cell.extractItems( request.copy(), Actionable.SIMULATE, src ); long retrieved = 0; @@ -1221,6 +1221,12 @@ public class Platform public static > T poweredInsert( final IEnergySource energy, final IMEInventory cell, final T input, final IActionSource src, final Actionable mode ) { + Preconditions.checkNotNull( energy ); + Preconditions.checkNotNull( cell ); + Preconditions.checkNotNull( input ); + Preconditions.checkNotNull( src ); + Preconditions.checkNotNull( mode ); + final T possible = cell.injectItems( input.copy(), Actionable.SIMULATE, src ); long stored = input.getStackSize(); @@ -1562,6 +1568,7 @@ public class Platform return ItemStack.EMPTY; } + // TODO wtf is this? public static ItemStack getContainerItem( final ItemStack stackInSlot ) { if( stackInSlot == null )