Can an entity only drop items when killed by players? READ DESC

Started by Radikid on

Topic category: Help with modding (Java Edition)

Last seen on 23:30, 25. Apr 2022
Joined Mar 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Can an entity only drop items when killed by players? READ DESC

I looked for a way to make my mob only drop items when it was killed by the player, but I couldn't figure out how to do this. Does anyone have any ideas or know if this is even possible? It would be even better if I could trigger a loot table when an entity dies but only when a player kills it. My entities are hostile to each other normally and I don't want the act of them killing each other to litter the ground with free items.

 

Thanks!

Last seen on 01:59, 29. Mar 2024
Joined Jul 2021
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
You could make a procedure …
Sun, 09/05/2021 - 22:24

You could make a procedure "When entity dies", then it checks if the source entity is player type, and if it is, then it'll just drop the items you want it to drop.

Last seen on 18:37, 16. Sep 2023
Joined Sep 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Sorry to "excavate" this old…
Tue, 09/05/2023 - 18:26

Sorry to "excavate" this old thread, but this is exact the problem I'm just stuck at: all I want is a global rule, that Mobs / entitites only drop loot according to their respective loot tables, when they are killed by a player (global anti-auto-farm control).

I found this :

but I'm struggling at the "do", how to link to the respective loot table of the entity ?

Any hint would be much appreciated !

cheers !

 

Last seen on 18:37, 16. Sep 2023
Joined Sep 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
picture link didn't work,…
Tue, 09/05/2023 - 18:29

picture link didn't work, this is the procedure so far:

@Mod.EventBusSubscriber

public class KillByPlayerProcedure {
@SubscribeEvent
public static void onEntityDeath(LivingDeathEvent event) {
if (event != null && event.getEntity() != null) {
execute(event, event.getEntity().level, event.getEntity().getX(), event.getEntity().getY(), event.getEntity().getZ(), event.getSource().getEntity());
}
}
public static void execute(LevelAccessor world, double x, double y, double z, Entity sourceentity) {
execute(null, world, x, y, z, sourceentity);
}
private static void execute(@Nullable Event event, LevelAccessor world, double x, double y, double z, Entity sourceentity) {
if (sourceentity == null)
return;
if (sourceentity instanceof Player) {
if (world instanceof Level _level && !_level.isClientSide()) {
ItemEntity entityToSpawn = new ItemEntity(_level, x, y, z, new ItemStack());
entityToSpawn.setPickUpDelay(10);
_level.addFreshEntity(entityToSpawn);
}
}
}
}