Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

(1.11.2) [Unresolved/Open] attackEntityFrom not working


DrLogiq
 Share

Recommended Posts

Okay, so I have an item which damages players holding it randomly, using .attackEntityFrom and a custom damage source. This works fine.

I also have a block which when placed in the world, damages nearby players on a randomTick event, or at least it's supposed to.

I've got it to detect all players who're nearby, but when I try to perform the attackEntityFrom, it returns false, and I'm not quite sure why. My function looks like this:

 

	@Override
	public void randomTick(World worldIn, BlockPos pos, IBlockState state, Random random)
	{
		System.out.println("Random Tick");
		List<EntityPlayer> players = Minecraft.getMinecraft().world.playerEntities;
		System.out.println("----- Found " + players.size() + " players");
		for (int i = 0; i < players.size(); i++)
		{
			EntityPlayer p = players.get(i);
			System.out.println("----- Testing " + p.getName());
			if (pos.distanceSq(p.getPosition()) < 10.0D)
			{
				System.out.println("----- Test positive");
				if (p.attackEntityFrom(Factori.radiation, 3))
				{
					System.out.println("----- Success");
				}
				else System.out.println("----- Failed :(");
			}
		}
	}

And the damage source is as follows:

public static DamageSource radiation = new DamageSource("radiation").setDamageAllowedInCreativeMode();

The console gives results such as:

Quote

[07:29:42] [Server thread/INFO]: [STDOUT]: Random Tick
[07:29:42] [Server thread/INFO]: [STDOUT]: ----- Found 1 players
[07:29:42] [Server thread/INFO]: [STDOUT]: ----- Testing Player598
[07:29:42] [Server thread/INFO]: [STDOUT]: ----- Test positive
[07:29:42] [Server thread/INFO]: [STDOUT]: ----- Failed :(

...every now and then, when standing near the block. My question is:

Why does the .attackEntityFrom() function return false when used in a Block class?

Another side question:

Am I able to increase the random tick chance to make it more frequent?

Link to comment
Share on other sites

You're accessing the players through the client World, which means two things:

  • Your code will crash the dedicated server, where the Minecraft class doesn't exist.
  • You're calling Entity#attackEntityFrom on instances of EntityPlayerSP or EntityOtherPlayerMP, both of which override it to do nothing but fire LivingAttackEvent and return false.

You already have a World argument, use this instead of always using the client world. This way you'll fire LivingAttackEvent and damage players when it's called on the server and still fire LivingAttackEvent when it's called on the client.

 

This is a general principle: Use the data you're provided (e.g. the World argument) instead of bypassing it and accessing it yourself (e.g. accessing the client World through Minecraft).

 

Instead of iterating through World#playerEntities, I recommend calling World#getEntitiesWithinAABB with EntityPlayer.class as the first argument. This will return a list of all players within the bounds of the AABB.

 

You can use the AxisAlignedBB(BlockPos) constructor to create an AABB with the BlockPos as the minimum bounds and the BlockPos + 1 as the maximum bounds (i.e. the 1x1x1 space occupied by the block) and then use AxisAlignedBB#expandXyz to expand it by the specified amount in all directions.

 

 

24 minutes ago, DrLogiq said:

Am I able to increase the random tick chance to make it more frequent?

 

No. Depending on how frequently you want it to tick, either schedule ticks with World#scheduleUpdate or give it a TileEntity that implements ITickable.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

Sorry for my terrible tired programming, I changed one line of code and now it works :P  I realized I was getting the world from Minecraft.getMinecraft() for literally no reason, *derp*

As for ticking entities, how would I go about doing that? TileEntities are confusing to me at the best of times. I'd like to be able to dynamically change the frequency of damage and damage amount based on how close players are. As for AABB's, I don't really understand those either. I feel stupid since I'm normally good at programming, but these things don't have enough comments in Minecraft and Forge's code

Link to comment
Share on other sites

7 hours ago, DrLogiq said:

As for ticking entities, how would I go about doing that? TileEntities are confusing to me at the best of times. I'd like to be able to dynamically change the frequency of damage and damage amount based on how close players are.

 

Create a class that extends TileEntity and implements net.minecraft.util.ITickable (not the ITickable in the client package). The implementation of the ITickable#update method will be called once per tick. You can store a counter in a field of the TileEntity and increment it each tick. When it reaches X (where X is some number you determine based on the proximity of nearby players), damage all nearby players and reset the counter.

 

You could calculate X every time the TileEntity damages nearby players or every tick, depending on how accurate you want it to be.

 

Register your TileEntity class with GameRegistry.registerTileEntity in preInit. Use your mod ID followed by a colon as a prefix for this name (e.g. "<yourmodid>:radiation_block"), since it will be converted to a ResourceLocation.

 

Override Block#hasTileEntity(IBlockState) to return true and override Block#createTileEntity to create an return a new instance of your TileEntity class.

 

 

7 hours ago, DrLogiq said:

As for AABB's, I don't really understand those either. I feel stupid since I'm normally good at programming, but these things don't have enough comments in Minecraft and Forge's code

 

An AABB (Axis Aligned Bounding Box) is just a box defined by two points in 3D space: the minimum x/y/z coordinates and the maximum x/y/z coordinates. World#getEntitiesWithinAABB simply returns a list of entities inside the AABB (i.e. entities whose own bounding boxes intersect the specified AABB).

  • Like 1

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

Thank you ever so much for your useful and well-formatted response. Yes, I've been trying to do this whilst extremely tired and to be honest, I didn't really have any idea what I was doing. But I've since slept properly and can think clearly enough to get the job done. I appreciate your help very very much. I'm almost ready to close this thread and mark it as resolved,but first I'm going to ensure everything's working properly so I can post the results for anyone who needs it in the future.

Edited by DrLogiq
Realized I derped
Link to comment
Share on other sites

So far, as a temporary solution before I change to this "AxisAlignedBB" nonsense, this is how I've achieved the ranged damage:

 

TileEntity class:

private final float RANGE = 150.0F;

@Override
	public void update()
	{
		if (!world.isRemote /*&& world.getMinecraftServer().getPlayerList().getCurrentPlayerCount() > 0*/)
		{			
			// TODO change to use AABB's
			
			PlayerList players = world.getMinecraftServer().getPlayerList();
			for (int i = 0; i < players.getCurrentPlayerCount(); i++)
			{
				EntityPlayerMP p = players.getPlayers().get(i);
				float distance = (float) pos.distanceSq(p.getPosition()); 
				if (distance <= RANGE)
				{
					float percent = 100.0F - ((distance / RANGE) * 100.0F);
					Random rand = new Random();
					
					float a = ((distance / RANGE) * 100.0F) / 100;
					int randomInt = (int) (a * rand.nextInt(30)); 
					
					if (randomInt < 0) randomInt = 1;
					if (randomInt == 0)
					{							
						p.attackEntityFrom(Factori.radiation, 0.5F);
					}
				}
			}
		}
	}

 

Link to comment
Share on other sites

PlayerList#getPlayers returns the players in every dimension, so your current code would damage players in any dimension if they happened to be close to the coordinates of the block.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.