diff --git a/gradle.properties b/gradle.properties index ef3ee1c..4411093 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.7 +mod_version=0.6.8 # 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/src/main/java/org/teacon/signmeup/config/WaypointSyncHandle.java b/src/main/java/org/teacon/signmeup/config/WaypointSyncHandle.java new file mode 100644 index 0000000..687e8d7 --- /dev/null +++ b/src/main/java/org/teacon/signmeup/config/WaypointSyncHandle.java @@ -0,0 +1,39 @@ +package org.teacon.signmeup.config; + +import cn.ussshenzhou.t88.config.ConfigHelper; +import cn.ussshenzhou.t88.network.NetworkHelper; +import net.minecraft.client.Minecraft; +import net.minecraft.client.player.LocalPlayer; +import net.minecraft.core.BlockPos; +import net.minecraft.core.Rotations; +import net.minecraft.server.level.ServerPlayer; +import net.neoforged.api.distmarker.Dist; +import net.neoforged.bus.api.SubscribeEvent; +import net.neoforged.fml.common.EventBusSubscriber; +import net.neoforged.fml.loading.FMLEnvironment; +import net.neoforged.neoforge.event.entity.player.PlayerEvent; +import org.teacon.signmeup.SignMeUp; +import org.teacon.signmeup.network.SetWaypointPacket; + +import java.util.Objects; + +@EventBusSubscriber(value = Dist.DEDICATED_SERVER, modid = SignMeUp.MODID, bus = EventBusSubscriber.Bus.GAME) +public class WaypointSyncHandle { + @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())) { + return; + } + } + + player.server.execute(() -> { + for (Waypoints.WayPoint waypoint : ConfigHelper.getConfigRead(Waypoints.class).waypoints) { + NetworkHelper.sendToPlayer(player, new SetWaypointPacket(waypoint.name, waypoint.description, new BlockPos(waypoint.x, waypoint.y, waypoint.z), new Rotations(waypoint.rx, 0, waypoint.ry))); + } + }); + } + } +} diff --git a/src/main/java/org/teacon/signmeup/gui/map/MapScreen.java b/src/main/java/org/teacon/signmeup/gui/map/MapScreen.java index 0632e49..9a2fc02 100644 --- a/src/main/java/org/teacon/signmeup/gui/map/MapScreen.java +++ b/src/main/java/org/teacon/signmeup/gui/map/MapScreen.java @@ -28,7 +28,7 @@ public class MapScreen extends TScreen { private final CommandsButtonPanel commandsButtonPanel = new CommandsButtonPanel(); private final WayPointsButtonPanel wayPointsButtonPanel = new WayPointsButtonPanel(); - public static void newInstance() { + public static void refreshInstance() { Screen screen = Minecraft.getInstance().screen; if (screen instanceof MapScreen || screen instanceof SettingsScreen) { ((TScreen) screen).onClose(false); diff --git a/src/main/java/org/teacon/signmeup/network/RemoveWaypointPacket.java b/src/main/java/org/teacon/signmeup/network/RemoveWaypointPacket.java index c508c1b..d7f87c5 100644 --- a/src/main/java/org/teacon/signmeup/network/RemoveWaypointPacket.java +++ b/src/main/java/org/teacon/signmeup/network/RemoveWaypointPacket.java @@ -28,6 +28,6 @@ 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::newInstance); + context.enqueueWork(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 16cea97..d980b76 100644 --- a/src/main/java/org/teacon/signmeup/network/SetWaypointPacket.java +++ b/src/main/java/org/teacon/signmeup/network/SetWaypointPacket.java @@ -5,8 +5,6 @@ import cn.ussshenzhou.t88.network.annotation.Codec; import cn.ussshenzhou.t88.network.annotation.NetPacket; import io.netty.buffer.ByteBuf; -import net.minecraft.client.Minecraft; -import net.minecraft.client.gui.screens.Screen; import net.minecraft.core.BlockPos; import net.minecraft.core.Rotations; import net.minecraft.network.codec.ByteBufCodecs; @@ -15,7 +13,6 @@ import org.teacon.signmeup.SignMeUp; import org.teacon.signmeup.config.Waypoints; import org.teacon.signmeup.gui.map.MapScreen; -import org.teacon.signmeup.gui.settings.SettingsScreen; /** * @author USS_Shenzhou @@ -39,7 +36,10 @@ public record SetWaypointPacket(String name, String description, BlockPos pos, R @ClientHandler public void clientHandler(IPayloadContext context) { var waypoint = new Waypoints.WayPoint(name, description, pos.getX(), pos.getY(), pos.getZ(), rotation.getX(), rotation.getZ()); - ConfigHelper.getConfigWrite(Waypoints.class, waypoints -> waypoints.waypoints.add(waypoint)); - context.enqueueWork(MapScreen::newInstance); + context.enqueueWork(() -> { + ConfigHelper.getConfigWrite(Waypoints.class, waypoints -> waypoints.waypoints.add(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 new file mode 100644 index 0000000..1a1c131 --- /dev/null +++ b/src/main/java/org/teacon/signmeup/network/SyncWaypointPacket.java @@ -0,0 +1,62 @@ +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.network.codec.ByteBufCodecs; +import net.minecraft.network.codec.StreamCodec; +import net.neoforged.neoforge.network.handling.IPayloadContext; +import org.jetbrains.annotations.NotNull; +import org.teacon.signmeup.SignMeUp; +import org.teacon.signmeup.config.Waypoints; +import org.teacon.signmeup.gui.map.MapScreen; + +import java.util.List; + +@NetPacket(modid = SignMeUp.MODID) +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); + } + }), + SyncWaypointPacket::waypoints, + SyncWaypointPacket::new + ); + + @ClientHandler + public void clientHandler(IPayloadContext context) { + context.enqueueWork(() -> { + ConfigHelper.getConfigWrite(Waypoints.class, waypoints -> { + waypoints.waypoints.clear(); + waypoints.waypoints.addAll(this.waypoints); + }); + + MapScreen.refreshInstance(); + }); + } +}