下面列出了怎么用net.minecraft.inventory.InventoryHelper的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
{
if (!keepInventory)
{
TileEntity tileentity = worldIn.getTileEntity(pos);
if (tileentity instanceof TileEntitySaltFurnace)
{
InventoryHelper.dropInventoryItems(worldIn, pos, (TileEntitySaltFurnace)tileentity);
worldIn.updateComparatorOutputLevel(pos, this);
}
}
super.breakBlock(worldIn, pos, state);
}
@Override
public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
{
if (!keepInventory)
{
TileEntity tileentity = worldIn.getTileEntity(pos);
if (tileentity instanceof TileEntityTFOven)
{
InventoryHelper.dropInventoryItems(worldIn, pos, (TileEntityTFOven)tileentity);
worldIn.updateComparatorOutputLevel(pos, this);
}
}
super.breakBlock(worldIn, pos, state);
}
@Override
public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
{
if (!keepInventory)
{
TileEntity tileentity = worldIn.getTileEntity(pos);
if (tileentity instanceof TileEntitySoymilkAdvancedAggregator)
{
InventoryHelper.dropInventoryItems(worldIn, pos, (TileEntitySoymilkAdvancedAggregator)tileentity);
worldIn.updateComparatorOutputLevel(pos, this);
}
}
super.breakBlock(worldIn, pos, state);
}
@Override
public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
{
if (!keepInventory)
{
TileEntity tileentity = worldIn.getTileEntity(pos);
if (tileentity instanceof TileEntitySoymilkAggregator)
{
InventoryHelper.dropInventoryItems(worldIn, pos, (TileEntitySoymilkAggregator)tileentity);
worldIn.updateComparatorOutputLevel(pos, this);
}
}
super.breakBlock(worldIn, pos, state);
}
@Override
public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
{
if (!keepInventory)
{
TileEntity tileentity = worldIn.getTileEntity(pos);
if (tileentity instanceof TileEntityTFCrasher)
{
InventoryHelper.dropInventoryItems(worldIn, pos, (TileEntityTFCrasher)tileentity);
worldIn.updateComparatorOutputLevel(pos, this);
}
}
super.breakBlock(worldIn, pos, state);
}
@Override
public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
{
if (!keepInventory)
{
TileEntity tileentity = worldIn.getTileEntity(pos);
if (tileentity instanceof TileEntityTFCompressor)
{
InventoryHelper.dropInventoryItems(worldIn, pos, (TileEntityTFCompressor)tileentity);
worldIn.updateComparatorOutputLevel(pos, this);
}
}
super.breakBlock(worldIn, pos, state);
}
@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn,
EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
if (worldIn.isRemote) {
return true;
}
if (facing == EnumFacing.UP) {
worldIn.setBlockState(pos, Blocks.GRAVEL.getDefaultState(), 3);
InventoryHelper.spawnItemStack(worldIn, pos.getX(), pos.offset(EnumFacing.UP).getY(), pos.getZ(),
new ItemStack(ModItems.plasticwaste));
return true;
}
return super.onBlockActivated(worldIn, pos, state, playerIn, hand, facing, hitX, hitY, hitZ);
}
@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn,
EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
if (worldIn.isRemote) {
return true;
}
if (facing == EnumFacing.UP) {
worldIn.setBlockState(pos, Blocks.DIRT.getDefaultState(), 3);
InventoryHelper.spawnItemStack(worldIn, pos.getX(), pos.offset(EnumFacing.UP).getY(), pos.getZ(),
new ItemStack(ModItems.plasticwaste));
return true;
}
return super.onBlockActivated(worldIn, pos, state, playerIn, hand, facing, hitX, hitY, hitZ);
}
@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn,
EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
if (worldIn.isRemote) {
return true;
}
if (facing == EnumFacing.UP) {
worldIn.setBlockState(pos, Blocks.SAND.getDefaultState(), 3);
InventoryHelper.spawnItemStack(worldIn, pos.getX(), pos.offset(EnumFacing.UP).getY(), pos.getZ(),
new ItemStack(ModItems.plasticwaste));
return true;
}
return super.onBlockActivated(worldIn, pos, state, playerIn, hand, facing, hitX, hitY, hitZ);
}
@Override
public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
{
TileEntity tile = worldIn.getTileEntity(pos);
if (tile != null)
{
IItemHandler itemHandler = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
if (itemHandler != null)
{
for (int i = 0; i < itemHandler.getSlots(); i++)
{
ItemStack stack = itemHandler.getStackInSlot(i);
if (!stack.isEmpty())
{
InventoryHelper.spawnItemStack(worldIn, pos.getX(), pos.getY(), pos.getZ(), stack);
}
}
}
}
super.breakBlock(worldIn, pos, state);
}
@Override
public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
{
TileEntity tileentity = worldIn.getTileEntity(pos);
if (tileentity instanceof TileEntityBlueprintArchive && !worldIn.isRemote)
{
TileEntityBlueprintArchive scanner = (TileEntityBlueprintArchive) tileentity;
for (int i = 0; i < scanner.slots.getSlots(); i++)
{
ItemStack stack = scanner.slots.getStackInSlot(i);
if (stack != null)
{
InventoryHelper.spawnItemStack(worldIn, pos.getX(), pos.getY(), pos.getZ(), stack);
}
}
}
super.breakBlock(worldIn, pos, state);
}
@Override
public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
{
TileEntity tileentity = worldIn.getTileEntity(pos);
if (tileentity instanceof TileEntityScanner && !worldIn.isRemote)
{
TileEntityScanner scanner = (TileEntityScanner) tileentity;
for (int i = 0; i < scanner.slots.getSlots(); i++)
{
ItemStack stack = scanner.slots.getStackInSlot(i);
if (stack != null)
{
InventoryHelper.spawnItemStack(worldIn, pos.getX(), pos.getY(), pos.getZ(), stack);
}
}
}
super.breakBlock(worldIn, pos, state);
}
@Override
public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
{
boolean top = state.getValue(HALF) == EnumEngineeringHalf.UPPER;
if (top)
{
TileEntity tileentity = worldIn.getTileEntity(pos);
if (tileentity instanceof TileEntityEngineeringTable && !worldIn.isRemote)
{
TileEntityEngineeringTable engineering = (TileEntityEngineeringTable) tileentity;
for (int i = 0; i < engineering.slots.getSlots(); i++)
{
ItemStack stack = engineering.slots.getStackInSlot(i);
if (stack != null)
{
InventoryHelper.spawnItemStack(worldIn, pos.getX(), pos.getY(), pos.getZ(), stack);
}
}
}
super.breakBlock(worldIn, pos, state);
}
}
@Override
public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
{
TileEntity tileentity = worldIn.getTileEntity(pos);
if (tileentity instanceof TileEntityComponentBox && !worldIn.isRemote)
{
TileEntityComponentBox box = (TileEntityComponentBox) tileentity;
if (box.doDrop)
{
ItemStack stackToDrop = new ItemStack(ib);
stackToDrop = getStack(box);
InventoryHelper.spawnItemStack(worldIn, pos.getX(), pos.getY(), pos.getZ(), stackToDrop);
}
}
super.breakBlock(worldIn, pos, state);
}
@Override
public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
{
TileEntity tileentity = worldIn.getTileEntity(pos);
if (tileentity instanceof TileEntitySurgery && !worldIn.isRemote)
{
TileEntitySurgery surgery = (TileEntitySurgery) tileentity;
for (int i = 0; i < surgery.slots.getSlots(); i++)
{
ItemStack stack = surgery.slots.getStackInSlot(i);
if (stack != null)
{
InventoryHelper.spawnItemStack(worldIn, pos.getX(), pos.getY(), pos.getZ(), stack);
}
}
}
super.breakBlock(worldIn, pos, state);
}
public void ejectContents(boolean ejectFuel)
{
if(ejectFuel)
{
InventoryHelper.spawnItemStack(world, getPos().getX(), getPos().getY(), getPos().getZ(), this.getStackInSlot(FUEL_SLOT));
this.setInventorySlotContents(FUEL_SLOT, ItemStack.EMPTY);
}
else
{
for(int i = TOOL_SLOT; i < this.getSizeInventory(); i++)
{
if(i == OUTPUT_SLOT && cookingTimer > 0)
{
continue;
}
InventoryHelper.spawnItemStack(world, getPos().getX(), getPos().getY(), getPos().getZ(), this.getStackInSlot(i));
this.setInventorySlotContents(i, ItemStack.EMPTY);
}
}
}
@Override
public void breakBlock(World worldIn, BlockPos pos, IBlockState state) {
TileEntity tileentity = worldIn.getTileEntity(pos);
if (tileentity instanceof TileEntityTofuBattery) {
InventoryHelper.dropInventoryItems(worldIn, pos, (TileEntityTofuBattery) tileentity);
worldIn.updateComparatorOutputLevel(pos, this);
}
super.breakBlock(worldIn, pos, state);
}
@Override
public void breakBlock(World worldIn, BlockPos pos, IBlockState state) {
TileEntity tileentity = worldIn.getTileEntity(pos);
if (tileentity instanceof TileEntityTFStorage) {
InventoryHelper.dropInventoryItems(worldIn, pos, (TileEntityTFStorage) tileentity);
worldIn.updateComparatorOutputLevel(pos, this);
}
super.breakBlock(worldIn, pos, state);
}
@Override
public void breakBlock(World worldIn, BlockPos pos, IBlockState state) {
if (!keepInventory) {
TileEntity te = worldIn.getTileEntity(pos);
if (te instanceof TileEntityMapleCauldron) {
InventoryHelper.dropInventoryItems(worldIn, pos, (TileEntityMapleCauldron)te);
worldIn.updateComparatorOutputLevel(pos, this);
}
}
super.breakBlock(worldIn, pos, state);
}
@Override
public void breakBlock(World worldIn, BlockPos pos, IBlockState state) {
TileEntity tileentity = worldIn.getTileEntity(pos);
if (tileentity instanceof TileEntityStoneMortar) {
InventoryHelper.dropInventoryItems(worldIn, pos, (TileEntityStoneMortar) tileentity);
worldIn.updateComparatorOutputLevel(pos, this);
}
super.breakBlock(worldIn, pos, state);
}
@Override
public void breakBlock(World worldIn, BlockPos pos, IBlockState state) {
if (!keepInventory) {
TileEntity te = worldIn.getTileEntity(pos);
if (te instanceof TileEntityCampfirePot) {
InventoryHelper.dropInventoryItems(worldIn, pos, (TileEntityCampfirePot)te);
worldIn.updateComparatorOutputLevel(pos, this);
}
spawnAsEntity(worldIn, pos, new ItemStack(Item.getItemFromBlock(BlockLoader.CAMPFIRE_IDLE)));
spawnAsEntity(worldIn, pos, new ItemStack(ItemLoader.POT));
}
super.breakBlock(worldIn, pos, state);
}
@Override
public void breakBlock(World world, BlockPos pos, IBlockState state) {
TileEntity te = world.getTileEntity(pos);
IItemHandler cap = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
for (int i = 0; i < cap.getSlots(); ++i) {
ItemStack itemstack = cap.getStackInSlot(i);
if (!itemstack.isEmpty()) {
InventoryHelper.spawnItemStack(world, pos.getX(), pos.getY(), pos.getZ(), itemstack);
}
}
}
@Override
public void onReplaced(BlockState state, World worldIn, BlockPos pos, BlockState newState, boolean isMoving) {
if (newState.getBlock() != this) {
TileEntity tileEntity = worldIn.getTileEntity(pos);
if (tileEntity != null) {
LazyOptional<IItemHandler> cap = tileEntity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY);
cap.ifPresent(handler -> {
for(int i = 0; i < handler.getSlots(); ++i) {
InventoryHelper.spawnItemStack(worldIn, pos.getX(), pos.getY(), pos.getZ(), handler.getStackInSlot(i));
}
});
}
super.onReplaced(state, worldIn, pos, newState, isMoving);
}
}
@Override
public void breakBlock(World worldIn, BlockPos pos, IBlockState state) {
notifyNeighbors(worldIn, pos, state);
TileEntity tileentity = worldIn.getTileEntity(pos);
if (tileentity instanceof TileEntityMinecoprocessor) {
InventoryHelper.dropInventoryItems(worldIn, pos, (TileEntityMinecoprocessor) tileentity);
}
super.breakBlock(worldIn, pos, state);
}
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, @Nullable ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ)
{
TileEntity tileentity = worldIn.getTileEntity(pos);
if (tileentity instanceof TileEntityComponentBox)
{
/*if (player.isCreative() && player.isSneaking())
{
TileEntityScanner scanner = ((TileEntityScanner) tileentity);
scanner.ticks = CyberwareConfig.SCANNER_TIME - 200;
}*/
if (player.isSneaking())
{
TileEntityComponentBox box = (TileEntityComponentBox) tileentity;
ItemStack toDrop = this.getStack(box);
if (player.inventory.mainInventory[player.inventory.currentItem] == null)
{
player.inventory.mainInventory[player.inventory.currentItem] = toDrop;
}
else
{
if (!player.inventory.addItemStackToInventory(toDrop))
{
InventoryHelper.spawnItemStack(worldIn, pos.getX(), pos.getY(), pos.getZ(), toDrop);
}
}
box.doDrop = false;
worldIn.setBlockToAir(pos);
}
else
{
player.openGui(Cyberware.INSTANCE, 5, worldIn, pos.getX(), pos.getY(), pos.getZ());
}
}
return true;
}
@Override
public void breakBlock(World worldIn, BlockPos pos, IBlockState state) {
TileEntityPhysicsInfuser tileEntity = (TileEntityPhysicsInfuser) worldIn.getTileEntity(pos);
// If there's a valid TileEntity, try dropping the contents of it's inventory.
if (tileEntity != null && !tileEntity.isInvalid()) {
IItemHandler handler = tileEntity
.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
// Safety in case the capabilities system breaks. If we can't find the handler then
// there isn't anything to drop anyways.
if (handler != null) {
// Drop all the items
for (int slot = 0; slot < handler.getSlots(); slot++) {
ItemStack stack = handler.getStackInSlot(slot);
InventoryHelper
.spawnItemStack(worldIn, pos.getX(), pos.getY(), pos.getZ(), stack);
}
}
}
// Remove the dummy block of this physics infuser, if there is one.
BlockPos dummyBlockPos = getDummyStatePos(state, pos);
super.breakBlock(worldIn, pos, state);
if (worldIn.getBlockState(dummyBlockPos)
.getBlock() == ValkyrienSkiesMod.INSTANCE.physicsInfuserDummy) {
worldIn.setBlockToAir(dummyBlockPos);
}
// Finally, delete the tile entity.
worldIn.removeTileEntity(pos);
}
public static void dropInventoryItems(World worldIn, BlockPos pos, IItemHandler inventory)
{
for (int i = 0; i < inventory.getSlots(); ++i)
{
ItemStack itemstack = inventory.getStackInSlot(i);
if (itemstack.getCount() > 0)
{
InventoryHelper.spawnItemStack(worldIn, (double) pos.getX(), (double) pos.getY(), (double) pos.getZ(), itemstack);
}
}
}
private static void dropInventoryItems(World worldIn, BlockPos pos, IItemHandler inventory)
{
for (int i = 0; i < inventory.getSlots(); ++i)
{
ItemStack itemstack = inventory.getStackInSlot(i);
if (itemstack.getCount() > 0)
{
InventoryHelper.spawnItemStack(worldIn, (double) pos.getX(), (double) pos.getY(), (double) pos.getZ(), itemstack);
}
}
}
private static void dropInventoryItems(World worldIn, BlockPos pos, IItemHandler inventory)
{
for (int i = 0; i < inventory.getSlots(); ++i)
{
ItemStack itemstack = inventory.getStackInSlot(i);
if (itemstack.getCount() > 0)
{
InventoryHelper.spawnItemStack(worldIn, (double) pos.getX(), (double) pos.getY(), (double) pos.getZ(), itemstack);
}
}
}
@Override
public void breakBlock(World worldIn, BlockPos pos, IBlockState state)
{
TileEntity tileentity = worldIn.getTileEntity(pos);
if (tileentity instanceof IInventory)
{
InventoryHelper.dropInventoryItems(worldIn, pos, (IInventory)tileentity);
worldIn.updateComparatorOutputLevel(pos, this);
}
super.breakBlock(worldIn, pos, state);
}