| Line 14: |
Line 14: |
| | public void onBlockBreak(final BlockEvent.BreakEvent event) | | public void onBlockBreak(final BlockEvent.BreakEvent event) |
| | { | | { |
| | + | IWorld world = event.getWorld(); |
| | if (!world.isClientSide() && event.getState().is(Blocks.SAND)) // use the base event to get the state and see what it is | | if (!world.isClientSide() && event.getState().is(Blocks.SAND)) // use the base event to get the state and see what it is |
| | { | | { |
| | event.setExpToDrop(10); // change the amount of experience we'll be dropping | | event.setExpToDrop(10); // change the amount of experience we'll be dropping |
| | PlayerEntity player = event.getPlayer(); | | PlayerEntity player = event.getPlayer(); |
| − | if (player.getUseItem().getItem().is(Tags.Items.SHEARS)) // check if the player is holding shears | + | if (player.getMainHandItem().getItem().equals(Items.DIAMOND_SHOVEL)) // check what we're holding |
| | { | | { |
| − | ItemHandlerHelper.giveItemToPlayer(player, new ItemStack(Items.BONE)); // give a bone to the player! | + | ItemHandlerHelper.giveItemToPlayer(player, new ItemStack(Items.BONE)); // give a bone |
| | } | | } |
| | } | | } |
| Line 44: |
Line 45: |
| | Example usage: | | Example usage: |
| | <syntaxhighlight lang="java"> | | <syntaxhighlight lang="java"> |
| − | public void onEntityPlace(final BlockEvent.BreakEvent event) | + | public void onEntityPlace(final BlockEvent.EntityPlaceEvent event) |
| | { | | { |
| − | Entity entity = event.getEntity(); | + | IWorld world = event.getWorld(); |
| − | if (!world.isClientSide() && entity != null) // check if the entity is null | + | if (!world.isClientSide() && event.getEntity() instanceof EndermanEntity) |
| | { | | { |
| − | Block block = event.getState().getBlock(); // get the block that was placed | + | event.setCanceled(true); // prevent enderman placement |
| − | if (entity instanceof PlayerEntity && block.is(Blocks.FROSTED_ICE))
| |
| − | {
| |
| − | event.setCanceled(true); // cancel the placing
| |
| − | event.getWorld().setBlock(event.getPos(), Blocks.COBBLESTONE.defaultBlockState(), 3); // set cobble instead
| |
| − | }
| |
| | } | | } |
| | } | | } |
| Line 79: |
Line 75: |
| | Example usage: | | Example usage: |
| | <syntaxhighlight lang="java"> | | <syntaxhighlight lang="java"> |
| − | public void onNeighborNotify(final BlockEvent.BreakEvent event) | + | public void onNeighborNotify(final BlockEvent.NeighborNotifyEvent event) |
| | { | | { |
| | + | IWorld world = event.getWorld(); |
| | if (world.isClientSide()) return; | | if (world.isClientSide()) return; |
| | BlockPos eventPos = event.getPos(); | | BlockPos eventPos = event.getPos(); |
| − | for (Direction direction : event.getNotifiedSides()) // cycle through notified directions | + | if (event.getState().is(Blocks.REDSTONE_WIRE)) // only do it for redstone |
| | { | | { |
| − | BlockPos pos = eventPos.relative(direction); // move to the direction given | + | for (Direction direction : event.getNotifiedSides()) // cycle through notified directions |
| − | double x = pos.getX() + 0.5D; // move to center of the block | + | { |
| − | double y = pos.getY() + 0.5D;
| + | BlockPos pos = eventPos.relative(direction); // move to that spot |
| − | double z = pos.getZ() + 0.5D;
| + | if (world.isEmptyBlock(pos)) |
| − | // add particle
| + | { |
| − | event.getWorld().addParticle(ParticleTypes.CLOUD, x, y, z, 0.0D, 0.0D, 0.0D);
| + | world.setBlock(pos, Blocks.GLOWSTONE.defaultBlockState(), 3); // place glowstone |
| | + | } |
| | + | } |
| | } | | } |
| | } | | } |
| Line 99: |
Line 98: |
| | * Making a block update detector block | | * Making a block update detector block |
| | | | |
| − | == CreateFluidSourceEvent == | + | ==CreateFluidSourceEvent== |
| | This event controls creation of fluid sources. This is different than cancellable events, because it has a <code>Result</code>. Setting it to <code>ALLOW</code> causes a source block to created, even if that's not normally what would happen. Setting it to <code>DENY</code> always prevents source creation. | | This event controls creation of fluid sources. This is different than cancellable events, because it has a <code>Result</code>. Setting it to <code>ALLOW</code> causes a source block to created, even if that's not normally what would happen. Setting it to <code>DENY</code> always prevents source creation. |
| | | | |
| | Example usage: | | Example usage: |
| | <syntaxhighlight lang="java"> | | <syntaxhighlight lang="java"> |
| − | public void onNeighborNotify(final BlockEvent.BreakEvent event) | + | public void onFluidSourceCreate(final BlockEvent.CreateFluidSourceEvent event) |
| | { | | { |
| | + | IWorldReader world = event.getWorld(); |
| | if (!world.isClientSide() && event.getPos().getY() > 100) | | if (!world.isClientSide() && event.getPos().getY() > 100) |
| | { | | { |
| Line 126: |
Line 126: |
| | Example usage: | | Example usage: |
| | <syntaxhighlight lang="java"> | | <syntaxhighlight lang="java"> |
| − | public void onBreak(final BlockEvent.BreakEvent event) | + | public void onFluidPlace(final BlockEvent.FluidPlaceBlockEvent event) |
| | { | | { |
| | + | IWorld world = event.getWorld(); |
| | if (world.isClientSide()) return; | | if (world.isClientSide()) return; |
| | BlockState newState = event.getNewState(); | | BlockState newState = event.getNewState(); |
| − | if (newState.is(Blocks.FIRE)) | + | if (newState.is(Blocks.COBBLESTONE) && world.dayTime() > 14000L) |
| − | {
| |
| − | event.setCanceled(true); // prevent fire placing from lava
| |
| − | }
| |
| − | else if (newState.is(Blocks.COBBLESTONE) && event.getWorld().dayTime() > 14000L)
| |
| | { | | { |
| | event.setNewState(Blocks.ANDESITE.defaultBlockState()); // change the block if done during nighttime. | | event.setNewState(Blocks.ANDESITE.defaultBlockState()); // change the block if done during nighttime. |
| Line 157: |
Line 154: |
| | public void onCropGrowPre(final BlockEvent.CropGrowEvent.Pre event) | | public void onCropGrowPre(final BlockEvent.CropGrowEvent.Pre event) |
| | { | | { |
| | + | IWorld world = event.getWorld(); |
| | if (world.isClientSide()) return; | | if (world.isClientSide()) return; |
| | BlockState state = event.getState(); | | BlockState state = event.getState(); |
| Line 165: |
Line 163: |
| | else if (state.is(Blocks.BAMBOO)) | | else if (state.is(Blocks.BAMBOO)) |
| | { | | { |
| − | if (event.getWorld().getRandom().nextFloat() > 0.2F) // take a 4/5 chance | + | if (world.getRandom().nextFloat() > 0.2F) // take a 4/5 chance |
| | { | | { |
| | event.setResult(Event.Result.DENY); // make bamboo grow less often | | event.setResult(Event.Result.DENY); // make bamboo grow less often |
| | + | } |
| | + | } |
| | + | } |
| | + | |
| | + | public void onCropsGrowPost(final BlockEvent.CropGrowEvent.Post event) |
| | + | { |
| | + | IWorld world = event.getWorld(); |
| | + | if (!world.isClientSide() && event.getState().is(Blocks.WHEAT)) // check wheat |
| | + | { |
| | + | BlockState newState = event.getState(); |
| | + | if (newState.is(Blocks.WHEAT)) // defensive check to make sure we can do this |
| | + | { |
| | + | world.setBlock(event.getPos(), newState.setValue(CropsBlock.AGE, 7), 3); // set max growth wheat |
| | } | | } |
| | } | | } |
| Line 189: |
Line 200: |
| | public void onTrample(final BlockEvent.FarmlandTrampleEvent event) | | public void onTrample(final BlockEvent.FarmlandTrampleEvent event) |
| | { | | { |
| | + | IWorld world = event.getWorld(); |
| | if (!world.isClientSide() && event.getFallDistance() < 10.0F) | | if (!world.isClientSide() && event.getFallDistance() < 10.0F) |
| | { | | { |
| Line 202: |
Line 214: |
| | | | |
| | This is mostly used for preventing portal spawning in certain dimensions (even the overworld). | | This is mostly used for preventing portal spawning in certain dimensions (even the overworld). |
| | + | |
| | + | Example usage: |
| | + | <syntaxhighlight lang="java"> |
| | + | public void onPortalLight(final BlockEvent.PortalSpawnEvent event) |
| | + | { |
| | + | event.setCanceled(true); |
| | + | } |
| | + | </syntaxhighlight> |
| | | | |
| | == BlockToolInteractEvent == | | == BlockToolInteractEvent == |
| Line 208: |
Line 228: |
| | <code>setFinalState()</code>: Set what you want the result to be. | | <code>setFinalState()</code>: Set what you want the result to be. |
| | | | |
| − | <code>getFinalState()</code>: Returns what it will be changed to. | + | <code>getFinalState()</code>: Returns what it will be changed to. This will normally just return whatever the original block is unless you or another modder has set it. It can't check, for example, if something *will* turn into farmland in the future. |
| | | | |
| | Example usage: | | Example usage: |
| Line 214: |
Line 234: |
| | public void onToolInteract(final BlockEvent.BlockToolInteractEvent event) | | public void onToolInteract(final BlockEvent.BlockToolInteractEvent event) |
| | { | | { |
| − | if (!world.isClientSide() && event.getFinalState().is(Blocks.FARMLAND) && event.getHeldItemStack().getItem() == Items.NETHERITE_HOE) | + | IWorld world = event.getWorld(); |
| | + | if (!world.isClientSide() && event.getHeldItemStack().getItem() == Items.NETHERITE_HOE) |
| | { | | { |
| − | event.setFinalState(Blocks.NETHERRACK.defaultBlockState()); // change the final state under certain conditions | + | event.setFinalState(Blocks.NETHERRACK.defaultBlockState()); // set the block to netherrack |
| | + | // note that this happens for any right click on a block, not just dirt |
| | } | | } |
| | } | | } |