Skip to content

Commit

Permalink
- Updated default configs. Delete the old one when updating the mod.
Browse files Browse the repository at this point in the history
- Added support for regex in screen name matching
- Better replacement of broken tools
  • Loading branch information
JaisDK committed May 4, 2024
1 parent 9c3eabf commit ed6c6f5
Show file tree
Hide file tree
Showing 13 changed files with 391 additions and 357 deletions.
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ jobs:
curseforge-id: 976858
curseforge-token: ${{ secrets.CURSEFORGE_TOKEN }}

github-generate-changelog: true
github-token: ${{ secrets.GITHUB_TOKEN }}

name: ""
Expand Down
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ minecraft {
// You can set various levels here.
// Please read: https://stackoverflow.com/questions/2031163/when-to-use-the-different-log-levels
property 'forge.logging.console.level', 'debug'
property 'mixin.env.remapRefMap', 'true'
property 'mixin.env.refMapRemappingFile', "${buildDir}/createSrgToMcp/output.srg"

mods {
"${mod_id}" {
Expand Down Expand Up @@ -117,6 +119,9 @@ repositories {
includeGroup "curse.maven"
}
}
flatDir {
dir 'libs'
}
}

dependencies {
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ org.gradle.jvmargs=-Xmx3G
org.gradle.daemon=false

minecraft_version=1.20.1
forge_version=47.1.0
forge_version=47.2.0
mapping_channel=official
mapping_version=1.20.1

group=invtweaks
mod_id=invtweaks
version=1.0.0
version=1.1.0
88 changes: 88 additions & 0 deletions src/main/java/invtweaks/config/Category.java
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;
}
}
70 changes: 70 additions & 0 deletions src/main/java/invtweaks/config/ContOverride.java
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;
}
}
Loading

0 comments on commit ed6c6f5

Please sign in to comment.