mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-21 20:52:34 +00:00
Add EnumHand and ItemStack to BonemealEvent (#3736)
This commit is contained in:
1 parent
fd0dabd60c
commit
5ffbbd3b9f
4 files changed
+107
-6
No files matched your search
@@ -361,9 +361,18 @@ public class ForgeEventFactory
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use {@link #onApplyBonemeal(EntityPlayer, World, BlockPos, IBlockState, ItemStack, EnumHand)} instead.
|
||||
*/
|
||||
@Deprecated
|
||||
public static int onApplyBonemeal(EntityPlayer player, World world, BlockPos pos, IBlockState state, ItemStack stack)
|
||||
{
|
||||
BonemealEvent event = new BonemealEvent(player, world, pos, state);
|
||||
return onApplyBonemeal(player, world, pos, state, stack, null);
|
||||
}
|
||||
|
||||
public static int onApplyBonemeal(@Nonnull EntityPlayer player, @Nonnull World world, @Nonnull BlockPos pos, @Nonnull IBlockState state, @Nonnull ItemStack stack, @Nullable EnumHand hand)
|
||||
{
|
||||
BonemealEvent event = new BonemealEvent(player, world, pos, state, hand, stack);
|
||||
if (MinecraftForge.EVENT_BUS.post(event)) return -1;
|
||||
if (event.getResult() == Result.ALLOW)
|
||||
{
|
||||
|
||||
@@ -23,9 +23,14 @@ import net.minecraftforge.fml.common.eventhandler.Cancelable;
|
||||
import net.minecraftforge.fml.common.eventhandler.Event;
|
||||
import net.minecraft.block.state.IBlockState;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.EnumHand;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* This event is called when a player attempts to use Bonemeal on a block.
|
||||
* It can be canceled to completely prevent any further processing.
|
||||
@@ -43,13 +48,27 @@ public class BonemealEvent extends PlayerEvent
|
||||
private final World world;
|
||||
private final BlockPos pos;
|
||||
private final IBlockState block;
|
||||
private final EnumHand hand;
|
||||
private final ItemStack stack;
|
||||
|
||||
/**
|
||||
* @deprecated Use {@link #BonemealEvent(EntityPlayer, World, BlockPos, IBlockState, EnumHand, ItemStack)} instead.
|
||||
*/
|
||||
@Deprecated
|
||||
public BonemealEvent(EntityPlayer player, World world, BlockPos pos, IBlockState block)
|
||||
{
|
||||
this(player, world, pos, block, null, ItemStack.EMPTY);
|
||||
}
|
||||
|
||||
public BonemealEvent(@Nonnull EntityPlayer player, @Nonnull World world, @Nonnull BlockPos pos, @Nonnull IBlockState block, @Nullable EnumHand hand,
|
||||
@Nonnull ItemStack stack)
|
||||
{
|
||||
super(player);
|
||||
this.world = world;
|
||||
this.pos = pos;
|
||||
this.block = block;
|
||||
this.hand = hand;
|
||||
this.stack = stack;
|
||||
}
|
||||
|
||||
public World getWorld()
|
||||
@@ -66,4 +85,16 @@ public class BonemealEvent extends PlayerEvent
|
||||
{
|
||||
return block;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public EnumHand getHand()
|
||||
{
|
||||
return hand;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
public ItemStack getStack()
|
||||
{
|
||||
return stack;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package net.minecraftforge.debug;
|
||||
|
||||
import net.minecraft.util.EnumHand;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.event.entity.player.BonemealEvent;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
|
||||
import net.minecraftforge.fml.common.eventhandler.Event;
|
||||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
@Mod(modid = BonemealEventTest.MODID, version = "1.0")
|
||||
public class BonemealEventTest
|
||||
{
|
||||
public static final String MODID = "bonemealeventtest";
|
||||
|
||||
private static final boolean ENABLED = false;
|
||||
private static Logger logger;
|
||||
|
||||
@Mod.EventHandler
|
||||
public void preInit(FMLPreInitializationEvent event)
|
||||
{
|
||||
logger = event.getModLog();
|
||||
|
||||
if (ENABLED)
|
||||
{
|
||||
MinecraftForge.EVENT_BUS.register(BonemealEventTest.class);
|
||||
}
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void onBonemeal(BonemealEvent event)
|
||||
{
|
||||
if (event.getHand() == EnumHand.MAIN_HAND)
|
||||
{
|
||||
// If the bone meal is being used from the main hand, set the result to ALLOW to use up the bone meal without growing the crop
|
||||
event.setResult(Event.Result.ALLOW);
|
||||
logger.info("Prevented bone meal growth effect from main hand");
|
||||
}
|
||||
else if (event.getHand() == EnumHand.OFF_HAND)
|
||||
{
|
||||
// If the bone meal is being used from the off hand, cancel the event to prevent it being used at all
|
||||
event.setCanceled(true);
|
||||
logger.info("Prevented bone meal use from off hand");
|
||||
}
|
||||
else
|
||||
{
|
||||
// If the bone meal is being used from a dispenser or something similar, allow it
|
||||
logger.info("Allowed bone meal use from dispenser");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user