Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixup luck and random implementation in CB loot-tables #11926

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,20 @@
output.accept(itemStack);
} else {
int count = itemStack.getCount();
@@ -141,9 +_,22 @@
@@ -141,9 +_,21 @@
}

public void fill(Container container, LootParams params, long seed) {
- LootContext lootContext = new LootContext.Builder(params).withOptionalRandomSeed(seed).create(this.randomSequence);
+ // CraftBukkit start
+ this.fillInventory(container, params, seed, false);
+ this.fillInventory(container, params, RandomSource.create(seed), false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not completely equivalent, a seed of 0 still produce random loots.

+ }
+
+ public void fillInventory(Container container, LootParams params, long seed, boolean plugin) {
+ public void fillInventory(Container container, LootParams params, RandomSource random, boolean plugin) {
+ // CraftBukkit end
LootContext lootContext = new LootContext.Builder(params).withOptionalRandomSeed(seed).create(this.randomSequence);
+ LootContext lootContext = new LootContext.Builder(params).withOptionalRandomSource(random).create(this.randomSequence);
ObjectArrayList<ItemStack> randomItems = this.getRandomItems(lootContext);
RandomSource random = lootContext.getRandom();
- RandomSource random = lootContext.getRandom();
+ // CraftBukkit start
+ org.bukkit.event.world.LootGenerateEvent event = org.bukkit.craftbukkit.event.CraftEventFactory.callLootGenerateEvent(container, this, lootContext, randomItems, plugin);
+ if (event.isCancelled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.bukkit.craftbukkit.inventory.CraftItemStack;
import org.bukkit.craftbukkit.util.CraftLocation;
import org.bukkit.craftbukkit.util.CraftNamespacedKey;
import org.bukkit.craftbukkit.util.RandomSourceWrapper;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.loot.LootContext;
Expand Down Expand Up @@ -68,8 +69,8 @@ public LootTable getHandle() {
@Override
public Collection<ItemStack> populateLoot(Random random, LootContext context) {
Preconditions.checkArgument(context != null, "LootContext cannot be null");
LootParams nmsContext = this.convertContext(context, random);
List<net.minecraft.world.item.ItemStack> nmsItems = this.handle.getRandomItems(nmsContext);
LootParams nmsContext = this.convertContext(context);
List<net.minecraft.world.item.ItemStack> nmsItems = this.handle.getRandomItems(nmsContext, new RandomSourceWrapper(random));
Collection<ItemStack> bukkit = new ArrayList<>(nmsItems.size());

for (net.minecraft.world.item.ItemStack item : nmsItems) {
Expand All @@ -86,32 +87,29 @@ public Collection<ItemStack> populateLoot(Random random, LootContext context) {
public void fillInventory(Inventory inventory, Random random, LootContext context) {
Preconditions.checkArgument(inventory != null, "Inventory cannot be null");
Preconditions.checkArgument(context != null, "LootContext cannot be null");
LootParams nmsContext = this.convertContext(context, random);
LootParams nmsContext = this.convertContext(context);
CraftInventory craftInventory = (CraftInventory) inventory;
Container handle = craftInventory.getInventory();

// TODO: When events are added, call event here w/ custom reason?
this.getHandle().fillInventory(handle, nmsContext, random.nextLong(), true);
this.getHandle().fillInventory(handle, nmsContext, new RandomSourceWrapper(random), true);
}

@Override
public NamespacedKey getKey() {
return this.key;
}

private LootParams convertContext(LootContext context, Random random) {
private LootParams convertContext(LootContext context) {
Preconditions.checkArgument(context != null, "LootContext cannot be null");
Location loc = context.getLocation();
Preconditions.checkArgument(loc.getWorld() != null, "LootContext.getLocation#getWorld cannot be null");
ServerLevel handle = ((CraftWorld) loc.getWorld()).getHandle();

LootParams.Builder builder = new LootParams.Builder(handle);
if (random != null) {
// builder = builder.withRandom(new RandomSourceWrapper(random));
}
this.setMaybe(builder, LootContextParams.ORIGIN, CraftLocation.toVec3D(loc));
if (this.getHandle() != LootTable.EMPTY) {
// builder.luck(context.getLuck());
builder.withLuck(context.getLuck());

if (context.getLootedEntity() != null) {
Entity nmsLootedEntity = ((CraftEntity) context.getLootedEntity()).getHandle();
Expand Down
Loading