Skip to content

Commit

Permalink
Work on networking and ByteArray code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hilligans committed Nov 1, 2023
1 parent f1a277f commit b339f5d
Show file tree
Hide file tree
Showing 30 changed files with 695 additions and 109 deletions.
3 changes: 0 additions & 3 deletions src/main/java/dev/hilligans/ourcraft/Client/Camera.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,4 @@ public class Camera {

public static BoundingBox playerBoundingBox = new BoundingBox(-0.35f,-1.9f,-0.35f,0.35f,0.0f,0.35f, -0.15f);

public static double newX = (float)ClientMain.getWindowX() / 2;
public static double newY = (float)ClientMain.getWindowY() / 2;

}
3 changes: 3 additions & 0 deletions src/main/java/dev/hilligans/ourcraft/Client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ public void onPress() {
public void onPress() {
if(screen != null) {
if(playerData.openContainer != null) {
/*
Slot slot = playerData.openContainer.getSlotAt((int)Camera.newX,(int)Camera.newY);
if(slot != null) {
if(KeyHandler.keyPressed[GLFW_KEY_LEFT_CONTROL]) {
Expand All @@ -281,6 +282,8 @@ public void onPress() {
sendPacket(new CDropItem(slot.id,(byte)1));
}
}
*/
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ default MatrixStack getScreenStack(int W, int H, int x, int y) {

default Matrix4d getPerspective(int W, int H, int x, int y, float fov, float aspectRatio, float zNear, float zFar) {
return new Matrix4d().translate(W - 1 - 2*x, H - 1 - 2*y, 0).scale(W, H, 1).perspective((float) Math.toRadians(fov), aspectRatio,zNear,zFar);
// return new Matrix4d().perspective((float) Math.toRadians(fov), aspectRatio,zNear,zFar);
}

Matrix4d getView();
Expand Down
10 changes: 0 additions & 10 deletions src/main/java/dev/hilligans/ourcraft/ClientMain.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
package dev.hilligans.ourcraft;

import dev.hilligans.ourcraft.Block.Blocks;
import dev.hilligans.ourcraft.Client.Client;
import dev.hilligans.ourcraft.Client.Rendering.Graphics.API.IGraphicsEngine;
import dev.hilligans.ourcraft.Util.ArgumentContainer;
import dev.hilligans.ourcraft.Util.Side;
import dev.hilligans.ourcraft.World.NewWorldSystem.GlobalPaletteAtomicSubChunk;
import org.lwjgl.system.MemoryUtil;
import sun.misc.Unsafe;

import java.io.IOException;
import java.lang.invoke.VarHandle;
import java.lang.reflect.Field;
import java.util.*;
import java.util.concurrent.locks.LockSupport;

import static java.lang.StringTemplate.STR;

public class ClientMain {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void writeData(PacketData packetData) {
packetData.writeInt(age);
packetData.writeInt(items.length);
for (ItemStack item : items) {
packetData.writeItemStack(item);
//packetData.writeItemStack(item);
}
}

Expand All @@ -91,7 +91,7 @@ public void readData(PacketData packetData) {
if(age > this.age) {
int size = packetData.readInt();
for (int x = 0; x < size; x++) {
items[x] = packetData.readItemStack();
// items[x] = packetData.readItemStack();
/* if(!items[x].isEmpty()) {
if(items[x].item instanceof BlockItem) {
System.out.println(((BlockItem) items[x].item).block);
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/dev/hilligans/ourcraft/Entity/IEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package dev.hilligans.ourcraft.Entity;

import dev.hilligans.ourcraft.World.NewWorldSystem.IWorld;

public interface IEntity {

IWorld getWorld();
void setWorld(IWorld world);

void setRot(float pitch, float yaw);
void setVel(float velX, float velY, float velZ);






}
16 changes: 12 additions & 4 deletions src/main/java/dev/hilligans/ourcraft/GameInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ public void loadContent() {
public void build(IGraphicsEngine<?,?,?> graphicsEngine, GraphicsContext graphicsContext) {
for(Registry<?> registry : REGISTRIES.ELEMENTS) {
for(Object o : registry.ELEMENTS) {
if(o instanceof IRegistryElement) {
((IRegistryElement) o).loadGraphics(graphicsEngine, graphicsContext);
if(o instanceof IRegistryElement registryElement) {
registryElement.loadGraphics(graphicsEngine, graphicsContext);
}
}
}
Expand All @@ -131,8 +131,8 @@ public void build(IGraphicsEngine<?,?,?> graphicsEngine, GraphicsContext graphic
public void cleanupGraphics(IGraphicsEngine<?,?,?> graphicsEngine, GraphicsContext graphicsContext) {
for(Registry<?> registry : REGISTRIES.ELEMENTS) {
for(Object o : registry.ELEMENTS) {
if(o instanceof IRegistryElement) {
((IRegistryElement) o).cleanupGraphics(graphicsEngine, graphicsContext);
if(o instanceof IRegistryElement registryElement) {
registryElement.cleanupGraphics(graphicsEngine, graphicsContext);
}
}
}
Expand Down Expand Up @@ -368,6 +368,14 @@ public void register(String[] names, Object[] objects) {
}
}

public boolean replace(String registryName, String resource, IRegistryElement registryElement) {
Registry<?> registry = REGISTRIES.get(registryName);
if(registry == null) {
return false;
}
return registry.replace(resource, registryElement);
}

public ByteBuffer getResource(ResourceLocation resourceLocation) {
return DATA_LOADER.get(resourceLocation);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import dev.hilligans.ourcraft.Resource.RegistryLoaders.RegistryLoader;
import dev.hilligans.ourcraft.Resource.Loaders.ResourceLoader;
import dev.hilligans.ourcraft.Util.ByteArray;
import dev.hilligans.ourcraft.Util.IByteArray;
import dev.hilligans.ourcraft.Util.Settings;
import dev.hilligans.ourcraft.Util.Util;
import dev.hilligans.ourcraft.World.Feature;
Expand Down Expand Up @@ -89,17 +90,17 @@ public ModContent(String modID, GameInstance gameInstance) {
this.gameInstance = gameInstance;
}

public ModContent(ByteArray packetData, GameInstance gameInstance) {
public ModContent(IByteArray packetData, GameInstance gameInstance) {
this.gameInstance = gameInstance;
readData(packetData);
if(Settings.cacheDownloadedMods) {
if(Settings.storeServerModsIndividually) {
String ip = ClientMain.getClient().serverIP.replace(':','_');
if(!ip.equals("")) {
WorldLoader.write("mod_cache/servers/" + ip + "/" + getModID() + "-" + version + ".dat",packetData.toByteBuffer());
// WorldLoader.write("mod_cache/servers/" + ip + "/" + getModID() + "-" + version + ".dat",packetData.toByteBuffer());
}
} else {
WorldLoader.write("mod_cache/mods/" + getModID() + "-" + version + ".dat",packetData.toByteBuffer());
// WorldLoader.write("mod_cache/mods/" + getModID() + "-" + version + ".dat",packetData.toByteBuffer());
}
}
}
Expand Down Expand Up @@ -309,13 +310,14 @@ public void registerShader(ShaderSource... shaderSources) {
this.shaders.addAll(Arrays.asList(shaderSources));
}

public void putData(ByteArray byteArray) {
public void putData(IByteArray byteArray) {
byteArray.writeInt(version);
byteArray.writeString(getModID());
byteArray.writeString(description);
byteArray.writeString(Util.toString(authors));
byteArray.writeString(Util.toString(getDependencies()));
byteArray.writeUTF16(getModID());
byteArray.writeUTF16(description);
byteArray.writeUTF16(Util.toString(authors));
byteArray.writeUTF16(Util.toString(getDependencies()));
byteArray.writeInt(models.size());
/*
for(IModel iModel : models) {
byteArray.writeString(iModel.getPath());
byteArray.writeString(iModel.getModel());
Expand Down Expand Up @@ -351,22 +353,25 @@ public void putData(ByteArray byteArray) {
byteArray.writeString(item.name);
byteArray.writeString(item.itemProperties.getJsonObject().toString());
}
*/
}

public void readData(ByteArray byteArray) {
public void readData(IByteArray byteArray) {
version = byteArray.readInt();
modID = byteArray.readString();
description = byteArray.readString();
byteArray.readString();
byteArray.readString();
modID = byteArray.readUTF16();
description = byteArray.readUTF16();
byteArray.readUTF16();
byteArray.readUTF16();
int size = byteArray.readInt();
for(int x = 0; x < size; x++) {
byteArray.readString();
byteArray.readString();
byteArray.readUTF16();
byteArray.readUTF16();
}
/*
size = byteArray.readInt();
for(int x = 0; x < size; x++) {
textures.add(new Texture(byteArray.readString(), byteArray.readImage()));
textures.add(new Texture(byteArray.readUTF16(), byteArray.readImage()));
}
size = byteArray.readInt();
for(int x = 0; x < size; x++) {
Expand Down Expand Up @@ -394,6 +399,8 @@ public void readData(ByteArray byteArray) {
String name = byteArray.readString();
items.add(ItemProperties.loadProperties(new JSONObject(byteArray.readString())).getItem(name,this));
}
*/
}

public String[] getDependencies() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public CTokenValid(String username, String token, String ip, String tempId) {

@Override
public void encode(PacketData packetData) {
packetData.writeString(username);
packetData.writeString(token);
packetData.writeString(ip);
packetData.writeString(tempId);
packetData.writeUTF16(username);
packetData.writeUTF16(token);
packetData.writeUTF16(ip);
packetData.writeUTF16(tempId);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void encode(PacketData packetData) {}

@Override
public void decode(PacketData packetData) {
response = packetData.readString();
response = packetData.readUTF16();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ public CHandshakePacket() {
@Override
public void encode(PacketData packetData) {
packetData.writeInt(Settings.gameVersion);
packetData.writeString(ClientMain.getClient().playerData.userName);
packetData.writeString(ClientMain.getClient().playerData.authToken);
packetData.writeUTF16(ClientMain.getClient().playerData.userName);
packetData.writeUTF16(ClientMain.getClient().playerData.authToken);
}

@Override
public void decode(PacketData packetData) {
id = packetData.readInt();
name = packetData.readString();
authToken = packetData.readString();
name = packetData.readUTF16();
authToken = packetData.readUTF16();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public CRequestContent(ArrayList<String> mods) {
public void encode(PacketData packetData) {
packetData.writeInt(mods.size());
for(String string : mods) {
packetData.writeString(string);
packetData.writeUTF16(string);
}
}

Expand All @@ -40,7 +40,7 @@ public void decode(PacketData packetData) {
int length = packetData.readInt();
mods = new ArrayList<>();
for(int x = 0; x < length; x++) {
mods.add(packetData.readString());
mods.add(packetData.readUTF16());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public CSendMessage(String message) {

@Override
public void encode(PacketData packetData) {
packetData.writeString(message);
packetData.writeUTF16(message);
}

@Override
public void decode(PacketData packetData) {
message = packetData.readString();
message = packetData.readUTF16();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.hilligans.ourcraft.Network.Packet.NewSystem.Client;
package dev.hilligans.ourcraft.Network.Packet.NewSystem.Server;

import dev.hilligans.ourcraft.ClientMain;
import dev.hilligans.ourcraft.Network.IClientPacketHandler;
Expand Down Expand Up @@ -28,8 +28,8 @@ public SSendChunk(IChunk chunk) {
@Override
public void encode(PacketData packetData) {
try {
int pos = Ourcraft.chainedChunkStream.fillBuffer(packetData.byteBuf, packetData.size, newChunk);
packetData.size = pos;
int pos = Ourcraft.chainedChunkStream.fillBuffer(packetData.byteBuf, (int)packetData.readerIndex(), newChunk);
packetData.setReaderIndex(pos);
} catch (Exception e) {
e.printStackTrace();
}
Expand All @@ -40,7 +40,7 @@ public void encode(PacketData packetData) {
public void decode(PacketData packetData) {
newChunk = new CubicChunk(ClientMain.getClient().newClientWorld,32,0,0, 0);
try {
Ourcraft.chainedChunkStream.fillChunk(packetData.byteBuf, packetData.size, newChunk);
Ourcraft.chainedChunkStream.fillChunk(packetData.byteBuf, (int)packetData.readerIndex(), newChunk);
} catch (Exception e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.hilligans.ourcraft.Network.Packet.NewSystem.Client;
package dev.hilligans.ourcraft.Network.Packet.NewSystem.Server;

import dev.hilligans.ourcraft.Network.IClientPacketHandler;
import dev.hilligans.ourcraft.Network.PacketBase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ public SChatMessage(String message) {

@Override
public void encode(PacketData packetData) {
packetData.writeString(message);
packetData.writeUTF16(message);
}

@Override
public void decode(PacketData packetData) {
message = packetData.readString();
message = packetData.readUTF16();

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void encode(PacketData packetData) {
packetData.writeInt(uniqueId);
packetData.writeShort((short) container.slots.size());
for(Slot slot : container.slots) {
packetData.writeItemStack(slot.getContents());
// packetData.writeItemStack(slot.getContents());
}
}

Expand All @@ -38,8 +38,8 @@ public void decode(PacketData packetData) {
container.uniqueId = packetData.readInt();
int slotCount = packetData.readShort();
for(int x = 0; x < slotCount; x++) {
ItemStack itemStack = packetData.readItemStack();
container.getSlot(x).setContents(itemStack);
//ItemStack itemStack = packetData.readItemStack();
// container.getSlot(x).setContents(itemStack);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public SSendChunkPacket(IChunk chunk) {
public void encode(PacketData packetData) {
try {
//System.out.println(buf.limit());
int pos = Ourcraft.chainedChunkStream.fillBuffer(packetData.byteBuf, packetData.size, newChunk);
packetData.size = pos;
int pos = Ourcraft.chainedChunkStream.fillBuffer(packetData.byteBuf, (int)packetData.length(), newChunk);
packetData.setReaderIndex(pos);
} catch (Exception e) {

e.printStackTrace();
Expand Down Expand Up @@ -118,7 +118,7 @@ public void decode(PacketData packetData) {
//IChunk chunk = new ClassicChunk(ClientMain.getClient().newClientWorld,256,0,0);
IChunk chunk = new CubicChunk(ClientMain.getClient().newClientWorld,32,0,0, 0);
try {
Ourcraft.chainedChunkStream.fillChunk(packetData.byteBuf, packetData.size, chunk);
Ourcraft.chainedChunkStream.fillChunk(packetData.byteBuf, (int)packetData.length(), chunk);
} catch (Exception e) {
e.printStackTrace();
}
Expand Down
Loading

0 comments on commit b339f5d

Please sign in to comment.