Skip to content

Commit

Permalink
Packet update and removal of old code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hilligans committed Mar 27, 2024
1 parent 13ffa5f commit 706af6c
Show file tree
Hide file tree
Showing 60 changed files with 149 additions and 280 deletions.
6 changes: 0 additions & 6 deletions src/main/java/dev/hilligans/ourcraft/GameInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -321,12 +321,6 @@ public ByteBuffer getResource(ResourceLocation resourceLocation, IBufferAllocato
return DATA_LOADER.get(resourceLocation, allocator);
}

public void registerDefaultContent() {
// Ourcraft.registerDefaultContent(OURCRAFT);
//Container.register();
//Entity.register();
}

public int getUniqueID() {
return gameInstanceUniversalID;
}
Expand Down
9 changes: 2 additions & 7 deletions src/main/java/dev/hilligans/ourcraft/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import dev.hilligans.ourcraft.client.rendering.ContainerScreen;
import dev.hilligans.ourcraft.client.rendering.graphics.api.IGraphicsEngine;
import dev.hilligans.ourcraft.client.rendering.graphics.api.IInputProvider;
import dev.hilligans.ourcraft.client.rendering.graphics.implementations.splitwindows.SplitWindow;
import dev.hilligans.ourcraft.client.rendering.graphics.implementations.splitwindows.SubWindow;
import dev.hilligans.ourcraft.client.rendering.graphics.opengl.OpenGLEngine;
import dev.hilligans.ourcraft.client.rendering.graphics.RenderWindow;
import dev.hilligans.ourcraft.client.rendering.graphics.vulkan.VulkanEngine;
Expand All @@ -25,12 +23,10 @@
import dev.hilligans.ourcraft.mod.handler.events.client.OpenScreenEvent;
import dev.hilligans.ourcraft.network.ClientNetwork;
import dev.hilligans.ourcraft.network.IClientPacketHandler;
import dev.hilligans.ourcraft.network.Protocol;
import dev.hilligans.ourcraft.network.packet.auth.CGetToken;
import dev.hilligans.ourcraft.network.PacketBase;
import dev.hilligans.ourcraft.network.packet.client.CCloseScreen;
import dev.hilligans.ourcraft.network.packet.client.CDropItem;
import dev.hilligans.ourcraft.network.packet.client.COpenScreen;
import dev.hilligans.ourcraft.network.PacketBase;
import dev.hilligans.ourcraft.client.audio.SoundBuffer;
import dev.hilligans.ourcraft.client.audio.SoundEngine;
import dev.hilligans.ourcraft.tag.CompoundNBTTag;
Expand All @@ -39,7 +35,6 @@
import dev.hilligans.ourcraft.util.ThreadContext;
import dev.hilligans.ourcraft.util.registry.Registry;
import dev.hilligans.ourcraft.world.newworldsystem.ClientCubicWorld;
import dev.hilligans.ourcraft.world.newworldsystem.CubicWorld;
import dev.hilligans.ourcraft.world.newworldsystem.IWorld;
import dev.hilligans.ourcraft.save.WorldLoader;
import dev.hilligans.ourcraft.server.MultiPlayerServer;
Expand Down Expand Up @@ -392,7 +387,7 @@ public void invoke(long window, double xoffset, double yoffset) {
public static long timeSinceLastDraw = 0;
public static float drawTime = 1000f * 1000000 / Settings.maxFps;

public void sendPacket(PacketBase packetBase) {
public void sendPacket(PacketBase<?> packetBase) {
network.sendPacket(packetBase);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,9 @@ public PlayerEntity getPlayerEntity() {
public ServerNetworkHandler getServerNetworkHandler() {
return null;
}

@Override
public void disconnect(String reason) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@

import java.lang.reflect.InvocationTargetException;
import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;

public class ModContainer {
Expand Down Expand Up @@ -192,27 +189,27 @@ public void registerModel(IModel... models) {
}

@SafeVarargs
public final void registerPacket(Supplier<PacketBase>... packets) {
public final void registerPacket(Supplier<PacketBase<?>>... packets) {
ModContainer self = this;
for(Supplier<PacketBase> packet : packets) {
for(Supplier<PacketBase<?>> packet : packets) {
Protocol protocol = protocolRegistry.computeIfAbsent("ourcraft:Play", (s -> new Protocol(s.split(":")[1]).setSource(self)));
protocol.register(packet);
}
}

@SafeVarargs
public final void registerPacket(String protocolName, Supplier<PacketBase>... packets) {
public final void registerPacket(String protocolName, Supplier<PacketBase<?>>... packets) {
ModContainer self = this;
for(Supplier<PacketBase> packet : packets) {
for(Supplier<PacketBase<?>> packet : packets) {
Protocol protocol = protocolRegistry.computeIfAbsent(protocolName, (s -> new Protocol(s.split(":")[1]).setSource(self)));
protocol.register(packet);
}
}

@SafeVarargs
public final void registerPacket(String protocolName, int id, Supplier<PacketBase>... packets) {
public final void registerPacket(String protocolName, int id, Supplier<PacketBase<?>>... packets) {
ModContainer self = this;
for(Supplier<PacketBase> packet : packets) {
for(Supplier<PacketBase<?>> packet : packets) {
Protocol protocol = protocolRegistry.computeIfAbsent(protocolName, (s -> new Protocol(s.split(":")[1]).setSource(self)));
protocol.register(packet,id);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,23 @@
package dev.hilligans.ourcraft.mod.handler.content;

import dev.hilligans.ourcraft.GameInstance;
import dev.hilligans.ourcraft.biome.Biome;
import dev.hilligans.ourcraft.block.Block;
import dev.hilligans.ourcraft.client.Client;
import dev.hilligans.ourcraft.client.audio.SoundBuffer;
import dev.hilligans.ourcraft.client.input.Input;
import dev.hilligans.ourcraft.client.input.InputHandlerProvider;
import dev.hilligans.ourcraft.client.rendering.ScreenBuilder;
import dev.hilligans.ourcraft.client.rendering.Texture;
import dev.hilligans.ourcraft.client.rendering.graphics.*;
import dev.hilligans.ourcraft.client.rendering.graphics.api.IGraphicsEngine;
import dev.hilligans.ourcraft.client.rendering.graphics.api.ILayoutEngine;
import dev.hilligans.ourcraft.client.rendering.newrenderer.IModel;
import dev.hilligans.ourcraft.data.descriptors.Tag;
import dev.hilligans.ourcraft.item.BlockItem;
import dev.hilligans.ourcraft.item.Item;
import dev.hilligans.ourcraft.item.data.ToolLevel;
import dev.hilligans.ourcraft.mod.handler.Mod;
import dev.hilligans.ourcraft.network.PacketBase;
import dev.hilligans.ourcraft.network.Protocol;
import dev.hilligans.ourcraft.resource.loaders.ResourceLoader;
import dev.hilligans.ourcraft.resource.registry.loaders.RegistryLoader;
import dev.hilligans.ourcraft.save.WorldLoader;
import dev.hilligans.ourcraft.util.IByteArray;
import dev.hilligans.ourcraft.util.Settings;
import dev.hilligans.ourcraft.util.Util;
import dev.hilligans.ourcraft.world.Feature;
import org.json.JSONArray;
import org.json.JSONObject;

import java.net.URLClassLoader;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.function.Supplier;

public class ModContent {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class ClientNetwork extends Network {

public Client client;
public GameInstance gameInstance;
public ArrayList<PacketBase> packets = new ArrayList<>();
public ArrayList<PacketBase<?>> packets = new ArrayList<>();

public ClientNetwork(Protocol protocol) {
super(protocol);
Expand Down Expand Up @@ -53,7 +53,7 @@ public ClientNetwork debug(boolean debug) {
}

@Override
public void sendPacket(PacketBase packetBase) {
public void sendPacket(PacketBase<?> packetBase) {
// System.out.println("Sending packet:" + packetBase.getClass());
if(networkHandler != null && ((ClientNetworkHandler)networkHandler).enabled) {
packetBase.packetId = sendProtocol.packetMap.get(packetBase.getClass());
Expand All @@ -64,13 +64,13 @@ public void sendPacket(PacketBase packetBase) {
}

public void flush() {
for (PacketBase packet : packets) {
for (PacketBase<?> packet : packets) {
((ClientNetworkHandler) networkHandler).sendPacket(packet);
}
}

@Override
public void sendPacketDirect(PacketBase packetBase) {
public void sendPacketDirect(PacketBase<?> packetBase) {
((ClientNetworkHandler)networkHandler).sendPacket(packetBase);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
import io.netty.channel.*;

import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.function.Consumer;

public class ClientNetworkHandler extends NetworkHandler {

public ClientNetwork network;
public ConcurrentLinkedQueue<PacketBase> packets = new ConcurrentLinkedQueue<>();
public ConcurrentLinkedQueue<PacketBase<?>> packets = new ConcurrentLinkedQueue<>();

public ClientNetworkHandler(ClientNetwork network) {
this.network = network;
Expand All @@ -35,7 +34,7 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception {

@Override
protected void channelRead0(ChannelHandlerContext ctx, IPacketByteArray msg) throws Exception {
PacketBase packetBase = msg.createPacket(network.receiveProtocol);
PacketBase<?> packetBase = msg.createPacket(network.receiveProtocol);
packets.add(packetBase);
}

Expand All @@ -46,13 +45,9 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
}

public void processPackets() {
PacketBase packetBase;
PacketBase<?> packetBase;
while((packetBase = packets.poll()) != null) {
if(packetBase instanceof PacketBaseNew<?> packetBaseNew) {
packetBaseNew.handle(network.client);
} else {
packetBase.handle();
}
packetBase.handle(network.client);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public interface IPacketByteArray extends IByteArray {
void setOwner(ChannelHandlerContext ctx);
ChannelHandlerContext getOwner();

default PacketBase createPacket(Protocol protocol) {
PacketBase packetBase = protocol.packets.get(getPacketID()).getPacket();
default PacketBase<?> createPacket(Protocol protocol) {
PacketBase<?> packetBase = protocol.packets.get(getPacketID()).getPacket();
packetBase.ctx = getOwner();
packetBase.decode(this);
return packetBase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public interface IServerPacketHandler extends IPacketHandler {

ServerNetworkHandler getServerNetworkHandler();

void disconnect(String reason);

default GameInstance getGameInstance() {
return getServer().getGameInstance();
}
Expand All @@ -41,15 +43,15 @@ default void handleDisconnect() {
getWorld().removeEntity(getPlayerEntity().id, 0);
}

default ChannelFuture sendPacket(PacketBase packetBase, ChannelHandlerContext ctx) {
default ChannelFuture sendPacket(PacketBase<?> packetBase, ChannelHandlerContext ctx) {
if(ServerNetworkHandler.debug) {
return ctx.channel().writeAndFlush(new PacketTraceByteArray(packetBase));
} else {
return ctx.channel().writeAndFlush(new PacketByteArray(packetBase));
}
}

default void sendPacket(PacketBase packetBase) {
default void sendPacket(PacketBase<?> packetBase) {
getServerNetworkHandler().sendPacketInternal(packetBase);
}
}
4 changes: 2 additions & 2 deletions src/main/java/dev/hilligans/ourcraft/network/Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ public Network debug(boolean debug) {
return this;
}

public void sendPacket(PacketBase packetBase) {}
public void sendPacket(PacketBase<?> packetBase) {}

public void sendPacketDirect(PacketBase packetBase) {}
public void sendPacketDirect(PacketBase<?> packetBase) {}

public void disconnect() {
if(networkHandler instanceof ClientNetworkHandler) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public NetworkHandler setData(Channel channel, EventLoopGroup group, String ip,
return this;
}

public ChannelFuture sendPacket(PacketBase packetBase) {
public ChannelFuture sendPacket(PacketBase<?> packetBase) {
if(channel != null) {
if(debug) {
return channel.writeAndFlush(new PacketTraceByteArray(packetBase));
Expand Down
17 changes: 10 additions & 7 deletions src/main/java/dev/hilligans/ourcraft/network/PacketBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@

import io.netty.channel.ChannelHandlerContext;

public abstract class PacketBase {
public abstract class PacketBase<T extends IPacketHandler> {

public ChannelHandlerContext ctx;

public int packetId;

public PacketBase(int id) {
this.packetId = id;
}

public PacketBase() {}

public PacketBase(int packetID) {
this.packetId = packetID;
}

public abstract void encode(IPacketByteArray packetData);

public abstract void decode(IPacketByteArray packetData);

public abstract void handle();
public abstract void handle(T t);

public void handle(Object t) {
this.handle((T)t);
}
}
22 changes: 0 additions & 22 deletions src/main/java/dev/hilligans/ourcraft/network/PacketBaseNew.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public PacketByteArray(ByteBuf byteBuf) {
this.index = byteBuf.readableBytes();
}

public PacketByteArray(PacketBase packetBase) {
public PacketByteArray(PacketBase<?> packetBase) {
byteBuf = Unpooled.buffer();
packetID = (short) packetBase.packetId;
packetBase.encode(this);
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/dev/hilligans/ourcraft/network/PacketFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ public class PacketFetcher {


public int id;
public Supplier<PacketBase> packet;
public Class<PacketBase> packetClass;
public Supplier<PacketBase<?>> packet;
public Class<PacketBase<?>> packetClass;

public PacketFetcher(int id, Supplier<PacketBase> packet) {
public PacketFetcher(int id, Supplier<PacketBase<?>> packet) {
this.packet = packet;
this.id = id;
packetClass = (Class<PacketBase>) packet.get().getClass();
packetClass = (Class<PacketBase<?>>) packet.get().getClass();
}

public PacketBase getPacket() {
PacketBase packetBase = packet.get();
public PacketBase<?> getPacket() {
PacketBase<?> packetBase = packet.get();
packetBase.packetId = id;
return packetBase;
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/dev/hilligans/ourcraft/network/Protocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ public class Protocol implements IRegistryElement {
public String protocolName;

public ArrayList<PacketFetcher> packets = new ArrayList<>();
public HashMap<Class<PacketBase>, Integer> packetMap = new HashMap<>();
public HashMap<Class<PacketBase<?>>, Integer> packetMap = new HashMap<>();
public Int2BooleanOpenHashMap requiredIds = new Int2BooleanOpenHashMap();
public boolean compressed;
public GameInstance gameInstance = Ourcraft.GAME_INSTANCE;
public ModContainer source;

public void register(Supplier<PacketBase> packet) {
public void register(Supplier<PacketBase<?>> packet) {
register(new PacketFetcher(packets.size(),packet));
}

Expand All @@ -41,7 +41,7 @@ public void register(PacketFetcher packetFetcher) {
packets.add(packetFetcher);
}

public void register(Supplier<PacketBase> packet, int id) {
public void register(Supplier<PacketBase<?>> packet, int id) {
register(new PacketFetcher(id, packet),id);
}

Expand Down
Loading

0 comments on commit 706af6c

Please sign in to comment.