Skip to content

Commit

Permalink
cardinal UI: add * for NBT Paths
Browse files Browse the repository at this point in the history
  • Loading branch information
sisby-folk committed Sep 1, 2023
1 parent ac46e02 commit 96a9818
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package folk.sisby.switchy.client.api;

import net.minecraft.command.argument.NbtPathArgumentType;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;

import java.util.List;
import java.util.function.Supplier;

public class AllValueElementNode implements NbtPathArgumentType.PathNode {
public static final AllValueElementNode INSTANCE = new AllValueElementNode();

private AllValueElementNode() {
}

@Override
public void get(NbtElement nbt, List<NbtElement> results) {
if (nbt instanceof NbtCompound compound) {
compound.getKeys().stream().map(compound::get).forEach(results::add);
}
}

@Override
public void getOrInit(NbtElement nbt, Supplier<NbtElement> source, List<NbtElement> results) {
if (nbt instanceof NbtCompound compound) {
if (compound.isEmpty()) {
results.add(source.get());
} else {
get(nbt, results);
}
}
}

@Override
public NbtElement init() {
return new NbtCompound();
}

@Override
public int set(NbtElement nbt, Supplier<NbtElement> source) {
if (nbt instanceof NbtCompound compound) {
if (compound.isEmpty()) {
compound.put("default", source.get());
return 1;
} else {
NbtElement nbtElement = source.get();
int unequal_count = (int) compound.getKeys().stream().map(compound::get).filter(nbtElement::equals).count();
if (unequal_count > 0) {
compound.getKeys().forEach(k -> compound.put(k, source.get()));
}
return unequal_count;
}
} else {
return 0;
}
}

@Override
public int clear(NbtElement nbt) {
if (nbt instanceof NbtCompound compound) {
int i = compound.getKeys().size();
if (i > 0) {
compound.getKeys().forEach(compound::remove);
return i;
}
}
return 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package folk.sisby.switchy.client.mixin;

import com.mojang.brigadier.StringReader;
import folk.sisby.switchy.client.api.AllValueElementNode;
import net.minecraft.command.argument.NbtPathArgumentType;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(NbtPathArgumentType.class)
public class NbtPathArgumentTypeMixin {
@Inject(method = "parseNode", at = @At(value = "INVOKE", target = "Lnet/minecraft/command/argument/NbtPathArgumentType;readName(Lcom/mojang/brigadier/StringReader;)Ljava/lang/String;"), cancellable = true)
private static void AddAllValueElementNode(StringReader reader, boolean root, CallbackInfoReturnable<NbtPathArgumentType.PathNode> cir) {
if (reader.peek() == '*') {
reader.skip();
cir.setReturnValue(AllValueElementNode.INSTANCE);
cir.cancel();
}
}
}
1 change: 1 addition & 0 deletions cardinal-ui/src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@
"modmenu": {
"parent": "switchy"
},
"mixins": ["switchy-cardinal-ui.mixins.json"],
"accessWidener": "switchy-cardinal-ui.accesswidener"
}
1 change: 1 addition & 0 deletions cardinal-ui/src/main/resources/quilt.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,6 @@
"minecraft": {
"environment": "client"
},
"mixin": "switchy-cardinal-ui.mixins.json",
"access_widener": "switchy-cardinal-ui.accesswidener"
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
accessWidener v1 named

accessible field net/minecraft/nbt/visitor/NbtTextFormatter result Lnet/minecraft/text/Text;
accessible class net/minecraft/command/argument/NbtPathArgumentType$PathNode
12 changes: 12 additions & 0 deletions cardinal-ui/src/main/resources/switchy-cardinal-ui.mixins.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"required": true,
"minVersion": "0.8",
"package": "folk.sisby.switchy.client.mixin",
"compatibilityLevel": "JAVA_17",
"client": [
"NbtPathArgumentTypeMixin"
],
"injectors": {
"defaultRequire": 1
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
org.gradle.jvmargs=-Xmx4G
org.gradle.parallel=true
# Mod Properties
version=2.7.3
version=2.8.0
maven_group=folk.sisby
archives_base_name=switchy

0 comments on commit 96a9818

Please sign in to comment.