Jump to content

[1.16] Finding source of a projectileEntity


Tavi007

Recommended Posts

Hey all,

 
I'm trying to locate the source of any projectileEntity (so I can set some data on the projectile). Please take a look at my current 'solution'. It does work, but I have a bunch of problems, that I would like to discuss.


@Mod.EventBusSubscriber(modid = ElementalCombat.MOD_ID, bus = Bus.FORGE)
public class ElementifyProjectileSpawnEvent {

	@SubscribeEvent
	public static void elementifyProjectileSpawnEvent(EntityJoinWorldEvent event)
	{
		if(!event.getWorld().isRemote()){ // only server side should check
			Entity entity = event.getEntity();
			//check if entity is a new spawning projectile.
			if(entity instanceof ProjectileEntity)
			{
				if(entity.ticksExisted == 0)
				{
					System.out.println("Newly spawned projectile found");
					
					ProjectileEntity projectile = (ProjectileEntity) entity;
					
					//find livingEntity source. (aka the nearest LivingEntity, that isn't the spawning projectzileEntity? What about dispensers?)
					Vector3d pos = projectile.getPositionVec();
					AxisAlignedBB boundingBox = new AxisAlignedBB(pos.x, pos.y, pos.z, pos.x, pos.y, pos.z); 
					List<Entity> nearbyEntities = event.getWorld().getEntitiesWithinAABBExcludingEntity(projectile, boundingBox);
					System.out.println("Entity in List: "+ nearbyEntities);
					
					//check if only one entity was found. This should be the sourceEntity
					//if List is empty, then source is a TileEntity (i.e Dispenser). 
					if(nearbyEntities.size() == 1)
					{
						LivingEntity sourceEntity = (LivingEntity) nearbyEntities.get(0);
						
						// If sourceEntity holds an item, use item data.
						// If not, use data from sourceEntity as default.
						IElementalAttackData sourceData;
						if(sourceEntity.hasItemInSlot(EquipmentSlotType.MAINHAND)){
							ItemStack item = sourceEntity.getActiveItemStack();
							sourceData = ElementalCombatAPI.getElementalAttackData(item);
						}
						else{
							sourceData = ElementalCombatAPI.getElementalAttackData(sourceEntity);
						}
						
						//copy elemental attack capability
						IElementalAttackData projectileData = ElementalCombatAPI.getElementalAttackData(projectile);
						projectileData.setAttackMap(sourceData.getAttackMap());
					}
				}
			}
		}
	}
}

The idea was to check the position of a projectile the second it's spawning to figure out the source. This does usually work, but in some cases unintended behaviour occurs:

  1. Projectiles shot from Dispensers usually returns an empty list (as it should be). But if an entity stands right in front of the dispenser, the list will be filled with that entity.
  2. If the position of the new projectile is not within the hitbox of the sourceEntity, the list will stay empty. This happens for example for Llama spit. In this case, the list should include the Llama Entity, but it doesn't.
  3. If sourceEntity is crammed into a hole with other entities, the list will be filled with these other entities aswell. Therefor it is not possible to identify the right sourceEntity. (Put a bunch of creepers into a 1x1 hole and then shot an arrow from within that hole. The list will include some of the creepers, since their hitbox overlaps with the player-hitbox)
  4. When joining a world with projectileEntities laying on the ground, the game will be stuck on getEntitiesWithinAABBExcludingEntity() function (happens in Singleplayer. Multiplayer is not tested yet). I have no idea, why this happens.

 

Trying to figure out the source of the projectile through hitbox comparison, isn't really an elegant solution anyway. Does anyone know of a better method or a fix for my problems?

 

PS: here is my repo:

https://github.com/Tavi007/ElementalCombat

 

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



×
×
  • Create New...

Important Information

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