Search in sources :

Example 1 with TileEntityChemicalTank

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

Aggregations

ArrayList (java.util.ArrayList)1 Nonnull (javax.annotation.Nonnull)1 IGasTank (mekanism.api.chemical.gas.IGasTank)1 IHasTileEntity (mekanism.common.block.interfaces.IHasTileEntity)1 TileEntityChemicalTank (mekanism.common.tile.TileEntityChemicalTank)1 TileEntityMekanism (mekanism.common.tile.base.TileEntityMekanism)1 ItemStack (net.minecraft.item.ItemStack)1 ListNBT (net.minecraft.nbt.ListNBT)1 TileEntity (net.minecraft.tileentity.TileEntity)1