Search in sources :

Example 1 with ISustainedInventory

use of mekanism.common.tile.interfaces.ISustainedInventory in project Mekanism by mekanism.

the class BlockMekanism method getPickBlock.

@Nonnull
@Override
public ItemStack getPickBlock(@Nonnull BlockState state, RayTraceResult target, @Nonnull IBlockReader world, @Nonnull BlockPos pos, PlayerEntity player) {
    ItemStack itemStack = new ItemStack(this);
    TileEntityMekanism tile = WorldUtils.getTileEntity(TileEntityMekanism.class, world, pos);
    if (tile == null) {
        return itemStack;
    }
    // TODO: Some of the data doesn't get properly "picked", because there are cases such as before opening the GUI where
    // the server doesn't bother syncing the data to the client. For example with what frequencies there are
    Item item = itemStack.getItem();
    if (tile.getFrequencyComponent().hasCustomFrequencies()) {
        tile.getFrequencyComponent().write(ItemDataUtils.getDataMap(itemStack));
    }
    if (item instanceof ISecurityItem && tile.hasSecurity()) {
        ISecurityItem securityItem = (ISecurityItem) item;
        securityItem.setOwnerUUID(itemStack, tile.getOwnerUUID());
        securityItem.setSecurity(itemStack, tile.getSecurityMode());
    }
    if (tile.supportsUpgrades()) {
        tile.getComponent().write(ItemDataUtils.getDataMap(itemStack));
    }
    if (tile instanceof ISideConfiguration) {
        ISideConfiguration config = (ISideConfiguration) tile;
        config.getConfig().write(ItemDataUtils.getDataMap(itemStack));
        config.getEjector().write(ItemDataUtils.getDataMap(itemStack));
    }
    if (tile instanceof ISustainedData) {
        ((ISustainedData) tile).writeSustainedData(itemStack);
    }
    if (tile.supportsRedstone()) {
        ItemDataUtils.setInt(itemStack, NBTConstants.CONTROL_TYPE, tile.getControlType().ordinal());
    }
    for (SubstanceType type : EnumUtils.SUBSTANCES) {
        if (tile.handles(type)) {
            ItemDataUtils.setList(itemStack, type.getContainerTag(), DataHandlerUtils.writeContainers(type.getContainers(tile)));
        }
    }
    if (item instanceof ISustainedInventory && tile.persistInventory() && tile.getSlots() > 0) {
        ((ISustainedInventory) item).setInventory(tile.getInventory(), itemStack);
    }
    return itemStack;
}
Also used : TileEntityMekanism(mekanism.common.tile.base.TileEntityMekanism) Item(net.minecraft.item.Item) ISecurityItem(mekanism.common.lib.security.ISecurityItem) ISecurityItem(mekanism.common.lib.security.ISecurityItem) ISustainedInventory(mekanism.common.tile.interfaces.ISustainedInventory) SubstanceType(mekanism.common.tile.base.SubstanceType) ItemStack(net.minecraft.item.ItemStack) ISustainedData(mekanism.common.tile.interfaces.ISustainedData) ISideConfiguration(mekanism.common.tile.interfaces.ISideConfiguration) Nonnull(javax.annotation.Nonnull)

Example 2 with ISustainedInventory

use of mekanism.common.tile.interfaces.ISustainedInventory in project Mekanism by mekanism.

the class RecipeUpgradeData method getSupportedTypes.

@Nonnull
static Set<RecipeUpgradeType> getSupportedTypes(ItemStack stack) {
    // TODO: Add more types of data that can be transferred such as side configs, auto sort, bucket mode, dumping mode
    if (stack.isEmpty()) {
        return Collections.emptySet();
    }
    Set<RecipeUpgradeType> supportedTypes = EnumSet.noneOf(RecipeUpgradeType.class);
    Item item = stack.getItem();
    TileEntityMekanism tile = null;
    if (item instanceof BlockItem) {
        Block block = ((BlockItem) item).getBlock();
        if (block instanceof IHasTileEntity) {
            TileEntity tileEntity = ((IHasTileEntity<?>) block).getTileType().create();
            if (tileEntity instanceof TileEntityMekanism) {
                tile = (TileEntityMekanism) tileEntity;
            }
        }
        if (Attribute.has(block, AttributeUpgradeSupport.class)) {
            supportedTypes.add(RecipeUpgradeType.UPGRADE);
        }
    }
    if (stack.getCapability(Capabilities.STRICT_ENERGY_CAPABILITY).isPresent() || tile != null && tile.handles(SubstanceType.ENERGY)) {
        // If we are for a block that handles energy, or we have an energy handler capability
        supportedTypes.add(RecipeUpgradeType.ENERGY);
    }
    if (FluidUtil.getFluidHandler(stack).isPresent() || tile != null && tile.handles(SubstanceType.FLUID)) {
        // If we are for a block that handles fluid, or we have a fluid handler capability
        supportedTypes.add(RecipeUpgradeType.FLUID);
    }
    if (stack.getCapability(Capabilities.GAS_HANDLER_CAPABILITY).isPresent() || tile != null && tile.handles(SubstanceType.GAS)) {
        // If we are for a block that handles gas, or we have a gas handler capability
        supportedTypes.add(RecipeUpgradeType.GAS);
    }
    if (stack.getCapability(Capabilities.INFUSION_HANDLER_CAPABILITY).isPresent() || tile != null && tile.handles(SubstanceType.INFUSION)) {
        // If we are for a block that handles infusion, or we have an infusion handler capability
        supportedTypes.add(RecipeUpgradeType.INFUSION);
    }
    if (stack.getCapability(Capabilities.PIGMENT_HANDLER_CAPABILITY).isPresent() || tile != null && tile.handles(SubstanceType.PIGMENT)) {
        // If we are for a block that handles pigment, or we have a pigment handler capability
        supportedTypes.add(RecipeUpgradeType.PIGMENT);
    }
    if (stack.getCapability(Capabilities.SLURRY_HANDLER_CAPABILITY).isPresent() || tile != null && tile.handles(SubstanceType.SLURRY)) {
        // If we are for a block that handles slurry, or we have a slurry handler capability
        supportedTypes.add(RecipeUpgradeType.SLURRY);
    }
    if (item instanceof ISustainedInventory || tile != null && tile.persistInventory()) {
        supportedTypes.add(RecipeUpgradeType.ITEM);
    }
    if (item instanceof ISecurityItem) {
        supportedTypes.add(RecipeUpgradeType.SECURITY);
    }
    if (item instanceof IQIODriveItem) {
        supportedTypes.add(RecipeUpgradeType.QIO_DRIVE);
    }
    return supportedTypes;
}
Also used : IHasTileEntity(mekanism.common.block.interfaces.IHasTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) IQIODriveItem(mekanism.common.content.qio.IQIODriveItem) Item(net.minecraft.item.Item) ISecurityItem(mekanism.common.lib.security.ISecurityItem) BlockItem(net.minecraft.item.BlockItem) TileEntityMekanism(mekanism.common.tile.base.TileEntityMekanism) ISustainedInventory(mekanism.common.tile.interfaces.ISustainedInventory) ISecurityItem(mekanism.common.lib.security.ISecurityItem) IHasTileEntity(mekanism.common.block.interfaces.IHasTileEntity) Block(net.minecraft.block.Block) BlockItem(net.minecraft.item.BlockItem) IQIODriveItem(mekanism.common.content.qio.IQIODriveItem) Nonnull(javax.annotation.Nonnull)

Example 3 with ISustainedInventory

use of mekanism.common.tile.interfaces.ISustainedInventory in project Mekanism by mekanism.

the class BlockMekanism method setPlacedBy.

@Override
public void setPlacedBy(@Nonnull World world, @Nonnull BlockPos pos, @Nonnull BlockState state, @Nullable LivingEntity placer, @Nonnull ItemStack stack) {
    super.setPlacedBy(world, pos, state, placer, stack);
    TileEntityMekanism tile = WorldUtils.getTileEntity(TileEntityMekanism.class, world, pos);
    if (tile == null) {
        return;
    }
    if (tile.supportsRedstone()) {
        tile.redstone = world.hasNeighborSignal(pos);
    }
    tile.onPlace();
    // Handle item
    Item item = stack.getItem();
    setTileData(world, pos, state, placer, stack, tile);
    // but there is a good chance a lot of this stuff has no real reason to need to be set on the client side at all
    if (!world.isClientSide && tile.getFrequencyComponent().hasCustomFrequencies()) {
        tile.getFrequencyComponent().read(ItemDataUtils.getDataMap(stack));
    }
    if (tile instanceof TileEntitySecurityDesk && placer != null) {
        tile.getSecurity().setOwnerUUID(placer.getUUID());
    }
    if (item instanceof ISecurityItem && tile.hasSecurity()) {
        ISecurityItem securityItem = (ISecurityItem) item;
        tile.setSecurityMode(securityItem.getSecurity(stack));
        UUID ownerUUID = securityItem.getOwnerUUID(stack);
        if (ownerUUID != null) {
            tile.getSecurity().setOwnerUUID(ownerUUID);
        } else if (placer != null) {
            tile.getSecurity().setOwnerUUID(placer.getUUID());
            if (!world.isClientSide) {
                // If the machine doesn't already have an owner, make sure we portray this
                Mekanism.packetHandler.sendToAll(new PacketSecurityUpdate(placer.getUUID(), null));
            }
        }
    }
    if (tile.supportsUpgrades()) {
        // The read method validates that data is stored
        tile.getComponent().read(ItemDataUtils.getDataMap(stack));
    }
    if (tile instanceof ISideConfiguration) {
        ISideConfiguration config = (ISideConfiguration) tile;
        // The read methods validate that data is stored
        config.getConfig().read(ItemDataUtils.getDataMap(stack));
        config.getEjector().read(ItemDataUtils.getDataMap(stack));
    }
    for (SubstanceType type : EnumUtils.SUBSTANCES) {
        if (type.canHandle(tile)) {
            DataHandlerUtils.readContainers(type.getContainers(tile), ItemDataUtils.getList(stack, type.getContainerTag()));
        }
    }
    if (tile instanceof ISustainedData && stack.hasTag()) {
        ((ISustainedData) tile).readSustainedData(stack);
    }
    if (tile.supportsRedstone() && ItemDataUtils.hasData(stack, NBTConstants.CONTROL_TYPE, NBT.TAG_INT)) {
        tile.setControlType(RedstoneControl.byIndexStatic(ItemDataUtils.getInt(stack, NBTConstants.CONTROL_TYPE)));
    }
    if (item instanceof ISustainedInventory && tile.persistInventory()) {
        tile.setInventory(((ISustainedInventory) item).getInventory(stack));
    }
}
Also used : TileEntityMekanism(mekanism.common.tile.base.TileEntityMekanism) Item(net.minecraft.item.Item) ISecurityItem(mekanism.common.lib.security.ISecurityItem) ISecurityItem(mekanism.common.lib.security.ISecurityItem) ISustainedInventory(mekanism.common.tile.interfaces.ISustainedInventory) SubstanceType(mekanism.common.tile.base.SubstanceType) TileEntitySecurityDesk(mekanism.common.tile.TileEntitySecurityDesk) UUID(java.util.UUID) ISustainedData(mekanism.common.tile.interfaces.ISustainedData) PacketSecurityUpdate(mekanism.common.network.to_client.PacketSecurityUpdate) ISideConfiguration(mekanism.common.tile.interfaces.ISideConfiguration)

Example 4 with ISustainedInventory

use of mekanism.common.tile.interfaces.ISustainedInventory in project Mekanism by mekanism.

the class ItemRecipeData method applyToStack.

@Override
public boolean applyToStack(ItemStack stack) {
    if (slots.isEmpty()) {
        return true;
    }
    Item item = stack.getItem();
    boolean isBin = item instanceof ItemBlockBin;
    Optional<IItemHandler> capability = stack.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).resolve();
    List<IInventorySlot> slots = new ArrayList<>();
    if (capability.isPresent()) {
        IItemHandler itemHandler = capability.get();
        for (int i = 0; i < itemHandler.getSlots(); i++) {
            int slot = i;
            slots.add(new DummyInventorySlot(itemHandler.getSlotLimit(slot), itemStack -> itemHandler.isItemValid(slot, itemStack), isBin));
        }
    } else if (item instanceof BlockItem) {
        TileEntityMekanism tile = getTileFromBlock(((BlockItem) item).getBlock());
        if (tile == null || !tile.persistInventory()) {
            // Something went wrong
            return false;
        }
        for (int i = 0; i < tile.getSlots(); i++) {
            int slot = i;
            slots.add(new DummyInventorySlot(tile.getSlotLimit(slot), itemStack -> tile.isItemValid(slot, itemStack), isBin));
        }
    } else if (item instanceof ItemRobit) {
        // Inventory slots
        for (int slotY = 0; slotY < 3; slotY++) {
            for (int slotX = 0; slotX < 9; slotX++) {
                slots.add(new DummyInventorySlot(BasicInventorySlot.DEFAULT_LIMIT, BasicInventorySlot.alwaysTrue, false));
            }
        }
        // Energy slot
        slots.add(new DummyInventorySlot(BasicInventorySlot.DEFAULT_LIMIT, itemStack -> {
            if (EnergyCompatUtils.hasStrictEnergyHandler(itemStack)) {
                return true;
            }
            ItemStackToEnergyRecipe foundRecipe = MekanismRecipeType.ENERGY_CONVERSION.getInputCache().findTypeBasedRecipe(null, itemStack);
            return foundRecipe != null && !foundRecipe.getOutput(itemStack).isZero();
        }, false));
        // Smelting input slot
        slots.add(new DummyInventorySlot(BasicInventorySlot.DEFAULT_LIMIT, itemStack -> MekanismRecipeType.SMELTING.getInputCache().containsInput(null, itemStack), false));
        // Smelting output slot
        slots.add(new DummyInventorySlot(BasicInventorySlot.DEFAULT_LIMIT, BasicInventorySlot.alwaysTrue, false));
    } else if (item instanceof ISustainedInventory) {
        // Fallback just save it all
        for (IInventorySlot slot : this.slots) {
            if (!slot.isEmpty()) {
                // We have no information about what our item supports, but we have at least some stacks we want to transfer
                ((ISustainedInventory) stack.getItem()).setInventory(DataHandlerUtils.writeContainers(this.slots), stack);
                return true;
            }
        }
        return true;
    } else {
        return false;
    }
    if (slots.isEmpty()) {
        // We don't actually have any tanks in the output
        return true;
    }
    // TODO: Improve the logic so that it maybe tries multiple different slot combinations
    IMekanismInventory outputHandler = new IMekanismInventory() {

        @Nonnull
        @Override
        public List<IInventorySlot> getInventorySlots(@Nullable Direction side) {
            return slots;
        }

        @Override
        public void onContentsChanged() {
        }
    };
    boolean hasData = false;
    for (IInventorySlot slot : this.slots) {
        if (!slot.isEmpty()) {
            if (!ItemHandlerHelper.insertItemStacked(outputHandler, slot.getStack(), false).isEmpty()) {
                // If we have a remainder something failed so bail
                return false;
            }
            hasData = true;
        }
    }
    if (hasData) {
        // We managed to transfer it all into valid slots, so save it to the stack
        ((ISustainedInventory) stack.getItem()).setInventory(DataHandlerUtils.writeContainers(slots), stack);
    }
    return true;
}
Also used : IInventorySlot(mekanism.api.inventory.IInventorySlot) IItemHandler(net.minecraftforge.items.IItemHandler) EnergyCompatUtils(mekanism.common.integration.energy.EnergyCompatUtils) IMekanismInventory(mekanism.api.inventory.IMekanismInventory) Item(net.minecraft.item.Item) BasicInventorySlot(mekanism.common.inventory.slot.BasicInventorySlot) Direction(net.minecraft.util.Direction) ParametersAreNonnullByDefault(javax.annotation.ParametersAreNonnullByDefault) ArrayList(java.util.ArrayList) ItemStack(net.minecraft.item.ItemStack) ItemHandlerHelper(net.minecraftforge.items.ItemHandlerHelper) Nonnull(javax.annotation.Nonnull) NonNull(mekanism.api.annotations.NonNull) ItemBlockBin(mekanism.common.item.block.ItemBlockBin) Nullable(javax.annotation.Nullable) TileEntityMekanism(mekanism.common.tile.base.TileEntityMekanism) ISustainedInventory(mekanism.common.tile.interfaces.ISustainedInventory) ListNBT(net.minecraft.nbt.ListNBT) MekanismRecipeType(mekanism.common.recipe.MekanismRecipeType) ItemRobit(mekanism.common.item.ItemRobit) Predicate(java.util.function.Predicate) DataHandlerUtils(mekanism.api.DataHandlerUtils) List(java.util.List) FieldsAreNonnullByDefault(mekanism.api.annotations.FieldsAreNonnullByDefault) IInventorySlot(mekanism.api.inventory.IInventorySlot) BlockItem(net.minecraft.item.BlockItem) NBTConstants(mekanism.api.NBTConstants) CapabilityItemHandler(net.minecraftforge.items.CapabilityItemHandler) ItemStackToEnergyRecipe(mekanism.api.recipes.ItemStackToEnergyRecipe) Optional(java.util.Optional) TileEntityMekanism(mekanism.common.tile.base.TileEntityMekanism) ISustainedInventory(mekanism.common.tile.interfaces.ISustainedInventory) IItemHandler(net.minecraftforge.items.IItemHandler) ItemRobit(mekanism.common.item.ItemRobit) ArrayList(java.util.ArrayList) BlockItem(net.minecraft.item.BlockItem) Direction(net.minecraft.util.Direction) ItemBlockBin(mekanism.common.item.block.ItemBlockBin) Item(net.minecraft.item.Item) BlockItem(net.minecraft.item.BlockItem) ItemStackToEnergyRecipe(mekanism.api.recipes.ItemStackToEnergyRecipe) IMekanismInventory(mekanism.api.inventory.IMekanismInventory) Nullable(javax.annotation.Nullable)

Aggregations

TileEntityMekanism (mekanism.common.tile.base.TileEntityMekanism)4 ISustainedInventory (mekanism.common.tile.interfaces.ISustainedInventory)4 Item (net.minecraft.item.Item)4 Nonnull (javax.annotation.Nonnull)3 ISecurityItem (mekanism.common.lib.security.ISecurityItem)3 SubstanceType (mekanism.common.tile.base.SubstanceType)2 ISideConfiguration (mekanism.common.tile.interfaces.ISideConfiguration)2 ISustainedData (mekanism.common.tile.interfaces.ISustainedData)2 BlockItem (net.minecraft.item.BlockItem)2 ItemStack (net.minecraft.item.ItemStack)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Optional (java.util.Optional)1 UUID (java.util.UUID)1 Predicate (java.util.function.Predicate)1 Nullable (javax.annotation.Nullable)1 ParametersAreNonnullByDefault (javax.annotation.ParametersAreNonnullByDefault)1 DataHandlerUtils (mekanism.api.DataHandlerUtils)1 NBTConstants (mekanism.api.NBTConstants)1 FieldsAreNonnullByDefault (mekanism.api.annotations.FieldsAreNonnullByDefault)1