Skip to content

Commit

Permalink
add matchAnyInventorySlot and matchAnyHotbarSlot
Browse files Browse the repository at this point in the history
  • Loading branch information
LLytho committed Oct 27, 2024
1 parent 2061d4d commit 89c2710
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 2 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
- /

- Added `matchAnyInventorySlot(itemFilter)` and `matchAnyHotbarSlot(itemFilter)`

## [3.1.2] - 2024-10-14

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ license = GNU Lesser General Public License v3.0
# Mod options
modId = lootjs
modName = LootJS
modVersion = 3.1.2
modVersion = 3.2.2
modAuthor = AlmostReliable
modDescription = Modify loot through KubeJS.

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/almostreliable/lootjs/LootJSConditions.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.almostreliable.lootjs;

import com.almostreliable.lootjs.core.filters.ItemFilter;
import com.almostreliable.lootjs.loot.condition.*;
import com.mojang.serialization.MapCodec;
import net.minecraft.advancements.critereon.DistancePredicate;
Expand Down Expand Up @@ -27,6 +28,9 @@ private static LootItemConditionType create(LootItemCondition unit) {

public static Holder<LootItemConditionType> MATCH_EQUIP = CONDITIONS.register("match_equip",
() -> create(new MatchEquipmentSlot()));
public static Holder<LootItemConditionType> MATCH_ANY_INVENTORY_SLOT = CONDITIONS.register(
"match_any_inventory_slot",
() -> create(new MatchAnyInventorySlot(ItemFilter.NONE, false)));
public static Holder<LootItemConditionType> DISTANCE = CONDITIONS.register("match_distance",
() -> create(new MatchKillerDistance(DistancePredicate.vertical(MinMaxBounds.Doubles.ANY))));
public static Holder<LootItemConditionType> ANY_STRUCTURE = CONDITIONS.register("match_structure",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ default C matchEquip(EquipmentSlot slot, ItemFilter filter) {
return addCondition(new MatchEquipmentSlot(slot, filter));
}

default C matchAnyInventorySlot(ItemFilter filter) {
return addCondition(new MatchAnyInventorySlot(filter, false));
}

default C matchAnyHotbarSlot(ItemFilter filter) {
return addCondition(new MatchAnyInventorySlot(filter, true));
}

default C survivesExplosion() {
return addCondition(ExplosionCondition.survivesExplosion().build());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.almostreliable.lootjs.loot.condition;

import com.almostreliable.lootjs.LootJSConditions;
import com.almostreliable.lootjs.core.filters.ItemFilter;
import com.almostreliable.lootjs.util.LootContextUtils;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
import net.minecraft.world.level.storage.loot.predicates.LootItemConditionType;

public record MatchAnyInventorySlot(ItemFilter filter, boolean hotbar) implements LootItemCondition {

@Override
public boolean test(LootContext ctx) {
ServerPlayer player = LootContextUtils.getPlayerOrNull(ctx);
if (player == null) {
return false;
}

var inv = player.getInventory();
int max = hotbar ? 9 : 36;
for (int i = 0; i < max; i++) {
if (filter.test(inv.getItem(i))) {
return true;
}
}

return false;
}

@Override
public LootItemConditionType getType() {
return LootJSConditions.MATCH_ANY_INVENTORY_SLOT.value();
}
}

0 comments on commit 89c2710

Please sign in to comment.