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