下面列出了怎么用net.minecraft.util.ActionResultType的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public ActionResultType onItemUseFirst(ItemStack stack, ItemUseContext context) {
World world = context.getWorld();
if (world.isRemote()) {
return ActionResultType.PASS;
}
TileEntity tile = world.getTileEntity(context.getPos());
if (tile instanceof TileEnderChest && context.getPlayer().isShiftKeyDown()) {
TileEnderChest chest = (TileEnderChest) tile;
Frequency frequency = chest.getFrequency().copy();
if (EnderStorageConfig.anarchyMode && !(frequency.owner != null && frequency.owner.equals(context.getPlayer().getUniqueID()))) {
frequency.setOwner(null);
}
frequency.writeToStack(stack);
return ActionResultType.SUCCESS;
}
return ActionResultType.PASS;
}
@Override
public ActionResultType onBlockActivated(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult result) {
if (!world.isRemote) {
TileEntity tileEntity = world.getTileEntity(pos);
if (tileEntity instanceof INamedContainerProvider) {
NetworkHooks.openGui((ServerPlayerEntity) player, (INamedContainerProvider) tileEntity, tileEntity.getPos());
} else {
throw new IllegalStateException("Our named container provider is missing!");
}
return ActionResultType.SUCCESS;
}
return ActionResultType.SUCCESS;
}
@Override
public ActionResult<ItemStack> onItemRightClick(World world, PlayerEntity player, Hand hand) {
ItemStack stack = player.getHeldItem(hand);
if (player.isShiftKeyDown()) {
return new ActionResult<>(ActionResultType.PASS, stack);
}
if (!world.isRemote) {
Frequency frequency = Frequency.readFromStack(stack);
EnderStorageManager.instance(world.isRemote).getStorage(frequency, EnderItemStorage.TYPE).openContainer((ServerPlayerEntity) player, new TranslationTextComponent(stack.getTranslationKey()));
}
return new ActionResult<>(ActionResultType.SUCCESS, stack);
}
@Deprecated
@Override
public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult blockRayTraceResult)
{
if (worldIn.isRemote)
return ActionResultType.SUCCESS;
TileEntity tileEntity = worldIn.getTileEntity(pos);
if (!(tileEntity instanceof INamedContainerProvider))
return ActionResultType.FAIL;
NetworkHooks.openGui((ServerPlayerEntity) player, (INamedContainerProvider) tileEntity);
return ActionResultType.SUCCESS;
}
@Deprecated
@Override
public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult blockRayTraceResult)
{
ItemStack heldItem = player.getHeldItem(hand);
if (worldIn.isRemote)
{
return (heldItem.getCount() <= 0) || ChoppingRecipe.getRecipe(worldIn, heldItem).isPresent() ?
ActionResultType.SUCCESS : ActionResultType.PASS;
}
TileEntity tileEntity = worldIn.getTileEntity(pos);
if (!(tileEntity instanceof ChoppingBlockTileEntity) || player.isSneaking())
return ActionResultType.PASS;
ChoppingBlockTileEntity chopper = (ChoppingBlockTileEntity) tileEntity;
if (heldItem.getCount() <= 0)
{
ItemStack extracted = chopper.getSlotInventory().extractItem(0, 1, false);
if (extracted.getCount() > 0)
{
ItemHandlerHelper.giveItemToPlayer(player, extracted);
return ActionResultType.SUCCESS;
}
return ActionResultType.PASS;
}
if (ChoppingRecipe.getRecipe(worldIn, heldItem)
.isPresent())
{
ItemStack remaining = chopper.getSlotInventory().insertItem(0, heldItem, false);
if (!player.isCreative())
{
if (remaining.getCount() > 0)
{
player.setHeldItem(hand, remaining);
}
else
{
player.setHeldItem(hand, ItemStack.EMPTY);
}
}
return remaining.getCount() < heldItem.getCount() ?
ActionResultType.SUCCESS : ActionResultType.PASS;
}
return ActionResultType.PASS;
}
private boolean interceptClick(World worldIn, BlockPos pos, BlockState state, PlayerEntity playerIn)
{
TileEntity tileentity = worldIn.getTileEntity(pos);
if (!(tileentity instanceof ChoppingBlockTileEntity))
return false;
ChoppingBlockTileEntity chopper = (ChoppingBlockTileEntity) tileentity;
if (chopper.getSlotInventory().getStackInSlot(0).getCount() <= 0)
return false;
if (worldIn.isRemote)
return true;
ItemStack heldItem = playerIn.getHeldItem(Hand.MAIN_HAND);
int harvestLevel = heldItem.getItem().getHarvestLevel(heldItem, ToolType.AXE, playerIn, null);
ActionResult<ItemStack> result = chopper.chop(playerIn, harvestLevel, EnchantmentHelper.getEnchantmentLevel(Enchantments.FORTUNE, heldItem));
if (result.getType() == ActionResultType.SUCCESS)
{
if (worldIn.rand.nextFloat() < ConfigManager.SERVER.choppingDegradeChance.get())
{
worldIn.setBlockState(pos, breaksInto.get());
}
if (ConfigManager.SERVER.choppingExhaustion.get() > 0)
playerIn.addExhaustion(ConfigManager.SERVER.choppingExhaustion.get().floatValue());
if (heldItem.getCount() > 0 && !playerIn.abilities.isCreativeMode)
{
heldItem.damageItem(1, playerIn, (stack) -> {
stack.sendBreakAnimation(Hand.MAIN_HAND);
});
}
}
if (result.getType() != ActionResultType.PASS)
{
((ServerWorld) worldIn).spawnParticle(new ItemParticleData(ParticleTypes.ITEM, result.getResult()),
pos.getX() + 0.5, pos.getY() + 0.6, pos.getZ() + 0.5, 8,
0, 0.1, 0, 0.02);
}
return true;
}