Search in sources :

Example 1 with ISustainedData

use of mekanism.common.tile.interfaces.ISustainedData 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 ISustainedData

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

the class BaseBlockLootTables method dropSelfWithContents.

protected void dropSelfWithContents(List<IBlockProvider> blockProviders) {
    // For example, when writing this we added dump mode for chemical tanks to getting transferred to the item
    for (IBlockProvider blockProvider : blockProviders) {
        Block block = blockProvider.getBlock();
        if (skipBlock(block)) {
            continue;
        }
        CopyNbt.Builder nbtBuilder = CopyNbt.copyData(Source.BLOCK_ENTITY);
        boolean hasData = false;
        boolean hasContents = false;
        @Nullable TileEntity tile = null;
        if (block instanceof IHasTileEntity) {
            tile = ((IHasTileEntity<?>) block).getTileType().create();
        }
        if (tile instanceof IFrequencyHandler && ((IFrequencyHandler) tile).getFrequencyComponent().hasCustomFrequencies()) {
            nbtBuilder.copy(NBTConstants.COMPONENT_FREQUENCY, NBTConstants.MEK_DATA + "." + NBTConstants.COMPONENT_FREQUENCY);
            hasData = true;
        }
        if (Attribute.has(block, AttributeSecurity.class)) {
            // TODO: Should we just save the entire security component?
            nbtBuilder.copy(NBTConstants.COMPONENT_SECURITY + "." + NBTConstants.OWNER_UUID, NBTConstants.MEK_DATA + "." + NBTConstants.OWNER_UUID);
            nbtBuilder.copy(NBTConstants.COMPONENT_SECURITY + "." + NBTConstants.SECURITY_MODE, NBTConstants.MEK_DATA + "." + NBTConstants.SECURITY_MODE);
            hasData = true;
        }
        if (Attribute.has(block, AttributeUpgradeSupport.class)) {
            nbtBuilder.copy(NBTConstants.COMPONENT_UPGRADE, NBTConstants.MEK_DATA + "." + NBTConstants.COMPONENT_UPGRADE);
            hasData = true;
        }
        if (tile instanceof ISideConfiguration) {
            nbtBuilder.copy(NBTConstants.COMPONENT_CONFIG, NBTConstants.MEK_DATA + "." + NBTConstants.COMPONENT_CONFIG);
            nbtBuilder.copy(NBTConstants.COMPONENT_EJECTOR, NBTConstants.MEK_DATA + "." + NBTConstants.COMPONENT_EJECTOR);
            hasData = true;
        }
        if (tile instanceof ISustainedData) {
            Set<Entry<String, String>> remapEntries = ((ISustainedData) tile).getTileDataRemap().entrySet();
            for (Entry<String, String> remapEntry : remapEntries) {
                nbtBuilder.copy(remapEntry.getKey(), NBTConstants.MEK_DATA + "." + remapEntry.getValue());
            }
            if (!remapEntries.isEmpty()) {
                hasData = true;
            }
        }
        if (Attribute.has(block, AttributeRedstone.class)) {
            nbtBuilder.copy(NBTConstants.CONTROL_TYPE, NBTConstants.MEK_DATA + "." + NBTConstants.CONTROL_TYPE);
            hasData = true;
        }
        if (tile instanceof TileEntityMekanism) {
            TileEntityMekanism tileEntity = (TileEntityMekanism) tile;
            for (SubstanceType type : EnumUtils.SUBSTANCES) {
                if (tileEntity.handles(type) && !type.getContainers(tileEntity).isEmpty()) {
                    nbtBuilder.copy(type.getContainerTag(), NBTConstants.MEK_DATA + "." + type.getContainerTag());
                    hasData = true;
                    if (type != SubstanceType.ENERGY && type != SubstanceType.HEAT) {
                        hasContents = true;
                    }
                }
            }
        }
        if (Attribute.has(block, AttributeInventory.class)) {
            // then only copy the slots if we actually have any slots because otherwise maybe something just went wrong
            if (!(tile instanceof IItemHandler) || ((IItemHandler) tile).getSlots() > 0) {
                // If we don't actually handle saving an inventory (such as the quantum entangloporter, don't actually add it as something to copy)
                if (!(tile instanceof TileEntityMekanism) || ((TileEntityMekanism) tile).persistInventory()) {
                    nbtBuilder.copy(NBTConstants.ITEMS, NBTConstants.MEK_DATA + "." + NBTConstants.ITEMS);
                    hasData = true;
                    hasContents = true;
                }
            }
        }
        if (block instanceof BlockCardboardBox) {
            // TODO: Do this better so that it doesn't have to be as hard coded to being a cardboard box
            nbtBuilder.copy(NBTConstants.DATA, NBTConstants.MEK_DATA + "." + NBTConstants.DATA);
            hasData = true;
        }
        if (!hasData) {
            // To keep the json as clean as possible don't bother even registering a blank accept function if we have no
            // persistent data that we want to copy. Also log a warning so that we don't have to attempt to check against
            // that block
            dropSelf(block);
        } else {
            add(block, LootTable.lootTable().withPool(applyExplosionCondition(hasContents, LootPool.lootPool().name("main").setRolls(ConstantRange.exactly(1)).add(ItemLootEntry.lootTableItem(block).apply(nbtBuilder)))));
        }
    }
}
Also used : TileEntityMekanism(mekanism.common.tile.base.TileEntityMekanism) IHasTileEntity(mekanism.common.block.interfaces.IHasTileEntity) IItemHandler(net.minecraftforge.items.IItemHandler) CopyNbt(net.minecraft.loot.functions.CopyNbt) IHasTileEntity(mekanism.common.block.interfaces.IHasTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) Entry(java.util.Map.Entry) ItemLootEntry(net.minecraft.loot.ItemLootEntry) LootEntry(net.minecraft.loot.LootEntry) SubstanceType(mekanism.common.tile.base.SubstanceType) IBlockProvider(mekanism.api.providers.IBlockProvider) IFrequencyHandler(mekanism.common.lib.frequency.IFrequencyHandler) Block(net.minecraft.block.Block) SlabBlock(net.minecraft.block.SlabBlock) BlockCardboardBox(mekanism.common.block.BlockCardboardBox) ISustainedData(mekanism.common.tile.interfaces.ISustainedData) Nullable(javax.annotation.Nullable) ISideConfiguration(mekanism.common.tile.interfaces.ISideConfiguration)

Example 3 with ISustainedData

use of mekanism.common.tile.interfaces.ISustainedData 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)

Aggregations

SubstanceType (mekanism.common.tile.base.SubstanceType)3 TileEntityMekanism (mekanism.common.tile.base.TileEntityMekanism)3 ISideConfiguration (mekanism.common.tile.interfaces.ISideConfiguration)3 ISustainedData (mekanism.common.tile.interfaces.ISustainedData)3 ISecurityItem (mekanism.common.lib.security.ISecurityItem)2 ISustainedInventory (mekanism.common.tile.interfaces.ISustainedInventory)2 Item (net.minecraft.item.Item)2 Entry (java.util.Map.Entry)1 UUID (java.util.UUID)1 Nonnull (javax.annotation.Nonnull)1 Nullable (javax.annotation.Nullable)1 IBlockProvider (mekanism.api.providers.IBlockProvider)1 BlockCardboardBox (mekanism.common.block.BlockCardboardBox)1 IHasTileEntity (mekanism.common.block.interfaces.IHasTileEntity)1 IFrequencyHandler (mekanism.common.lib.frequency.IFrequencyHandler)1 PacketSecurityUpdate (mekanism.common.network.to_client.PacketSecurityUpdate)1 TileEntitySecurityDesk (mekanism.common.tile.TileEntitySecurityDesk)1 Block (net.minecraft.block.Block)1 SlabBlock (net.minecraft.block.SlabBlock)1 ItemStack (net.minecraft.item.ItemStack)1