mirror of
				https://github.com/SquidDev-CC/CC-Tweaked
				synced 2025-10-31 21:52:59 +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:
		| @@ -24,13 +24,16 @@ import net.minecraft.tileentity.TileEntity; | |||||||
| import net.minecraft.tileentity.TileEntitySign; | import net.minecraft.tileentity.TileEntitySign; | ||||||
| import net.minecraft.util.ActionResult; | import net.minecraft.util.ActionResult; | ||||||
| import net.minecraft.util.EnumActionResult; | import net.minecraft.util.EnumActionResult; | ||||||
|  | import net.minecraft.util.EnumFacing; | ||||||
| import net.minecraft.util.EnumHand; | import net.minecraft.util.EnumHand; | ||||||
| import net.minecraft.util.math.BlockPos; | 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.math.Vec3d; | ||||||
|  | import net.minecraft.util.text.TextComponentString; | ||||||
| import net.minecraft.world.World; | import net.minecraft.world.World; | ||||||
| import net.minecraft.world.WorldServer; | 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 org.apache.commons.lang3.tuple.Pair; | ||||||
|  |  | ||||||
| import javax.annotation.Nonnull; | import javax.annotation.Nonnull; | ||||||
| @@ -215,7 +218,6 @@ public class TurtlePlaceCommand implements ITurtleCommand | |||||||
|         } |         } | ||||||
|  |  | ||||||
|         // Load up the turtle's inventory |         // Load up the turtle's inventory | ||||||
|         Item item = stack.getItem(); |  | ||||||
|         ItemStack stackCopy = stack.copy(); |         ItemStack stackCopy = stack.copy(); | ||||||
|         turtlePlayer.loadInventory( stackCopy ); |         turtlePlayer.loadInventory( stackCopy ); | ||||||
|  |  | ||||||
| @@ -237,23 +239,28 @@ public class TurtlePlaceCommand implements ITurtleCommand | |||||||
|  |  | ||||||
|         // Place on the entity |         // Place on the entity | ||||||
|         boolean placed = false; |         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; |             placed = true; | ||||||
|             turtlePlayer.loadInventory( stackCopy ); |             turtlePlayer.loadInventory( stackCopy ); | ||||||
|         } |         } | ||||||
|         else if( hitEntity.processInitialInteract( turtlePlayer, stackCopy, EnumHand.MAIN_HAND ) ) |         else if( !ForgeHooks.onInteractEntity( turtlePlayer, hitEntity, stack, EnumHand.MAIN_HAND ) ) | ||||||
|  |         { | ||||||
|  |             // See EntityPlayer.interact | ||||||
|  |             if( hitEntity.processInitialInteract( turtlePlayer, stackCopy, EnumHand.MAIN_HAND ) ) | ||||||
|             { |             { | ||||||
|                 placed = true; |                 placed = true; | ||||||
|             } |             } | ||||||
|             else if( hitEntity instanceof EntityLivingBase ) |             else if( hitEntity instanceof EntityLivingBase ) | ||||||
|             { |             { | ||||||
|             placed = item.itemInteractionForEntity( stackCopy, turtlePlayer, (EntityLivingBase)hitEntity, EnumHand.MAIN_HAND ); |                 placed = stackCopy.interactWithEntity( turtlePlayer, (EntityLivingBase) hitEntity, EnumHand.MAIN_HAND ); | ||||||
|                 if( placed ) |                 if( placed ) | ||||||
|                 { |                 { | ||||||
|                     turtlePlayer.loadInventory( stackCopy ); |                     turtlePlayer.loadInventory( stackCopy ); | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|  |         } | ||||||
|  |  | ||||||
|         // Stop claiming drops |         // Stop claiming drops | ||||||
|         ComputerCraft.clearEntityDropConsumer( hitEntity ); |         ComputerCraft.clearEntityDropConsumer( hitEntity ); | ||||||
| @@ -352,19 +359,29 @@ public class TurtlePlaceCommand implements ITurtleCommand | |||||||
|  |  | ||||||
|         // Do the deploying (put everything in the players inventory) |         // Do the deploying (put everything in the players inventory) | ||||||
|         boolean placed = false; |         boolean placed = false; | ||||||
|  |  | ||||||
|  |  | ||||||
|  |         // See PlayerInteractionManager.processRightClickBlock | ||||||
|  |         PlayerInteractEvent.RightClickBlock event = ForgeHooks.onRightClickBlock( turtlePlayer, EnumHand.MAIN_HAND, stackCopy, position, side, new Vec3d( hitX, hitY, hitZ ) ); | ||||||
|  |         if( !event.isCanceled() ) | ||||||
|  |         { | ||||||
|             if( item.onItemUseFirst( stackCopy, turtlePlayer, turtle.getWorld(), position, side, hitX, hitY, hitZ, EnumHand.MAIN_HAND ) == EnumActionResult.SUCCESS ) |             if( item.onItemUseFirst( stackCopy, turtlePlayer, turtle.getWorld(), position, side, hitX, hitY, hitZ, EnumHand.MAIN_HAND ) == EnumActionResult.SUCCESS ) | ||||||
|             { |             { | ||||||
|                 placed = true; |                 placed = true; | ||||||
|                 turtlePlayer.loadInventory( stackCopy ); |                 turtlePlayer.loadInventory( stackCopy ); | ||||||
|             } |             } | ||||||
|         else if( item.onItemUse( stackCopy, turtlePlayer, turtle.getWorld(), position, EnumHand.MAIN_HAND, side, hitX, hitY, hitZ ) == EnumActionResult.SUCCESS ) |             else if( event.getUseItem() != Event.Result.DENY && | ||||||
|  |                 stackCopy.onItemUse( turtlePlayer, turtle.getWorld(), position, EnumHand.MAIN_HAND, side, hitX, hitY, hitZ ) == EnumActionResult.SUCCESS ) | ||||||
|             { |             { | ||||||
|                 placed = true; |                 placed = true; | ||||||
|                 turtlePlayer.loadInventory( stackCopy ); |                 turtlePlayer.loadInventory( stackCopy ); | ||||||
|             } |             } | ||||||
|         else if( item instanceof ItemBucket || item instanceof ItemBoat || item instanceof ItemLilyPad || item instanceof ItemGlassBottle ) |         } | ||||||
|  |  | ||||||
|  |         if( !placed && (item instanceof ItemBucket || item instanceof ItemBoat || item instanceof ItemLilyPad || item instanceof ItemGlassBottle) | ||||||
|  |             && ForgeHooks.onItemRightClick( turtlePlayer, EnumHand.MAIN_HAND, stackCopy ) ) | ||||||
|         { |         { | ||||||
|             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() ) ) |             if( result.getType() == EnumActionResult.SUCCESS && !ItemStack.areItemStacksEqual( stack, result.getResult() ) ) | ||||||
|             { |             { | ||||||
|                 placed = true; |                 placed = true; | ||||||
|   | |||||||
| @@ -22,14 +22,22 @@ import net.minecraft.client.renderer.block.model.IBakedModel; | |||||||
| import net.minecraft.entity.Entity; | import net.minecraft.entity.Entity; | ||||||
| import net.minecraft.entity.SharedMonsterAttributes; | import net.minecraft.entity.SharedMonsterAttributes; | ||||||
| import net.minecraft.entity.item.EntityArmorStand; | import net.minecraft.entity.item.EntityArmorStand; | ||||||
|  | import net.minecraft.entity.player.EntityPlayer; | ||||||
| import net.minecraft.init.Blocks; | import net.minecraft.init.Blocks; | ||||||
| import net.minecraft.item.Item; | import net.minecraft.item.Item; | ||||||
| import net.minecraft.item.ItemStack; | import net.minecraft.item.ItemStack; | ||||||
| import net.minecraft.util.*; | import net.minecraft.util.DamageSource; | ||||||
| import net.minecraft.util.math.*; | 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.World; | ||||||
| import net.minecraft.world.WorldServer; | import net.minecraft.world.WorldServer; | ||||||
| import net.minecraftforge.common.ForgeHooks; | 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.Side; | ||||||
| import net.minecraftforge.fml.relauncher.SideOnly; | import net.minecraftforge.fml.relauncher.SideOnly; | ||||||
| import org.apache.commons.lang3.tuple.Pair; | import org.apache.commons.lang3.tuple.Pair; | ||||||
| @@ -189,9 +197,10 @@ public class TurtleTool implements ITurtleUpgrade | |||||||
|                 } |                 } | ||||||
|             } ); |             } ); | ||||||
|  |  | ||||||
|             // Place on the entity |             // Attack the entity | ||||||
|             boolean placed = false; |             boolean attacked = false; | ||||||
|             if( hitEntity.canBeAttackedWithItem() && !hitEntity.hitByEntity( turtlePlayer ) ) |             if( hitEntity.canBeAttackedWithItem() && !hitEntity.hitByEntity( turtlePlayer ) | ||||||
|  |                 && !MinecraftForge.EVENT_BUS.post( new AttackEntityEvent( turtlePlayer, hitEntity ) ) ) | ||||||
|             { |             { | ||||||
|                 float damage = (float)turtlePlayer.getEntityAttribute( SharedMonsterAttributes.ATTACK_DAMAGE ).getAttributeValue(); |                 float damage = (float)turtlePlayer.getEntityAttribute( SharedMonsterAttributes.ATTACK_DAMAGE ).getAttributeValue(); | ||||||
|                 damage *= getDamageMultiplier(); |                 damage *= getDamageMultiplier(); | ||||||
| @@ -206,13 +215,13 @@ public class TurtleTool implements ITurtleUpgrade | |||||||
|                         { |                         { | ||||||
|                             hitEntity.attackEntityFrom( source, damage ); |                             hitEntity.attackEntityFrom( source, damage ); | ||||||
|                         } |                         } | ||||||
|                         placed = true; |                         attacked = true; | ||||||
|                     } |                     } | ||||||
|                     else |                     else | ||||||
|                     { |                     { | ||||||
|                         if( hitEntity.attackEntityFrom( source, damage ) ) |                         if( hitEntity.attackEntityFrom( source, damage ) ) | ||||||
|                         { |                         { | ||||||
|                             placed = true; |                             attacked = true; | ||||||
|                         } |                         } | ||||||
|                     } |                     } | ||||||
|                 } |                 } | ||||||
| @@ -222,7 +231,7 @@ public class TurtleTool implements ITurtleUpgrade | |||||||
|             ComputerCraft.clearEntityDropConsumer( hitEntity ); |             ComputerCraft.clearEntityDropConsumer( hitEntity ); | ||||||
|  |  | ||||||
|             // Put everything we collected into the turtles inventory, then return |             // Put everything we collected into the turtles inventory, then return | ||||||
|             if( placed ) |             if( attacked ) | ||||||
|             { |             { | ||||||
|                 turtlePlayer.unloadInventory( turtle ); |                 turtlePlayer.unloadInventory( turtle ); | ||||||
|                 return TurtleCommandResult.success(); |                 return TurtleCommandResult.success(); | ||||||
| @@ -243,10 +252,16 @@ public class TurtleTool implements ITurtleUpgrade | |||||||
|             !world.isAirBlock( newPosition ) && |             !world.isAirBlock( newPosition ) && | ||||||
|             !WorldUtil.isLiquidBlock( world, newPosition ) ) |             !WorldUtil.isLiquidBlock( world, newPosition ) ) | ||||||
|         { |         { | ||||||
|  |             TurtlePlayer turtlePlayer = TurtlePlaceCommand.createPlayer( turtle, position, direction ); | ||||||
|             if( ComputerCraft.turtlesObeyBlockProtection ) |             if( ComputerCraft.turtlesObeyBlockProtection ) | ||||||
|             { |             { | ||||||
|                 // Check spawn protection |                 // 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 ) ) |                 if( !ComputerCraft.isBlockEditable( world, newPosition, turtlePlayer ) ) | ||||||
|                 { |                 { | ||||||
|                     return TurtleCommandResult.failure( "Cannot break protected block" ); |                     return TurtleCommandResult.failure( "Cannot break protected block" ); | ||||||
| @@ -262,7 +277,7 @@ public class TurtleTool implements ITurtleUpgrade | |||||||
|             // Consume the items the block drops |             // Consume the items the block drops | ||||||
|             if( canHarvestBlock( world, newPosition ) ) |             if( canHarvestBlock( world, newPosition ) ) | ||||||
|             { |             { | ||||||
|                 List<ItemStack> items = getBlockDropped( world, newPosition ); |                 List<ItemStack> items = getBlockDropped( world, newPosition, turtlePlayer ); | ||||||
|                 if( items != null && items.size() > 0 ) |                 if( items != null && items.size() > 0 ) | ||||||
|                 { |                 { | ||||||
|                     for( ItemStack stack : items ) |                     for( ItemStack stack : items ) | ||||||
| @@ -295,9 +310,20 @@ public class TurtleTool implements ITurtleUpgrade | |||||||
|         return TurtleCommandResult.failure( "Nothing to dig here" ); |         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(); |         IBlockState state = world.getBlockState( pos ); | ||||||
|         return block.getDrops( world, pos, world.getBlockState( pos ), 0 ); |         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; | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 SquidDev
					SquidDev