Commits
Doc authored and md_5 committed e0a6aa36e8b
34 34 | this.handle = handle; |
35 35 | this.key = key; |
36 36 | } |
37 37 | |
38 38 | public LootTable getHandle() { |
39 39 | return handle; |
40 40 | } |
41 41 | |
42 42 | |
43 43 | public Collection<ItemStack> populateLoot(Random random, LootContext context) { |
44 - | LootTableInfo nmsContext = convertContext(context); |
44 + | LootTableInfo nmsContext = convertContext(context, random); |
45 45 | List<net.minecraft.world.item.ItemStack> nmsItems = handle.getRandomItems(nmsContext); |
46 46 | Collection<ItemStack> bukkit = new ArrayList<>(nmsItems.size()); |
47 47 | |
48 48 | for (net.minecraft.world.item.ItemStack item : nmsItems) { |
49 49 | if (item.isEmpty()) { |
50 50 | continue; |
51 51 | } |
52 52 | bukkit.add(CraftItemStack.asBukkitCopy(item)); |
53 53 | } |
54 54 | |
55 55 | return bukkit; |
56 56 | } |
57 57 | |
58 58 | |
59 59 | public void fillInventory(Inventory inventory, Random random, LootContext context) { |
60 - | LootTableInfo nmsContext = convertContext(context); |
60 + | LootTableInfo nmsContext = convertContext(context, random); |
61 61 | CraftInventory craftInventory = (CraftInventory) inventory; |
62 62 | IInventory handle = craftInventory.getInventory(); |
63 63 | |
64 64 | // TODO: When events are added, call event here w/ custom reason? |
65 65 | getHandle().fill(handle, nmsContext); |
66 66 | } |
67 67 | |
68 68 | |
69 69 | public NamespacedKey getKey() { |
70 70 | return key; |
71 71 | } |
72 72 | |
73 - | private LootTableInfo convertContext(LootContext context) { |
73 + | private LootTableInfo convertContext(LootContext context, Random random) { |
74 74 | Location loc = context.getLocation(); |
75 75 | WorldServer handle = ((CraftWorld) loc.getWorld()).getHandle(); |
76 76 | |
77 77 | LootTableInfo.Builder builder = new LootTableInfo.Builder(handle); |
78 + | if (random != null) { |
79 + | builder = builder.withRandom(random); |
80 + | } |
78 81 | setMaybe(builder, LootContextParameters.ORIGIN, new Vec3D(loc.getX(), loc.getY(), loc.getZ())); |
79 82 | if (getHandle() != LootTable.EMPTY) { |
80 83 | // builder.luck(context.getLuck()); |
81 84 | |
82 85 | if (context.getLootedEntity() != null) { |
83 86 | Entity nmsLootedEntity = ((CraftEntity) context.getLootedEntity()).getHandle(); |
84 87 | setMaybe(builder, LootContextParameters.THIS_ENTITY, nmsLootedEntity); |
85 88 | setMaybe(builder, LootContextParameters.DAMAGE_SOURCE, DamageSource.GENERIC); |
86 89 | setMaybe(builder, LootContextParameters.ORIGIN, nmsLootedEntity.position()); |
87 90 | } |