use of mekanism.api.inventory.IMekanismInventory 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;
}
use of mekanism.api.inventory.IMekanismInventory in project Mekanism by mekanism.
the class ItemConfigurator method useOn.
@Nonnull
@Override
public ActionResultType useOn(ItemUseContext context) {
PlayerEntity player = context.getPlayer();
World world = context.getLevel();
if (!world.isClientSide && player != null) {
BlockPos pos = context.getClickedPos();
Direction side = context.getClickedFace();
ItemStack stack = context.getItemInHand();
TileEntity tile = WorldUtils.getTileEntity(world, pos);
ConfiguratorMode mode = getMode(stack);
if (mode.isConfigurating()) {
// Configurate
TransmissionType transmissionType = Objects.requireNonNull(mode.getTransmission(), "Configurating state requires transmission type");
if (tile instanceof ISideConfiguration && ((ISideConfiguration) tile).getConfig().supports(transmissionType)) {
ISideConfiguration config = (ISideConfiguration) tile;
ConfigInfo info = config.getConfig().getConfig(transmissionType);
if (info != null) {
RelativeSide relativeSide = RelativeSide.fromDirections(config.getDirection(), side);
DataType dataType = info.getDataType(relativeSide);
if (!player.isShiftKeyDown()) {
player.sendMessage(MekanismUtils.logFormat(MekanismLang.CONFIGURATOR_VIEW_MODE.translate(transmissionType, dataType.getColor(), dataType, dataType.getColor().getColoredName())), Util.NIL_UUID);
} else if (SecurityUtils.canAccess(player, tile)) {
if (!player.isCreative()) {
IEnergyContainer energyContainer = StorageUtils.getEnergyContainer(stack, 0);
FloatingLong energyPerConfigure = MekanismConfig.gear.configuratorEnergyPerConfigure.get();
if (energyContainer == null || energyContainer.extract(energyPerConfigure, Action.SIMULATE, AutomationType.MANUAL).smallerThan(energyPerConfigure)) {
return ActionResultType.FAIL;
}
energyContainer.extract(energyPerConfigure, Action.EXECUTE, AutomationType.MANUAL);
}
DataType old = dataType;
dataType = info.incrementDataType(relativeSide);
if (dataType != old) {
player.sendMessage(MekanismUtils.logFormat(MekanismLang.CONFIGURATOR_TOGGLE_MODE.translate(transmissionType, dataType.getColor(), dataType, dataType.getColor().getColoredName())), Util.NIL_UUID);
config.getConfig().sideChanged(transmissionType, relativeSide);
}
} else {
SecurityUtils.displayNoAccess(player);
}
}
return ActionResultType.SUCCESS;
}
if (SecurityUtils.canAccess(player, tile)) {
Optional<IConfigurable> capability = CapabilityUtils.getCapability(tile, Capabilities.CONFIGURABLE_CAPABILITY, side).resolve();
if (capability.isPresent()) {
IConfigurable config = capability.get();
if (player.isShiftKeyDown()) {
return config.onSneakRightClick(player, side);
}
return config.onRightClick(player, side);
}
} else {
SecurityUtils.displayNoAccess(player);
return ActionResultType.SUCCESS;
}
} else if (mode == ConfiguratorMode.EMPTY) {
// Empty
if (tile instanceof IMekanismInventory) {
IMekanismInventory inv = (IMekanismInventory) tile;
if (inv.hasInventory()) {
if (SecurityUtils.canAccess(player, tile)) {
boolean creative = player.isCreative();
IEnergyContainer energyContainer = creative ? null : StorageUtils.getEnergyContainer(stack, 0);
if (!creative && energyContainer == null) {
return ActionResultType.FAIL;
}
// TODO: Switch this to items being handled by TileEntityMekanism, energy handled here (via lambdas?)
FloatingLong energyPerItemDump = MekanismConfig.gear.configuratorEnergyPerItem.get();
for (IInventorySlot inventorySlot : inv.getInventorySlots(null)) {
if (!inventorySlot.isEmpty()) {
if (!creative) {
if (energyContainer.extract(energyPerItemDump, Action.SIMULATE, AutomationType.MANUAL).smallerThan(energyPerItemDump)) {
break;
}
energyContainer.extract(energyPerItemDump, Action.EXECUTE, AutomationType.MANUAL);
}
Block.popResource(world, pos, inventorySlot.getStack().copy());
inventorySlot.setEmpty();
}
}
return ActionResultType.SUCCESS;
} else {
SecurityUtils.displayNoAccess(player);
return ActionResultType.FAIL;
}
}
}
} else if (mode == ConfiguratorMode.ROTATE) {
// Rotate
if (tile instanceof TileEntityMekanism) {
if (SecurityUtils.canAccess(player, tile)) {
TileEntityMekanism tileMekanism = (TileEntityMekanism) tile;
if (Attribute.get(tileMekanism.getBlockType(), AttributeStateFacing.class).canRotate()) {
if (!player.isShiftKeyDown()) {
tileMekanism.setFacing(side);
} else if (player.isShiftKeyDown()) {
tileMekanism.setFacing(side.getOpposite());
}
}
} else {
SecurityUtils.displayNoAccess(player);
}
}
return ActionResultType.SUCCESS;
} else if (mode == ConfiguratorMode.WRENCH) {
// Wrench
return ActionResultType.PASS;
}
}
return ActionResultType.PASS;
}
Aggregations