Skip to content

Commit

Permalink
Use new rlib networking
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexNijjar committed Jan 31, 2024
1 parent 19852d3 commit 4c1d58d
Show file tree
Hide file tree
Showing 13 changed files with 196 additions and 185 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ subprojects {
"modCompileOnly"(group = "me.shedaniel", name = "RoughlyEnoughItems-default-plugin", version = reiVersion)
} else {
"modLocalRuntime"(group = "earth.terrarium.prometheus", name = "prometheus-$modLoader-$minecraftVersion", version = prometheusVersion)
"modLocalRuntime"(group = "me.shedaniel", name = "RoughlyEnoughItems-$modLoader", version = reiVersion)
// "modLocalRuntime"(group = "me.shedaniel", name = "RoughlyEnoughItems-$modLoader", version = reiVersion)
"modCompileOnly"(group = "me.shedaniel", name = "RoughlyEnoughItems-api-$modLoader", version = reiVersion)
"modCompileOnly"(group = "me.shedaniel", name = "RoughlyEnoughItems-default-plugin-$modLoader", version = reiVersion)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package earth.terrarium.cadmus.client;

import com.mojang.blaze3d.platform.InputConstants;
import com.teamresourceful.resourcefullib.common.utils.modinfo.ModInfoUtils;
import earth.terrarium.cadmus.client.claims.ClaimScreen;
import earth.terrarium.cadmus.common.compat.prometheus.PrometheusIntegration;
import earth.terrarium.cadmus.common.constants.ConstantComponents;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import earth.terrarium.cadmus.common.network.NetworkHandler;
import earth.terrarium.cadmus.common.network.messages.ServerboundListenToChunksPacket;
import it.unimi.dsi.fastutil.objects.Object2BooleanMap;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.level.ChunkPos;
Expand Down Expand Up @@ -39,7 +40,7 @@ public void removeListener(String id) {
}
}

public void update(Component displayName, int color, Map<ChunkPos, Boolean> claims) {
public void update(Component displayName, int color, Object2BooleanMap<ChunkPos> claims) {
Entry entry = new Entry(displayName, color);
claims.forEach((chunkPos, isClaimed) -> {
if (isClaimed) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.FormattedCharSequence;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.ChunkPos;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -95,12 +95,13 @@ public ClaimScreen(Map<ChunkPos, Pair<String, ClaimType>> claims, @Nullable Stri
this.maxChunkLoaded = maxChunkLoaded;
}

public static void createFromPacket(Player player, ClientboundSendClaimedChunksPacket message) {
public static void createFromPacket(ClientboundSendClaimedChunksPacket message) {
Minecraft.getInstance().setScreen(new ClaimScreen(
message.claims(),
message.id(),
message.color(),
message.displayName().map(Component::nullToEmpty).orElse(player.getDisplayName()),
message.displayName().map(Component::nullToEmpty)
.orElse(Objects.requireNonNull(Minecraft.getInstance().player).getDisplayName()),
message.teamDisplayNames(),
message.claimedCount(),
message.chunkLoadedCount(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package earth.terrarium.cadmus.common.claims;

import com.mojang.datafixers.util.Pair;
import com.teamresourceful.resourcefullib.common.utils.modinfo.ModInfoUtils;
import earth.terrarium.cadmus.Cadmus;
import earth.terrarium.cadmus.api.claims.ClaimApi;
import earth.terrarium.cadmus.api.claims.InteractionType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package earth.terrarium.cadmus.common.network;

import com.teamresourceful.resourcefullib.common.networking.NetworkChannel;

import com.teamresourceful.resourcefullib.common.network.Network;
import earth.terrarium.cadmus.Cadmus;
import earth.terrarium.cadmus.common.network.messages.*;
import net.minecraft.network.protocol.PacketFlow;
import net.minecraft.resources.ResourceLocation;

public class NetworkHandler {
public static final NetworkChannel CHANNEL = new NetworkChannel(Cadmus.MOD_ID, 1, "main", true);
public static final Network CHANNEL = new Network(new ResourceLocation(Cadmus.MOD_ID, "main"), 1, true);

public static void init() {
CHANNEL.registerPacket(PacketFlow.SERVERBOUND, ServerboundRequestClaimedChunksPacket.ID, ServerboundRequestClaimedChunksPacket.HANDLER, ServerboundRequestClaimedChunksPacket.class);
CHANNEL.registerPacket(PacketFlow.SERVERBOUND, ServerboundUpdateClaimedChunksPacket.ID, ServerboundUpdateClaimedChunksPacket.HANDLER, ServerboundUpdateClaimedChunksPacket.class);
CHANNEL.registerPacket(PacketFlow.SERVERBOUND, ServerboundClearChunksPacket.ID, ServerboundClearChunksPacket.HANDLER, ServerboundClearChunksPacket.class);
CHANNEL.registerPacket(PacketFlow.SERVERBOUND, ServerboundListenToChunksPacket.ID, ServerboundListenToChunksPacket.HANDLER, ServerboundListenToChunksPacket.class);
CHANNEL.register(ServerboundRequestClaimedChunksPacket.TYPE);
CHANNEL.register(ServerboundUpdateClaimedChunksPacket.TYPE);
CHANNEL.register(ServerboundClearChunksPacket.TYPE);
CHANNEL.register(ServerboundListenToChunksPacket.TYPE);

CHANNEL.registerPacket(PacketFlow.CLIENTBOUND, ClientboundSendClaimedChunksPacket.ID, ClientboundSendClaimedChunksPacket.HANDLER, ClientboundSendClaimedChunksPacket.class);
CHANNEL.registerPacket(PacketFlow.CLIENTBOUND, ClientboundUpdateListeningChunksPacket.ID, ClientboundUpdateListeningChunksPacket.HANDLER, ClientboundUpdateListeningChunksPacket.class);
CHANNEL.register(ClientboundSendClaimedChunksPacket.TYPE);
CHANNEL.register(ClientboundUpdateListeningChunksPacket.TYPE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import com.teamresourceful.bytecodecs.base.object.ObjectByteCodec;
import com.teamresourceful.bytecodecs.defaults.MapCodec;
import com.teamresourceful.resourcefullib.common.bytecodecs.ExtraByteCodecs;
import com.teamresourceful.resourcefullib.common.networking.base.CodecPacketHandler;
import com.teamresourceful.resourcefullib.common.networking.base.Packet;
import com.teamresourceful.resourcefullib.common.networking.base.PacketContext;
import com.teamresourceful.resourcefullib.common.networking.base.PacketHandler;
import com.teamresourceful.resourcefullib.common.network.Packet;
import com.teamresourceful.resourcefullib.common.network.base.ClientboundPacketType;
import com.teamresourceful.resourcefullib.common.network.base.PacketType;
import com.teamresourceful.resourcefullib.common.network.defaults.CodecPacketType;
import earth.terrarium.cadmus.Cadmus;
import earth.terrarium.cadmus.client.claims.ClaimScreen;
import earth.terrarium.cadmus.common.claims.ClaimType;
Expand All @@ -28,20 +28,14 @@ public record ClientboundSendClaimedChunksPacket(Map<ChunkPos, Pair<String, Clai
int maxChunkLoaded,
int viewDistance) implements Packet<ClientboundSendClaimedChunksPacket> {

public static final ResourceLocation ID = new ResourceLocation(Cadmus.MOD_ID, "send_claimed_chunks");
public static final Handler HANDLER = new Handler();
public static final ClientboundPacketType<ClientboundSendClaimedChunksPacket> TYPE = new Type();

@Override
public ResourceLocation getID() {
return ID;
public PacketType<ClientboundSendClaimedChunksPacket> type() {
return TYPE;
}

@Override
public PacketHandler<ClientboundSendClaimedChunksPacket> getHandler() {
return HANDLER;
}

private static class Handler extends CodecPacketHandler<ClientboundSendClaimedChunksPacket> {
private static class Type extends CodecPacketType<ClientboundSendClaimedChunksPacket> implements ClientboundPacketType<ClientboundSendClaimedChunksPacket> {
private static final MapCodec<ChunkPos, Pair<String, ClaimType>> CHUNK_POS_CLAIM_CODEC = new MapCodec<>(
ExtraByteCodecs.CHUNK_POS,
ObjectByteCodec.create(
Expand All @@ -51,25 +45,29 @@ private static class Handler extends CodecPacketHandler<ClientboundSendClaimedCh
)
);

public Handler() {
super(ObjectByteCodec.create(
CHUNK_POS_CLAIM_CODEC.fieldOf(ClientboundSendClaimedChunksPacket::claims),
ByteCodec.STRING.fieldOf(ClientboundSendClaimedChunksPacket::id),
ByteCodec.ofEnum(ChatFormatting.class).fieldOf(ClientboundSendClaimedChunksPacket::color),
ByteCodec.STRING.optionalFieldOf(ClientboundSendClaimedChunksPacket::displayName),
new MapCodec<>(ByteCodec.STRING, ExtraByteCodecs.COMPONENT).fieldOf(ClientboundSendClaimedChunksPacket::teamDisplayNames),
ByteCodec.VAR_INT.fieldOf(ClientboundSendClaimedChunksPacket::claimedCount),
ByteCodec.VAR_INT.fieldOf(ClientboundSendClaimedChunksPacket::chunkLoadedCount),
ByteCodec.VAR_INT.fieldOf(ClientboundSendClaimedChunksPacket::maxClaims),
ByteCodec.VAR_INT.fieldOf(ClientboundSendClaimedChunksPacket::maxChunkLoaded),
ByteCodec.VAR_INT.fieldOf(ClientboundSendClaimedChunksPacket::viewDistance),
ClientboundSendClaimedChunksPacket::new
));
public Type() {
super(
ClientboundSendClaimedChunksPacket.class,
new ResourceLocation(Cadmus.MOD_ID, "send_claimed_chunks"),
ObjectByteCodec.create(
CHUNK_POS_CLAIM_CODEC.fieldOf(ClientboundSendClaimedChunksPacket::claims),
ByteCodec.STRING.fieldOf(ClientboundSendClaimedChunksPacket::id),
ByteCodec.ofEnum(ChatFormatting.class).fieldOf(ClientboundSendClaimedChunksPacket::color),
ByteCodec.STRING.optionalFieldOf(ClientboundSendClaimedChunksPacket::displayName),
new MapCodec<>(ByteCodec.STRING, ExtraByteCodecs.COMPONENT).fieldOf(ClientboundSendClaimedChunksPacket::teamDisplayNames),
ByteCodec.VAR_INT.fieldOf(ClientboundSendClaimedChunksPacket::claimedCount),
ByteCodec.VAR_INT.fieldOf(ClientboundSendClaimedChunksPacket::chunkLoadedCount),
ByteCodec.VAR_INT.fieldOf(ClientboundSendClaimedChunksPacket::maxClaims),
ByteCodec.VAR_INT.fieldOf(ClientboundSendClaimedChunksPacket::maxChunkLoaded),
ByteCodec.VAR_INT.fieldOf(ClientboundSendClaimedChunksPacket::viewDistance),
ClientboundSendClaimedChunksPacket::new
)
);
}

@Override
public PacketContext handle(ClientboundSendClaimedChunksPacket message) {
return (player, level) -> ClaimScreen.createFromPacket(player, message);
public Runnable handle(ClientboundSendClaimedChunksPacket packet) {
return () -> ClaimScreen.createFromPacket(packet);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,57 @@
import com.teamresourceful.bytecodecs.base.object.ObjectByteCodec;
import com.teamresourceful.bytecodecs.defaults.MapCodec;
import com.teamresourceful.resourcefullib.common.bytecodecs.ExtraByteCodecs;
import com.teamresourceful.resourcefullib.common.networking.base.CodecPacketHandler;
import com.teamresourceful.resourcefullib.common.networking.base.Packet;
import com.teamresourceful.resourcefullib.common.networking.base.PacketContext;
import com.teamresourceful.resourcefullib.common.networking.base.PacketHandler;
import com.teamresourceful.resourcefullib.common.network.Packet;
import com.teamresourceful.resourcefullib.common.network.base.ClientboundPacketType;
import com.teamresourceful.resourcefullib.common.network.base.PacketType;
import com.teamresourceful.resourcefullib.common.network.defaults.CodecPacketType;
import earth.terrarium.cadmus.Cadmus;
import earth.terrarium.cadmus.client.ClientClaims;
import it.unimi.dsi.fastutil.objects.Object2BooleanMap;
import it.unimi.dsi.fastutil.objects.Object2BooleanOpenHashMap;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.Level;

import java.util.Map;

public record ClientboundUpdateListeningChunksPacket(
ResourceKey<Level> dimension,
Component displayName, int color,
Map<ChunkPos, Boolean> claims // true if claimed, false if unclaimed
Object2BooleanMap<ChunkPos> claims // true if claimed, false if unclaimed
) implements Packet<ClientboundUpdateListeningChunksPacket> {

public static final ResourceLocation ID = new ResourceLocation(Cadmus.MOD_ID, "update_listening_chunks");
public static final Handler HANDLER = new Handler();
public static final ClientboundPacketType<ClientboundUpdateListeningChunksPacket> TYPE = new Type();

@Override
public ResourceLocation getID() {
return ID;
public PacketType<ClientboundUpdateListeningChunksPacket> type() {
return TYPE;
}

@Override
public PacketHandler<ClientboundUpdateListeningChunksPacket> getHandler() {
return HANDLER;
}

private static class Handler extends CodecPacketHandler<ClientboundUpdateListeningChunksPacket> {

public Handler() {
super(ObjectByteCodec.create(
ExtraByteCodecs.DIMENSION.fieldOf(ClientboundUpdateListeningChunksPacket::dimension),
ExtraByteCodecs.COMPONENT.fieldOf(ClientboundUpdateListeningChunksPacket::displayName),
ByteCodec.VAR_INT.fieldOf(ClientboundUpdateListeningChunksPacket::color),
new MapCodec<>(ExtraByteCodecs.CHUNK_POS, ByteCodec.BOOLEAN).fieldOf(ClientboundUpdateListeningChunksPacket::claims),
ClientboundUpdateListeningChunksPacket::new
));
private static class Type extends CodecPacketType<ClientboundUpdateListeningChunksPacket> implements ClientboundPacketType<ClientboundUpdateListeningChunksPacket> {

public Type() {
super(
ClientboundUpdateListeningChunksPacket.class,
new ResourceLocation(Cadmus.MOD_ID, "update_listening_chunks"),
ObjectByteCodec.create(
ExtraByteCodecs.DIMENSION.fieldOf(ClientboundUpdateListeningChunksPacket::dimension),
ExtraByteCodecs.COMPONENT.fieldOf(ClientboundUpdateListeningChunksPacket::displayName),
ByteCodec.VAR_INT.fieldOf(ClientboundUpdateListeningChunksPacket::color),
new MapCodec<>(ExtraByteCodecs.CHUNK_POS, ByteCodec.BOOLEAN).map(map -> {
Object2BooleanMap<ChunkPos> claims = new Object2BooleanOpenHashMap<>(map.size());
claims.putAll(map);
return claims;
}, map -> map
).fieldOf(ClientboundUpdateListeningChunksPacket::claims),
ClientboundUpdateListeningChunksPacket::new
)
);
}

@Override
public PacketContext handle(ClientboundUpdateListeningChunksPacket message) {
return (player, level) -> ClientClaims.get(message.dimension).update(message.displayName(), message.color(), message.claims());
public Runnable handle(ClientboundUpdateListeningChunksPacket packet) {
return () -> ClientClaims.get(packet.dimension()).update(packet.displayName(), packet.color(), packet.claims());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,50 @@

import com.teamresourceful.bytecodecs.base.ByteCodec;
import com.teamresourceful.bytecodecs.base.object.ObjectByteCodec;
import com.teamresourceful.resourcefullib.common.networking.base.CodecPacketHandler;
import com.teamresourceful.resourcefullib.common.networking.base.Packet;
import com.teamresourceful.resourcefullib.common.networking.base.PacketContext;
import com.teamresourceful.resourcefullib.common.networking.base.PacketHandler;
import com.teamresourceful.resourcefullib.common.network.Packet;
import com.teamresourceful.resourcefullib.common.network.base.PacketType;
import com.teamresourceful.resourcefullib.common.network.base.ServerboundPacketType;
import com.teamresourceful.resourcefullib.common.network.defaults.CodecPacketType;
import earth.terrarium.cadmus.Cadmus;
import earth.terrarium.cadmus.common.claims.ClaimHandler;
import earth.terrarium.cadmus.common.teams.TeamHelper;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.player.Player;

import java.util.function.Consumer;

public record ServerboundClearChunksPacket(boolean allDimensions) implements Packet<ServerboundClearChunksPacket> {

public static final ResourceLocation ID = new ResourceLocation(Cadmus.MOD_ID, "clear_chunks");
public static final Handler HANDLER = new Handler();
public static final ServerboundPacketType<ServerboundClearChunksPacket> TYPE = new Type();

@Override
public ResourceLocation getID() {
return ID;
public PacketType<ServerboundClearChunksPacket> type() {
return TYPE;
}

@Override
public PacketHandler<ServerboundClearChunksPacket> getHandler() {
return HANDLER;
}
private static class Type extends CodecPacketType<ServerboundClearChunksPacket> implements ServerboundPacketType<ServerboundClearChunksPacket> {

private static class Handler extends CodecPacketHandler<ServerboundClearChunksPacket> {
public Handler() {
super(ObjectByteCodec.create(
ByteCodec.BOOLEAN.fieldOf(ServerboundClearChunksPacket::allDimensions),
ServerboundClearChunksPacket::new
));
public Type() {
super(
ServerboundClearChunksPacket.class,
new ResourceLocation(Cadmus.MOD_ID, "clear_chunks"),
ObjectByteCodec.create(
ByteCodec.BOOLEAN.fieldOf(ServerboundClearChunksPacket::allDimensions),
ServerboundClearChunksPacket::new
)
);
}

@Override
public PacketContext handle(ServerboundClearChunksPacket message) {
return (player, level) -> {
public Consumer<Player> handle(ServerboundClearChunksPacket message) {
return player -> {
ServerLevel level = (ServerLevel) player.level();
String id = TeamHelper.getTeamId(player.getServer(), player.getUUID());
if (!message.allDimensions) {
ClaimHandler.clear((ServerLevel) level, id);
ClaimHandler.clear(level, id);
} else {
((ServerLevel) level).getServer().getAllLevels().forEach(l -> ClaimHandler.clear(l, id));
level.getServer().getAllLevels().forEach(l -> ClaimHandler.clear(l, id));
}
};
}
Expand Down
Loading

0 comments on commit 4c1d58d

Please sign in to comment.