From af76a4fef650e4c830901dd6fc7f0dbd9337470c Mon Sep 17 00:00:00 2001 From: Sollace Date: Sun, 10 Sep 2023 23:28:53 +0100 Subject: [PATCH] Added ad_astra integration --- .../com/minelittlepony/unicopia/Config.java | 5 +- .../unicopia/UnicopiaMixinPlugin.java | 3 + .../unicopia/command/ConfigCommand.java | 94 +++++++++++++++++++ .../unicopia/command/UnicopiaCommand.java | 1 + .../unicopia/compat/ad_astra/OxygenUtils.java | 18 ++++ .../unicopia/entity/player/PlayerPhysics.java | 5 +- .../mixin/ad_astra/MixinOxygenUtils.java | 20 ++++ .../unicopia/util/RegistryUtils.java | 4 + .../resources/assets/unicopia/lang/en_us.json | 5 + src/main/resources/unicopia.mixin.json | 3 +- 10 files changed, 155 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/minelittlepony/unicopia/command/ConfigCommand.java create mode 100644 src/main/java/com/minelittlepony/unicopia/compat/ad_astra/OxygenUtils.java create mode 100644 src/main/java/com/minelittlepony/unicopia/mixin/ad_astra/MixinOxygenUtils.java diff --git a/src/main/java/com/minelittlepony/unicopia/Config.java b/src/main/java/com/minelittlepony/unicopia/Config.java index feefdcdc..c0f86374 100644 --- a/src/main/java/com/minelittlepony/unicopia/Config.java +++ b/src/main/java/com/minelittlepony/unicopia/Config.java @@ -18,8 +18,11 @@ public class Config extends com.minelittlepony.common.util.settings.Config { public final Setting> wantItNeedItEntityExcludelist = value("server", "wantItNeedItEntityExcludelist", (Set)new HashSet<>(Set.of("minecraft:creeper"))) .addComment("A list of entity types that are immune to the want it need it spell's effects"); + public final Setting> dimensionsWithoutAtmosphere = value("server", "dimensionsWithoutAtmosphere", (Set)new HashSet()) + .addComment("A list of dimensions ids that do not have an atmosphere, and thus shouldn't allow pegasi to fly."); + public final Setting enableCheats = value("server", "enableCheats", false) - .addComment("Allows use of the /race, /disguise, and /gravity commands"); + .addComment("Allows use of the /tribe, /unicopia disguise, and /unicopia gravity commands"); public final Setting preferredRace = value("client", "preferredRace", Race.EARTH) .addComment("The default preferred race") diff --git a/src/main/java/com/minelittlepony/unicopia/UnicopiaMixinPlugin.java b/src/main/java/com/minelittlepony/unicopia/UnicopiaMixinPlugin.java index 6f16b965..48dcf6b7 100644 --- a/src/main/java/com/minelittlepony/unicopia/UnicopiaMixinPlugin.java +++ b/src/main/java/com/minelittlepony/unicopia/UnicopiaMixinPlugin.java @@ -32,6 +32,9 @@ public class UnicopiaMixinPlugin implements IMixinConfigPlugin { if (mixinClassName.indexOf("seasons") != -1) { return FabricLoader.getInstance().isModLoaded("seasons"); } + if (mixinClassName.indexOf("ad_astra") != -1) { + return FabricLoader.getInstance().isModLoaded("ad_astra"); + } } return true; } diff --git a/src/main/java/com/minelittlepony/unicopia/command/ConfigCommand.java b/src/main/java/com/minelittlepony/unicopia/command/ConfigCommand.java new file mode 100644 index 00000000..10cd162a --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/command/ConfigCommand.java @@ -0,0 +1,94 @@ +package com.minelittlepony.unicopia.command; + +import java.util.HashSet; +import java.util.Objects; +import java.util.Set; +import java.util.function.Function; +import com.minelittlepony.common.util.settings.Config; +import com.minelittlepony.common.util.settings.Setting; +import com.minelittlepony.unicopia.Unicopia; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +import com.mojang.brigadier.suggestion.SuggestionProvider; + +import net.minecraft.command.CommandRegistryAccess; +import net.minecraft.command.CommandSource; +import net.minecraft.command.argument.*; +import net.minecraft.registry.Registry; +import net.minecraft.registry.RegistryKey; +import net.minecraft.registry.RegistryKeys; +import net.minecraft.registry.RegistryWrapper; +import net.minecraft.server.command.CommandManager; +import net.minecraft.server.command.ServerCommandSource; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.text.Text; +import net.minecraft.util.Identifier; + +public class ConfigCommand { + + public static LiteralArgumentBuilder create(CommandRegistryAccess registries) { + return CommandManager.literal("config").requires(s -> s.hasPermissionLevel(2)) + .then(createSet("dimensionswithoutatmosphere", createSuggestion(registries, RegistryKeys.DIMENSION_TYPE))) + .then(createSet("wantitneeditentityexcludelist", createSuggestion(registries, RegistryKeys.ENTITY_TYPE))); + } + + private static LiteralArgumentBuilder createSet(String configName, SuggestionProvider suggestions) { + return CommandManager.literal(configName) + .then(CommandManager.literal("clear").executes(source -> { + source.getSource().sendFeedback(() -> Text.translatable("command.unicopia.config.clear", configName), true); + return changeProperty(configName, values -> new HashSet<>()); + })) + .then(CommandManager.literal("add").then( + CommandManager.argument("id", IdentifierArgumentType.identifier()).suggests(suggestions).executes(source -> ConfigCommand.>changeProperty(configName, values -> { + String value = IdentifierArgumentType.getIdentifier(source, "id").toString(); + source.getSource().sendFeedback(() -> Text.translatable("command.unicopia.config.add", value, configName), true); + values.add(value); + return values; + }))) + ) + .then(CommandManager.literal("remove").then( + CommandManager.argument("id", IdentifierArgumentType.identifier()).suggests((context, builder) -> { + return CommandSource.suggestIdentifiers(Unicopia.getConfig().getCategory("server").>get(configName).get().stream() + .map(Identifier::tryParse) + .filter(Objects::nonNull) + .toList(), builder); + }).executes(source -> ConfigCommand.>changeProperty(configName, values -> { + String value = IdentifierArgumentType.getIdentifier(source, "id").toString(); + source.getSource().sendFeedback(() -> Text.translatable("command.unicopia.config.remove", value, configName), true); + values.remove(value); + return values; + }))) + ) + .then(CommandManager.literal("list").executes(source -> ConfigCommand.>getProperty(configName, values -> { + ServerPlayerEntity player = source.getSource().getPlayerOrThrow(); + + player.sendMessage(Text.translatable("command.unicopia.config.list", configName, values.size()), false); + values.forEach(line -> player.sendMessage(Text.literal(line))); + })) + ); + } + + private static SuggestionProvider createSuggestion(CommandRegistryAccess registries, RegistryKey> registryKey) { + RegistryWrapper wrapper = registries.createWrapper(registryKey); + return (context, builder) -> CommandSource.suggestIdentifiers(wrapper.streamKeys().map(RegistryKey::getValue), builder); + } + + private static int changeProperty(String configName, Function changer) { + Config config = Unicopia.getConfig(); + Setting setting = config.getCategory("server").get(configName); + setting.set(changer.apply(setting.get())); + config.save(); + return 0; + } + + private static int getProperty(String configName, UnsafeConsumer reader) throws CommandSyntaxException { + Config config = Unicopia.getConfig(); + Setting setting = config.getCategory("server").get(configName); + reader.accept(setting.get()); + return 0; + } + + interface UnsafeConsumer { + void accept(T t) throws CommandSyntaxException; + } +} diff --git a/src/main/java/com/minelittlepony/unicopia/command/UnicopiaCommand.java b/src/main/java/com/minelittlepony/unicopia/command/UnicopiaCommand.java index 508d912b..39cfcb84 100644 --- a/src/main/java/com/minelittlepony/unicopia/command/UnicopiaCommand.java +++ b/src/main/java/com/minelittlepony/unicopia/command/UnicopiaCommand.java @@ -11,6 +11,7 @@ class UnicopiaCommand { public static LiteralArgumentBuilder create(CommandRegistryAccess registries, RegistrationEnvironment environment) { return CommandManager.literal("unicopia") .then(EmoteCommand.create()) + .then(ConfigCommand.create(registries)) .then(SpeciesCommand.create(environment)) .then(RacelistCommand.create()) .then(WorldTribeCommand.create()) diff --git a/src/main/java/com/minelittlepony/unicopia/compat/ad_astra/OxygenUtils.java b/src/main/java/com/minelittlepony/unicopia/compat/ad_astra/OxygenUtils.java new file mode 100644 index 00000000..ca4956a4 --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/compat/ad_astra/OxygenUtils.java @@ -0,0 +1,18 @@ +package com.minelittlepony.unicopia.compat.ad_astra; + +import com.minelittlepony.unicopia.mixin.ad_astra.MixinOxygenUtils; + +import net.fabricmc.loader.api.FabricLoader; +import net.minecraft.entity.LivingEntity; +import net.minecraft.world.World; + +public interface OxygenUtils { + boolean MOD_LOADED = FabricLoader.getInstance().isModLoaded("ad_astra"); + + static boolean entityHasOxygen(World world, LivingEntity entity) { + if (MOD_LOADED) { + return MixinOxygenUtils.entityHasOxygen(world, entity); + } + return false; + } +} diff --git a/src/main/java/com/minelittlepony/unicopia/entity/player/PlayerPhysics.java b/src/main/java/com/minelittlepony/unicopia/entity/player/PlayerPhysics.java index aa302521..6a6e689b 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/player/PlayerPhysics.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/player/PlayerPhysics.java @@ -9,6 +9,7 @@ import com.minelittlepony.unicopia.ability.magic.spell.effect.SpellType; import com.minelittlepony.unicopia.advancement.UCriteria; import com.minelittlepony.unicopia.client.minelittlepony.MineLPDelegate; import com.minelittlepony.unicopia.client.render.PlayerPoser.Animation; +import com.minelittlepony.unicopia.compat.ad_astra.OxygenUtils; import com.minelittlepony.unicopia.entity.*; import com.minelittlepony.unicopia.entity.damage.UDamageTypes; import com.minelittlepony.unicopia.entity.duck.LivingEntityDuck; @@ -182,7 +183,9 @@ public class PlayerPhysics extends EntityPhysics implements Tickab public FlightType getFlightType() { DimensionType dimension = entity.getWorld().getDimension(); - if (RegistryUtils.isIn(entity.getWorld(), dimension, RegistryKeys.DIMENSION_TYPE, UTags.HAS_NO_ATMOSPHERE)) { + if ((RegistryUtils.isIn(entity.getWorld(), dimension, RegistryKeys.DIMENSION_TYPE, UTags.HAS_NO_ATMOSPHERE) + || Unicopia.getConfig().dimensionsWithoutAtmosphere.get().contains(RegistryUtils.getId(entity.getWorld(), dimension, RegistryKeys.DIMENSION_TYPE).toString())) + && !OxygenUtils.entityHasOxygen(entity.getWorld(), entity)) { return FlightType.NONE; } diff --git a/src/main/java/com/minelittlepony/unicopia/mixin/ad_astra/MixinOxygenUtils.java b/src/main/java/com/minelittlepony/unicopia/mixin/ad_astra/MixinOxygenUtils.java new file mode 100644 index 00000000..be39ab53 --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/mixin/ad_astra/MixinOxygenUtils.java @@ -0,0 +1,20 @@ +package com.minelittlepony.unicopia.mixin.ad_astra; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Pseudo; +import org.spongepowered.asm.mixin.gen.Invoker; + +import net.minecraft.entity.LivingEntity; +import net.minecraft.world.World; + +@Pseudo +@Mixin( + targets = { "earth.terrarium.ad_astra.common.util.OxygenUtils" }, + remap = false +) +public interface MixinOxygenUtils { + @Invoker("entityHasOxygen") + static boolean entityHasOxygen(World world, LivingEntity entity) { + return true; + } +} diff --git a/src/main/java/com/minelittlepony/unicopia/util/RegistryUtils.java b/src/main/java/com/minelittlepony/unicopia/util/RegistryUtils.java index 03c35aef..bda52aae 100644 --- a/src/main/java/com/minelittlepony/unicopia/util/RegistryUtils.java +++ b/src/main/java/com/minelittlepony/unicopia/util/RegistryUtils.java @@ -53,4 +53,8 @@ public interface RegistryUtils { static boolean isIn(World world, T obj, RegistryKey> registry, TagKey tag) { return world.getRegistryManager().get(registry).getEntry(obj).isIn(tag); } + + static Identifier getId(World world, T obj, RegistryKey> registry) { + return world.getRegistryManager().get(registry).getId(obj); + } } diff --git a/src/main/resources/assets/unicopia/lang/en_us.json b/src/main/resources/assets/unicopia/lang/en_us.json index baf6ef6c..5f10a3f4 100644 --- a/src/main/resources/assets/unicopia/lang/en_us.json +++ b/src/main/resources/assets/unicopia/lang/en_us.json @@ -681,6 +681,11 @@ "unicopia.options.world": "World Settings", "unicopia.options.world.default_race": "Default Race: %s", "unicopia.options.lan": "Multiplayer (LAN) Settings", + + "command.unicopia.config.add": "[Config] Added %s to property %s", + "command.unicopia.config.remove": "[Config] Removed %s from property %s", + "command.unicopia.config.list": "[Config] Property (%s) contains (%s) entries: ", + "command.unicopia.config.clear": "[Config] Cleared all values from property %s", "unicopia.race.unset": "Unset", "unicopia.race.unset.alt": "Unset", diff --git a/src/main/resources/unicopia.mixin.json b/src/main/resources/unicopia.mixin.json index 2ad8314e..805fe03b 100644 --- a/src/main/resources/unicopia.mixin.json +++ b/src/main/resources/unicopia.mixin.json @@ -49,7 +49,8 @@ "trinkets.MixinTrinketItem", "trinkets.MixinTrinketInventory", "trinkets.MixinScreenHandler", - "seasons.MixinFertilizableUtil" + "seasons.MixinFertilizableUtil", + "ad_astra.MixinOxygenUtils" ], "client": [ "client.MixinAnimalModel",