Skip to content

Instantly share code, notes, and snippets.

@Jikoo
Created May 21, 2019 17:41
Show Gist options
  • Save Jikoo/c903747803ea19c593e67c30da634d55 to your computer and use it in GitHub Desktop.
Save Jikoo/c903747803ea19c593e67c30da634d55 to your computer and use it in GitHub Desktop.
LootTableTest plugin
name: LootTableTest
main: com.github.jikoo.TestPlugin
author: Jikoo
version: ${project.version}
api-version: 1.13
package com.github.jikoo;
import java.util.concurrent.ThreadLocalRandom;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeInstance;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.loot.LootContext;
import org.bukkit.loot.LootTable;
import org.bukkit.loot.Lootable;
import org.bukkit.plugin.java.JavaPlugin;
public class TestPlugin extends JavaPlugin implements Listener {
@Override
public void onEnable() {
getServer().getPluginManager().registerEvents(this, this);
}
@EventHandler
public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
if (!(event.getDamager() instanceof Player) || !(event.getEntity() instanceof Lootable)
|| !(event.getEntity() instanceof LivingEntity)) {
return;
}
LivingEntity entity = (LivingEntity) event.getEntity();
Lootable lootable = (Lootable) event.getEntity();
LootTable lootTable = lootable.getLootTable();
if (lootTable == null) {
return;
}
Player killer = (Player) event.getDamager();
LootContext.Builder contextBuilder = new LootContext.Builder(entity.getLocation()).lootedEntity(entity).killer(killer);
AttributeInstance attributeLuck = killer.getAttribute(Attribute.GENERIC_LUCK);
if (attributeLuck != null) {
contextBuilder.luck(((float) attributeLuck.getValue()));
}
contextBuilder.lootingModifier(killer.getInventory().getItemInMainHand().getEnchantmentLevel(Enchantment.LOOT_BONUS_MOBS));
LootContext context = contextBuilder.build();
for (ItemStack loot : lootTable.populateLoot(ThreadLocalRandom.current(), context)) {
entity.getWorld().dropItemNaturally(entity.getLocation(), loot);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment