Jump to content

(1.11.2) [Unresolved/Open] attackEntityFrom not working


DrLogiq

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
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
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.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Baba  Serege [[+27-73 590 8989]] has experience of 27 years in helping and guiding many people from all over the world. His psychic abilities may help you answer and resolve many unanswered questions. He specialize in helping women and men from all walks of life.. 1) – Bring back lost lover. even if lost for a long time. 2) – My lover is abusing alcohol, partying and cheating on me I urgently need help” 3) – Divorce or court issues. 4) – Is your love falling apart? 5) – Do you want your love to grow stronger? 6) – Is your partner losing interest in you? 7) – Do you want to catch your partner cheating on you? – We help to keep your partner faithful and loyal to you. 9) – We recover love and happiness when relationship breaks down. 10) – Making your partner loves you alone. 11) – We create loyalty and everlasting love between couples. 12) – Get a divorce settlement quickly from your ex-partner. 13) – We create everlasting love between couples. 14) – We help you look for the best suitable partner. 15) – We bring back lost lover even if lost for a long time. 16) – We strengthen bonds in all love relationship and marriages 17) – Are you an herbalist who wants to get more powers? 18) – Buy a house or car of your dream. 19) – Unfinished jobs by other doctors come to me. 20) – I help those seeking employment. 21) – Pensioners free treatment. 22) – Win business tenders and contracts. 23) – Do you need to recover your lost property? 24) – Promotion at work and better pay. 25) – Do you want to be protected from bad spirits and nightmares? 26) – Financial problems. 27) – Why you can’t keep money or lovers? 28) – Why you have a lot of enemies? 29) – Why you are fired regularly on jobs? 30) – Speed up money claim spell, delayed payments, pension and accident funds 31) – I help students pass their exams/interviews. 33) – Removal of bad luck and debts. 34) – Are struggling to sleep because of a spiritual wife or husband. 35- ) Recover stolen property
    • OLXTOTO adalah situs bandar togel online resmi terbesar dan terpercaya di Indonesia. Bergabunglah dengan OLXTOTO dan nikmati pengalaman bermain togel yang aman dan terjamin. Koleksi toto 4D dan togel toto terlengkap di OLXTOTO membuat para member memiliki pilihan taruhan yang lebih banyak. Sebagai situs togel terpercaya, OLXTOTO menjaga keamanan dan kenyamanan para membernya dengan sistem keamanan terbaik dan enkripsi data. Transaksi yang cepat, aman, dan terpercaya merupakan jaminan dari OLXTOTO. Nikmati layanan situs toto terbaik dari OLXTOTO dengan tampilan yang user-friendly dan mudah digunakan. Layanan pelanggan tersedia 24/7 untuk membantu para member. Bergabunglah dengan OLXTOTO sekarang untuk merasakan pengalaman bermain togel yang menyenangkan dan menguntungkan.
    • Baba  Serege [[+27-73 590 8989]] has experience of 27 years in helping and guiding many people from all over the world. His psychic abilities may help you answer and resolve many unanswered questions. He specialize in helping women and men from all walks of life.. 1) – Bring back lost lover. even if lost for a long time. 2) – My lover is abusing alcohol, partying and cheating on me I urgently need help” 3) – Divorce or court issues. 4) – Is your love falling apart? 5) – Do you want your love to grow stronger? 6) – Is your partner losing interest in you? 7) – Do you want to catch your partner cheating on you? – We help to keep your partner faithful and loyal to you. 9) – We recover love and happiness when relationship breaks down. 10) – Making your partner loves you alone. 11) – We create loyalty and everlasting love between couples. 12) – Get a divorce settlement quickly from your ex-partner. 13) – We create everlasting love between couples. 14) – We help you look for the best suitable partner. 15) – We bring back lost lover even if lost for a long time. 16) – We strengthen bonds in all love relationship and marriages 17) – Are you an herbalist who wants to get more powers? 18) – Buy a house or car of your dream. 19) – Unfinished jobs by other doctors come to me. 20) – I help those seeking employment. 21) – Pensioners free treatment. 22) – Win business tenders and contracts. 23) – Do you need to recover your lost property? 24) – Promotion at work and better pay. 25) – Do you want to be protected from bad spirits and nightmares? 26) – Financial problems. 27) – Why you can’t keep money or lovers? 28) – Why you have a lot of enemies? 29) – Why you are fired regularly on jobs? 30) – Speed up money claim spell, delayed payments, pension and accident funds 31) – I help students pass their exams/interviews. 33) – Removal of bad luck and debts. 34) – Are struggling to sleep because of a spiritual wife or husband. 35- ) Recover stolen property
    • BD303 merupakan salah satu situs slot mudah scatter paling populer dan digemari oleh kalangan slot online di tahun 2024 mainkan sekarang dengan kesempatan yang mudah menang jackpot jutaan rupiah.
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.