diff --git a/src/main/java/one/oth3r/sit/Sit.java b/src/main/java/one/oth3r/sit/Sit.java index f7f03f8..9d0d444 100644 --- a/src/main/java/one/oth3r/sit/Sit.java +++ b/src/main/java/one/oth3r/sit/Sit.java @@ -1,49 +1,15 @@ package one.oth3r.sit; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.reflect.TypeToken; import net.fabricmc.api.ModInitializer; -import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry; -import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; -import net.fabricmc.loader.api.FabricLoader; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.command.CommandManager; -import net.minecraft.server.network.ServerPlayerEntity; -import one.oth3r.sit.file.Data; -import one.oth3r.sit.packet.SitPayloads; +import one.oth3r.sit.file.FileData; import one.oth3r.sit.utl.Events; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.lang.reflect.Type; -import java.util.HashMap; public class Sit implements ModInitializer { - public static final String MOD_ID = "oth3r-sit"; - public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID); - public static String CONFIG_DIR = FabricLoader.getInstance().getConfigDir().toFile()+"/sit!/"; - - public static HashMap> playerSettings = new HashMap<>(); - public static final String ENTITY_NAME = "-sit!-entity-"; - public static MinecraftServer server; - public static CommandManager commandManager; - - public static boolean client = false; - public static boolean singleplayer = false; @Override public void onInitialize() { - Data.loadFiles(true); + FileData.loadFiles(true); Events.registerCommon(); - - //PACKETS - PayloadTypeRegistry.playC2S().register(SitPayloads.SettingsPayload.ID, SitPayloads.SettingsPayload.CODEC); - ServerPlayNetworking.registerGlobalReceiver(SitPayloads.SettingsPayload.ID,((payload, context) -> server.execute(() -> { - Type hashMapToken = new TypeToken>() {}.getType(); - Gson gson = new GsonBuilder().disableHtmlEscaping().create(); - playerSettings.put(context.player(),gson.fromJson(payload.value(),hashMapToken)); - }))); } } \ No newline at end of file diff --git a/src/main/java/one/oth3r/sit/SitClient.java b/src/main/java/one/oth3r/sit/SitClient.java index 105afb6..43833dc 100644 --- a/src/main/java/one/oth3r/sit/SitClient.java +++ b/src/main/java/one/oth3r/sit/SitClient.java @@ -1,40 +1,16 @@ package one.oth3r.sit; import net.fabricmc.api.ClientModInitializer; -import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents; -import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; -import one.oth3r.sit.file.Data; -import one.oth3r.sit.packet.SitPayloads; -import one.oth3r.sit.utl.Utl; +import one.oth3r.sit.utl.Data; +import one.oth3r.sit.utl.Events; public class SitClient implements ClientModInitializer { - private static boolean IN_GAME = false; @Override public void onInitializeClient() { - Sit.client = true; - - ClientPlayConnectionEvents.JOIN.register((handler, sender, client) -> { - IN_GAME = true; - if (client.isInSingleplayer()) Sit.singleplayer = true; - // send a data packet whenever joining a server - sendSettingsPackets(); - }); - - // reset cashed things - ClientPlayConnectionEvents.DISCONNECT.register((handler, client) -> { - IN_GAME = false; - if (Sit.singleplayer) { - // flip the single player switch - Sit.singleplayer = false; - } - }); + Data.setClient(true); + Events.registerClient(); } - /** - * sends the settings packets to the server - */ - public static void sendSettingsPackets() { - if (IN_GAME) ClientPlayNetworking.send(new SitPayloads.SettingsPayload(Utl.getGson().toJson(Data.getHandConfig()))); - } + } diff --git a/src/main/java/one/oth3r/sit/command/SitCommand.java b/src/main/java/one/oth3r/sit/command/SitCommand.java index 9b308bd..5ab50f1 100644 --- a/src/main/java/one/oth3r/sit/command/SitCommand.java +++ b/src/main/java/one/oth3r/sit/command/SitCommand.java @@ -11,8 +11,8 @@ import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.TextColor; import net.minecraft.util.Formatting; import net.minecraft.util.math.BlockPos; +import one.oth3r.sit.utl.Data; import one.oth3r.sit.utl.Logic; -import one.oth3r.sit.Sit; import one.oth3r.sit.utl.Utl; import one.oth3r.sit.file.FileData; @@ -51,7 +51,7 @@ public class SitCommand { if (player == null) { if (args[0].equalsIgnoreCase("reload")) { Logic.reload(); - Sit.LOGGER.info(Utl.lang("msg.reloaded").getString()); + Data.LOGGER.info(Utl.lang("msg.reloaded").getString()); } return 1; } diff --git a/src/main/java/one/oth3r/sit/file/LangReader.java b/src/main/java/one/oth3r/sit/file/LangReader.java index 672c7d6..807ad98 100644 --- a/src/main/java/one/oth3r/sit/file/LangReader.java +++ b/src/main/java/one/oth3r/sit/file/LangReader.java @@ -5,6 +5,7 @@ import com.google.gson.reflect.TypeToken; import net.minecraft.text.MutableText; import net.minecraft.text.Text; import one.oth3r.sit.Sit; +import one.oth3r.sit.utl.Data; import java.io.InputStream; import java.io.InputStreamReader; @@ -98,7 +99,7 @@ public class LangReader { // close the input stream inputStream.close(); } catch (Exception e) { - Sit.LOGGER.error(e.getMessage()); + Data.LOGGER.error(e.getMessage()); } } public static String getLanguageValue(String key) { diff --git a/src/main/java/one/oth3r/sit/file/ServerConfig.java b/src/main/java/one/oth3r/sit/file/ServerConfig.java index baed8dc..a33f82b 100644 --- a/src/main/java/one/oth3r/sit/file/ServerConfig.java +++ b/src/main/java/one/oth3r/sit/file/ServerConfig.java @@ -1,7 +1,7 @@ package one.oth3r.sit.file; import com.google.gson.annotations.SerializedName; -import one.oth3r.sit.Sit; +import one.oth3r.sit.utl.Data; import one.oth3r.sit.utl.Utl; import java.io.BufferedReader; @@ -127,7 +127,7 @@ public class ServerConfig { public static File getFile() { - return new File(Sit.CONFIG_DIR+"server-config.json"); + return new File(Data.CONFIG_DIR+"server-config.json"); } /** @@ -139,14 +139,14 @@ public class ServerConfig { if (!file.exists()) { // try to make the config directory try { - Files.createDirectories(Paths.get(Sit.CONFIG_DIR)); + Files.createDirectories(Paths.get(Data.CONFIG_DIR)); } catch (Exception e) { - Sit.LOGGER.error("Failed to create config directory. Canceling all config loading..."); + Data.LOGGER.error("Failed to create config directory. Canceling all config loading..."); return; } // if loading from legacy, try checking the old config directory for the file if (tryLegacy && Updater.ServerConfigFile.Legacy.getLegacyFile().exists()) { - Sit.LOGGER.info("Updating Sit!.properties to sit!/config.json"); + Data.LOGGER.info("Updating Sit!.properties to sit!/config.json"); Updater.ServerConfigFile.Legacy.run(); } save(); @@ -155,7 +155,7 @@ public class ServerConfig { try (BufferedReader reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)) { Updater.ServerConfigFile.run(reader); } catch (Exception e) { - Sit.LOGGER.error(String.format("ERROR LOADING '%s`: %s", file.getName(),e.getMessage())); + Data.LOGGER.error(String.format("ERROR LOADING '%s`: %s", file.getName(),e.getMessage())); } // save after loading save(); @@ -166,12 +166,12 @@ public class ServerConfig { */ public static void save() { if (!getFile().exists()) { - Sit.LOGGER.info(String.format("Creating new `%s`", getFile().getName())); + Data.LOGGER.info(String.format("Creating new `%s`", getFile().getName())); } try (BufferedWriter writer = Files.newBufferedWriter(getFile().toPath(), StandardCharsets.UTF_8)) { writer.write(Utl.getGson().toJson(FileData.getServerConfig())); } catch (Exception e) { - Sit.LOGGER.info(String.format("ERROR SAVING '%s`: %s", getFile().getName(), e.getMessage())); + Data.LOGGER.info(String.format("ERROR SAVING '%s`: %s", getFile().getName(), e.getMessage())); } } } diff --git a/src/main/java/one/oth3r/sit/file/SittingConfig.java b/src/main/java/one/oth3r/sit/file/SittingConfig.java index f5f7f1d..fd9fd8a 100644 --- a/src/main/java/one/oth3r/sit/file/SittingConfig.java +++ b/src/main/java/one/oth3r/sit/file/SittingConfig.java @@ -2,7 +2,7 @@ package one.oth3r.sit.file; import com.google.gson.annotations.SerializedName; import net.minecraft.util.Hand; -import one.oth3r.sit.Sit; +import one.oth3r.sit.utl.Data; import one.oth3r.sit.utl.Utl; import java.io.BufferedReader; @@ -48,10 +48,22 @@ public class SittingConfig { return version; } + public Boolean getEnabled() { + return enabled; + } + + public void setEnabled(Boolean enabled) { + this.enabled = enabled; + } + public boolean canSitWithHand() { return handSitting; } + public void setHandSitting(Boolean handSitting) { + this.handSitting = handSitting; + } + public HandSetting getHand(Hand handType) { return handType.equals(Hand.MAIN_HAND) ? mainHand : offHand; } @@ -65,7 +77,7 @@ public class SittingConfig { } public static File getFile() { - return new File(Sit.CONFIG_DIR+"sitting-config.json"); + return new File(Data.CONFIG_DIR+"sitting-config.json"); } /** @@ -79,7 +91,7 @@ public class SittingConfig { try (BufferedReader reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)) { Updater.SittingConfigFile.run(reader); } catch (Exception e) { - Sit.LOGGER.error(String.format("ERROR LOADING '%s`: %s", file.getName(),e.getMessage())); + Data.LOGGER.error(String.format("ERROR LOADING '%s`: %s", file.getName(),e.getMessage())); } // save after loading save(); @@ -90,12 +102,12 @@ public class SittingConfig { */ public static void save() { if (!getFile().exists()) { - Sit.LOGGER.info(String.format("Creating new `%s`", getFile().getName())); + Data.LOGGER.info(String.format("Creating new `%s`", getFile().getName())); } try (BufferedWriter writer = Files.newBufferedWriter(getFile().toPath(), StandardCharsets.UTF_8)) { writer.write(Utl.getGson().toJson(FileData.getSittingConfig())); } catch (Exception e) { - Sit.LOGGER.error(String.format("ERROR SAVING '%s`: %s", getFile().getName(), e.getMessage())); + Data.LOGGER.error(String.format("ERROR SAVING '%s`: %s", getFile().getName(), e.getMessage())); } } } diff --git a/src/main/java/one/oth3r/sit/file/Updater.java b/src/main/java/one/oth3r/sit/file/Updater.java index 7fe94cd..74312ab 100644 --- a/src/main/java/one/oth3r/sit/file/Updater.java +++ b/src/main/java/one/oth3r/sit/file/Updater.java @@ -5,7 +5,7 @@ import com.google.gson.GsonBuilder; import com.google.gson.JsonSyntaxException; import com.google.gson.reflect.TypeToken; import net.minecraft.util.Hand; -import one.oth3r.sit.Sit; +import one.oth3r.sit.utl.Data; import one.oth3r.sit.utl.Utl; import java.io.BufferedReader; @@ -108,7 +108,7 @@ public class Updater { */ public static File getLegacyFile() { // strip the new directory - return new File(Sit.CONFIG_DIR.substring(0,Sit.CONFIG_DIR.length()-5)+"Sit!.properties"); + return new File(Data.CONFIG_DIR.substring(0, Data.CONFIG_DIR.length()-5)+"Sit!.properties"); } /** @@ -131,15 +131,15 @@ public class Updater { loadVersion(properties,Double.parseDouble(ver)); } catch (Exception e) { - Sit.LOGGER.error("Error loading legacy config file: {}", e.getMessage()); + Data.LOGGER.error("Error loading legacy config file: {}", e.getMessage()); } // delete the old file try { Files.delete(file.toPath()); - Sit.LOGGER.info("Deleted " + file.getName()); + Data.LOGGER.info("Deleted " + file.getName()); } catch (Exception e) { - Sit.LOGGER.error("Failed to delete the old Sit! config."); + Data.LOGGER.error("Failed to delete the old Sit! config."); } } @@ -278,7 +278,7 @@ public class Updater { ServerConfig.save(); SittingConfig.save(); } catch (Exception e) { - Sit.LOGGER.error("Error loading legacy config: {}", e.getMessage()); + Data.LOGGER.error("Error loading legacy config: {}", e.getMessage()); } } } diff --git a/src/main/java/one/oth3r/sit/packet/SitPayloads.java b/src/main/java/one/oth3r/sit/packet/SitPayloads.java index caae9cb..e1aaba9 100644 --- a/src/main/java/one/oth3r/sit/packet/SitPayloads.java +++ b/src/main/java/one/oth3r/sit/packet/SitPayloads.java @@ -5,12 +5,16 @@ import net.minecraft.network.codec.PacketCodec; import net.minecraft.network.codec.PacketCodecs; import net.minecraft.network.packet.CustomPayload; import net.minecraft.util.Identifier; -import one.oth3r.sit.Sit; +import one.oth3r.sit.utl.Data; public class SitPayloads { + /** + * the packet that the client sends to the server + * @param value the sitting settings for the client + */ public record SettingsPayload(String value) implements CustomPayload { - public static final Id ID = new Id<>(Identifier.of(Sit.MOD_ID,"settings_v2.0")); + public static final Id ID = new Id<>(Identifier.of(Data.MOD_ID,"settings_v2.0")); public static final PacketCodec CODEC = PacketCodecs.STRING.xmap(SettingsPayload::new, SettingsPayload::value).cast(); @@ -19,4 +23,21 @@ public class SitPayloads { return ID; } } + + /** + * the packet that the server sends to the client when responding to the settings payload + */ + public record ResponsePayload(String value) implements CustomPayload { + + public static final String VERSION = "response_v1.0"; + + public static final Id ID = new Id<>(Identifier.of(Data.MOD_ID,VERSION)); + + public static final PacketCodec CODEC = PacketCodecs.STRING.xmap(ResponsePayload::new, ResponsePayload::value).cast(); + + @Override + public Id getId() { + return ID; + } + } } diff --git a/src/main/java/one/oth3r/sit/utl/Data.java b/src/main/java/one/oth3r/sit/utl/Data.java new file mode 100644 index 0000000..7531f6d --- /dev/null +++ b/src/main/java/one/oth3r/sit/utl/Data.java @@ -0,0 +1,65 @@ +package one.oth3r.sit.utl; + +import net.fabricmc.loader.api.FabricLoader; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.command.CommandManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class Data { + public static final String MOD_ID = "sit-oth3r"; + public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID); + + public static final String CONFIG_DIR = FabricLoader.getInstance().getConfigDir().toFile()+"/sit!/"; + + public static final String ENTITY_NAME = "-sit!-entity-"; + + // init on server load + private static MinecraftServer server; + + public static MinecraftServer getServer() { + return server; + } + + public static void setServer(MinecraftServer server) { + Data.server = server; + } + + // client booleans + private static boolean client = false; + private static boolean inGame = false; + private static boolean singleplayer = false; + private static boolean supportedServer = false; + + public static boolean isClient() { + return client; + } + + public static void setClient(boolean client) { + Data.client = client; + } + + public static boolean isInGame() { + return inGame; + } + + public static void setInGame(boolean inGame) { + Data.inGame = inGame; + } + + public static boolean isSingleplayer() { + return singleplayer; + } + + public static void setSingleplayer(boolean singleplayer) { + Data.singleplayer = singleplayer; + } + + public static boolean isSupportedServer() { + return supportedServer; + } + + public static void setSupportedServer(boolean supportedServer) { + Data.supportedServer = supportedServer; + } +} diff --git a/src/main/java/one/oth3r/sit/utl/Events.java b/src/main/java/one/oth3r/sit/utl/Events.java index 80d860d..cb60485 100644 --- a/src/main/java/one/oth3r/sit/utl/Events.java +++ b/src/main/java/one/oth3r/sit/utl/Events.java @@ -1,43 +1,157 @@ package one.oth3r.sit.utl; +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; +import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; import net.fabricmc.fabric.api.event.player.UseBlockCallback; +import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry; import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; +import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; +import net.minecraft.client.network.ClientPlayerEntity; +import net.minecraft.client.option.KeyBinding; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.util.ActionResult; -import one.oth3r.sit.Sit; +import net.minecraft.util.Formatting; import one.oth3r.sit.command.SitCommand; -import one.oth3r.sit.file.Data; +import one.oth3r.sit.file.FileData; +import one.oth3r.sit.file.SittingConfig; +import one.oth3r.sit.packet.SitPayloads; +import org.lwjgl.glfw.GLFW; public class Events { - public static void playerConnections() { + private static class Keybindings { + private static KeyBinding toggle_key; + + private static void register() { + toggle_key = KeyBindingHelper.registerKeyBinding(new KeyBinding( + "key.toggle", + GLFW.GLFW_KEY_UNKNOWN, + "category.sit" + )); + } + + private static void loopLogic(ClientPlayerEntity player) { + while (toggle_key.isPressed()) { + toggle_key.setPressed(false); + if (Data.isInGame()) { + if (Data.isSupportedServer()) { + // get the sitting config + SittingConfig config = FileData.getSittingConfig(); + // toggle the setting + config.setEnabled(!config.getEnabled()); + + // set the sitting config to the new value + FileData.setSittingConfig(config); + // save the changes to the file + SittingConfig.save(); + // send the changes to the server + Utl.sendSettingsPackets(); + + + // get the message settings + String messageKey = "msg.sit_toggle."+(config.getEnabled()?"on":"off"); + Formatting messageColor = config.getEnabled()?Formatting.GREEN:Formatting.RED; + + // send the player the actionbar message + player.sendMessage(Utl.lang("msg.sit_toggle", + Utl.lang(messageKey).formatted(messageColor)), true); + } else { + player.sendMessage(Utl.lang("msg.sit_toggle.unsupported") + .formatted(Formatting.RED), true); + } + } + } + } + } + + private static class Packet { + private static void common() { + // register the data + PayloadTypeRegistry.playC2S().register(SitPayloads.SettingsPayload.ID, SitPayloads.SettingsPayload.CODEC); + + PayloadTypeRegistry.playS2C().register(SitPayloads.ResponsePayload.ID, SitPayloads.ResponsePayload.CODEC); + + // server receiver is common + ServerPlayNetworking.registerGlobalReceiver(SitPayloads.SettingsPayload.ID,((payload, context) -> Data.getServer().execute(() -> { + FileData.setPlayerSetting(context.player(),Utl.getGson().fromJson(payload.value(), SittingConfig.class)); + // send the player back a packet for conformation + ServerPlayNetworking.send(context.player(),new SitPayloads.ResponsePayload(SitPayloads.ResponsePayload.VERSION)); + }))); + } + + private static void client() { + ClientPlayNetworking.registerGlobalReceiver(SitPayloads.ResponsePayload.ID, ((payload, context) -> { + // only update when needed + if (!Data.isSupportedServer()) { + Data.setSupportedServer(true); + Data.LOGGER.info("Connected to Sit! server! packet: {}", payload.value()); + } + })); + } + } + + private static void clientMisc() { + // client tick loop + ClientTickEvents.END_CLIENT_TICK.register(client -> { + assert client.player != null; + Keybindings.loopLogic(client.player); + }); + } + + /** + * registers all client connection code + */ + private static void clientConnections() { + ClientPlayConnectionEvents.JOIN.register((handler, sender, client) -> { + Data.setInGame(true); + if (client.isInSingleplayer()) Data.setSingleplayer(true); + // send a data packet whenever joining a server + Utl.sendSettingsPackets(); + }); + + // reset cashed things on disconnect + ClientPlayConnectionEvents.DISCONNECT.register((handler, client) -> { + Data.setInGame(false); + Data.setSingleplayer(false); + Data.setSupportedServer(false); + }); + } + + /** + * registers all common server player connection code + */ + private static void playerConnections() { // PLAYER JOIN ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> { - Data.setPlayerSetting(handler.player,Data.getHandConfig()); - Data.setCheckPlayer(handler.player, 5); + FileData.setPlayerSetting(handler.player, FileData.getSittingConfig()); + FileData.setCheckPlayer(handler.player, 5); }); ServerPlayConnectionEvents.DISCONNECT.register((handler, server) -> { // if keep is off, remove the entity - if (!Data.getServerConfig().isKeepActive()) { + if (!FileData.getServerConfig().isKeepActive()) { Logic.removeEntity(handler.player); } - Data.removePlayerSetting(handler.player); + FileData.removePlayerSetting(handler.player); }); } - public static void server() { + /** + * registers all server lifecycle events + */ + private static void serverLifecycle() { ServerLifecycleEvents.SERVER_STARTED.register(s -> { - Sit.server = s; - Sit.commandManager = s.getCommandManager(); + Data.setServer(s); // right click on block event UseBlockCallback.EVENT.register((pl, world, hand, hitResult) -> { // get the server player - ServerPlayerEntity player = Sit.server.getPlayerManager().getPlayer(pl.getUuid()); + ServerPlayerEntity player = Data.getServer().getPlayerManager().getPlayer(pl.getUuid()); // make sure the player isn't null, and make sure they aren't in spectator if (player == null || player.isSpectator()) return ActionResult.PASS; @@ -49,22 +163,31 @@ public class Events { }); ServerLifecycleEvents.SERVER_STOPPED.register(s -> { + // clear the server + Data.setServer(null); // clear all player settings (singleplayer and such) - Data.clearPlayerSettings(); + FileData.clearPlayerSettings(); }); - } - public static void misc() { - // loop setup + // server loop setup ServerTickEvents.END_SERVER_TICK.register(minecraftServer -> minecraftServer.execute(LoopManager::tick)); - // command setup + // server command setup CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> SitCommand.register(dispatcher)); } + // a one call method for the common and client + public static void registerCommon() { playerConnections(); - server(); - misc(); + serverLifecycle(); + Packet.common(); + } + + public static void registerClient() { + Keybindings.register(); + clientConnections(); + clientMisc(); + Packet.client(); } } diff --git a/src/main/java/one/oth3r/sit/utl/LoopManager.java b/src/main/java/one/oth3r/sit/utl/LoopManager.java index 5a4c537..5001207 100644 --- a/src/main/java/one/oth3r/sit/utl/LoopManager.java +++ b/src/main/java/one/oth3r/sit/utl/LoopManager.java @@ -3,7 +3,6 @@ package one.oth3r.sit.utl; import net.minecraft.entity.Entity; import net.minecraft.entity.decoration.DisplayEntity; import net.minecraft.server.network.ServerPlayerEntity; -import one.oth3r.sit.Sit; import one.oth3r.sit.file.FileData; import java.util.HashMap; @@ -41,7 +40,7 @@ public class LoopManager { if (player.getVehicle() != null) { Entity entity = player.getVehicle(); - if (entity instanceof DisplayEntity.TextDisplayEntity tde && entity.getName().getString().equals(Sit.ENTITY_NAME)) { + if (entity instanceof DisplayEntity.TextDisplayEntity tde && entity.getName().getString().equals(Data.ENTITY_NAME)) { // bind the entity to the player FileData.addSitEntity(player, tde); // check if the player is still allowed to sit diff --git a/src/main/java/one/oth3r/sit/utl/Utl.java b/src/main/java/one/oth3r/sit/utl/Utl.java index 9c772be..bc19aeb 100644 --- a/src/main/java/one/oth3r/sit/utl/Utl.java +++ b/src/main/java/one/oth3r/sit/utl/Utl.java @@ -5,6 +5,7 @@ import com.google.gson.reflect.TypeToken; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter; import com.google.gson.stream.MalformedJsonException; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; import net.minecraft.block.*; import net.minecraft.block.enums.BlockHalf; import net.minecraft.block.enums.SlabType; @@ -23,8 +24,8 @@ import net.minecraft.util.UseAction; import net.minecraft.util.hit.BlockHitResult; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; -import one.oth3r.sit.Sit; import one.oth3r.sit.file.*; +import one.oth3r.sit.packet.SitPayloads; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -227,7 +228,7 @@ public class Utl { public static DisplayEntity.TextDisplayEntity create(World world, BlockPos blockPos, double sitHeight) { DisplayEntity.TextDisplayEntity entity = new DisplayEntity.TextDisplayEntity(EntityType.TEXT_DISPLAY,world); - entity.setCustomName(Text.of(Sit.ENTITY_NAME)); + entity.setCustomName(Text.of(Data.ENTITY_NAME)); entity.setCustomNameVisible(false); entity.setInvulnerable(true); entity.setInvisible(true); @@ -275,7 +276,7 @@ public class Utl { // get a list of sit entities List list = player.getServerWorld() .getEntitiesByType(TypeFilter.instanceOf(DisplayEntity.TextDisplayEntity.class), - entity -> entity.getName().getString().equals(Sit.ENTITY_NAME)); + entity -> entity.getName().getString().equals(Data.ENTITY_NAME)); // remove each one for (DisplayEntity.TextDisplayEntity entity : list) { @@ -290,20 +291,14 @@ public class Utl { } } - - + /** + * gets a MutableText using the language key, if on server, using the custom lang reader + */ public static MutableText lang(String key, Object... args) { - if (Sit.client) return Text.translatable(key, args); + if (Data.isClient()) return Text.translatable(key, args); else return LangReader.of(key, args).getTxT(); } -// public static class ConfigExamples { -// public static final String CUSTOM_BLOCKS = "\"minecraft:campfire|0.255|1|lit=false\""; -// public static final String REQUIREMENT_OPTIONS = String.format("%s, %s, %s", -// configFile.HandRequirement.empty,configFile.HandRequirement.restrictive,configFile.HandRequirement.none); -// public static final String LIST = "\"minecraft:torch\""; -// } - public static class Enum { public static > T get(Object enumString, Class enumType) { @@ -327,6 +322,16 @@ public class Utl { } } + // todo call when editing a config on the client + /** + * sends the settings packets to the server, if client & in game + */ + public static void sendSettingsPackets() { + if (Data.isClient() && Data.isInGame()) { + ClientPlayNetworking.send(new SitPayloads.SettingsPayload(Utl.getGson().toJson(FileData.getSittingConfig()))); + } + } + /** * gets a Gson with the LenientTypeAdapter */ diff --git a/src/main/resources/assets/sit/lang/en_us.json b/src/main/resources/assets/sit/lang/en_us.json index 703f71c..27e2a16 100644 --- a/src/main/resources/assets/sit/lang/en_us.json +++ b/src/main/resources/assets/sit/lang/en_us.json @@ -45,8 +45,10 @@ "config.hand.restriction.blacklist": "Blacklist", "config.example": "Example: %s", - "msg.sit_toggle.on": "Enabled Sitting!", - "msg.sit_toggle.off": "Disabled Sitting!", + "msg.sit_toggle": "%s Sitting!", + "msg.sit_toggle.on": "Enabled", + "msg.sit_toggle.off": "Disabled", + "msg.sit_toggle.unsupported": "Sitting is not available on this server.", "msg.reloaded": "Reloaded the config!", "msg.purged": "Purged all active chair entities!",