SPIGOT-1936: LootTable API

By: Senmori <thesenmori@gmail.com>
This commit is contained in:
CraftBukkit/Spigot
2018-08-12 18:39:51 +10:00
parent 277f0db6c9
commit 39c4334d76
15 changed files with 434 additions and 21 deletions

View File

@@ -0,0 +1,109 @@
package org.bukkit.craftbukkit;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Random;
import net.minecraft.server.BlockPosition;
import net.minecraft.server.DamageSource;
import net.minecraft.server.Entity;
import net.minecraft.server.EntityHuman;
import net.minecraft.server.IInventory;
import net.minecraft.server.LootTable;
import net.minecraft.server.LootTableInfo;
import net.minecraft.server.WorldServer;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.craftbukkit.entity.CraftEntity;
import org.bukkit.craftbukkit.entity.CraftHumanEntity;
import org.bukkit.craftbukkit.inventory.CraftInventory;
import org.bukkit.craftbukkit.inventory.CraftItemStack;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.loot.LootContext;
public class CraftLootTable implements org.bukkit.loot.LootTable {
private final LootTable handle;
private final NamespacedKey key;
public CraftLootTable(NamespacedKey key, LootTable handle) {
this.handle = handle;
this.key = key;
}
public LootTable getHandle() {
return handle;
}
@Override
public Collection<ItemStack> populateLoot(Random random, LootContext context) {
LootTableInfo nmsContext = convertContext(context);
List<net.minecraft.server.ItemStack> nmsItems = handle.a(random, nmsContext); // PAIL rename populateLoot
Collection<ItemStack> bukkit = new ArrayList<>(nmsItems.size());
for (net.minecraft.server.ItemStack item : nmsItems) {
if (item.isEmpty()) {
continue;
}
bukkit.add(CraftItemStack.asBukkitCopy(item));
}
return bukkit;
}
@Override
public void fillInventory(Inventory inventory, Random random, LootContext context) {
LootTableInfo nmsContext = convertContext(context);
CraftInventory craftInventory = (CraftInventory) inventory;
IInventory handle = craftInventory.getInventory();
// TODO: When events are added, call event here w/ custom reason?
getHandle().a(handle, random, nmsContext); // PAIL rename fillInventory
}
@Override
public NamespacedKey getKey() {
return key;
}
private LootTableInfo convertContext(LootContext context) {
Location loc = context.getLocation();
WorldServer handle = ((CraftWorld) loc.getWorld()).getHandle();
LootTableInfo.a builder = new LootTableInfo.a(handle); // PAIL rename Builder
builder.a(context.getLuck()); // PAIL rename withLuck, luck
if (context.getLootedEntity() != null) {
Entity nmsLootedEntity = ((CraftEntity) context.getLootedEntity()).getHandle();
builder.a(nmsLootedEntity); // PAIL Rename withLootedEntity, lootedEntity
builder.a(DamageSource.GENERIC); // PAIL rename withDamageSource, damageSource
builder.a(new BlockPosition(nmsLootedEntity)); // PAIL rename withPosition, position
}
if (context.getKiller() != null) {
EntityHuman nmsKiller = ((CraftHumanEntity) context.getKiller()).getHandle();
builder.a(nmsKiller); // PAIL rename withKiller, killer
// If there is a player killer, damage source should reflect that in case loot tables use that information
builder.a(DamageSource.playerAttack(nmsKiller)); // PAIL rename withDamageSource, damageSource
}
return builder.a(); // PAIL rename build
}
@Override
public String toString() {
return getKey().toString();
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof org.bukkit.loot.LootTable)) {
return false;
}
org.bukkit.loot.LootTable table = (org.bukkit.loot.LootTable) obj;
return table.getKey().equals(this.getKey());
}
}