1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-24 06:03:28 +00:00

Fire Forge events where appropriate

- BlockEvent.BreakEvent and BlockEvent.HarvestDropsEvent are fired when
   digging.
 - AttackEntityEvent is fired when attacking.
 - Various PlayerInteractEvent.* events are fired when placing.

Closes #103, closes #100
This commit is contained in:
SquidDev 2017-05-31 12:41:31 +01:00
parent 114c49e3f6
commit 255dc925fb
2 changed files with 79 additions and 36 deletions

View File

@ -24,13 +24,16 @@
import net.minecraft.tileentity.TileEntitySign;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.ForgeHooks;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.fml.common.eventhandler.Event;
import org.apache.commons.lang3.tuple.Pair;
import javax.annotation.Nonnull;
@ -215,7 +218,6 @@ private static ItemStack deployOnEntity( ItemStack stack, final ITurtleAccess tu
}
// Load up the turtle's inventory
Item item = stack.getItem();
ItemStack stackCopy = stack.copy();
turtlePlayer.loadInventory( stackCopy );
@ -237,21 +239,26 @@ public void consumeDrop( Entity entity, ItemStack drop )
// Place on the entity
boolean placed = false;
if( hitEntity.applyPlayerInteraction( turtlePlayer, hitPos, stackCopy, EnumHand.MAIN_HAND ) == EnumActionResult.SUCCESS )
if( !ForgeHooks.onInteractEntityAt( turtlePlayer, hitEntity, hitPos, stack, EnumHand.MAIN_HAND ) &&
hitEntity.applyPlayerInteraction( turtlePlayer, hitPos, stackCopy, EnumHand.MAIN_HAND ) == EnumActionResult.SUCCESS )
{
placed = true;
turtlePlayer.loadInventory( stackCopy );
}
else if( hitEntity.processInitialInteract( turtlePlayer, stackCopy, EnumHand.MAIN_HAND ) )
else if( !ForgeHooks.onInteractEntity( turtlePlayer, hitEntity, stack, EnumHand.MAIN_HAND ) )
{
placed = true;
}
else if( hitEntity instanceof EntityLivingBase )
{
placed = item.itemInteractionForEntity( stackCopy, turtlePlayer, (EntityLivingBase)hitEntity, EnumHand.MAIN_HAND );
if( placed )
// See EntityPlayer.interact
if( hitEntity.processInitialInteract( turtlePlayer, stackCopy, EnumHand.MAIN_HAND ) )
{
turtlePlayer.loadInventory( stackCopy );
placed = true;
}
else if( hitEntity instanceof EntityLivingBase )
{
placed = stackCopy.interactWithEntity( turtlePlayer, (EntityLivingBase) hitEntity, EnumHand.MAIN_HAND );
if( placed )
{
turtlePlayer.loadInventory( stackCopy );
}
}
}
@ -352,19 +359,29 @@ private static ItemStack deployOnBlock( ItemStack stack, ITurtleAccess turtle, T
// Do the deploying (put everything in the players inventory)
boolean placed = false;
if( item.onItemUseFirst( stackCopy, turtlePlayer, turtle.getWorld(), position, side, hitX, hitY, hitZ, EnumHand.MAIN_HAND ) == EnumActionResult.SUCCESS )
// See PlayerInteractionManager.processRightClickBlock
PlayerInteractEvent.RightClickBlock event = ForgeHooks.onRightClickBlock( turtlePlayer, EnumHand.MAIN_HAND, stackCopy, position, side, new Vec3d( hitX, hitY, hitZ ) );
if( !event.isCanceled() )
{
placed = true;
turtlePlayer.loadInventory( stackCopy );
if( item.onItemUseFirst( stackCopy, turtlePlayer, turtle.getWorld(), position, side, hitX, hitY, hitZ, EnumHand.MAIN_HAND ) == EnumActionResult.SUCCESS )
{
placed = true;
turtlePlayer.loadInventory( stackCopy );
}
else if( event.getUseItem() != Event.Result.DENY &&
stackCopy.onItemUse( turtlePlayer, turtle.getWorld(), position, EnumHand.MAIN_HAND, side, hitX, hitY, hitZ ) == EnumActionResult.SUCCESS )
{
placed = true;
turtlePlayer.loadInventory( stackCopy );
}
}
else if( item.onItemUse( stackCopy, turtlePlayer, turtle.getWorld(), position, EnumHand.MAIN_HAND, side, hitX, hitY, hitZ ) == EnumActionResult.SUCCESS )
if( !placed && (item instanceof ItemBucket || item instanceof ItemBoat || item instanceof ItemLilyPad || item instanceof ItemGlassBottle)
&& ForgeHooks.onItemRightClick( turtlePlayer, EnumHand.MAIN_HAND, stackCopy ) )
{
placed = true;
turtlePlayer.loadInventory( stackCopy );
}
else if( item instanceof ItemBucket || item instanceof ItemBoat || item instanceof ItemLilyPad || item instanceof ItemGlassBottle )
{
ActionResult<ItemStack> result = item.onItemRightClick( stackCopy, turtle.getWorld(), turtlePlayer, EnumHand.MAIN_HAND );
ActionResult<ItemStack> result = stackCopy.useItemRightClick( turtle.getWorld(), turtlePlayer, EnumHand.MAIN_HAND );
if( result.getType() == EnumActionResult.SUCCESS && !ItemStack.areItemStacksEqual( stack, result.getResult() ) )
{
placed = true;

View File

@ -22,14 +22,22 @@
import net.minecraft.entity.Entity;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.item.EntityArmorStand;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.*;
import net.minecraft.util.math.*;
import net.minecraft.util.DamageSource;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.ForgeHooks;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.ForgeEventFactory;
import net.minecraftforge.event.entity.player.AttackEntityEvent;
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.apache.commons.lang3.tuple.Pair;
@ -189,9 +197,10 @@ public void consumeDrop( Entity entity, ItemStack drop )
}
} );
// Place on the entity
boolean placed = false;
if( hitEntity.canBeAttackedWithItem() && !hitEntity.hitByEntity( turtlePlayer ) )
// Attack the entity
boolean attacked = false;
if( hitEntity.canBeAttackedWithItem() && !hitEntity.hitByEntity( turtlePlayer )
&& !MinecraftForge.EVENT_BUS.post( new AttackEntityEvent( turtlePlayer, hitEntity ) ) )
{
float damage = (float)turtlePlayer.getEntityAttribute( SharedMonsterAttributes.ATTACK_DAMAGE ).getAttributeValue();
damage *= getDamageMultiplier();
@ -206,13 +215,13 @@ public void consumeDrop( Entity entity, ItemStack drop )
{
hitEntity.attackEntityFrom( source, damage );
}
placed = true;
attacked = true;
}
else
{
if( hitEntity.attackEntityFrom( source, damage ) )
{
placed = true;
attacked = true;
}
}
}
@ -222,7 +231,7 @@ public void consumeDrop( Entity entity, ItemStack drop )
ComputerCraft.clearEntityDropConsumer( hitEntity );
// Put everything we collected into the turtles inventory, then return
if( placed )
if( attacked )
{
turtlePlayer.unloadInventory( turtle );
return TurtleCommandResult.success();
@ -243,10 +252,16 @@ private TurtleCommandResult dig( ITurtleAccess turtle, EnumFacing direction )
!world.isAirBlock( newPosition ) &&
!WorldUtil.isLiquidBlock( world, newPosition ) )
{
TurtlePlayer turtlePlayer = TurtlePlaceCommand.createPlayer( turtle, position, direction );
if( ComputerCraft.turtlesObeyBlockProtection )
{
// Check spawn protection
TurtlePlayer turtlePlayer = TurtlePlaceCommand.createPlayer( turtle, position, direction );
if( MinecraftForge.EVENT_BUS.post( new BlockEvent.BreakEvent( world, newPosition, world.getBlockState( newPosition ), turtlePlayer ) ) )
{
return TurtleCommandResult.failure( "Cannot break protected block" );
}
if( !ComputerCraft.isBlockEditable( world, newPosition, turtlePlayer ) )
{
return TurtleCommandResult.failure( "Cannot break protected block" );
@ -262,7 +277,7 @@ private TurtleCommandResult dig( ITurtleAccess turtle, EnumFacing direction )
// Consume the items the block drops
if( canHarvestBlock( world, newPosition ) )
{
List<ItemStack> items = getBlockDropped( world, newPosition );
List<ItemStack> items = getBlockDropped( world, newPosition, turtlePlayer );
if( items != null && items.size() > 0 )
{
for( ItemStack stack : items )
@ -279,7 +294,7 @@ private TurtleCommandResult dig( ITurtleAccess turtle, EnumFacing direction )
// Destroy the block
IBlockState previousState = world.getBlockState( newPosition );
world.playEvent(2001, newPosition, Block.getStateId(previousState));
world.playEvent(2001, newPosition, Block.getStateId(previousState));
world.setBlockToAir( newPosition );
// Remember the previous block
@ -295,9 +310,20 @@ private TurtleCommandResult dig( ITurtleAccess turtle, EnumFacing direction )
return TurtleCommandResult.failure( "Nothing to dig here" );
}
private java.util.List<ItemStack> getBlockDropped( World world, BlockPos pos )
private List<ItemStack> getBlockDropped( World world, BlockPos pos, EntityPlayer player )
{
Block block = world.getBlockState( pos ).getBlock();
return block.getDrops( world, pos, world.getBlockState( pos ), 0 );
IBlockState state = world.getBlockState( pos );
Block block = state.getBlock();
List<ItemStack> drops = block.getDrops( world, pos, world.getBlockState( pos ), 0 );
double chance = ForgeEventFactory.fireBlockHarvesting( drops, world, pos, state, 0, 1, false, player );
for( int i = drops.size() - 1; i >= 0; i-- )
{
if( world.rand.nextFloat() > chance )
{
drops.remove( i );
}
}
return drops;
}
}