Skip to content
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 @@ -11,6 +11,7 @@
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadLocalRandom;

import javax.annotation.Nonnull;

Expand Down Expand Up @@ -38,7 +39,6 @@
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
import io.github.thebusybiscuit.slimefun4.implementation.guide.CheatSheetSlimefunGuide;
import io.github.thebusybiscuit.slimefun4.implementation.guide.SurvivalSlimefunGuide;

import me.mrCookieSlime.Slimefun.api.BlockInfoConfig;
import me.mrCookieSlime.Slimefun.api.BlockStorage;
import me.mrCookieSlime.Slimefun.api.inventory.BlockMenuPreset;
Expand Down Expand Up @@ -282,6 +282,19 @@ public Set<ItemStack> getBarteringDrops() {
return barterDrops;
}

/**
* Returns a shuffled snapshot of the current bartering drops to avoid
* iteration order bias when evaluating random chances.
*
* @return A shuffled List of bartering drops
*/
@Nonnull
public List<ItemStack> getRandomizedBarteringDrops() {
List<ItemStack> list = new ArrayList<>(barterDrops);
Collections.shuffle(list, ThreadLocalRandom.current());
return list;
}

@Nonnull
public Set<SlimefunItem> getRadioactiveItems() {
return radioactive;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.github.thebusybiscuit.slimefun4.implementation.listeners.entity;

import java.util.Set;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

import javax.annotation.Nonnull;
Expand Down Expand Up @@ -78,21 +78,12 @@ public void onInteract(PlayerInteractEntityEvent e) {
@EventHandler
public void onPiglinDropItem(EntityDropItemEvent e) {
if (e.getEntity() instanceof Piglin) {
Set<ItemStack> drops = Slimefun.getRegistry().getBarteringDrops();

/*
* NOTE: Getting a new random number each iteration because multiple items could have the same
* % chance to drop, and if one fails all items with that number will fail.
* Getting a new random number will allow multiple items with the same % chance to drop.
*/

List<ItemStack> drops = Slimefun.getRegistry().getRandomizedBarteringDrops();

for (ItemStack is : drops) {
SlimefunItem sfi = SlimefunItem.getByItem(is);
// Check the getBarteringLootChance and compare against a random number 0-100,
// if the random number is greater then replace the item.
if (sfi instanceof PiglinBarterDrop piglinBarterDrop) {
int chance = piglinBarterDrop.getBarteringLootChance();

if (chance < 1 || chance >= 100) {
sfi.warn("The Piglin Bartering chance must be between 1-99% on item: " + sfi.getId());
} else if (chance > ThreadLocalRandom.current().nextInt(100)) {
Expand Down
Loading