|
| 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 | +} |
0 commit comments