blob: d76fe60fbb230042bd7dd654b960e9487c574bff (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
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.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.populateLoot(random, nmsContext);
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().fillInventory(handle, random, nmsContext);
}
@Override
public NamespacedKey getKey() {
return key;
}
private LootTableInfo convertContext(LootContext context) {
Location loc = context.getLocation();
WorldServer handle = ((CraftWorld) loc.getWorld()).getHandle();
LootTableInfo.Builder builder = new LootTableInfo.Builder(handle);
builder.luck(context.getLuck());
if (context.getLootedEntity() != null) {
Entity nmsLootedEntity = ((CraftEntity) context.getLootedEntity()).getHandle();
builder.entity(nmsLootedEntity);
builder.damageSource(DamageSource.GENERIC);
builder.position(new BlockPosition(nmsLootedEntity));
}
if (context.getKiller() != null) {
EntityHuman nmsKiller = ((CraftHumanEntity) context.getKiller()).getHandle();
builder.killer(nmsKiller);
// If there is a player killer, damage source should reflect that in case loot tables use that information
builder.damageSource(DamageSource.playerAttack(nmsKiller));
}
return builder.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());
}
}
|