Jump to content

Recommended Posts

Posted

I am attempting to detect when a mob hits something and then check to see if the Entity is listed in an external file.I am wondering how I would do this. This is what I have currently got,
Detect Entity attacking:
 

//Mob attacking
	@SubscribeEvent
	public static void testEntityEvent(LivingHurtEvent event)
	{
		DamageSource damageSource = event.getSource();
		if (damageSource.getTrueSource() instanceof MobEntity)
        { 
			Item air = Item.getItemByID(0);
          	LivingEntity livingEntity = (LivingEntity) damageSource.getTrueSource();
			ItemStack stack = livingEntity.getHeldItemMainhand();
			Item item = stack.getItem();
			if (item != air) 
			{
				System.out.println(DamageType.getFromItem(item));
			}
			
			else
			{
				EntityType<?> entity = damageSource.getTrueSource().getEntity();
				System.out.println(DamageType.getFromEntity(entity));
			}
		}

This is how I check DamageType:
 

@Nonnull
	public static DamageType getFromEntity(EntityType<?> entity)
	{
		if (slashingDamageEntities.contains(entity))
		{
			return SLASHING;
		}
		else if (piercingDamageEntities.contains(entity))
		{
			return PIERCING;
		}
		
		return BLUDGEONING;
	}

And this is how I register the Resource Location:
 

public static final EntityTypeTags.Wrapper slashingDamageEntities = new EntityTypeTags.Wrapper(new ResourceLocation(dungeoncraft.MOD_ID, "slashing_damage_mobs"));
public static final EntityTypeTags.Wrapper piercingDamageEntities = new EntityTypeTags.Wrapper(new ResourceLocation(dungeoncraft.MOD_ID, "piercing_damage_mobs"));


Now I am aware that I am checking the Tags for EntityType<?> instead of Entity but I am wondering how to do it using an Entity instead. Or a better way to do it using EntityType<?> by modifying the first file.

For reference this is my Tags file and the other is very similar.
 

{
	"replace": false,
	"values": [
		"minecraft:chicken",
		"minecraft:fox",
		"minecraft:parrot",
		"minecraft:pufferfish",
		"minecraft:turtle",
		"minecraft:bee",
		"minecraft:wolf",
		"minecraft:cave_spider",
		"minecraft:spider",
		"minecraft:elder_guardian",
		"minecraft:endermite",
		"minecraft:guardian",
		"minecraft:phantom",
		"minecraft:shulker",
		"minecraft:silverfish"
	]
}

 

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.