Skip to content

Commit

Permalink
Fix loot modification being skipped when loot table doesn't contain a…
Browse files Browse the repository at this point in the history
…ny entries
  • Loading branch information
LLytho committed Oct 14, 2024
1 parent 6b46056 commit bee2baf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ The format is based on [Keep a Changelog],
and this project adheres to [Semantic Versioning].

## Unreleased
- /

- Fix loot modification being skipped when loot table doesn't contain any entries

## [3.1.1] - 2024-08-25

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
public class GroupedLootAction implements LootAction {

private final List<LootAction> actions;
private final ItemFilter containsLootFilter;
@Nullable private final ItemFilter containsLootFilter;
private final boolean exact;
private final Predicate<LootContext> compositeCondition;
private final BiFunction<ItemStack, LootContext, ItemStack> compositeFunction;
private final NumberProvider rolls;
private final List<LootItemFunction> functions;

public GroupedLootAction(NumberProvider rolls, List<LootItemCondition> conditions, List<LootItemFunction> functions, Collection<LootAction> actions, ItemFilter containsLootFilter, boolean exact) {
public GroupedLootAction(NumberProvider rolls, List<LootItemCondition> conditions, List<LootItemFunction> functions, Collection<LootAction> actions, @Nullable ItemFilter containsLootFilter, boolean exact) {
this.rolls = rolls;
this.compositeCondition = Util.allOf(List.copyOf(conditions));
this.functions = List.copyOf(functions);
Expand All @@ -43,8 +43,7 @@ public GroupedLootAction(NumberProvider rolls, List<LootItemCondition> condition

@Override
public void apply(LootContext context, LootBucket loot) {
boolean containsLoot = exact ? matchExact(loot) : match(loot);
if (!containsLoot) {
if (!matchLoot(loot)) {
return;
}

Expand All @@ -64,7 +63,15 @@ public void apply(LootContext context, LootBucket loot) {
}
}

private boolean matchLoot(LootBucket loot) {
return exact ? matchExact(loot) : match(loot);
}

private boolean match(LootBucket loot) {
if (containsLootFilter == null) {
return true;
}

for (ItemStack itemStack : loot) {
if (containsLootFilter.test(itemStack)) {
return true;
Expand All @@ -75,6 +82,10 @@ private boolean match(LootBucket loot) {
}

private boolean matchExact(LootBucket loot) {
if (containsLootFilter == null) {
return true;
}

for (ItemStack itemStack : loot) {
if (!containsLootFilter.test(itemStack)) {
return false;
Expand All @@ -88,7 +99,7 @@ public static class Filtered extends GroupedLootAction {

private final ItemFilter lootItemsFilter;

public Filtered(NumberProvider rolls, List<LootItemCondition> conditions, List<LootItemFunction> functions, Collection<LootAction> actions, ItemFilter lootItemsFilter, ItemFilter containsLootFilter, boolean exact) {
public Filtered(NumberProvider rolls, List<LootItemCondition> conditions, List<LootItemFunction> functions, Collection<LootAction> actions, ItemFilter lootItemsFilter, @Nullable ItemFilter containsLootFilter, boolean exact) {
super(rolls, conditions, functions, actions, containsLootFilter, exact);
this.lootItemsFilter = lootItemsFilter;
}
Expand All @@ -101,18 +112,16 @@ public void apply(LootContext context, LootBucket loot) {
}
}

public static class Builder implements LootConditionsContainer<Builder>,
LootFunctionsContainer<Builder>,
LootActionContainer<Builder> {
public static class Builder
implements LootConditionsContainer<Builder>, LootFunctionsContainer<Builder>, LootActionContainer<Builder> {

protected final List<LootAction> actions = new ArrayList<>();
protected final List<LootItemCondition> conditions = new ArrayList<>();
protected final List<LootItemFunction> functions = new ArrayList<>();
protected NumberProvider rolls = ConstantValue.exactly(1f);
protected ItemFilter containsLootFilter = ItemFilter.ANY;
@Nullable protected ItemFilter containsLootFilter = null;
protected boolean exact = false;
@Nullable
private final ItemFilter lootItemsFilter;
@Nullable private final ItemFilter lootItemsFilter;

public Builder(ItemFilter lootItemsFilter) {
this.lootItemsFilter = lootItemsFilter;
Expand Down

0 comments on commit bee2baf

Please sign in to comment.