Skip to content

Commit

Permalink
Update fabric
Browse files Browse the repository at this point in the history
  • Loading branch information
pop4959 committed Apr 26, 2024
1 parent fde6859 commit 57acebd
Show file tree
Hide file tree
Showing 20 changed files with 133 additions and 218 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import org.popcraft.chunkyborder.util.Particles;
import org.popcraft.chunkyborder.util.PluginMessage;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -298,13 +297,7 @@ public void onPlayerRegisterChannel(final PlayerRegisterChannelEvent e) {
}

private void sendBorderPacket(final Collection<? extends org.bukkit.entity.Player> players, final World world, final Shape shape) {
final byte[] data;
try {
data = PluginMessage.writeBorderData(world, shape);
} catch (IOException e) {
e.printStackTrace();
return;
}
final byte[] data = PluginMessage.writeBorder(world, shape);
for (final org.bukkit.entity.Player player : players) {
player.sendPluginMessage(this, PLAY_BORDER_PACKET_ID, data);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.popcraft.chunkyborder.util;

import org.popcraft.chunkyborder.shape.BorderShape;

public record ClientBorder(String worldKey, BorderShape borderShape) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,67 @@
import org.popcraft.chunky.shape.AbstractEllipse;
import org.popcraft.chunky.shape.AbstractPolygon;
import org.popcraft.chunky.shape.Shape;
import org.popcraft.chunkyborder.shape.EllipseBorderShape;
import org.popcraft.chunkyborder.shape.PolygonBorderShape;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.List;

public class PluginMessage {
public static byte[] writeBorderData(final World world, final Shape shape) throws IOException {
public static final int VERSION = 0;
public static final byte[] INVALID_MESSAGE = ByteBuffer.allocate(4).putInt(-1).array();

public static ClientBorder readBorder(final byte[] bytes) {
try (final ByteArrayInputStream in = new ByteArrayInputStream(bytes); final DataInputStream data = new DataInputStream(in)) {
final int version = data.readInt();
if (version == 0) {
final String worldKey = data.readUTF();
final byte type = data.readByte();
return switch (type) {
case 1 -> {
final int numPoints = data.readInt();
final double[] pointsX = new double[numPoints];
final double[] pointsZ = new double[numPoints];
for (int i = 0; i < numPoints; ++i) {
pointsX[i] = data.readDouble();
pointsZ[i] = data.readDouble();
}
yield new ClientBorder(worldKey, new PolygonBorderShape(pointsX, pointsZ));
}
case 2 -> {
final double centerX = data.readDouble();
final double centerZ = data.readDouble();
final double radiusX = data.readDouble();
final double radiusZ = data.readDouble();
yield new ClientBorder(worldKey, new EllipseBorderShape(centerX, centerZ, radiusX, radiusZ));
}
default -> new ClientBorder(worldKey, null);
};
}
} catch (IOException e) {
e.printStackTrace();
}
return new ClientBorder(null, null);
}

public static byte[] writeBorder(final World world, final Shape shape) {
try (final ByteArrayOutputStream out = new ByteArrayOutputStream(); final DataOutputStream data = new DataOutputStream(out)) {
data.writeInt(0);
data.writeInt(VERSION);
data.writeUTF(world.getKey());
if (shape instanceof AbstractPolygon polygon) {
if (shape instanceof final AbstractPolygon polygon) {
data.writeByte(1);
final List<Vector2> points = polygon.points();
data.writeInt(points.size());
for (final Vector2 point : points) {
data.writeDouble(point.getX());
data.writeDouble(point.getZ());
}
} else if (shape instanceof AbstractEllipse ellipse) {
} else if (shape instanceof final AbstractEllipse ellipse) {
data.writeByte(2);
final Vector2 center = ellipse.center();
final Vector2 radii = ellipse.radii();
Expand All @@ -36,6 +77,9 @@ public static byte[] writeBorderData(final World world, final Shape shape) throw
data.writeByte(0);
}
return out.toByteArray();
} catch (final IOException e) {
e.printStackTrace();
return INVALID_MESSAGE;
}
}
}
8 changes: 4 additions & 4 deletions fabric/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ repositories {
}

dependencies {
minecraft(group = "com.mojang", name = "minecraft", version = "1.20.2")
mappings(group = "net.fabricmc", name = "yarn", version = "1.20.2+build.4", classifier = "v2")
modImplementation(group = "net.fabricmc", name = "fabric-loader", version = "0.14.22")
modImplementation(group = "net.fabricmc.fabric-api", name = "fabric-api", version = "0.89.3+1.20.2")
minecraft(group = "com.mojang", name = "minecraft", version = "1.20.5")
mappings(group = "net.fabricmc", name = "yarn", version = "1.20.5+build.1", classifier = "v2")
modImplementation(group = "net.fabricmc", name = "fabric-loader", version = "0.15.10")
modImplementation(group = "net.fabricmc.fabric-api", name = "fabric-api", version = "0.97.6+1.20.5")
modImplementation(group = "org.popcraft", name = "chunky-fabric", version = "${project.property("target")}")
compileOnly(group = "us.dynmap", name = "DynmapCoreAPI", version = "${project.property("target_dynmap")}")
compileOnly(group = "com.github.BlueMap-Minecraft", name = "BlueMapAPI", version = "${project.property("target_bluemap")}")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package org.popcraft.chunkyborder;

import io.netty.buffer.Unpooled;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.fabricmc.fabric.api.networking.v1.S2CPlayChannelEvents;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.packet.CustomPayload;
import net.minecraft.network.packet.s2c.common.CustomPayloadS2CPacket;
import net.minecraft.particle.DustParticleEffect;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import org.joml.Vector3f;
import org.popcraft.chunky.Chunky;
Expand All @@ -26,19 +25,17 @@
import org.popcraft.chunky.util.Translator;
import org.popcraft.chunkyborder.command.BorderCommand;
import org.popcraft.chunkyborder.event.border.BorderChangeEvent;
import org.popcraft.chunkyborder.packet.BorderPayload;
import org.popcraft.chunkyborder.platform.Config;
import org.popcraft.chunkyborder.platform.MapIntegrationLoader;
import org.popcraft.chunkyborder.util.BorderColor;
import org.popcraft.chunkyborder.util.Particles;
import org.popcraft.chunkyborder.util.PluginMessage;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Collection;
import java.util.List;

public class ChunkyBorderFabric implements ModInitializer {
private static final Identifier PLAY_BORDER_PACKET_ID = new Identifier("chunky", "border");
private ChunkyBorder chunkyBorder;
private boolean registered;

Expand All @@ -52,6 +49,7 @@ public void onInitialize() {
Translator.addCustomTranslation("custom_border_message", config.message());
BorderColor.parseColor(config.visualizerColor());
new BorderInitializationTask(chunkyBorder).run();
PayloadTypeRegistry.playS2C().register(BorderPayload.ID, CustomPayload.codecOf(BorderPayload::write, BorderPayload::new));
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> {
for (final World world : chunkyBorder.getChunky().getServer().getWorlds()) {
final Shape shape = chunkyBorder.getBorder(world.getName()).map(BorderData::getBorder).orElse(null);
Expand All @@ -63,7 +61,7 @@ public void onInitialize() {
chunkyBorder.getChunky().getEventBus().subscribe(BorderChangeEvent.class, e -> sendBorderPacket(server.getPlayerManager().getPlayerList(), e.world(), e.shape()));
registered = true;
}
if (channels.contains(PLAY_BORDER_PACKET_ID)) {
if (channels.contains(BorderPayload.ID.id())) {
chunkyBorder.getPlayerData(handler.player.getUuid()).setUsingMod(true);
}
});
Expand Down Expand Up @@ -114,17 +112,8 @@ private void startVisualizer() {
}

private void sendBorderPacket(final Collection<ServerPlayerEntity> players, final World world, final Shape shape) {
final PacketByteBuf data;
try {
data = new PacketByteBuf(Unpooled.buffer())
.writeIdentifier(PLAY_BORDER_PACKET_ID)
.writeBytes(PluginMessage.writeBorderData(world, shape));
} catch (IOException e) {
e.printStackTrace();
return;
}
for (final ServerPlayerEntity player : players) {
player.networkHandler.sendPacket(new CustomPayloadS2CPacket(data));
player.networkHandler.sendPacket(new CustomPayloadS2CPacket(new BorderPayload(world, shape)));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,41 +1,28 @@
package org.popcraft.chunkyborder;

import io.netty.buffer.ByteBufInputStream;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.network.packet.CustomPayload;
import net.minecraft.util.Identifier;
import org.popcraft.chunkyborder.packet.BorderPayload;
import org.popcraft.chunkyborder.platform.Config;
import org.popcraft.chunkyborder.shape.BorderShape;
import org.popcraft.chunkyborder.shape.EllipseBorderShape;
import org.popcraft.chunkyborder.shape.PolygonBorderShape;
import org.popcraft.chunkyborder.util.BorderColor;
import org.popcraft.chunkyborder.util.ClientBorder;

import java.io.DataInputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Environment(EnvType.CLIENT)
public class ChunkyBorderFabricClient implements ClientModInitializer {
private static final Identifier PLAY_BORDER_PACKET_ID = new Identifier("chunky", "border");
private static final Map<Identifier, BorderShape> borderShapes = new ConcurrentHashMap<>();
private static Config config;

public static void setBorderShape(final String id, final BorderShape borderShape) {
final Identifier identifier = Identifier.tryParse(id);
if (identifier != null) {
if (borderShape == null) {
ChunkyBorderFabricClient.borderShapes.remove(identifier);
} else {
ChunkyBorderFabricClient.borderShapes.put(identifier, borderShape);
}
}
}

public static BorderShape getBorderShape(final Identifier identifier) {
return ChunkyBorderFabricClient.borderShapes.get(identifier);
}
Expand All @@ -53,35 +40,21 @@ public void onInitializeClient() {
final Path configPath = FabricLoader.getInstance().getConfigDir().resolve("chunkyborder/config.json");
ChunkyBorderFabricClient.setConfig(new FabricConfig(configPath));
BorderColor.parseColor(config.visualizerColor());
ClientPlayNetworking.registerGlobalReceiver(PLAY_BORDER_PACKET_ID, (client, handler, buf, responseSender) -> {
try (final ByteBufInputStream in = new ByteBufInputStream(buf); final DataInputStream data = new DataInputStream(in)) {
final int version = data.readInt();
final String worldKey = data.readUTF();
if (version == 0) {
final byte type = data.readByte();
switch (type) {
case 1 -> {
final int numPoints = data.readInt();
final double[] pointsX = new double[numPoints];
final double[] pointsZ = new double[numPoints];
for (int i = 0; i < numPoints; ++i) {
pointsX[i] = data.readDouble();
pointsZ[i] = data.readDouble();
}
ChunkyBorderFabricClient.setBorderShape(worldKey, new PolygonBorderShape(pointsX, pointsZ));
}
case 2 -> {
final double centerX = data.readDouble();
final double centerZ = data.readDouble();
final double radiusX = data.readDouble();
final double radiusZ = data.readDouble();
ChunkyBorderFabricClient.setBorderShape(worldKey, new EllipseBorderShape(centerX, centerZ, radiusX, radiusZ));
}
default -> ChunkyBorderFabricClient.setBorderShape(worldKey, null);
}
}
} catch (IOException e) {
e.printStackTrace();
PayloadTypeRegistry.playS2C().register(BorderPayload.ID, CustomPayload.codecOf(BorderPayload::write, BorderPayload::new));
ClientPlayNetworking.registerGlobalReceiver(BorderPayload.ID, (borderPayload, context) -> {
final ClientBorder clientBorder = borderPayload.getBorder();
if (clientBorder.worldKey() == null) {
return;
}
final Identifier identifier = Identifier.tryParse(clientBorder.worldKey());
if (identifier == null) {
return;
}
final BorderShape borderShape = clientBorder.borderShape();
if (borderShape == null) {
ChunkyBorderFabricClient.borderShapes.remove(identifier);
} else {
ChunkyBorderFabricClient.borderShapes.put(identifier, borderShape);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import net.minecraft.client.render.VertexFormat;
import net.minecraft.client.render.VertexFormats;
import net.minecraft.client.render.WorldRenderer;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.util.Identifier;
import net.minecraft.util.Util;
Expand Down Expand Up @@ -45,7 +44,7 @@ public class WorldRendererMixin {
@Inject(method = "renderWorldBorder", at = @At("HEAD"), cancellable = true)
@SuppressWarnings("java:S3776")
private void renderWorldBorder(final Camera camera, final CallbackInfo ci) {
final BorderShape borderShape = ChunkyBorderFabricClient.getBorderShape(this.world.getDimensionKey().getValue());
final BorderShape borderShape = ChunkyBorderFabricClient.getBorderShape(this.world.getRegistryKey().getValue());
if (borderShape == null) {
return;
}
Expand Down Expand Up @@ -87,9 +86,6 @@ private void renderWorldBorder(final Camera camera, final CallbackInfo ci) {
RenderSystem.blendFuncSeparate(GlStateManager.SrcFactor.SRC_ALPHA, GlStateManager.DstFactor.ONE, GlStateManager.SrcFactor.ONE, GlStateManager.DstFactor.ZERO);
RenderSystem.setShaderTexture(0, FORCEFIELD);
RenderSystem.depthMask(MinecraftClient.isFabulousGraphicsOrBetter());
MatrixStack matrixStack = RenderSystem.getModelViewStack();
matrixStack.push();
RenderSystem.applyModelViewMatrix();
final int color = BorderColor.getColor();
final float red = (color >> 16 & 255) / 255.0F;
final float green = (color >> 8 & 255) / 255.0F;
Expand Down Expand Up @@ -207,8 +203,6 @@ private void renderWorldBorder(final Camera camera, final CallbackInfo ci) {
RenderSystem.disablePolygonOffset();
RenderSystem.disableBlend();
RenderSystem.defaultBlendFunc();
matrixStack.pop();
RenderSystem.applyModelViewMatrix();
RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
RenderSystem.depthMask(true);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.popcraft.chunkyborder.packet;

import io.netty.buffer.ByteBuf;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.packet.CustomPayload;
import org.popcraft.chunky.platform.World;
import org.popcraft.chunky.shape.Shape;
import org.popcraft.chunkyborder.util.ClientBorder;
import org.popcraft.chunkyborder.util.PluginMessage;

public class BorderPayload implements CustomPayload {
public static final CustomPayload.Id<BorderPayload> ID = CustomPayload.id("chunky:border");
private World world;
private Shape shape;
private ClientBorder border;

public BorderPayload(final World world, final Shape shape) {
this.world = world;
this.shape = shape;
}

public BorderPayload(final PacketByteBuf buf) {
final ByteBuf unwrapped = buf.unwrap();
final byte[] bytes = new byte[unwrapped.readableBytes()];
unwrapped.readBytes(bytes);
this.border = PluginMessage.readBorder(bytes);
}

public void write(final PacketByteBuf buf) {
buf.writeBytes(PluginMessage.writeBorder(world, shape));
}

public ClientBorder getBorder() {
return border;
}

@Override
public CustomPayload.Id<BorderPayload> getId() {
return ID;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,9 @@ public void onServerTick(final TickEvent.ServerTickEvent event) {
}

private void sendBorderPacket(final Collection<ServerPlayer> players, final World world, final Shape shape) {
final FriendlyByteBuf data;
try {
data = new FriendlyByteBuf(Unpooled.buffer())
.writeResourceLocation(PLAY_BORDER_PACKET_ID)
.writeBytes(PluginMessage.writeBorderData(world, shape));
} catch (IOException e) {
e.printStackTrace();
return;
}
final FriendlyByteBuf data = new FriendlyByteBuf(Unpooled.buffer())
.writeResourceLocation(PLAY_BORDER_PACKET_ID)
.writeBytes(PluginMessage.writeBorder(world, shape));
for (final ServerPlayer player : players) {
player.connection.send(new ClientboundCustomPayloadPacket(data));
}
Expand Down
Loading

0 comments on commit 57acebd

Please sign in to comment.