forked from reo-ar/InvTweaksRenewed
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Updated default configs. Delete the old one when updating the mod.
- Added support for regex in screen name matching - Better replacement of broken tools
- Loading branch information
Showing
13 changed files
with
391 additions
and
357 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package invtweaks.config; | ||
|
||
import com.electronwill.nightconfig.core.CommentedConfig; | ||
import invtweaks.InvTweaksMod; | ||
import net.minecraft.ResourceLocationException; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.tags.TagKey; | ||
import net.minecraft.world.item.BlockItem; | ||
import net.minecraft.world.item.Item; | ||
import net.minecraft.world.item.ItemStack; | ||
import net.minecraft.world.level.block.Block; | ||
import net.minecraftforge.registries.ForgeRegistries; | ||
|
||
import java.util.*; | ||
import java.util.function.Predicate; | ||
import java.util.stream.IntStream; | ||
|
||
public class Category | ||
{ | ||
private final List<String> spec; | ||
private final List<List<Predicate<ItemStack>>> compiledSpec = new ArrayList<>(); | ||
|
||
public Category(List<String> spec) { | ||
this.spec = spec; | ||
for (String subspec : spec) { | ||
List<Predicate<ItemStack>> compiledSubspec = new ArrayList<>(); | ||
for (String clause : subspec.split("\\s*;\\s*")) { | ||
compileClause(clause).ifPresent(compiledSubspec::add); | ||
} | ||
compiledSpec.add(compiledSubspec); | ||
} | ||
} | ||
|
||
public Category(String... spec) { | ||
this(Arrays.asList(spec)); | ||
} | ||
|
||
private static Optional<Predicate<ItemStack>> compileClause(String clause) { | ||
if (clause.startsWith("!")) { | ||
return compileClause(clause.substring(1)).map(Predicate::negate); | ||
} | ||
|
||
String[] parts = clause.split(":", 2); | ||
if (parts[0].equals("/tag")) { | ||
TagKey<Item> itemKey = TagKey.create(ForgeRegistries.ITEMS.getRegistryKey(), new ResourceLocation(parts[1])); | ||
TagKey<Block> blockKey = TagKey.create(ForgeRegistries.BLOCKS.getRegistryKey(), new ResourceLocation(parts[1])); | ||
|
||
return Optional.of(stack -> stack.is(itemKey) || | ||
(stack.getItem() instanceof BlockItem blockItem && blockItem.getBlock().defaultBlockState().is(blockKey))); | ||
} else if (parts[0].equals("/instanceof") || parts[0].equals("/class")) { // use this for e.g. pickaxes | ||
try { | ||
Class<?> clazz = Class.forName(parts[1]); | ||
if (parts[0].equals("/instanceof")) { | ||
return Optional.of(st -> clazz.isInstance(st.getItem())); | ||
} else { | ||
return Optional.of(st -> st.getItem().getClass().equals(clazz)); | ||
} | ||
} catch (ClassNotFoundException e) { | ||
InvTweaksMod.LOGGER.warn("Class not found! Ignoring clause " + clause); | ||
return Optional.empty(); | ||
} | ||
} else if (parts[0].equals("/isFood")) { | ||
return Optional.of(stack -> stack.getFoodProperties(null) != null); | ||
} else { // default to standard item checking | ||
try { | ||
return Optional.of(st -> Objects.equals(ForgeRegistries.ITEMS.getKey(st.getItem()), new ResourceLocation(clause))); | ||
} catch (ResourceLocationException e) { | ||
InvTweaksMod.LOGGER.warn("Invalid item resource location found. " + clause); | ||
return Optional.empty(); | ||
} | ||
} | ||
} | ||
|
||
// returns an index for sorting within a category | ||
public int checkStack(ItemStack stack) { | ||
return IntStream.range(0, compiledSpec.size()) | ||
.filter(idx -> compiledSpec.get(idx).stream().allMatch(pr -> pr.test(stack))) | ||
.findFirst() | ||
.orElse(-1); | ||
} | ||
|
||
public CommentedConfig toConfig(String catName) { | ||
CommentedConfig result = CommentedConfig.inMemory(); | ||
result.set("name", catName); | ||
result.set("spec", spec); | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package invtweaks.config; | ||
|
||
import com.electronwill.nightconfig.core.CommentedConfig; | ||
import invtweaks.InvTweaksMod; | ||
import it.unimi.dsi.fastutil.ints.IntArrayList; | ||
import it.unimi.dsi.fastutil.ints.IntList; | ||
import it.unimi.dsi.fastutil.ints.IntLists; | ||
|
||
import javax.annotation.Nullable; | ||
import java.util.Arrays; | ||
import java.util.stream.IntStream; | ||
|
||
public class ContOverride | ||
{ | ||
private final int x, y; | ||
@Nullable | ||
private final IntList sortRange; | ||
private final String sortRangeSpec; | ||
|
||
public ContOverride(int x, int y, String sortRangeSpec) { | ||
this.x = x; | ||
this.y = y; | ||
this.sortRangeSpec = sortRangeSpec; | ||
IntList tmp = null; | ||
if (sortRangeSpec.isEmpty()) { | ||
tmp = IntLists.EMPTY_LIST; | ||
} else if (!sortRangeSpec.equalsIgnoreCase(InvTweaksConfig.NO_SPEC_OVERRIDE)) { | ||
try { | ||
tmp = Arrays.stream(sortRangeSpec.split("\\s*,\\s*")) | ||
.flatMapToInt( | ||
str -> { | ||
String[] rangeSpec = str.split("\\s*-\\s*"); | ||
return IntStream.rangeClosed( | ||
Integer.parseInt(rangeSpec[0]), Integer.parseInt(rangeSpec[1])); | ||
}) | ||
.collect(IntArrayList::new, IntList::add, IntList::addAll); | ||
} catch (NumberFormatException e) { | ||
InvTweaksMod.LOGGER.warn("Invalid slot spec: " + sortRangeSpec); | ||
tmp = IntLists.EMPTY_LIST; | ||
} | ||
} | ||
sortRange = tmp; | ||
} | ||
|
||
public int getX() { | ||
return x; | ||
} | ||
|
||
public int getY() { | ||
return y; | ||
} | ||
|
||
public @Nullable | ||
IntList getSortRange() { | ||
return sortRange; | ||
} | ||
|
||
public boolean isSortDisabled() { | ||
return sortRange != null && sortRange.isEmpty(); | ||
} | ||
|
||
public CommentedConfig toConfig(String contClass) { | ||
CommentedConfig result = CommentedConfig.inMemory(); | ||
result.set("containerClass", contClass); | ||
if (x != InvTweaksConfig.NO_POS_OVERRIDE) result.set("x", x); | ||
if (y != InvTweaksConfig.NO_POS_OVERRIDE) result.set("y", y); | ||
if (!sortRangeSpec.equalsIgnoreCase(InvTweaksConfig.NO_SPEC_OVERRIDE)) result.set("sortRange", sortRangeSpec); | ||
return result; | ||
} | ||
} |
Oops, something went wrong.