From 3d5fe409507c8e5e2311a7763bcfc22e0b7ba905 Mon Sep 17 00:00:00 2001 From: Sollace Date: Fri, 27 Jan 2023 14:37:25 +0000 Subject: [PATCH] Fixed server-wide default race being set to human and added a command to change it --- .../unicopia/WorldTribeManager.java | 14 +----- .../unicopia/command/Commands.java | 7 +-- .../unicopia/command/GravityCommand.java | 44 +++++++++---------- .../unicopia/command/SpeciesCommand.java | 6 +-- .../unicopia/command/WorldTribeCommand.java | 40 +++++++++++++++++ .../resources/assets/unicopia/lang/en_us.json | 7 ++- 6 files changed, 76 insertions(+), 42 deletions(-) create mode 100644 src/main/java/com/minelittlepony/unicopia/command/WorldTribeCommand.java diff --git a/src/main/java/com/minelittlepony/unicopia/WorldTribeManager.java b/src/main/java/com/minelittlepony/unicopia/WorldTribeManager.java index 57f5f477..cb9e8f23 100644 --- a/src/main/java/com/minelittlepony/unicopia/WorldTribeManager.java +++ b/src/main/java/com/minelittlepony/unicopia/WorldTribeManager.java @@ -2,14 +2,11 @@ package com.minelittlepony.unicopia; import net.minecraft.nbt.NbtCompound; import net.minecraft.server.world.ServerWorld; -import net.minecraft.registry.entry.RegistryEntry; import net.minecraft.world.PersistentState; -import net.minecraft.world.dimension.DimensionType; -import net.minecraft.world.dimension.DimensionTypes; public class WorldTribeManager extends PersistentState { - private Race defaultRace = Race.HUMAN; + private Race defaultRace = Race.UNSET; public WorldTribeManager() {} @@ -33,14 +30,7 @@ public class WorldTribeManager extends PersistentState { return tag; } - public static String nameFor(RegistryEntry dimension) { - if (dimension.matchesKey(DimensionTypes.THE_END)) { - return "unicopia:tribes_end"; - } - return "unicopia:tribes"; - } - public static WorldTribeManager forWorld(ServerWorld world) { - return world.getPersistentStateManager().getOrCreate(WorldTribeManager::new, WorldTribeManager::new, nameFor(world.getDimensionEntry())); + return world.getPersistentStateManager().getOrCreate(WorldTribeManager::new, WorldTribeManager::new, "unicopia:tribes"); } } diff --git a/src/main/java/com/minelittlepony/unicopia/command/Commands.java b/src/main/java/com/minelittlepony/unicopia/command/Commands.java index 54079c61..2765af5f 100644 --- a/src/main/java/com/minelittlepony/unicopia/command/Commands.java +++ b/src/main/java/com/minelittlepony/unicopia/command/Commands.java @@ -22,6 +22,7 @@ public class Commands { EmoteCommand.register(dispatcher); if (Unicopia.getConfig().enableCheats.get() || environment.dedicated) { SpeciesCommand.register(dispatcher, environment); + WorldTribeCommand.register(dispatcher); } if (Unicopia.getConfig().enableCheats.get()) { GravityCommand.register(dispatcher); @@ -32,9 +33,9 @@ public class Commands { } } }); - Object game = FabricLoader.getInstance().getGameInstance(); - if (game instanceof MinecraftServer) { - ((MinecraftServer)game).setFlightEnabled(true); + + if (FabricLoader.getInstance().getGameInstance() instanceof MinecraftServer server) { + server.setFlightEnabled(true); } } } diff --git a/src/main/java/com/minelittlepony/unicopia/command/GravityCommand.java b/src/main/java/com/minelittlepony/unicopia/command/GravityCommand.java index 88b9cfaf..eabd4789 100644 --- a/src/main/java/com/minelittlepony/unicopia/command/GravityCommand.java +++ b/src/main/java/com/minelittlepony/unicopia/command/GravityCommand.java @@ -1,5 +1,9 @@ package com.minelittlepony.unicopia.command; +import java.util.Arrays; +import java.util.stream.Stream; + +import com.google.common.collect.Streams; import com.minelittlepony.unicopia.entity.player.Pony; import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.arguments.FloatArgumentType; @@ -36,39 +40,33 @@ class GravityCommand { } static int get(ServerCommandSource source, PlayerEntity player, boolean isSelf) throws CommandSyntaxException { - String translationKey = "commands.gravity.get"; - - Pony iplayer = Pony.of(player); - - float gravity = iplayer.getPhysics().getGravityModifier(); - - if (source.getPlayer() == player) { - player.sendMessage(Text.translatable(translationKey, gravity), false); - } else { - source.sendFeedback(Text.translatable(translationKey + ".other", player.getName(), gravity), true); - } - + sendFeedback(source, player, "get", false, Pony.of(player).getPhysics().getGravityModifier()); return 0; } static int set(ServerCommandSource source, PlayerEntity player, float gravity, boolean isSelf) { - String translationKey = "commands.gravity.set"; Pony iplayer = Pony.of(player); iplayer.getPhysics().setBaseGravityModifier(gravity); iplayer.setDirty(); - if (source.getEntity() == player) { - source.sendFeedback(Text.translatable("commands.gamemode.success.self", gravity), true); - } else { - if (source.getWorld().getGameRules().getBoolean(GameRules.SEND_COMMAND_FEEDBACK)) { - player.sendMessage(Text.translatable(translationKey, gravity)); - } - - source.sendFeedback(Text.translatable(translationKey + ".other", player.getName(), gravity), true); - } - + sendFeedback(source, player, "set", true, gravity); return 0; } + + + static void sendFeedback(ServerCommandSource source, PlayerEntity player, String key, boolean notifyTarget, Object...arguments) { + String translationKey = "commands.gravity." + key; + + if (source.getEntity() == player) { + source.sendFeedback(Text.translatable(translationKey + ".self", arguments), true); + } else { + if (notifyTarget && source.getWorld().getGameRules().getBoolean(GameRules.SEND_COMMAND_FEEDBACK)) { + player.sendMessage(Text.translatable(translationKey, arguments)); + } + + source.sendFeedback(Text.translatable(translationKey + ".other", Streams.concat(Stream.of(player.getDisplayName()), Arrays.stream(arguments)).toArray()), true); + } + } } diff --git a/src/main/java/com/minelittlepony/unicopia/command/SpeciesCommand.java b/src/main/java/com/minelittlepony/unicopia/command/SpeciesCommand.java index a971374d..4b92ade2 100644 --- a/src/main/java/com/minelittlepony/unicopia/command/SpeciesCommand.java +++ b/src/main/java/com/minelittlepony/unicopia/command/SpeciesCommand.java @@ -64,11 +64,11 @@ class SpeciesCommand { pony.setSpecies(race); pony.setDirty(); - if (!isSelf) { - source.sendFeedback(Text.translatable("commands.race.success.other", player.getName(), race.getDisplayName()), true); + if (player == source.getPlayer()) { + source.sendFeedback(Text.translatable("commands.race.success.self", player.getName(), race.getDisplayName()), true); } else { if (player.getEntityWorld().getGameRules().getBoolean(GameRules.SEND_COMMAND_FEEDBACK)) { - player.sendMessage(Text.translatable("commands.race.success.self"), false); + player.sendMessage(Text.translatable("commands.race.success.other"), false); } source.sendFeedback(Text.translatable("commands.race.success.otherself", player.getName(), race.getDisplayName()), true); } diff --git a/src/main/java/com/minelittlepony/unicopia/command/WorldTribeCommand.java b/src/main/java/com/minelittlepony/unicopia/command/WorldTribeCommand.java new file mode 100644 index 00000000..c6ab172f --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/command/WorldTribeCommand.java @@ -0,0 +1,40 @@ +package com.minelittlepony.unicopia.command; + +import com.minelittlepony.unicopia.Race; +import com.minelittlepony.unicopia.WorldTribeManager; +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import com.mojang.brigadier.exceptions.CommandSyntaxException; + +import net.minecraft.server.command.CommandManager; +import net.minecraft.server.command.ServerCommandSource; +import net.minecraft.text.Text; + +class WorldTribeCommand { + static void register(CommandDispatcher dispatcher) { + LiteralArgumentBuilder builder = CommandManager + .literal("worldtribe") + .requires(s -> s.hasPermissionLevel(4)); + + builder.then(CommandManager.literal("get").executes(context -> get(context.getSource()))); + builder.then(CommandManager.literal("set") + .then(CommandManager.argument("race", Race.argument()) + .executes(context -> set(context.getSource(), Race.fromArgument(context, "race"))))); + + dispatcher.register(builder); + } + + static int get(ServerCommandSource source) throws CommandSyntaxException { + WorldTribeManager manager = WorldTribeManager.forWorld(source.getWorld()); + source.sendFeedback(Text.translatable("commands.worldtribe.success.get", manager.getDefaultRace().getDisplayName()), true); + return 0; + } + + static int set(ServerCommandSource source, Race race) { + WorldTribeManager manager = WorldTribeManager.forWorld(source.getWorld()); + manager.setDefaultRace(race); + + source.sendFeedback(Text.translatable("commands.worldtribe.success.set", race.getDisplayName()), true); + return 0; + } +} diff --git a/src/main/resources/assets/unicopia/lang/en_us.json b/src/main/resources/assets/unicopia/lang/en_us.json index 1bc1ab4d..491b40a9 100644 --- a/src/main/resources/assets/unicopia/lang/en_us.json +++ b/src/main/resources/assets/unicopia/lang/en_us.json @@ -448,6 +448,9 @@ "commands.racelist.disallowed": "Removed %1$s from the whitelist.", "commands.racelist.disallowed.failed": "%1$s is not on the whitelist.", + "commands.worldtribe.success.get": "Default race for all new players is currently set to: %s", + "commands.worldtribe.success.set": "Set default race for new players is now set to: %s", + "commands.disguise.usage": "/disguise [nbt]", "commands.disguise.notfound": "The entity id '%s' does not exist.", "commands.disguise.removed": "Your disguise has been removed.", @@ -478,6 +481,8 @@ "unicopia.options.world.default_race": "Default Race: %s", "unicopia.options.lan": "Multiplayer (LAN) Settings", + "unicopia.race.unset": "Unset", + "unicopia.race.unset.alt": "Unset", "unicopia.race.human": "Human", "unicopia.race.human.alt": "Humans", "unicopia.race.earth": "Earth Pony", @@ -530,7 +535,7 @@ "death.attack.kick.player": "%2$s kicked %1$s really hard", "death.attack.stalagmite.pegasus": "%1$s tried to perch on a stalagmite", "death.attack.stalagmite.pegasus.player": "%1$s flew into a stalagmite whilst fighting %2$s", - + "death.fell.accident.ladder.pegasus": "%1$s forgot they could fly and fell off a ladder", "death.fell.accident.vines.pegasus": "%1$s forgot they could fly and fell off some vines", "death.fell.accident.weeping_vines.pegasus": "%1$s forgot they could fly and fell off some weeping vines",