Search in sources :

Example 1 with IHasTileEntity

use of mekanism.common.block.interfaces.IHasTileEntity in project Mekanism by mekanism.

the class GuiDictionaryTarget method setTargetSlot.

public void setTargetSlot(Object newTarget, boolean playSound) {
    // Clear cached tags
    tags.clear();
    if (newTarget == null) {
        target = null;
    } else if (newTarget instanceof ItemStack) {
        ItemStack itemStack = (ItemStack) newTarget;
        if (itemStack.isEmpty()) {
            target = null;
        } else {
            ItemStack stack = StackUtils.size(itemStack, 1);
            target = stack;
            Item item = stack.getItem();
            tags.put(DictionaryTagType.ITEM, TagCache.getItemTags(stack));
            if (item instanceof BlockItem) {
                Block block = ((BlockItem) item).getBlock();
                tags.put(DictionaryTagType.BLOCK, TagCache.getTagsAsStrings(block.getTags()));
                if (block instanceof IHasTileEntity || block.hasTileEntity(block.defaultBlockState())) {
                    tags.put(DictionaryTagType.TILE_ENTITY_TYPE, TagCache.getTileEntityTypeTags(block));
                }
            }
            // Entity type tags
            if (item instanceof SpawnEggItem) {
                tags.put(DictionaryTagType.ENTITY_TYPE, TagCache.getTagsAsStrings(((SpawnEggItem) item).getType(stack.getTag()).getTags()));
            }
            // Enchantment tags
            Map<Enchantment, Integer> enchantments = EnchantmentHelper.getEnchantments(stack);
            if (!enchantments.isEmpty()) {
                Set<ResourceLocation> enchantmentTags = new HashSet<>();
                for (Enchantment enchantment : enchantments.keySet()) {
                    enchantmentTags.addAll(enchantment.getTags());
                }
                tags.put(DictionaryTagType.ENCHANTMENT, TagCache.getTagsAsStrings(enchantmentTags));
            }
            // Get any potion tags
            Potion potion = PotionUtils.getPotion(itemStack);
            if (potion != Potions.EMPTY) {
                tags.put(DictionaryTagType.POTION, TagCache.getTagsAsStrings(potion.getTags()));
            }
            // Get tags of any contained fluids
            FluidUtil.getFluidHandler(stack).ifPresent(fluidHandler -> {
                Set<ResourceLocation> fluidTags = new HashSet<>();
                for (int tank = 0; tank < fluidHandler.getTanks(); tank++) {
                    FluidStack fluidInTank = fluidHandler.getFluidInTank(tank);
                    if (!fluidInTank.isEmpty()) {
                        fluidTags.addAll(fluidInTank.getFluid().getTags());
                    }
                }
                tags.put(DictionaryTagType.FLUID, TagCache.getTagsAsStrings(fluidTags));
            });
            // Get tags of any contained chemicals
            addChemicalTags(DictionaryTagType.GAS, stack, Capabilities.GAS_HANDLER_CAPABILITY);
            addChemicalTags(DictionaryTagType.INFUSE_TYPE, stack, Capabilities.INFUSION_HANDLER_CAPABILITY);
            addChemicalTags(DictionaryTagType.PIGMENT, stack, Capabilities.PIGMENT_HANDLER_CAPABILITY);
            addChemicalTags(DictionaryTagType.SLURRY, stack, Capabilities.SLURRY_HANDLER_CAPABILITY);
        // TODO: Support other types of things?
        }
    } else if (newTarget instanceof FluidStack) {
        FluidStack fluidStack = (FluidStack) newTarget;
        if (fluidStack.isEmpty()) {
            target = null;
        } else {
            target = fluidStack.copy();
            tags.put(DictionaryTagType.FLUID, TagCache.getTagsAsStrings(((FluidStack) target).getFluid().getTags()));
        }
    } else if (newTarget instanceof ChemicalStack) {
        ChemicalStack<?> chemicalStack = (ChemicalStack<?>) newTarget;
        if (chemicalStack.isEmpty()) {
            target = null;
        } else {
            target = chemicalStack.copy();
            List<String> chemicalTags = TagCache.getTagsAsStrings(((ChemicalStack<?>) target).getType().getTags());
            if (target instanceof GasStack) {
                tags.put(DictionaryTagType.GAS, chemicalTags);
            } else if (target instanceof InfusionStack) {
                tags.put(DictionaryTagType.INFUSE_TYPE, chemicalTags);
            } else if (target instanceof PigmentStack) {
                tags.put(DictionaryTagType.PIGMENT, chemicalTags);
            } else if (target instanceof SlurryStack) {
                tags.put(DictionaryTagType.SLURRY, chemicalTags);
            }
        }
    } else {
        Mekanism.logger.warn("Unable to get tags for unknown type: {}", newTarget);
        return;
    }
    // Update the list being viewed
    tagSetter.accept(tags.keySet());
    if (playSound) {
        playClickSound();
    }
}
Also used : Enchantment(net.minecraft.enchantment.Enchantment) FluidUtil(net.minecraftforge.fluids.FluidUtil) ChemicalStack(mekanism.api.chemical.ChemicalStack) GuiElement(mekanism.client.gui.element.GuiElement) Item(net.minecraft.item.Item) SlurryStack(mekanism.api.chemical.slurry.SlurryStack) HashSet(java.util.HashSet) FluidType(mekanism.client.render.MekanismRenderer.FluidType) ItemStack(net.minecraft.item.ItemStack) Block(net.minecraft.block.Block) Capabilities(mekanism.common.capabilities.Capabilities) Map(java.util.Map) IGuiWrapper(mekanism.client.gui.IGuiWrapper) GasStack(mekanism.api.chemical.gas.GasStack) IHasTileEntity(mekanism.common.block.interfaces.IHasTileEntity) Nonnull(javax.annotation.Nonnull) MatrixStack(com.mojang.blaze3d.matrix.MatrixStack) Nullable(javax.annotation.Nullable) PotionUtils(net.minecraft.potion.PotionUtils) Mekanism(mekanism.common.Mekanism) EnumMap(java.util.EnumMap) StackUtils(mekanism.common.util.StackUtils) Potions(net.minecraft.potion.Potions) Set(java.util.Set) IChemicalHandler(mekanism.api.chemical.IChemicalHandler) EnchantmentHelper(net.minecraft.enchantment.EnchantmentHelper) Capability(net.minecraftforge.common.capabilities.Capability) Consumer(java.util.function.Consumer) DictionaryTagType(mekanism.client.gui.item.GuiDictionary.DictionaryTagType) IJEIGhostTarget(mekanism.client.jei.interfaces.IJEIGhostTarget) List(java.util.List) InfusionStack(mekanism.api.chemical.infuse.InfusionStack) Screen(net.minecraft.client.gui.screen.Screen) MekanismRenderer(mekanism.client.render.MekanismRenderer) BlockItem(net.minecraft.item.BlockItem) ResourceLocation(net.minecraft.util.ResourceLocation) PigmentStack(mekanism.api.chemical.pigment.PigmentStack) SpawnEggItem(net.minecraft.item.SpawnEggItem) TilingDirection(mekanism.client.gui.GuiUtils.TilingDirection) FluidStack(net.minecraftforge.fluids.FluidStack) TextComponentUtil(mekanism.api.text.TextComponentUtil) Collections(java.util.Collections) Potion(net.minecraft.potion.Potion) TagCache(mekanism.common.base.TagCache) PigmentStack(mekanism.api.chemical.pigment.PigmentStack) HashSet(java.util.HashSet) Set(java.util.Set) IHasTileEntity(mekanism.common.block.interfaces.IHasTileEntity) Potion(net.minecraft.potion.Potion) FluidStack(net.minecraftforge.fluids.FluidStack) BlockItem(net.minecraft.item.BlockItem) Item(net.minecraft.item.Item) BlockItem(net.minecraft.item.BlockItem) SpawnEggItem(net.minecraft.item.SpawnEggItem) InfusionStack(mekanism.api.chemical.infuse.InfusionStack) SpawnEggItem(net.minecraft.item.SpawnEggItem) Block(net.minecraft.block.Block) List(java.util.List) GasStack(mekanism.api.chemical.gas.GasStack) ItemStack(net.minecraft.item.ItemStack) Enchantment(net.minecraft.enchantment.Enchantment) Map(java.util.Map) EnumMap(java.util.EnumMap) ChemicalStack(mekanism.api.chemical.ChemicalStack) SlurryStack(mekanism.api.chemical.slurry.SlurryStack)

Example 2 with IHasTileEntity

use of mekanism.common.block.interfaces.IHasTileEntity in project Mekanism by mekanism.

the class BlockMekanism method getDrops.

@Nonnull
@Override
@Deprecated
public List<ItemStack> getDrops(@Nonnull BlockState state, @Nonnull LootContext.Builder builder) {
    List<ItemStack> drops = super.getDrops(state, builder);
    // Check if we need to clear any radioactive materials from the stored tanks as those will be dumped via the tile being removed
    if (state.getBlock() instanceof IHasTileEntity) {
        TileEntity tile = ((IHasTileEntity<?>) state.getBlock()).getTileType().create();
        if (tile instanceof TileEntityMekanism) {
            TileEntityMekanism mekTile = (TileEntityMekanism) tile;
            // Skip tiles that have no tanks and skip chemical creative tanks
            if (!mekTile.getGasTanks(null).isEmpty() && (!(mekTile instanceof TileEntityChemicalTank) || ((TileEntityChemicalTank) mekTile).getTier() != ChemicalTankTier.CREATIVE)) {
                for (ItemStack drop : drops) {
                    ListNBT gasTankList = ItemDataUtils.getList(drop, NBTConstants.GAS_TANKS);
                    if (!gasTankList.isEmpty()) {
                        int count = DataHandlerUtils.getMaxId(gasTankList, NBTConstants.TANK);
                        List<IGasTank> tanks = new ArrayList<>(count);
                        for (int i = 0; i < count; i++) {
                            tanks.add(ChemicalTankBuilder.GAS.createDummy(Long.MAX_VALUE));
                        }
                        DataHandlerUtils.readContainers(tanks, gasTankList);
                        boolean hasRadioactive = false;
                        for (IGasTank tank : tanks) {
                            if (!tank.isEmpty() && tank.getStack().has(GasAttributes.Radiation.class)) {
                                // If the tank isn't empty and has a radioactive gas in it, clear the tank and mark we need to update the item
                                hasRadioactive = true;
                                tank.setEmpty();
                            }
                        }
                        if (hasRadioactive) {
                            // If the item has any gas tanks stored, check if any have radioactive substances in them
                            // and if so clear them out
                            ListNBT newGasTankList = DataHandlerUtils.writeContainers(tanks);
                            if (newGasTankList.isEmpty()) {
                                // If the list is now empty remove it
                                ItemDataUtils.removeData(drop, NBTConstants.GAS_TANKS);
                            } else {
                                // Otherwise, update the list
                                ItemDataUtils.setList(drop, NBTConstants.GAS_TANKS, newGasTankList);
                            }
                        }
                    }
                }
            }
        }
    }
    return drops;
}
Also used : TileEntityMekanism(mekanism.common.tile.base.TileEntityMekanism) IHasTileEntity(mekanism.common.block.interfaces.IHasTileEntity) IGasTank(mekanism.api.chemical.gas.IGasTank) ArrayList(java.util.ArrayList) IHasTileEntity(mekanism.common.block.interfaces.IHasTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) ListNBT(net.minecraft.nbt.ListNBT) TileEntityChemicalTank(mekanism.common.tile.TileEntityChemicalTank) ItemStack(net.minecraft.item.ItemStack) Nonnull(javax.annotation.Nonnull)

Example 3 with IHasTileEntity

use of mekanism.common.block.interfaces.IHasTileEntity 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 4 with IHasTileEntity

use of mekanism.common.block.interfaces.IHasTileEntity 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)

Aggregations

IHasTileEntity (mekanism.common.block.interfaces.IHasTileEntity)4 Nonnull (javax.annotation.Nonnull)3 TileEntityMekanism (mekanism.common.tile.base.TileEntityMekanism)3 Block (net.minecraft.block.Block)3 TileEntity (net.minecraft.tileentity.TileEntity)3 Nullable (javax.annotation.Nullable)2 BlockItem (net.minecraft.item.BlockItem)2 Item (net.minecraft.item.Item)2 ItemStack (net.minecraft.item.ItemStack)2 MatrixStack (com.mojang.blaze3d.matrix.MatrixStack)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 EnumMap (java.util.EnumMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Set (java.util.Set)1 Consumer (java.util.function.Consumer)1 ChemicalStack (mekanism.api.chemical.ChemicalStack)1