-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
24f2bb3
commit e217bca
Showing
6 changed files
with
109 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/main/java/org/teacon/signmeup/config/WaypointSyncHandle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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))); | ||
} | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/main/java/org/teacon/signmeup/network/SyncWaypointPacket.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.WayPoint> waypoints) { | ||
@Codec | ||
public static final StreamCodec<ByteBuf, SyncWaypointPacket> STREAM_CODEC = StreamCodec.composite( | ||
ByteBufCodecs.<ByteBuf, Waypoints.WayPoint>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(); | ||
}); | ||
} | ||
} |