下面列出了怎么用net.minecraft.util.ItemScatterer的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void onBreak(World world, BlockPos blockPos, BlockState blockState, PlayerEntity playerEntity) {
super.onBreak(world, blockPos, blockState, playerEntity);
if (playerEntity.getActiveItem().getItem() instanceof ShearsItem) {
ItemScatterer.spawn(world, blockPos.getX(), blockPos.getY(), blockPos.getZ(), new ItemStack(this.asItem(), 1));
}
}
private void dropLoot(ServerWorld serverWorld) {
// set up loot objects
LootTable supplier = serverWorld.getServer().getLootManager().getSupplier(new Identifier(TheHallow.MOD_ID, "gameplay/treasure_chest_common"));
LootContext.Builder builder =
new LootContext.Builder(serverWorld)
.setRandom(serverWorld.random)
.put(LootContextParameters.POSITION, getBlockPos());
// build & add loot to output
List<ItemStack> stacks = supplier.getDrops(builder.build(LootContextTypes.CHEST));
stacks.forEach(stack -> ItemScatterer.spawn(world, getBlockPos(), new BasicInventory(stack)));
}
@SuppressWarnings("deprecation")
@Override
public void onBlockRemoved(BlockState state1, World world, BlockPos pos, BlockState state2, boolean flag) {
if (state1.getBlock() != state2.getBlock()) {
BlockEntity be = world.getBlockEntity(pos);
if (be instanceof TinyPumpkinBlockEntity) {
ItemScatterer.spawn(world, pos, ((TinyPumpkinBlockEntity) be).getAllItems());
world.updateHorizontalAdjacent(pos, this);
}
super.onBlockRemoved(state1, world, pos, state2, flag);
}
}
public ActionResult use(PlayerEntity player, Hand hand, BlockHitResult hit) {
Direction facing = getCachedState().get(HorizontalFacingBlock.FACING);
Direction hitSide = hit.getSide();
if (hitSide != facing.rotateYClockwise() && hitSide != facing.rotateYCounterclockwise()) {
return ActionResult.PASS;
}
if (!world.isClient) {
ItemStack handStack = player.getStackInHand(hand);
boolean isLeft = hitSide == facing.rotateYCounterclockwise();
ItemStack heldItem = isLeft ? leftItem : rightItem;
if (!heldItem.isEmpty()) {
ItemScatterer.spawn(world, pos, DefaultedList.copyOf(ItemStack.EMPTY, heldItem));
if (isLeft) {
leftItem = ItemStack.EMPTY;
} else {
rightItem = ItemStack.EMPTY;
}
sync();
markDirty();
} else if (!handStack.isEmpty()) {
if (isLeft) {
leftItem = handStack.copy();
leftItem.setCount(1);
} else {
rightItem = handStack.copy();
rightItem.setCount(1);
}
handStack.decrement(1);
sync();
markDirty();
}
}
return ActionResult.SUCCESS;
}