use of net.minecraft.util.ActionResultType in project AgriCraft by AgriCraft.
the class ItemClipper method onItemUse.
@Nonnull
@Override
public ActionResultType onItemUse(@Nonnull ItemUseContext context) {
World world = context.getWorld();
if (world.isRemote()) {
return ActionResultType.PASS;
}
BlockPos pos = context.getPos();
ItemStack stack = context.getItem();
PlayerEntity player = context.getPlayer();
return AgriApi.getCrop(world, pos).map(crop -> {
if (!crop.hasPlant() || !crop.getPlant().allowsClipping(crop.getGrowthStage(), stack, player)) {
if (player != null) {
player.sendMessage(AgriToolTips.MSG_CLIPPING_IMPOSSIBLE, player.getUniqueID());
}
return ActionResultType.FAIL;
}
if (MinecraftForge.EVENT_BUS.post(new AgriCropEvent.Clip.Pre(crop, stack, player))) {
return ActionResultType.FAIL;
}
List<ItemStack> drops = Lists.newArrayList();
crop.getPlant().getClipProducts(drops::add, stack, crop.getGrowthStage(), crop.getStats(), world.getRandom());
crop.setGrowthStage(crop.getPlant().getInitialGrowthStage());
crop.getPlant().onClipped(crop, stack, player);
AgriCropEvent.Clip.Post event = new AgriCropEvent.Clip.Post(crop, stack, drops, player);
MinecraftForge.EVENT_BUS.post(event);
event.getDrops().forEach(crop::dropItem);
return ActionResultType.SUCCESS;
}).orElse(ActionResultType.FAIL);
}
use of net.minecraft.util.ActionResultType in project AgriCraft by AgriCraft.
the class ItemTrowel method tryNewPlant.
protected ActionResultType tryNewPlant(World world, BlockPos pos, ItemStack stack, @Nullable PlayerEntity player) {
if (AgriCraft.instance.getConfig().allowPlantingOutsideCropSticks()) {
BlockState newState = AgriCraft.instance.getModBlockRegistry().crop_plant.getStateForPlacement(world, pos);
if (newState != null && world.setBlockState(pos, newState, 11)) {
boolean success = AgriApi.getCrop(world, pos).map(crop -> {
if (MinecraftForge.EVENT_BUS.post(new AgriCropEvent.Trowel.Pre(crop, stack, player))) {
return false;
}
return this.getGenome(stack).map(genome -> this.getGrowthStage(stack).map(stage -> {
boolean result = crop.plantGenome(genome, player) && this.setGrowthStage(crop, stage);
if (result) {
this.removePlant(stack);
MinecraftForge.EVENT_BUS.post(new AgriCropEvent.Trowel.Post(crop, stack, player));
}
return result;
}).orElse(false)).orElse(false);
}).orElse(false);
if (success) {
return ActionResultType.SUCCESS;
} else {
world.setBlockState(pos, Blocks.AIR.getDefaultState());
}
}
}
return ActionResultType.FAIL;
}
use of net.minecraft.util.ActionResultType in project AgriCraft by AgriCraft.
the class BlockCropBase method onBlockActivated.
@Override
@Deprecated
@SuppressWarnings("deprecation")
public final ActionResultType onBlockActivated(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult hit) {
// fetch crop
Optional<IAgriCrop> optional = this.getCrop(world, pos);
if (!optional.isPresent()) {
return ActionResultType.FAIL;
}
IAgriCrop crop = optional.get();
// run pre logic
if (crop.hasPlant()) {
IAgriPlant plant = crop.getPlant();
if (plant.isPlant()) {
Optional<ActionResultType> result = plant.onRightClickPre(crop, player.getHeldItem(hand), player);
if (result.isPresent()) {
return result.get();
}
}
}
// run default logic
ActionResultType result = this.onCropRightClicked(world, pos, crop, player, hand, hit);
// run post logic
if (crop.hasPlant()) {
IAgriPlant plant = crop.getPlant();
if (plant.isPlant()) {
Optional<ActionResultType> override = plant.onRightClickPost(crop, player.getHeldItem(hand), player);
if (override.isPresent()) {
return override.get();
}
}
}
return result;
}
use of net.minecraft.util.ActionResultType in project Overloaded by CJ-MC-Mods.
the class PlayerInteractionUtil method placeBlock.
@Nonnull
public static BlockPlaceResult placeBlock(@Nonnull ItemStack searchStack, @Nonnull ServerPlayerEntity player, @Nonnull World worldIn, @Nonnull BlockPos newPosition, @Nonnull Direction facing, @Nonnull IEnergyStorage energy, float hitX, float hitY, float hitZ) {
// Can we place a block at this Pos
BlockItem itemBlock = ((BlockItem) searchStack.getItem());
// if (worldIn.loadedAndEntityCanStandOn(newPosition, player)) {
// return BlockPlaceResult.FAIL_DENY;
// }
BlockSnapshot blockSnapshot = BlockSnapshot.create(worldIn.dimension(), worldIn, newPosition);
BlockState placedAgainst = blockSnapshot.getWorld().getBlockState(blockSnapshot.getPos().relative(facing.getOpposite()));
BlockEvent.EntityPlaceEvent event = new BlockEvent.EntityPlaceEvent(blockSnapshot, placedAgainst, player);
MinecraftForge.EVENT_BUS.post(event);
if (event.isCanceled()) {
return BlockPlaceResult.FAIL_DENY;
}
long distance = Math.round(Math.sqrt(player.blockPosition().distSqr(newPosition)));
long cost = OverloadedConfig.INSTANCE.multiToolConfig.placeBaseCost + OverloadedConfig.INSTANCE.multiToolConfig.costPerMeterAway * distance;
if (!player.abilities.instabuild && (cost > Integer.MAX_VALUE || cost < 0 || energy.getEnergyStored() < cost))
return BlockPlaceResult.FAIL_ENERGY;
LazyOptional<IItemHandler> opInventory = player.getCapability(ITEM_HANDLER_CAPABILITY, Direction.UP);
if (!opInventory.isPresent()) {
Overloaded.logger.warn("Player has no ItemHandler Capability? NBT: " + player.serializeNBT());
return BlockPlaceResult.FAIL_PREREQUISITE;
}
IItemHandler inventory = opInventory.orElseThrow(() -> new RuntimeException("Impossible Condition"));
int foundStackSlot = findItemStackSlot(searchStack, inventory);
if (foundStackSlot == -1) {
return BlockPlaceResult.FAIL_PREREQUISITE;
}
ItemStack foundStack = inventory.extractItem(foundStackSlot, 1, player.abilities.instabuild);
BlockItemUseContext context = new BlockItemUseContextPublic(worldIn, player, Hand.MAIN_HAND, foundStack, new BlockRayTraceResult(new Vector3d(hitX + newPosition.getX(), hitY + newPosition.getY(), hitZ + newPosition.getZ()), facing, newPosition, false));
ActionResultType result = ForgeHooks.onPlaceItemIntoWorld(context);
switch(result) {
case CONSUME:
case SUCCESS:
SoundType soundtype = worldIn.getBlockState(newPosition).getBlock().getSoundType(worldIn.getBlockState(newPosition), worldIn, newPosition, player);
worldIn.playSound(null, newPosition, soundtype.getPlaceSound(), SoundCategory.BLOCKS, (soundtype.getVolume() + 1.0F) / 2.0F, soundtype.getPitch() * 0.8F);
if (!player.abilities.instabuild) {
energy.extractEnergy((int) cost, false);
}
return BlockPlaceResult.SUCCESS;
case PASS:
case FAIL:
default:
inventory.insertItem(foundStackSlot, foundStack, player.abilities.instabuild);
return BlockPlaceResult.FAIL_DENY;
}
}
use of net.minecraft.util.ActionResultType in project AgriCraft by AgriCraft.
the class ItemTrowel method onItemUse.
@Nonnull
@Override
public ActionResultType onItemUse(@Nonnull ItemUseContext context) {
World world = context.getWorld();
BlockPos pos = context.getPos();
ItemStack stack = context.getItem();
PlayerEntity player = context.getPlayer();
return AgriApi.getCrop(world, pos).map(crop -> this.tryUseOnCrop(crop, stack, player)).orElseGet(() -> this.tryPlantOnSoil(world, pos, stack, player));
}
Aggregations