Skip to content

Commit

Permalink
0.6.8
Browse files Browse the repository at this point in the history
  • Loading branch information
burningtnt committed Dec 15, 2024
1 parent 24f2bb3 commit e217bca
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 8 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/org/teacon/signmeup/config/WaypointSyncHandle.java
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)));
}
});
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/teacon/signmeup/gui/map/MapScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
10 changes: 5 additions & 5 deletions src/main/java/org/teacon/signmeup/network/SetWaypointPacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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();
});
}
}
62 changes: 62 additions & 0 deletions src/main/java/org/teacon/signmeup/network/SyncWaypointPacket.java
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();
});
}
}

0 comments on commit e217bca

Please sign in to comment.