From f9dd64c00ecb84e50fd684d6d665f5d372d913fc Mon Sep 17 00:00:00 2001 From: burningtnt Date: Sun, 15 Dec 2024 15:25:26 +0800 Subject: [PATCH] 0.6.10 --- gradle.properties | 2 +- run/config/SignMeUp/Waypoints.json | 22 ----- .../java/org/teacon/signmeup/SignMeUp.java | 3 - .../teacon/signmeup/command/OpCommands.java | 59 +++++++------ .../org/teacon/signmeup/config/Waypoints.java | 60 ------------- .../signmeup/config/waypoints/Waypoint.java | 83 ++++++++++++++++++ .../waypoints/WaypointClientEvents.java | 15 ++++ .../config/waypoints/WaypointSavedData.java | 84 +++++++++++++++++++ .../WaypointServerEvents.java} | 17 ++-- .../org/teacon/signmeup/gui/map/MapPanel.java | 4 +- .../signmeup/gui/map/WayPointsPanel.java | 45 +++++----- .../gui/map/bp/WayPointsButtonPanel.java | 13 ++- .../network/RemoveWaypointPacket.java | 15 +++- .../signmeup/network/SetWaypointPacket.java | 24 ++---- .../signmeup/network/SyncWaypointPacket.java | 43 +++------- .../network/TeleportToWayPointPacket.java | 20 ++--- 16 files changed, 287 insertions(+), 222 deletions(-) delete mode 100644 run/config/SignMeUp/Waypoints.json delete mode 100644 src/main/java/org/teacon/signmeup/config/Waypoints.java create mode 100644 src/main/java/org/teacon/signmeup/config/waypoints/Waypoint.java create mode 100644 src/main/java/org/teacon/signmeup/config/waypoints/WaypointClientEvents.java create mode 100644 src/main/java/org/teacon/signmeup/config/waypoints/WaypointSavedData.java rename src/main/java/org/teacon/signmeup/config/{WaypointSyncHandle.java => waypoints/WaypointServerEvents.java} (66%) diff --git a/gradle.properties b/gradle.properties index 7fd7ae8..b73d156 100644 --- a/gradle.properties +++ b/gradle.properties @@ -34,7 +34,7 @@ mod_name=Sign Me Up # The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default. mod_license=BSD-3-Clause # The mod version. See https://semver.org/ -mod_version=0.6.9 +mod_version=0.6.10 # The group ID for the mod. It is only important when publishing as an artifact to a Maven repository. # This should match the base package used for the mod sources. # See https://maven.apache.org/guides/mini/guide-naming-conventions.html diff --git a/run/config/SignMeUp/Waypoints.json b/run/config/SignMeUp/Waypoints.json deleted file mode 100644 index 1ca5123..0000000 --- a/run/config/SignMeUp/Waypoints.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "waypoints": [ - { - "name": "#主展馆", - "description": "描述内容", - "x": -130, - "y": 74, - "z": 28, - "rx": 9.150063, - "ry": 53.999992 - }, - { - "name": "展馆1", - "description": "\"描述\"", - "x": -118, - "y": 70, - "z": 14, - "rx": 82.19973, - "ry": 43.800064 - } - ] -} \ No newline at end of file diff --git a/src/main/java/org/teacon/signmeup/SignMeUp.java b/src/main/java/org/teacon/signmeup/SignMeUp.java index 6d6ff12..6672eef 100644 --- a/src/main/java/org/teacon/signmeup/SignMeUp.java +++ b/src/main/java/org/teacon/signmeup/SignMeUp.java @@ -1,7 +1,6 @@ package org.teacon.signmeup; import cn.ussshenzhou.t88.config.ConfigHelper; -import net.minecraft.client.Minecraft; import net.minecraft.resources.ResourceLocation; import net.neoforged.bus.api.IEventBus; import net.neoforged.fml.ModContainer; @@ -11,7 +10,6 @@ import org.teacon.signmeup.config.Map; import org.teacon.signmeup.config.MiniMap; import org.teacon.signmeup.config.PlayerCommands; -import org.teacon.signmeup.config.Waypoints; /** * @author USS_Shenzhou @@ -28,7 +26,6 @@ public SignMeUp(IEventBus bus, ModContainer mod) { ConfigHelper.loadConfig(new PlayerCommands()); ConfigHelper.loadConfig(new Map()); - ConfigHelper.loadConfig(new Waypoints()); ConfigHelper.loadConfig(new MiniMap()); } diff --git a/src/main/java/org/teacon/signmeup/command/OpCommands.java b/src/main/java/org/teacon/signmeup/command/OpCommands.java index a8f0bff..c102b9e 100644 --- a/src/main/java/org/teacon/signmeup/command/OpCommands.java +++ b/src/main/java/org/teacon/signmeup/command/OpCommands.java @@ -1,6 +1,5 @@ package org.teacon.signmeup.command; -import cn.ussshenzhou.t88.config.ConfigHelper; import cn.ussshenzhou.t88.network.NetworkHelper; import com.mojang.brigadier.Command; import com.mojang.brigadier.CommandDispatcher; @@ -13,16 +12,13 @@ import net.minecraft.commands.arguments.coordinates.RotationArgument; import net.minecraft.commands.arguments.coordinates.Vec3Argument; import net.minecraft.commands.arguments.coordinates.WorldCoordinates; -import net.minecraft.core.Rotations; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.Style; import org.teacon.signmeup.command.argument.SpaceBreakStringArgumentType; -import org.teacon.signmeup.config.Waypoints; +import org.teacon.signmeup.config.waypoints.Waypoint; import org.teacon.signmeup.network.RemoveWaypointPacket; import org.teacon.signmeup.network.SetWaypointPacket; -import java.util.Optional; - /** * @author USS_Shenzhou */ @@ -46,8 +42,8 @@ public static void waypoints(CommandDispatcher dispatcher) { .then(Commands.literal("remove") .then(Commands.argument("name", SpaceBreakStringArgumentType.string()) .suggests((context, builder) -> SharedSuggestionProvider.suggest( - ConfigHelper.getConfigRead(Waypoints.class).waypoints.stream().map(wayPoint -> wayPoint.name), builder)) - .executes(OpCommands::removeWaypoint) + Waypoint.INSTANCES.keySet(), builder + )).executes(OpCommands::removeWaypoint) ) ) ); @@ -58,37 +54,38 @@ private static int setWaypoint(CommandContext context) { var name = context.getArgument("name", String.class); var description = context.getArgument("description", String.class); var rotation = context.getArgument("rotation", WorldCoordinates.class).getRotation(context.getSource()); - var waypoint = new Waypoints.WayPoint(name, description, pos.getX(), pos.getY(), pos.getZ(), rotation.y, rotation.x); + var waypoint = new Waypoint(name, description, pos.getX(), pos.getY(), pos.getZ(), rotation.y, rotation.x); + + Waypoint previous = Waypoint.INSTANCES.get(name); + if (previous != null) { + context.getSource().sendSuccess(() -> Component.literal(previous + " already exists. Remove it first if you want to replace it."), true); + } else { + Waypoint.INSTANCES.put(name, waypoint); + NetworkHelper.sendToAllPlayers(new SetWaypointPacket(waypoint)); + + context.getSource().sendSuccess(() -> Component.literal(waypoint + " has been added."), true); + if (description.startsWith("\"") && description.endsWith("\"")) { + context.getSource().sendSuccess(() -> Component.literal("The waypoint description is quoted with '\"'. This is a greedy string where '\"' is unnecessary.").setStyle(Style.EMPTY.withColor(ChatFormatting.YELLOW)), true); + } + } - ConfigHelper.getConfigWrite(Waypoints.class, waypoints -> { - waypoints.waypoints.stream().filter(w -> w.name.equals(waypoint.name)).findFirst().ifPresentOrElse( - point -> context.getSource().sendSuccess(() -> Component.literal(point + " already exists. Remove it first if you want to replace it."), true), - () -> { - waypoints.waypoints.add(waypoint); - NetworkHelper.sendToAllPlayers(new SetWaypointPacket(name, description, pos, new Rotations(rotation.y, 0, rotation.x))); - context.getSource().sendSuccess(() -> Component.literal(waypoint + " has been added."), true); - if (description.startsWith("\"") && description.endsWith("\"")) { - context.getSource().sendSuccess(() -> Component.literal("The waypoint description is quoted with '\"'. This is a greedy string where '\"' is unnecessary.").setStyle(Style.EMPTY.withColor(ChatFormatting.YELLOW)), true); - } - } - ); - }); + Waypoint.save(); return Command.SINGLE_SUCCESS; } private static int removeWaypoint(CommandContext context) { var name = context.getArgument("name", String.class); - var waypoint = Waypoints.WayPoint.dumbWayPoint(name); - ConfigHelper.getConfigWrite(Waypoints.class, waypoints -> { - if (waypoints.waypoints.remove(waypoint)) { - context.getSource().sendSuccess(() -> Component.literal(waypoint + " has been removed."), true); - Optional.ofNullable(context.getSource().getPlayer()) - .ifPresent(player -> NetworkHelper.sendToAllPlayers(new RemoveWaypointPacket(name))); - } else { - context.getSource().sendFailure(Component.literal("No waypoint called " + name)); - } - }); + Waypoint waypoint = Waypoint.INSTANCES.remove(name); + + if (waypoint != null) { + context.getSource().sendSuccess(() -> Component.literal(waypoint + " has been removed."), true); + NetworkHelper.sendToAllPlayers(new RemoveWaypointPacket(name)); + } else { + context.getSource().sendFailure(Component.literal("No waypoint called " + name)); + } + + Waypoint.save(); return Command.SINGLE_SUCCESS; } } diff --git a/src/main/java/org/teacon/signmeup/config/Waypoints.java b/src/main/java/org/teacon/signmeup/config/Waypoints.java deleted file mode 100644 index 1b83551..0000000 --- a/src/main/java/org/teacon/signmeup/config/Waypoints.java +++ /dev/null @@ -1,60 +0,0 @@ -package org.teacon.signmeup.config; - -import cn.ussshenzhou.t88.config.TConfig; -import net.minecraft.core.BlockPos; - -import java.util.LinkedHashSet; - -/** - * @author USS_Shenzhou - */ -public class Waypoints implements TConfig { - @Override - public String getChildDirName() { - return "SignMeUp"; - } - - public LinkedHashSet waypoints = new LinkedHashSet<>(); - - public static class WayPoint { - public String name, description; - public int x, y, z; - public float rx, ry; - - public WayPoint(String name, String description, int x, int y, int z, float rx, float ry) { - this.name = name; - this.description = description; - this.x = x; - this.y = y; - this.z = z; - this.rx = rx; - this.ry = ry; - } - - public WayPoint() { - this("", "", 0, 0, 0, 0, 0); - } - - public static WayPoint dumbWayPoint(String name) { - return new WayPoint(name, "", 0, 0, 0, 0, 0); - } - - @Override - public boolean equals(Object obj) { - if (obj instanceof WayPoint that) { - return this.name.equals(that.name); - } - return false; - } - - @Override - public int hashCode() { - return name.hashCode(); - } - - @Override - public String toString() { - return "[WayPoint name=" + name + ", description=" + description + ", <" + x + ", " + y + ", " + z + ">, rotation=<" + rx + ", " + ry + ">]"; - } - } -} diff --git a/src/main/java/org/teacon/signmeup/config/waypoints/Waypoint.java b/src/main/java/org/teacon/signmeup/config/waypoints/Waypoint.java new file mode 100644 index 0000000..98d7813 --- /dev/null +++ b/src/main/java/org/teacon/signmeup/config/waypoints/Waypoint.java @@ -0,0 +1,83 @@ +package org.teacon.signmeup.config.waypoints; + +import io.netty.buffer.ByteBuf; +import net.minecraft.network.codec.ByteBufCodecs; +import net.minecraft.network.codec.StreamCodec; +import org.jetbrains.annotations.NotNull; + +import java.util.Map; +import java.util.Objects; +import java.util.concurrent.ConcurrentHashMap; + +public class Waypoint { + public static final Map INSTANCES = new ConcurrentHashMap<>(); + + public static final StreamCodec CODEC = new StreamCodec<>() { + @Override + public @NotNull Waypoint decode(@NotNull ByteBuf buffer) { + return new Waypoint( + ByteBufCodecs.STRING_UTF8.decode(buffer), + ByteBufCodecs.STRING_UTF8.decode(buffer), + ByteBufCodecs.VAR_INT.decode(buffer), + ByteBufCodecs.VAR_INT.decode(buffer), + ByteBufCodecs.VAR_INT.decode(buffer), + ByteBufCodecs.FLOAT.decode(buffer), + ByteBufCodecs.FLOAT.decode(buffer) + ); + } + + @Override + public void encode(@NotNull ByteBuf buffer, @NotNull Waypoint value) { + ByteBufCodecs.STRING_UTF8.encode(buffer, value.name); + ByteBufCodecs.STRING_UTF8.encode(buffer, value.description); + ByteBufCodecs.VAR_INT.encode(buffer, value.x); + ByteBufCodecs.VAR_INT.encode(buffer, value.y); + ByteBufCodecs.VAR_INT.encode(buffer, value.z); + ByteBufCodecs.FLOAT.encode(buffer, value.rx); + ByteBufCodecs.FLOAT.encode(buffer, value.ry); + } + }; + + public static void load() { + for (Waypoint waypoint : WaypointSavedData.load()) { + INSTANCES.put(waypoint.name, waypoint); + } + } + + public static void save() { + WaypointSavedData.save(INSTANCES.values()); + } + + public final String name, description; + public final int x, y, z; + + public final float rx, ry; + + public Waypoint(String name, String description, int x, int y, int z, float rx, float ry) { + this.name = name; + this.description = description; + this.x = x; + this.y = y; + this.z = z; + this.rx = rx; + this.ry = ry; + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof Waypoint that) { + return Objects.equals(that.name, name); + } + return false; + } + + @Override + public int hashCode() { + return name.hashCode(); + } + + @Override + public String toString() { + return "[WayPoint name=" + name + ", description=" + description + ", <" + x + ", " + y + ", " + z + ">, rotation=<" + rx + ", " + ry + ">]"; + } +} diff --git a/src/main/java/org/teacon/signmeup/config/waypoints/WaypointClientEvents.java b/src/main/java/org/teacon/signmeup/config/waypoints/WaypointClientEvents.java new file mode 100644 index 0000000..fb84863 --- /dev/null +++ b/src/main/java/org/teacon/signmeup/config/waypoints/WaypointClientEvents.java @@ -0,0 +1,15 @@ +package org.teacon.signmeup.config.waypoints; + +import net.neoforged.api.distmarker.Dist; +import net.neoforged.bus.api.SubscribeEvent; +import net.neoforged.fml.common.EventBusSubscriber; +import net.neoforged.neoforge.client.event.ClientPlayerNetworkEvent; +import org.teacon.signmeup.SignMeUp; + +@EventBusSubscriber(value = Dist.CLIENT, modid = SignMeUp.MODID, bus = EventBusSubscriber.Bus.GAME) +public class WaypointClientEvents { + @SubscribeEvent + public static void onPlayerLoggedOut(ClientPlayerNetworkEvent.LoggingOut event) { + Waypoint.INSTANCES.clear(); + } +} diff --git a/src/main/java/org/teacon/signmeup/config/waypoints/WaypointSavedData.java b/src/main/java/org/teacon/signmeup/config/waypoints/WaypointSavedData.java new file mode 100644 index 0000000..2eed191 --- /dev/null +++ b/src/main/java/org/teacon/signmeup/config/waypoints/WaypointSavedData.java @@ -0,0 +1,84 @@ +package org.teacon.signmeup.config.waypoints; + +import net.minecraft.client.Minecraft; +import net.minecraft.core.HolderLookup; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.server.MinecraftServer; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.saveddata.SavedData; +import net.neoforged.fml.loading.FMLEnvironment; +import net.neoforged.neoforge.server.ServerLifecycleHooks; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.teacon.signmeup.SignMeUp; + +import java.util.*; + +public class WaypointSavedData extends SavedData { + private static final Factory FACTORY = new Factory<>(WaypointSavedData::new, WaypointSavedData::load); + + private List POINT = new ArrayList<>(); + + private static WaypointSavedData load(CompoundTag tag, HolderLookup.Provider provider) { + WaypointSavedData data = new WaypointSavedData(); + for (String name : tag.getAllKeys()) { + CompoundTag point = tag.getCompound(name); + CompoundTag rotation = point.getCompound("rotation"); + int[] pos = point.getIntArray("pos"); + + data.POINT.add(new Waypoint( + name, point.getString("description"), + pos[0], pos[1], pos[2], rotation.getFloat("x"), rotation.getFloat("y") + )); + } + + data.POINT = Collections.unmodifiableList(data.POINT); + return data; + } + + public static Collection load() { + WaypointSavedData instance = getInstance(); + return instance == null ? Collections.emptyList() : instance.POINT; + } + + public static void save(Collection values) { + WaypointSavedData data = getInstance(); + if (data == null) return; + data.POINT = List.copyOf(values); + data.setDirty(); + } + + private static @Nullable WaypointSavedData getInstance() { + MinecraftServer server = switch (FMLEnvironment.dist) { + case CLIENT -> Minecraft.getInstance().getSingleplayerServer(); + case DEDICATED_SERVER -> ServerLifecycleHooks.getCurrentServer(); + }; + if (server == null) { + return null; + } + + WaypointSavedData data = Objects.requireNonNull(server.getLevel(Level.OVERWORLD), "Level 'overworld' should exist.").getDataStorage() + .computeIfAbsent(WaypointSavedData.FACTORY, SignMeUp.MODID + "_waypoints"); + return data; + } + + @Override + public @NotNull CompoundTag save(@NotNull CompoundTag tag, HolderLookup.@NotNull Provider registries) { + for (Waypoint waypoint : Waypoint.INSTANCES.values()) { + CompoundTag point = new CompoundTag(); + { + point.putString("description", waypoint.description); + point.putIntArray("pos", new int[]{waypoint.x, waypoint.y, waypoint.z}); + CompoundTag rotation = new CompoundTag(); + { + rotation.putFloat("x", waypoint.rx); + rotation.putFloat("y", waypoint.ry); + } + point.put("rotation", rotation); + } + tag.put(waypoint.name, point); + } + + return tag; + } +} diff --git a/src/main/java/org/teacon/signmeup/config/WaypointSyncHandle.java b/src/main/java/org/teacon/signmeup/config/waypoints/WaypointServerEvents.java similarity index 66% rename from src/main/java/org/teacon/signmeup/config/WaypointSyncHandle.java rename to src/main/java/org/teacon/signmeup/config/waypoints/WaypointServerEvents.java index 31b5b8a..aaf46b3 100644 --- a/src/main/java/org/teacon/signmeup/config/WaypointSyncHandle.java +++ b/src/main/java/org/teacon/signmeup/config/waypoints/WaypointServerEvents.java @@ -1,6 +1,5 @@ -package org.teacon.signmeup.config; +package org.teacon.signmeup.config.waypoints; -import cn.ussshenzhou.t88.config.ConfigHelper; import cn.ussshenzhou.t88.network.NetworkHelper; import net.minecraft.client.Minecraft; import net.minecraft.client.player.LocalPlayer; @@ -10,26 +9,34 @@ import net.neoforged.fml.common.EventBusSubscriber; import net.neoforged.fml.loading.FMLEnvironment; import net.neoforged.neoforge.event.entity.player.PlayerEvent; +import net.neoforged.neoforge.event.server.ServerLifecycleEvent; +import net.neoforged.neoforge.event.server.ServerStartedEvent; import org.teacon.signmeup.SignMeUp; import org.teacon.signmeup.network.SyncWaypointPacket; import java.util.List; import java.util.Objects; -@EventBusSubscriber(value = Dist.DEDICATED_SERVER, modid = SignMeUp.MODID, bus = EventBusSubscriber.Bus.GAME) -public class WaypointSyncHandle { +@EventBusSubscriber(modid = SignMeUp.MODID, bus = EventBusSubscriber.Bus.GAME) +public class WaypointServerEvents { + @SubscribeEvent + public static void onServerLaunch(ServerStartedEvent event) { + Waypoint.load(); + } + @SubscribeEvent public static void onPlayerLoggedIn(PlayerEvent.PlayerLoggedInEvent event) { if (event.getEntity() instanceof ServerPlayer player) { if (FMLEnvironment.dist == Dist.CLIENT) { LocalPlayer lp = Minecraft.getInstance().player; if (lp != null && Objects.equals(lp.getUUID(), player.getUUID())) { + // If this is a client, we only use in-memory communication with the client. return; } } player.server.execute(() -> NetworkHelper.sendToPlayer(player, new SyncWaypointPacket( - List.copyOf(ConfigHelper.getConfigRead(Waypoints.class).waypoints) + List.copyOf(Waypoint.INSTANCES.values()) ))); } } diff --git a/src/main/java/org/teacon/signmeup/gui/map/MapPanel.java b/src/main/java/org/teacon/signmeup/gui/map/MapPanel.java index dbab49c..b06998d 100644 --- a/src/main/java/org/teacon/signmeup/gui/map/MapPanel.java +++ b/src/main/java/org/teacon/signmeup/gui/map/MapPanel.java @@ -15,7 +15,7 @@ import org.joml.Vector2i; import org.teacon.signmeup.SignMeUp; import org.teacon.signmeup.config.Map; -import org.teacon.signmeup.config.Waypoints; +import org.teacon.signmeup.config.waypoints.Waypoint; import java.util.List; @@ -179,7 +179,7 @@ protected ResourceLocation getScrollerVerticalTexture() { return SCROLLER_VERTICAL; } - public List getHighlightWaypoints(double pMouseX, double pMouseY) { + public List getHighlightWaypoints(double pMouseX, double pMouseY) { return wayPointsPanel.getHighlightWaypoints(pMouseX + scrollAmountX, pMouseY + scrollAmountY); } diff --git a/src/main/java/org/teacon/signmeup/gui/map/WayPointsPanel.java b/src/main/java/org/teacon/signmeup/gui/map/WayPointsPanel.java index 65ca99e..2beba69 100644 --- a/src/main/java/org/teacon/signmeup/gui/map/WayPointsPanel.java +++ b/src/main/java/org/teacon/signmeup/gui/map/WayPointsPanel.java @@ -1,6 +1,5 @@ package org.teacon.signmeup.gui.map; -import cn.ussshenzhou.t88.config.ConfigHelper; import cn.ussshenzhou.t88.gui.util.HorizontalAlignment; import cn.ussshenzhou.t88.gui.util.LayoutHelper; import cn.ussshenzhou.t88.gui.widegt.TImage; @@ -16,7 +15,7 @@ import net.minecraft.world.level.Level; import org.joml.Vector2i; import org.teacon.signmeup.SignMeUp; -import org.teacon.signmeup.config.Waypoints; +import org.teacon.signmeup.config.waypoints.Waypoint; import org.teacon.signmeup.network.TeleportToWayPointPacket; import java.util.*; @@ -26,7 +25,7 @@ * @author USS_Shenzhou */ public class WayPointsPanel extends TPanel { - private final BiMap logicWaypoints = HashBiMap.create(ConfigHelper.getConfigRead(Waypoints.class).waypoints.size()); + private final BiMap logicWaypoints = HashBiMap.create(Waypoint.INSTANCES.size()); private final ArrayList visualWayPoints = new ArrayList<>(); private static final int DOT_SIZE = 18; @@ -38,7 +37,7 @@ private int getMergeRange() { return (int) (DOT_SIZE * 0.75); } - public List getHighlightWaypoints(double mouseX, double mouseY) { + public List getHighlightWaypoints(double mouseX, double mouseY) { for (WayPointDot dot : visualWayPoints) { if (dot.isVisibleT() && dot.isInRange(mouseX, mouseY)) { return dot.getLogicWaypoints(); @@ -50,8 +49,8 @@ public List getHighlightWaypoints(double mouseX, double mous public Vector2i lookupWaypoint(String waypoint) { for (WayPointDot dot : visualWayPoints) { - List ps = dot.getLogicWaypoints(); - for (Waypoints.WayPoint p : ps) { + List ps = dot.getLogicWaypoints(); + for (Waypoint p : ps) { if (p.name.equals(waypoint)) { return new Vector2i(dot.getXT() + dot.getWidth() / 2, dot.getYT() + dot.getHeight() / 2); } @@ -62,13 +61,11 @@ public Vector2i lookupWaypoint(String waypoint) { protected void update() { if (logicWaypoints.isEmpty()) { - ConfigHelper.getConfigRead(Waypoints.class).waypoints.forEach( - wayPoint -> { - WayPointDot dot = new WayPointDot(SignMeUp.id("textures/gui/waypoint.png")); - dot.setTooltip(Tooltip.create(Component.translatable("gui.sign_up.map.teleport", wayPoint.name))); - logicWaypoints.put(wayPoint, dot); - } - ); + Waypoint.INSTANCES.values().forEach(wayPoint -> { + WayPointDot dot = new WayPointDot(SignMeUp.id("textures/gui/waypoint.png")); + dot.setTooltip(Tooltip.create(Component.translatable("gui.sign_up.map.teleport", wayPoint.name))); + logicWaypoints.put(wayPoint, dot); + }); } logicWaypoints.forEach((wayPoint, wayPointDot) -> { var pos = WayPointsPanel.this.getParentInstanceOf(MapPanel.class).map.worldToGui(wayPoint.x, wayPoint.z); @@ -107,7 +104,7 @@ public WayPointDot(ResourceLocation imageLocation) { super(imageLocation); } - public List getLogicWaypoints() { + public List getLogicWaypoints() { return List.of(logicWaypoints.inverse().get(this)); } @@ -136,7 +133,7 @@ public boolean mouseClicked(double pMouseX, double pMouseY, int pButton) { } public class WayPointMultiDot extends WayPointDot { - private final ArrayList containedWaypoints = new ArrayList<>(); + private final ArrayList containedWaypoints = new ArrayList<>(); private final TLabel number = new TLabel(); public WayPointMultiDot(ResourceLocation imageLocation) { @@ -148,7 +145,7 @@ public WayPointMultiDot(ResourceLocation imageLocation) { } @Override - public List getLogicWaypoints() { + public List getLogicWaypoints() { return Collections.unmodifiableList(containedWaypoints); } @@ -158,19 +155,19 @@ public void layout() { super.layout(); } - private void joinAll(Collection wayPoints) { - containedWaypoints.addAll(wayPoints); - var x = wayPoints.stream() + private void joinAll(Collection waypoints) { + containedWaypoints.addAll(waypoints); + var x = waypoints.stream() .mapToInt(waypoint -> waypoint.x) - .sum() / wayPoints.size(); - var z = wayPoints.stream() + .sum() / waypoints.size(); + var z = waypoints.stream() .mapToInt(waypoint -> waypoint.z) - .sum() / wayPoints.size(); + .sum() / waypoints.size(); var pos = WayPointsPanel.this.getParentInstanceOf(MapPanel.class).map.worldToGui(x, z); this.setAbsBounds(pos.x - DOT_SIZE / 2, pos.y - DOT_SIZE / 2, DOT_SIZE, DOT_SIZE); - number.setText(Component.literal(String.valueOf(wayPoints.size()))); + number.setText(Component.literal(String.valueOf(waypoints.size()))); - setTooltip(Tooltip.create(Component.literal(wayPoints.stream().map(p -> p.name).collect(Collectors.joining("\n"))))); + setTooltip(Tooltip.create(Component.literal(waypoints.stream().map(p -> p.name).collect(Collectors.joining("\n"))))); } } } diff --git a/src/main/java/org/teacon/signmeup/gui/map/bp/WayPointsButtonPanel.java b/src/main/java/org/teacon/signmeup/gui/map/bp/WayPointsButtonPanel.java index 875ce3b..f371bde 100644 --- a/src/main/java/org/teacon/signmeup/gui/map/bp/WayPointsButtonPanel.java +++ b/src/main/java/org/teacon/signmeup/gui/map/bp/WayPointsButtonPanel.java @@ -1,6 +1,5 @@ package org.teacon.signmeup.gui.map.bp; -import cn.ussshenzhou.t88.config.ConfigHelper; import cn.ussshenzhou.t88.gui.advanced.THoverSensitiveImageButton; import cn.ussshenzhou.t88.gui.widegt.TWidget; import cn.ussshenzhou.t88.network.NetworkHelper; @@ -12,7 +11,7 @@ import net.minecraft.world.level.Level; import org.jetbrains.annotations.Nullable; import org.teacon.signmeup.SignMeUp; -import org.teacon.signmeup.config.Waypoints; +import org.teacon.signmeup.config.waypoints.Waypoint; import org.teacon.signmeup.gui.map.ButtonPanelBase; import org.teacon.signmeup.network.TeleportToWayPointPacket; @@ -41,11 +40,11 @@ public THoverSensitiveImageButtonImpl(String waypointName, Button.OnPress onPres public WayPointsButtonPanel() { super(true); - Waypoints.WayPoint[] waypoints = ConfigHelper.getConfigRead(Waypoints.class).waypoints.toArray(Waypoints.WayPoint[]::new); + Waypoint[] waypoints = Waypoint.INSTANCES.values().toArray(Waypoint[]::new); int staticWP = 0; for (int i = 0; i < waypoints.length; i++) { - Waypoints.WayPoint item = waypoints[i]; + Waypoint item = waypoints[i]; if (item.name.startsWith("#")) { waypoints[i] = waypoints[staticWP]; waypoints[staticWP] = item; @@ -57,12 +56,12 @@ public WayPointsButtonPanel() { Random random = new Random(uuid.getLeastSignificantBits() ^ uuid.getMostSignificantBits()); for (int i = waypoints.length; i > staticWP + 1; --i) { int k1 = random.nextInt(i - staticWP) + staticWP, k2 = i - 1; - Waypoints.WayPoint t = waypoints[k2]; + Waypoint t = waypoints[k2]; waypoints[k2] = waypoints[k1]; waypoints[k1] = t; } - for (Waypoints.WayPoint wayPoint : waypoints) { + for (Waypoint wayPoint : waypoints) { var button = new THoverSensitiveImageButtonImpl( wayPoint.name, b -> { @@ -82,7 +81,7 @@ public WayPointsButtonPanel() { } } - public void highlight(List highlightWaypoints) { + public void highlight(List highlightWaypoints) { if (highlightWaypoints.isEmpty()) { for (TWidget child : this.buttons.getChildren()) { if (child instanceof THoverSensitiveImageButton btn) { diff --git a/src/main/java/org/teacon/signmeup/network/RemoveWaypointPacket.java b/src/main/java/org/teacon/signmeup/network/RemoveWaypointPacket.java index d7f87c5..e8b73d8 100644 --- a/src/main/java/org/teacon/signmeup/network/RemoveWaypointPacket.java +++ b/src/main/java/org/teacon/signmeup/network/RemoveWaypointPacket.java @@ -1,15 +1,17 @@ package org.teacon.signmeup.network; -import cn.ussshenzhou.t88.config.ConfigHelper; import cn.ussshenzhou.t88.network.annotation.ClientHandler; import cn.ussshenzhou.t88.network.annotation.Codec; import cn.ussshenzhou.t88.network.annotation.NetPacket; +import com.mojang.authlib.minecraft.client.MinecraftClient; import io.netty.buffer.ByteBuf; import net.minecraft.network.codec.ByteBufCodecs; import net.minecraft.network.codec.StreamCodec; +import net.neoforged.neoforge.event.server.ServerLifecycleEvent; import net.neoforged.neoforge.network.handling.IPayloadContext; +import net.neoforged.neoforge.server.ServerLifecycleHooks; import org.teacon.signmeup.SignMeUp; -import org.teacon.signmeup.config.Waypoints; +import org.teacon.signmeup.config.waypoints.Waypoint; import org.teacon.signmeup.gui.map.MapScreen; /** @@ -27,7 +29,12 @@ public record RemoveWaypointPacket(String name) { @ClientHandler public void clientHandler(IPayloadContext context) { - ConfigHelper.getConfigWrite(Waypoints.class, waypoints -> waypoints.waypoints.remove(Waypoints.WayPoint.dumbWayPoint(name))); - context.enqueueWork(MapScreen::refreshInstance); + context.enqueueWork(() -> { + if (ServerLifecycleHooks.getCurrentServer() == null) { + Waypoint.INSTANCES.remove(name); + } + + MapScreen.refreshInstance(); + }); } } diff --git a/src/main/java/org/teacon/signmeup/network/SetWaypointPacket.java b/src/main/java/org/teacon/signmeup/network/SetWaypointPacket.java index d980b76..92a1ae9 100644 --- a/src/main/java/org/teacon/signmeup/network/SetWaypointPacket.java +++ b/src/main/java/org/teacon/signmeup/network/SetWaypointPacket.java @@ -1,43 +1,35 @@ package org.teacon.signmeup.network; -import cn.ussshenzhou.t88.config.ConfigHelper; import cn.ussshenzhou.t88.network.annotation.ClientHandler; import cn.ussshenzhou.t88.network.annotation.Codec; import cn.ussshenzhou.t88.network.annotation.NetPacket; import io.netty.buffer.ByteBuf; -import net.minecraft.core.BlockPos; -import net.minecraft.core.Rotations; -import net.minecraft.network.codec.ByteBufCodecs; import net.minecraft.network.codec.StreamCodec; import net.neoforged.neoforge.network.handling.IPayloadContext; +import net.neoforged.neoforge.server.ServerLifecycleHooks; import org.teacon.signmeup.SignMeUp; -import org.teacon.signmeup.config.Waypoints; +import org.teacon.signmeup.config.waypoints.Waypoint; import org.teacon.signmeup.gui.map.MapScreen; /** * @author USS_Shenzhou */ @NetPacket(modid = SignMeUp.MODID) -public record SetWaypointPacket(String name, String description, BlockPos pos, Rotations rotation) { +public record SetWaypointPacket(Waypoint waypoint) { @Codec public static final StreamCodec STREAM_CODEC = StreamCodec.composite( - ByteBufCodecs.STRING_UTF8, - SetWaypointPacket::name, - ByteBufCodecs.STRING_UTF8, - SetWaypointPacket::description, - BlockPos.STREAM_CODEC, - SetWaypointPacket::pos, - Rotations.STREAM_CODEC, - SetWaypointPacket::rotation, + Waypoint.CODEC, + SetWaypointPacket::waypoint, SetWaypointPacket::new ); @ClientHandler public void clientHandler(IPayloadContext context) { - var waypoint = new Waypoints.WayPoint(name, description, pos.getX(), pos.getY(), pos.getZ(), rotation.getX(), rotation.getZ()); context.enqueueWork(() -> { - ConfigHelper.getConfigWrite(Waypoints.class, waypoints -> waypoints.waypoints.add(waypoint)); + if (ServerLifecycleHooks.getCurrentServer() == null) { + Waypoint.INSTANCES.put(waypoint.name, waypoint); + } MapScreen.refreshInstance(); }); diff --git a/src/main/java/org/teacon/signmeup/network/SyncWaypointPacket.java b/src/main/java/org/teacon/signmeup/network/SyncWaypointPacket.java index 1a1c131..85013db 100644 --- a/src/main/java/org/teacon/signmeup/network/SyncWaypointPacket.java +++ b/src/main/java/org/teacon/signmeup/network/SyncWaypointPacket.java @@ -1,6 +1,5 @@ package org.teacon.signmeup.network; -import cn.ussshenzhou.t88.config.ConfigHelper; import cn.ussshenzhou.t88.network.annotation.ClientHandler; import cn.ussshenzhou.t88.network.annotation.Codec; import cn.ussshenzhou.t88.network.annotation.NetPacket; @@ -8,42 +7,18 @@ import net.minecraft.network.codec.ByteBufCodecs; import net.minecraft.network.codec.StreamCodec; import net.neoforged.neoforge.network.handling.IPayloadContext; -import org.jetbrains.annotations.NotNull; +import net.neoforged.neoforge.server.ServerLifecycleHooks; import org.teacon.signmeup.SignMeUp; -import org.teacon.signmeup.config.Waypoints; +import org.teacon.signmeup.config.waypoints.Waypoint; import org.teacon.signmeup.gui.map.MapScreen; import java.util.List; @NetPacket(modid = SignMeUp.MODID) -public record SyncWaypointPacket(List waypoints) { +public record SyncWaypointPacket(List waypoints) { @Codec public static final StreamCodec STREAM_CODEC = StreamCodec.composite( - ByteBufCodecs.list().apply(new StreamCodec<>() { - @Override - public Waypoints.@NotNull WayPoint decode(@NotNull ByteBuf buffer) { - return new Waypoints.WayPoint( - ByteBufCodecs.STRING_UTF8.decode(buffer), - ByteBufCodecs.STRING_UTF8.decode(buffer), - ByteBufCodecs.VAR_INT.decode(buffer), - ByteBufCodecs.VAR_INT.decode(buffer), - ByteBufCodecs.VAR_INT.decode(buffer), - ByteBufCodecs.FLOAT.decode(buffer), - ByteBufCodecs.FLOAT.decode(buffer) - ); - } - - @Override - public void encode(@NotNull ByteBuf buffer, Waypoints.@NotNull WayPoint value) { - ByteBufCodecs.STRING_UTF8.encode(buffer, value.name); - ByteBufCodecs.STRING_UTF8.encode(buffer, value.description); - ByteBufCodecs.VAR_INT.encode(buffer, value.x); - ByteBufCodecs.VAR_INT.encode(buffer, value.y); - ByteBufCodecs.VAR_INT.encode(buffer, value.z); - ByteBufCodecs.FLOAT.encode(buffer, value.rx); - ByteBufCodecs.FLOAT.encode(buffer, value.ry); - } - }), + ByteBufCodecs.list().apply(Waypoint.CODEC), SyncWaypointPacket::waypoints, SyncWaypointPacket::new ); @@ -51,10 +26,12 @@ public void encode(@NotNull ByteBuf buffer, Waypoints.@NotNull WayPoint value) { @ClientHandler public void clientHandler(IPayloadContext context) { context.enqueueWork(() -> { - ConfigHelper.getConfigWrite(Waypoints.class, waypoints -> { - waypoints.waypoints.clear(); - waypoints.waypoints.addAll(this.waypoints); - }); + if (ServerLifecycleHooks.getCurrentServer() == null) { + Waypoint.INSTANCES.clear(); + for (Waypoint waypoint : waypoints) { + Waypoint.INSTANCES.put(waypoint.name, waypoint); + } + } MapScreen.refreshInstance(); }); diff --git a/src/main/java/org/teacon/signmeup/network/TeleportToWayPointPacket.java b/src/main/java/org/teacon/signmeup/network/TeleportToWayPointPacket.java index 67ccc53..1b5da18 100644 --- a/src/main/java/org/teacon/signmeup/network/TeleportToWayPointPacket.java +++ b/src/main/java/org/teacon/signmeup/network/TeleportToWayPointPacket.java @@ -1,6 +1,5 @@ package org.teacon.signmeup.network; -import cn.ussshenzhou.t88.config.ConfigHelper; import cn.ussshenzhou.t88.network.annotation.Codec; import cn.ussshenzhou.t88.network.annotation.NetPacket; import cn.ussshenzhou.t88.network.annotation.ServerHandler; @@ -11,20 +10,15 @@ import net.minecraft.server.level.ServerLevel; import net.neoforged.neoforge.network.handling.IPayloadContext; import org.teacon.signmeup.SignMeUp; -import org.teacon.signmeup.config.Waypoints; +import org.teacon.signmeup.config.waypoints.Waypoint; -import java.util.Map; import java.util.Set; -import java.util.stream.Collectors; /** * @author USS_Shenzhou */ @NetPacket(modid = SignMeUp.MODID) public record TeleportToWayPointPacket(String name) { - private static final Map WAYPOINTS = ConfigHelper.getConfigRead(Waypoints.class).waypoints.stream() - .collect(Collectors.toMap(wayPoint -> wayPoint.name, wayPoint -> wayPoint)); - @Codec public static final StreamCodec STREAM_CODEC = StreamCodec.composite( ByteBufCodecs.STRING_UTF8, @@ -40,14 +34,12 @@ public void serverHandler(IPayloadContext context) { return; } - for (Waypoints.WayPoint waypoint : ConfigHelper.getConfigRead(Waypoints.class).waypoints) { - if (waypoint.name.equals(name)) { - player.teleportTo(level, waypoint.x, waypoint.y, waypoint.z, Set.of(), waypoint.rx, waypoint.ry); - return; - } + Waypoint waypoint = Waypoint.INSTANCES.get(name); + if (waypoint != null) { + player.teleportTo(level, waypoint.x, waypoint.y, waypoint.z, Set.of(), waypoint.rx, waypoint.ry); + } else { + player.sendSystemMessage(Component.literal("Open a new SMU map! The waypoint data is out of date.")); } - - player.sendSystemMessage(Component.literal("Open a new SMU map! The waypoint data is out of date.")); }); } }