Skip to content

Commit 5e42d77

Browse files
add has_shard selector
1 parent 73f85bf commit 5e42d77

File tree

5 files changed

+107
-2
lines changed

5 files changed

+107
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package net.modfest.scatteredshards.mixin;
2+
3+
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
4+
import net.minecraft.command.CommandSource;
5+
import net.minecraft.command.EntitySelectorOptions;
6+
import net.minecraft.command.EntitySelectorReader;
7+
import net.minecraft.server.network.ServerPlayerEntity;
8+
import net.minecraft.text.Text;
9+
import net.minecraft.util.Identifier;
10+
import net.modfest.scatteredshards.api.ScatteredShardsAPI;
11+
import net.modfest.scatteredshards.api.shard.Shard;
12+
import net.modfest.scatteredshards.mixinsupport.ShardArgument;
13+
import org.spongepowered.asm.mixin.Final;
14+
import org.spongepowered.asm.mixin.Mixin;
15+
import org.spongepowered.asm.mixin.Shadow;
16+
import org.spongepowered.asm.mixin.Unique;
17+
import org.spongepowered.asm.mixin.injection.At;
18+
import org.spongepowered.asm.mixin.injection.Inject;
19+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
20+
21+
import java.util.Map;
22+
import java.util.Optional;
23+
import java.util.function.Predicate;
24+
25+
@Mixin(EntitySelectorOptions.class)
26+
public class EntitySelectorOptionsMixin {
27+
28+
@Unique
29+
private static final DynamicCommandExceptionType scards$UNKNOWN_ID = new DynamicCommandExceptionType(
30+
shardId -> Text.stringifiedTranslatable("argument.scattered_shards.entity.options.has_shard.invalid", shardId)
31+
);
32+
33+
// stolen with permission from fireblanket
34+
@Shadow
35+
private static void putOption(String id, EntitySelectorOptions.SelectorHandler handler, Predicate<EntitySelectorReader> condition, Text description) {
36+
throw new IllegalStateException("Unimplemented mixin");
37+
}
38+
39+
@SuppressWarnings("rawtypes")
40+
@Shadow
41+
@Final
42+
private static Map OPTIONS;
43+
44+
@Inject(method = "register", at = @At("TAIL"))
45+
private static void injectShard(CallbackInfo info) {
46+
if (!OPTIONS.containsKey("has_shard")) {
47+
putOption("has_shard", reader -> {
48+
reader.setSuggestionProvider((builder, consumer) -> {
49+
CommandSource.suggestIdentifiers(ScatteredShardsAPI.getServerLibrary().shards().streamKeys(), builder);
50+
return builder.buildFuture();
51+
});
52+
53+
int i = reader.getReader().getCursor();
54+
reader.setLocalWorldOnly();
55+
Identifier id = Identifier.fromCommandInput(reader.getReader());
56+
Optional<Shard> shard = ScatteredShardsAPI.getServerLibrary().shards().get(id);
57+
if (shard.isEmpty()) {
58+
reader.getReader().setCursor(i);
59+
throw scards$UNKNOWN_ID.create(id);
60+
}
61+
62+
((ShardArgument) reader).setHasShard(true);
63+
reader.addPredicate(
64+
entity -> entity instanceof ServerPlayerEntity player && ScatteredShardsAPI.getServerCollection(player).contains(id)
65+
);
66+
}, reader -> !((ShardArgument) reader).selectsByShard(), Text.translatable("argument.scattered_shards.entity.options.has_shard.description"));
67+
}
68+
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package net.modfest.scatteredshards.mixin;
2+
3+
import net.minecraft.command.EntitySelectorReader;
4+
import net.modfest.scatteredshards.mixinsupport.ShardArgument;
5+
import org.spongepowered.asm.mixin.Mixin;
6+
import org.spongepowered.asm.mixin.Unique;
7+
8+
@Mixin(EntitySelectorReader.class)
9+
public class EntitySelectorReaderMixin implements ShardArgument {
10+
11+
@Unique
12+
private boolean scards$hasShard = false;
13+
14+
@Override
15+
public void setHasShard(boolean value) {
16+
scards$hasShard = value;
17+
}
18+
19+
@Override
20+
public boolean selectsByShard() {
21+
return scards$hasShard;
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package net.modfest.scatteredshards.mixinsupport;
2+
3+
public interface ShardArgument {
4+
void setHasShard(boolean value);
5+
boolean selectsByShard();
6+
}

src/main/resources/assets/scattered_shards/lang/en_us.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,8 @@
5353
"gui.scattered_shards.tablet.click_on_a_shard": "Click on a Shard to the left",
5454
"gui.scattered_shards.tablet.tooltip.global_collection": "%s of players have this shard",
5555
"gui.scattered_shards.tablet.label.progress.started": "%s Started",
56-
"gui.scattered_shards.tablet.label.progress.total": "%s Total"
56+
"gui.scattered_shards.tablet.label.progress.total": "%s Total",
57+
58+
"argument.scattered_shards.entity.options.has_shard.description": "Shard Identifier",
59+
"argument.scattered_shards.entity.options.has_shard.invalid": "Unknown shard identifier '%s'"
5760
}

src/main/resources/scattered_shards.mixins.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,9 @@
88
],
99
"injectors": {
1010
"defaultRequire": 1
11-
}
11+
},
12+
"mixins": [
13+
"EntitySelectorOptionsMixin",
14+
"EntitySelectorReaderMixin"
15+
]
1216
}

0 commit comments

Comments
 (0)