下面列出了怎么用net.minecraft.util.DefaultedList的API类实例代码及写法,或者点击链接到github查看源代码。
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;
}
public DefaultedList<ItemStack> getAllItems() {
return DefaultedList.copyOf(ItemStack.EMPTY, leftItem, rightItem);
}
@Override
public DefaultedList<ItemStack> getInventory()
{
return inventory;
}
@Inject(method = "dispense", at = @At("HEAD"), cancellable = true)
private void tryCraft(World world_1, BlockPos blockPos_1, CallbackInfo ci)
{
if (!CarpetExtraSettings.autoCraftingDropper) return;
BlockPos front = blockPos_1.offset(world_1.getBlockState(blockPos_1).get(DispenserBlock.FACING));
if (world_1.getBlockState(front).getBlock() != Blocks.CRAFTING_TABLE) return;
DispenserBlockEntity dispenserBlockEntity_1 = (DispenserBlockEntity) world_1.getBlockEntity(blockPos_1);
if (dispenserBlockEntity_1 == null) return;
CraftingInventory craftingInventory = new CraftingInventory(new VoidContainer(), 3, 3);
for (int i=0; i < 9; i++) craftingInventory.setInvStack(i, dispenserBlockEntity_1.getInvStack(i));
CraftingRecipe recipe = world_1.getRecipeManager().getFirstMatch(RecipeType.CRAFTING, craftingInventory, world_1).orElse(null);
if (recipe == null) return;
// crafting it
Vec3d target = new Vec3d(front).add(0.5, 0.2, 0.5);
ItemStack result = recipe.craft(craftingInventory);
spawn(world_1, target.x, target.y, target.z, result);
// copied from CraftingResultSlot.onTakeItem()
DefaultedList<ItemStack> defaultedList_1 = world_1.getRecipeManager().getRemainingStacks(RecipeType.CRAFTING, craftingInventory, world_1);
for(int int_1 = 0; int_1 < defaultedList_1.size(); ++int_1) {
ItemStack itemStack_2 = dispenserBlockEntity_1.getInvStack(int_1);
ItemStack itemStack_3 = defaultedList_1.get(int_1);
if (!itemStack_2.isEmpty()) {
dispenserBlockEntity_1.takeInvStack(int_1, 1);
itemStack_2 = dispenserBlockEntity_1.getInvStack(int_1);
}
if (!itemStack_3.isEmpty()) {
if (itemStack_2.isEmpty()) {
dispenserBlockEntity_1.setInvStack(int_1, itemStack_3);
} else if (ItemStack.areItemsEqualIgnoreDamage(itemStack_2, itemStack_3) && ItemStack.areTagsEqual(itemStack_2, itemStack_3)) {
itemStack_3.increment(itemStack_2.getCount());
dispenserBlockEntity_1.setInvStack(int_1, itemStack_3);
} else {
spawn(world_1, target.x, target.y, target.z, itemStack_3);
}
}
}
Vec3d vec = new Vec3d(blockPos_1).add(0.5, 0.5, 0.5);
ServerWorld world = (ServerWorld) world_1;
world.playSound(null, blockPos_1, SoundEvents.ENTITY_VILLAGER_WORK_MASON, SoundCategory.BLOCKS, 0.2f, 2.0f);
ci.cancel();
}
/**
* Performs the shear function on this object.
*
* <p>This is called on both the client and the server.
* The object should perform all actions related to being sheared,
* except for dropping of the items, and removal of the block.
* Those functions are handled by {@link net.minecraft.item.ShearsItem} itself.</p>
*
* <p>For entities, they should trust their internal location information
* over the values passed into this function.</p>
*
* @param item The {@link ItemStack} that is being used, may be empty.
* @param world The current world.
* @param pos If this is a block, the block's position in world.
* @param fortune The fortune level of the shears being used.
* @return a list of items to be dropped as a result of the shearing process.
*/
default List<ItemStack> onSheared(ItemStack item, IWorld world, BlockPos pos, int fortune) {
return DefaultedList.of();
}
public DefaultedList<ItemStack> getInventory();