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

Started by Radikid on

Topic category: Help with Minecraft modding (Java Edition)

Active 5 months ago
Joined Mar 2020
Points:
717

User statistics:

  • Modifications: 1
  • Forum topics: 11
  • Wiki pages: 0
  • MCreator plugins: 0
  • Comments: 32
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!

Active 4 months ago
Joined Jul 2021
Points:
705

User statistics:

  • Modifications: 3
  • Forum topics: 12
  • Wiki pages: 0
  • MCreator plugins: 0
  • Comments: 127
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.

Active 2 years ago
Joined Sep 2023
Points:
197

User statistics:

  • Modifications: 0
  • Forum topics: 2
  • Wiki pages: 0
  • MCreator plugins: 0
  • Comments: 10
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 !

 

Active 2 years ago
Joined Sep 2023
Points:
197

User statistics:

  • Modifications: 0
  • Forum topics: 2
  • Wiki pages: 0
  • MCreator plugins: 0
  • Comments: 10
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);
}
}
}
}