use of net.minecraft.tileentity.DispenserTileEntity in project Arclight by IzzelAliz.
the class DropperBlockMixin method dispense.
/**
* @author IzzelAliz
* @reason
*/
@Overwrite
public void dispense(World worldIn, BlockPos pos) {
ProxyBlockSource proxyblocksource = new ProxyBlockSource(worldIn, pos);
DispenserTileEntity dispensertileentity = proxyblocksource.getBlockTileEntity();
int i = dispensertileentity.getDispenseSlot();
if (i < 0) {
worldIn.playEvent(1001, pos, 0);
} else {
ItemStack itemstack = dispensertileentity.getStackInSlot(i);
if (!itemstack.isEmpty() && net.minecraftforge.items.VanillaInventoryCodeHooks.dropperInsertHook(worldIn, pos, dispensertileentity, i, itemstack)) {
Direction direction = worldIn.getBlockState(pos).get(DispenserBlock.FACING);
IInventory iinventory = HopperTileEntity.getInventoryAtPosition(worldIn, pos.offset(direction));
ItemStack itemstack1;
if (iinventory == null) {
itemstack1 = DISPENSE_BEHAVIOR.dispense(proxyblocksource, itemstack);
} else {
ItemStack split = itemstack.copy().split(1);
CraftItemStack craftItemStack = CraftItemStack.asCraftMirror(split);
Inventory destinationInventory;
// Have to special case large chests as they work oddly
if (iinventory instanceof DoubleSidedInventory) {
destinationInventory = new CraftInventoryDoubleChest((DoubleSidedInventory) iinventory);
} else {
destinationInventory = ((IInventoryBridge) iinventory).getOwner().getInventory();
}
InventoryMoveItemEvent event = new InventoryMoveItemEvent(((IInventoryBridge) dispensertileentity).getOwner().getInventory(), craftItemStack, destinationInventory, true);
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled()) {
return;
}
itemstack1 = HopperTileEntity.putStackInInventoryAllSlots(dispensertileentity, iinventory, CraftItemStack.asNMSCopy(event.getItem()), direction.getOpposite());
if (event.getItem().equals(craftItemStack) && itemstack1.isEmpty()) {
itemstack1 = itemstack.copy();
itemstack1.shrink(1);
} else {
itemstack1 = itemstack.copy();
}
}
dispensertileentity.setInventorySlotContents(i, itemstack1);
}
}
}
use of net.minecraft.tileentity.DispenserTileEntity in project LoliServer by Loli-Server.
the class VanillaInventoryCodeHooks method dropperInsertHook.
/**
* Copied from BlockDropper#dispense and added capability support
*/
public static boolean dropperInsertHook(World world, BlockPos pos, DispenserTileEntity dropper, int slot, @Nonnull ItemStack stack) {
Direction enumfacing = world.getBlockState(pos).getValue(DropperBlock.FACING);
BlockPos blockpos = pos.relative(enumfacing);
return getItemHandler(world, (double) blockpos.getX(), (double) blockpos.getY(), (double) blockpos.getZ(), enumfacing.getOpposite()).map(destinationResult -> {
IItemHandler itemHandler = destinationResult.getKey();
Object destination = destinationResult.getValue();
ItemStack dispensedStack = stack.copy().split(1);
ItemStack remainder = putStackInInventoryAllSlots(dropper, destination, itemHandler, dispensedStack);
if (remainder.isEmpty()) {
remainder = stack.copy();
remainder.shrink(1);
} else {
remainder = stack.copy();
}
dropper.setItem(slot, remainder);
return false;
}).orElse(true);
}
use of net.minecraft.tileentity.DispenserTileEntity in project Magma-1.16.x by magmafoundation.
the class VanillaInventoryCodeHooks method dropperInsertHook.
/**
* Copied from BlockDropper#dispense and added capability support
*/
public static boolean dropperInsertHook(World world, BlockPos pos, DispenserTileEntity dropper, int slot, @Nonnull ItemStack stack) {
Direction enumfacing = world.getBlockState(pos).getValue(DropperBlock.FACING);
BlockPos blockpos = pos.relative(enumfacing);
return getItemHandler(world, (double) blockpos.getX(), (double) blockpos.getY(), (double) blockpos.getZ(), enumfacing.getOpposite()).map(destinationResult -> {
IItemHandler itemHandler = destinationResult.getKey();
Object destination = destinationResult.getValue();
ItemStack dispensedStack = stack.copy().split(1);
ItemStack remainder = putStackInInventoryAllSlots(dropper, destination, itemHandler, dispensedStack);
if (remainder.isEmpty()) {
remainder = stack.copy();
remainder.shrink(1);
} else {
remainder = stack.copy();
}
dropper.setItem(slot, remainder);
return false;
}).orElse(true);
}
use of net.minecraft.tileentity.DispenserTileEntity in project Magma-1.16.x by magmafoundation.
the class DispenseFluidContainer method fillContainer.
/**
* Picks up fluid in front of a Dispenser and fills a container with it.
*/
@Nonnull
private ItemStack fillContainer(@Nonnull IBlockSource source, @Nonnull ItemStack stack) {
World world = source.getLevel();
Direction dispenserFacing = source.getBlockState().getValue(DispenserBlock.FACING);
BlockPos blockpos = source.getPos().relative(dispenserFacing);
FluidActionResult actionResult = FluidUtil.tryPickUpFluid(stack, null, world, blockpos, dispenserFacing.getOpposite());
ItemStack resultStack = actionResult.getResult();
if (!actionResult.isSuccess() || resultStack.isEmpty()) {
return super.execute(source, stack);
}
if (stack.getCount() == 1) {
return resultStack;
} else if (((DispenserTileEntity) source.getEntity()).addItem(resultStack) < 0) {
this.dispenseBehavior.dispense(source, resultStack);
}
ItemStack stackCopy = stack.copy();
stackCopy.shrink(1);
return stackCopy;
}
use of net.minecraft.tileentity.DispenserTileEntity in project LoliServer by Loli-Server.
the class DispenseFluidContainer method fillContainer.
/**
* Picks up fluid in front of a Dispenser and fills a container with it.
*/
@Nonnull
private ItemStack fillContainer(@Nonnull IBlockSource source, @Nonnull ItemStack stack) {
World world = source.getLevel();
Direction dispenserFacing = source.getBlockState().getValue(DispenserBlock.FACING);
BlockPos blockpos = source.getPos().relative(dispenserFacing);
FluidActionResult actionResult = FluidUtil.tryPickUpFluid(stack, null, world, blockpos, dispenserFacing.getOpposite());
ItemStack resultStack = actionResult.getResult();
if (!actionResult.isSuccess() || resultStack.isEmpty()) {
return super.execute(source, stack);
}
if (stack.getCount() == 1) {
return resultStack;
} else if (((DispenserTileEntity) source.getEntity()).addItem(resultStack) < 0) {
this.dispenseBehavior.dispense(source, resultStack);
}
ItemStack stackCopy = stack.copy();
stackCopy.shrink(1);
return stackCopy;
}
Aggregations