forked from virt-mirrors/Sit
parent
df46547cbf
commit
648f20e5db
3 changed files with 47 additions and 26 deletions
35
src/main/java/one/oth3r/sit/PacketBuilder.java
Normal file
35
src/main/java/one/oth3r/sit/PacketBuilder.java
Normal file
|
@ -0,0 +1,35 @@
|
|||
package one.oth3r.sit;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
|
||||
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
|
||||
import net.minecraft.network.PacketByteBuf;
|
||||
import net.minecraft.util.Identifier;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class PacketBuilder {
|
||||
public static final String SETTINGS = "settings_v1.0";
|
||||
private final String message;
|
||||
private final PacketByteBuf packetByteBuf = PacketByteBufs.create();
|
||||
public PacketBuilder(ByteBuf buf) {
|
||||
// Read any data sent in the packet
|
||||
message = buf.toString(StandardCharsets.UTF_8);
|
||||
packetByteBuf.writeBytes(buf);
|
||||
}
|
||||
public PacketBuilder(String message) {
|
||||
this.message = message;
|
||||
packetByteBuf.writeBytes(ByteBuffer.wrap(message.getBytes(StandardCharsets.UTF_8)).array());
|
||||
}
|
||||
public static Identifier getIdentifier() {
|
||||
// only 1 packet rn
|
||||
return new Identifier(Sit.MOD_ID, SETTINGS);
|
||||
}
|
||||
public void send() {
|
||||
ClientPlayNetworking.send(getIdentifier(), packetByteBuf);
|
||||
}
|
||||
public String getMessage() {
|
||||
return this.message;
|
||||
}
|
||||
}
|
|
@ -5,18 +5,12 @@ 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.minecraft.network.RegistryByteBuf;
|
||||
import net.minecraft.network.codec.PacketCodec;
|
||||
import net.minecraft.network.codec.PacketCodecs;
|
||||
import net.minecraft.network.packet.CustomPayload;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.server.command.CommandManager;
|
||||
import net.minecraft.server.network.ServerPlayerEntity;
|
||||
import net.minecraft.text.MutableText;
|
||||
import net.minecraft.text.Text;
|
||||
import net.minecraft.util.Identifier;
|
||||
import one.oth3r.sit.file.Config;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
@ -41,22 +35,16 @@ public class Sit implements ModInitializer {
|
|||
Config.load();
|
||||
Events.register();
|
||||
//PACKETS
|
||||
PayloadTypeRegistry.playC2S().register(SettingsPayload.ID, SettingsPayload.CODEC);
|
||||
ServerPlayNetworking.registerGlobalReceiver(SettingsPayload.ID,((payload, context) -> {
|
||||
ServerPlayNetworking.registerGlobalReceiver(PacketBuilder.getIdentifier(),
|
||||
(server, player, handler, buf, responseSender) -> {
|
||||
// copy to not throw errors
|
||||
PacketBuilder packet = new PacketBuilder(buf.copy());
|
||||
server.execute(() -> {
|
||||
Type hashMapToken = new TypeToken<HashMap<String, Object>>() {}.getType();
|
||||
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
|
||||
playerSettings.put(context.player(),gson.fromJson(payload.value,hashMapToken));
|
||||
playerSettings.put(player,gson.fromJson(packet.getMessage(),hashMapToken));
|
||||
});
|
||||
});
|
||||
}));
|
||||
}
|
||||
public record SettingsPayload(String value) implements CustomPayload {
|
||||
public static final CustomPayload.Id<SettingsPayload> ID = new CustomPayload.Id<>(new Identifier(MOD_ID,"settings_v1.1"));
|
||||
public static final PacketCodec<RegistryByteBuf, SettingsPayload> CODEC = PacketCodecs.STRING.xmap(SettingsPayload::new, SettingsPayload::value).cast();
|
||||
@Override
|
||||
public Id<SettingsPayload> getId() {
|
||||
return ID;
|
||||
}
|
||||
}
|
||||
public static MutableText lang(String key, Object... args) {
|
||||
if (isClient) return Text.translatable(key, args);
|
||||
|
|
|
@ -4,8 +4,6 @@ import com.google.gson.Gson;
|
|||
import com.google.gson.GsonBuilder;
|
||||
import net.fabricmc.api.ClientModInitializer;
|
||||
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
|
||||
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
|
||||
import net.minecraft.network.packet.CustomPayload;
|
||||
|
||||
public class SitClient implements ClientModInitializer {
|
||||
public static boolean inGame = false;
|
||||
|
@ -15,13 +13,13 @@ public class SitClient implements ClientModInitializer {
|
|||
ClientPlayConnectionEvents.JOIN.register((handler, sender, client) -> {
|
||||
inGame = true;
|
||||
// send a data packet whenever joining a server
|
||||
ClientPlayNetworking.send(sendPackets());
|
||||
client.execute(SitClient::sendPackets);
|
||||
});
|
||||
// reset inGame
|
||||
ClientPlayConnectionEvents.DISCONNECT.register((handler, client) -> inGame = false);
|
||||
}
|
||||
public static CustomPayload sendPackets() {
|
||||
public static void sendPackets() {
|
||||
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
|
||||
return new Sit.SettingsPayload(gson.toJson(Utl.getHandSettings()));
|
||||
new PacketBuilder(gson.toJson(Utl.HandSettings.getHandSettings())).send();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue