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();
}
}
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;
}
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;
}
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)))));
}
}
}
Aggregations