Allow result of PlayerInteractEvent to be changed when it's cancelled (#3744)

This commit is contained in:
Vincent Lee authored and LexManos committed 2017-04-07 16:38:56 -07:00
1 parent 5ffbbd3b9f
commit 72dd06ca85
7 files changed
+170 -67

No files matched your search

@@ -5,9 +5,11 @@ import net.minecraft.entity.monster.EntityCreeper;
import net.minecraft.entity.monster.EntitySkeleton;
import net.minecraft.entity.passive.EntityHorse;
import net.minecraft.init.Items;
import net.minecraft.item.ItemBlock;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityChest;
import net.minecraft.tileentity.TileEntityDropper;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumHand;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
@@ -49,7 +51,7 @@ public class PlayerInteractEventTest
if (!ENABLE) return;
logger.info("HIT VEC: {}", evt.getHitVec());
if (evt.getItemStack() != null)
if (!evt.getItemStack().isEmpty())
{
if (evt.getItemStack().getItem() == Items.GOLDEN_PICKAXE)
evt.setCanceled(true); // Redstone should not activate and pick should not be able to dig anything
@@ -89,22 +91,34 @@ public class PlayerInteractEventTest
// Case: Flint and steel in main hand on top of a TE will light a fire, not open the TE.
// Note that if you do this on a chest, the f+s will fail, but then your off hand will open the chest
// If you dual wield flints and steels and right click a chest nothing should happen
if (evt.getItemStack() != null && evt.getItemStack().getItem() == Items.FLINT_AND_STEEL)
if (!evt.getItemStack().isEmpty() && evt.getItemStack().getItem() == Items.FLINT_AND_STEEL)
evt.setUseBlock(Event.Result.DENY);
// Case: Painting in main hand
// Opening a TE will also place a painting on the TE if possible
if (evt.getHand() == EnumHand.MAIN_HAND && evt.getItemStack() != null && evt.getItemStack().getItem() == Items.PAINTING) {
if (evt.getHand() == EnumHand.MAIN_HAND && !evt.getItemStack().isEmpty() && evt.getItemStack().getItem() == Items.PAINTING) {
evt.setUseItem(Event.Result.ALLOW);
}
// Spawn egg in main hand, block in offhand -> block should be placed
// Sword in main hand, spawn egg in offhand -> nothing should happen
if (evt.getItemStack() != null && evt.getItemStack().getItem() == Items.SPAWN_EGG) {
if (!evt.getItemStack().isEmpty()
&& evt.getItemStack().getItem() == Items.SPAWN_EGG
&& evt.getHand() == EnumHand.MAIN_HAND
&& !evt.getEntityPlayer().getHeldItemOffhand().isEmpty()
&& evt.getEntityPlayer().getHeldItemOffhand().getItem() instanceof ItemBlock) {
evt.setCanceled(true);
}
// Spawn egg in main hand, potion in offhand -> potion should NOT be thrown
if (!evt.getItemStack().isEmpty()
&& evt.getItemStack().getItem() == Items.SPAWN_EGG
&& evt.getHand() == EnumHand.MAIN_HAND
&& !evt.getEntityPlayer().getHeldItemOffhand().isEmpty()
&& evt.getEntityPlayer().getHeldItemOffhand().getItem() == Items.SPLASH_POTION) {
evt.setCanceled(true);
// Fake spawn egg success so splash potion does not trigger
evt.setCancellationResult(EnumActionResult.SUCCESS);
}
}
@SubscribeEvent
@@ -112,12 +126,24 @@ public class PlayerInteractEventTest
{
if (!ENABLE) return;
// Use survival mode
// Case: Ender pearl in main hand, block in offhand -> Block is NOT placed
if (!evt.getItemStack().isEmpty()
&& evt.getItemStack().getItem() == Items.ENDER_PEARL
&& evt.getHand() == EnumHand.MAIN_HAND
&& !evt.getEntityPlayer().getHeldItemOffhand().isEmpty()
&& evt.getEntityPlayer().getHeldItemOffhand().getItem() instanceof ItemBlock)
{
evt.setCanceled(true);
evt.setCancellationResult(EnumActionResult.SUCCESS); // We fake success on the ender pearl so block is not placed
return;
}
// Case: Ender pearl in main hand, bow in offhand with arrows in inv -> Bow should trigger
// Case: Sword in main hand, ender pearl in offhand -> Nothing should happen
if (evt.getItemStack() != null && evt.getItemStack().getItem() == Items.ENDER_PEARL)
if (!evt.getItemStack().isEmpty() && evt.getItemStack().getItem() == Items.ENDER_PEARL)
{
evt.setCanceled(true);
}
}
@SubscribeEvent
@@ -126,17 +152,27 @@ public class PlayerInteractEventTest
if (!ENABLE) return;
logger.info("LOCAL POS: {}", evt.getLocalPos());
if (evt.getItemStack() != null
if (!evt.getItemStack().isEmpty()
&& evt.getTarget() instanceof EntityArmorStand
&& evt.getItemStack().getItem() == Items.IRON_HELMET)
evt.setCanceled(true); // Should not be able to place iron helmet onto armor stand (you will put it on instead)
if (evt.getWorld().isRemote
if (!evt.getItemStack().isEmpty()
&& evt.getTarget() instanceof EntityArmorStand
&& evt.getItemStack().getItem() == Items.GOLDEN_HELMET)
{
evt.setCanceled(true);
evt.setCancellationResult(EnumActionResult.SUCCESS);
// Should not be able to place golden helmet onto armor stand
// However you will NOT put it on because we fake success on the armorstand.
}
if (!evt.getWorld().isRemote
&& evt.getTarget() instanceof EntitySkeleton
&& evt.getLocalPos().yCoord > evt.getTarget().height / 2.0)
{
// If we right click the upper half of a skeleton it dies.
((EntitySkeleton) evt.getTarget()).setDead();
evt.getTarget().setDead();
evt.setCanceled(true);
}
}
@@ -146,10 +182,18 @@ public class PlayerInteractEventTest
{
if (!ENABLE) return;
if (evt.getItemStack() != null && (evt.getTarget() instanceof EntityHorse || evt.getTarget() instanceof EntityCreeper))
if (!evt.getItemStack().isEmpty() && evt.getTarget() instanceof EntityHorse) {
// Should not be able to feed wild horses with golden apple (you will start eating it in survival)
// Should not be able to ignite creeper with F+S
// Applies to both hands
evt.setCanceled(true);
if (evt.getItemStack().getItem() == Items.GOLDEN_APPLE
&& evt.getItemStack().getItemDamage() == 0)
evt.setCanceled(true);
// Should not be able to feed wild horses with notch apple but you will NOT eat it
if (evt.getItemStack().getItem() == Items.GOLDEN_APPLE
&& evt.getItemStack().getItemDamage() == 1)
{
evt.setCanceled(true);
evt.setCancellationResult(EnumActionResult.SUCCESS);
}
}
}
}