Skip to content

Commit

Permalink
Improved customizability within inventoryComponent (slightly)
Browse files Browse the repository at this point in the history
  • Loading branch information
B1n-ry committed Feb 22, 2024
1 parent 49dedba commit b2776cc
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 12 deletions.
5 changes: 3 additions & 2 deletions src/main/java/com/b1n_ry/yigd/compat/CompatComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.util.math.Vec3d;

import java.util.function.Consumer;
import java.util.function.Predicate;

public abstract class CompatComponent<T> {
Expand Down Expand Up @@ -103,10 +104,10 @@ public boolean containsAny(Predicate<ItemStack> predicate) {
}
return false;
}
public void setDropRules(Predicate<ItemStack> predicate, DropRule dropRule) {
public void handleItemPairs(Predicate<ItemStack> predicate, Consumer<Pair<ItemStack, DropRule>> modification) {
for (Pair<ItemStack, DropRule> pair : this.getAsStackDropList()) {
if (predicate.test(pair.getLeft())) {
pair.setRight(dropRule);
modification.accept(pair);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/b1n_ry/yigd/compat/InvModCompat.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.b1n_ry.yigd.compat;

import com.b1n_ry.yigd.compat.miscCompatMods.CommonProtectionApiCompat;
import com.b1n_ry.yigd.compat.miscCompatMods.OrpheusCompat;
import com.b1n_ry.yigd.compat.misc_compat_mods.CommonProtectionApiCompat;
import com.b1n_ry.yigd.compat.misc_compat_mods.OrpheusCompat;
import com.b1n_ry.yigd.config.YigdConfig;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.nbt.NbtCompound;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.b1n_ry.yigd.compat.miscCompatMods;
package com.b1n_ry.yigd.compat.misc_compat_mods;

import com.b1n_ry.yigd.config.YigdConfig;
import com.b1n_ry.yigd.events.AllowBlockUnderGraveGenerationEvent;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.b1n_ry.yigd.compat.miscCompatMods;
package com.b1n_ry.yigd.compat.misc_compat_mods;

import com.b1n_ry.yigd.events.AdjustDropRuleEvent;
import com.b1n_ry.yigd.util.DropRule;
Expand All @@ -13,7 +13,7 @@ public static void init() {
if (!orpheusLyrePower.equals("keep") && !orpheusLyrePower.equals("both")) return;

if (inventory.containsAny(stack -> stack.isOf(ItemsRegistry.ORPHEUS_LYRE.get()), s -> true, i -> true)) {
inventory.setDropRules(stack -> true, s -> true, i -> true, DropRule.KEEP);
inventory.handleItemPairs(stack -> true, s -> true, i -> true, pair -> pair.setRight(DropRule.KEEP));
}
});
}
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/com/b1n_ry/yigd/components/InventoryComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import net.minecraft.util.math.Vec3d;

import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;

Expand Down Expand Up @@ -437,22 +438,23 @@ public boolean containsAny(Predicate<ItemStack> itemPredicate, Predicate<String>
}

/**
* Set drop rules for items in the component based on filters/predicates
* Executes code to modify items in the component based on predicate filters
* @param itemPredicate Items matching this are affected
* @param modPredicate Mods matching this are affected ("vanilla" for vanilla inventory)
* @param slotPredicate Slots matching this are affected (only applies to vanilla inventory)
* @param dropRule Drop rule to set
* @param modification Modification done to pair
*/
public void setDropRules(Predicate<ItemStack> itemPredicate, Predicate<String> modPredicate, Predicate<Integer> slotPredicate, DropRule dropRule) {
public void handleItemPairs(Predicate<ItemStack> itemPredicate, Predicate<String> modPredicate,
Predicate<Integer> slotPredicate, Consumer<Pair<ItemStack, DropRule>> modification) {
if (modPredicate.test("vanilla")) {
for (int i = 0; i < this.items.size(); i++) {
Pair<ItemStack, DropRule> pair = this.items.get(i);
if (slotPredicate.test(i) && itemPredicate.test(pair.getLeft())) pair.setRight(dropRule);
if (slotPredicate.test(i) && itemPredicate.test(pair.getLeft()))modification.accept(pair);
}
}
for (Map.Entry<String, CompatComponent<?>> entry : this.modInventoryItems.entrySet()) {
CompatComponent<?> compatComponent = entry.getValue();
if (modPredicate.test(entry.getKey())) compatComponent.setDropRules(itemPredicate, dropRule);
if (modPredicate.test(entry.getKey())) compatComponent.handleItemPairs(itemPredicate, modification);
}
}

Expand Down

0 comments on commit b2776cc

Please sign in to comment.