Skip to content

Commit

Permalink
Removed client singleton and got multi client functionality partially…
Browse files Browse the repository at this point in the history
… working
  • Loading branch information
Hilligans committed Dec 29, 2023
1 parent e65b789 commit 5a381b4
Show file tree
Hide file tree
Showing 69 changed files with 359 additions and 250 deletions.
16 changes: 9 additions & 7 deletions src/main/java/dev/hilligans/ourcraft/ClientMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@

public class ClientMain {

public static Client client;

public static Client getClient() {
return client;
}
public static GameInstance gameInstance = Ourcraft.GAME_INSTANCE;
//public static GameInstance gameInstance = Ourcraft.GAME_INSTANCE;

public static ArgumentContainer argumentContainer;

Expand All @@ -29,7 +24,10 @@ public static Client getClient() {
public static void main(String[] args) throws IOException {
startTime = System.currentTimeMillis();
argumentContainer = new ArgumentContainer(args);

System.out.println(STR."Starting client with PID \{ProcessHandle.current().pid()}");

GameInstance gameInstance = Ourcraft.GAME_INSTANCE;
gameInstance.handleArgs(args);
gameInstance.side = Side.CLIENT;
gameInstance.loadContent();
Expand All @@ -38,18 +36,22 @@ public static void main(String[] args) throws IOException {
try {
Thread thread = new Thread(() -> ServerMain.server(gameInstance, argumentContainer));
thread.setName("Server-Thread");
thread.setDaemon(true);
thread.start();
} catch (Exception e) {
e.printStackTrace();
}
}

client = new Client(gameInstance, argumentContainer);
Client client = new Client(gameInstance, argumentContainer);
Client client1 = new Client(gameInstance, argumentContainer);
String graphicsEngine = argumentContainer.getString("--graphicsEngine", null);
if(graphicsEngine != null) {
System.out.println(graphicsEngine);
client.setGraphicsEngine(gameInstance.GRAPHICS_ENGINES.get(graphicsEngine));
client1.setGraphicsEngine(gameInstance.GRAPHICS_ENGINES.get(graphicsEngine));
}
client1.setupClient();
client.startClient();
}
}
1 change: 0 additions & 1 deletion src/main/java/dev/hilligans/ourcraft/Ourcraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,6 @@ public void press(RenderWindow renderWindow, float strength) {
}
});
}
Ourcraft.getResourceManager().gameInstance = modContent.gameInstance;
}

public static DoubleBuffer getMousePos(long window) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/dev/hilligans/ourcraft/ServerMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ public static void server(GameInstance gameInstance, ArgumentContainer argumentC
//ServerWorld world = new ServerWorld(gameInstance);
//world.worldBuilders.add(new OreBuilder("ore", Blocks.GRASS,Blocks.STONE).setFrequency(20));

server = new MultiPlayerServer();
server = new MultiPlayerServer(gameInstance);
//server.addWorld(0,world);
//server.addWorld(new SimpleServerWorld(0, "server_world"));
IServerWorld world1 = new ServerCubicWorld(0, "planet", 64, new PlanetWorldHeightBuilder(new IWorldHeightBuilder[]{
IServerWorld world1 = new ServerCubicWorld(gameInstance, 0, "planet", 64, new PlanetWorldHeightBuilder(new IWorldHeightBuilder[]{
new SimpleHeightBuilder(),
new SimpleHeightBuilder(),
new SimpleHeightBuilder(),
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dev/hilligans/ourcraft/block/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ public static int getSide(int side, int rotX, int rotY) {
@Override
public void load(GameInstance gameInstance) {
if(blockProperties.path != null && blockProperties.fromFile) {
JSONObject jsonObject = (JSONObject) Ourcraft.GAME_INSTANCE.RESOURCE_LOADER.getResource(new ResourceLocation(blockProperties.path, modId));
JSONObject jsonObject = (JSONObject) gameInstance.RESOURCE_LOADER.getResource(new ResourceLocation(blockProperties.path, modId));
if(jsonObject != null) {
if (overrides != null) {
BlockProperties.recursivelyOverride(jsonObject, overrides);
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/dev/hilligans/ourcraft/client/ChatWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class ChatWindow implements Screen {

static boolean typing;

public RenderWindow renderWindow;

public ChatWindow() {
messageIndex = -1;
}
Expand All @@ -33,6 +35,7 @@ public ChatWindow(String message) {
string = message;
}

/*
static {
KeyHandler.register(new KeyPress() {
@Override
Expand Down Expand Up @@ -154,18 +157,20 @@ public void onRepeat(char key) {
});
}
*/

public static String getString() {
return messageIndex == -1 ? string : sentMessages.size() > messageIndex ? sentMessages.get(sentMessages.size() - messageIndex - 1) : "";
}

@Override
public void setWindow(RenderWindow renderWindow) {

this.renderWindow = renderWindow;
}

@Override
public RenderWindow getWindow() {
return null;
return this.renderWindow;
}

@Override
Expand Down
50 changes: 33 additions & 17 deletions src/main/java/dev/hilligans/ourcraft/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import dev.hilligans.ourcraft.client.rendering.graphics.vulkan.VulkanEngine;
import dev.hilligans.ourcraft.client.rendering.Screen;
import dev.hilligans.ourcraft.client.rendering.ScreenBuilder;
import dev.hilligans.ourcraft.client.rendering.screens.JoinScreen;
import dev.hilligans.ourcraft.client.rendering.screens.container.screens.CreativeInventoryScreen;
import dev.hilligans.ourcraft.client.rendering.screens.container.screens.InventoryScreen;
import dev.hilligans.ourcraft.ClientMain;
Expand Down Expand Up @@ -69,13 +70,9 @@ public class Client implements IClientPacketHandler {
public Screen screen;
public SoundEngine soundEngine;

public int texture;

public boolean refreshTexture = true;

public ClientPlayerData playerData = new ClientPlayerData();
//public IWorld newClientWorld = new SimpleWorld(0,"");
public IWorld newClientWorld = new CubicWorld(0,"", 64);
public IWorld newClientWorld;

public MultiPlayerServer multiPlayerServer;
public boolean rendering = false;
Expand All @@ -95,6 +92,7 @@ public class Client implements IClientPacketHandler {

public Client(GameInstance gameInstance, ArgumentContainer argumentContainer) {
this.gameInstance = gameInstance;
this.newClientWorld = new CubicWorld(gameInstance, 0,"", 64);
logger = gameInstance.LOGGER.withKey("client");
graphicsEngine = gameInstance.GRAPHICS_ENGINES.get("ourcraft:openglEngine");
((OpenGLEngine)graphicsEngine).client = this;
Expand All @@ -114,9 +112,9 @@ public Client setGraphicsEngine(IGraphicsEngine<?,?,?> graphicsEngine) {
return this;
}

public void startClient() {
public void setupClient() {
network = new ClientNetwork(gameInstance.PROTOCOLS.get("Play")).debug(argumentContainer.getBoolean("--packetTrace", false));
authNetwork = new ClientNetwork(gameInstance.PROTOCOLS.get("Auth"));
/*authNetwork = new ClientNetwork(gameInstance.PROTOCOLS.get("Auth"));
CompoundNBTTag tag = WorldLoader.loadTag("clientData.dat");
if(tag != null) {
Expand All @@ -126,30 +124,43 @@ public void startClient() {
} catch (Exception e) {}
});
thread.setName("authenticate");
thread.setDaemon(true);
thread.start();
}
if(tag != null) {
readUsernameAndPassword(tag);
}
authNetwork.sendPacket(new CGetToken(playerData.userName, playerData.login_token));
*/

RenderWindow window = graphicsEngine.startEngine();
window.setClient(this);
client.screen = new JoinScreen();
client.screen.setWindow(window);

window.setClearColor(0.2f, 0.3f, 0.3f, 1.0f);
soundEngine.init(window.getCamera());
soundEngine.setAttenuationModel(AL11.AL_LINEAR_DISTANCE_CLAMPED);
registerKeyHandlers();
if(soundEngine.camera == null) {
soundEngine.init(window.getCamera());
soundEngine.setAttenuationModel(AL11.AL_LINEAR_DISTANCE_CLAMPED);
}
//registerKeyHandlers();

// SplitWindow splitWindow = new SplitWindow(graphicsEngine, window);
// SubWindow subWindow = new SubWindow(graphicsEngine, splitWindow, window.getWindowWidth()/2, window.getWindowHeight()/2, graphicsEngine.getContext());
// subWindow.setRenderPipeline(window.renderPipeline);
// splitWindow.setRenderPipeline("ourcraft:split_window_pipeline");
// splitWindow.addWindow(subWindow);
// SplitWindow splitWindow = new SplitWindow(graphicsEngine, window);
// SubWindow subWindow = new SubWindow(graphicsEngine, splitWindow, window.getWindowWidth()/2, window.getWindowHeight()/2, graphicsEngine.getContext());
// subWindow.setRenderPipeline(window.renderPipeline);
// splitWindow.setRenderPipeline("ourcraft:split_window_pipeline");
// splitWindow.addWindow(subWindow);

rWindow = window;
}

public void startClient() {
setupClient();

System.err.println("Time to start running: " + (System.currentTimeMillis() - ClientMain.startTime));
graphicsEngine.createRenderLoop(gameInstance, window).run();
graphicsEngine.createRenderLoop(gameInstance, rWindow).run();
graphicsEngine.close();
cleanUp();
System.exit(1);
}

public RenderWindow rWindow;
Expand Down Expand Up @@ -414,4 +425,9 @@ public Client getClient() {
public IWorld getWorld() {
return newClientWorld;
}

@Override
public GameInstance getGameInstance() {
return gameInstance;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,14 @@ private static void onRepeat(int id) {
}

private static char getChar(int id) {
if(glfwGetKey(ClientMain.getClient().window,GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS || glfwGetKey(ClientMain.getClient().window,GLFW_KEY_RIGHT_SHIFT) == GLFW_PRESS) {
/* if(glfwGetKey(ClientMain.getClient().window,GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS || glfwGetKey(ClientMain.getClient().window,GLFW_KEY_RIGHT_SHIFT) == GLFW_PRESS) {
return shiftMappedKeys.getOrDefault(id,mappedKeys.getOrDefault(id,Character.MIN_VALUE));
} else {
return mappedKeys.getOrDefault(id,Character.MIN_VALUE);
}
*/
throw new RuntimeException();
}

private static void setCallback(long window) {
Expand Down Expand Up @@ -128,11 +131,11 @@ private static void putName(int id, String name) {
}

static {
if(ClientMain.getClient().glStarted) {
// if(ClientMain.getClient().glStarted) {
//setCallback(ClientMain.getClient().window);
} else {
// } else {
// Ourcraft.GAME_INSTANCE.EVENT_BUS.register(GLInitEvent.class,KeyHandler::setCallback);
}
// }

mappedKeys.put(32,' ');
mappedKeys.put(39, '\'');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void setContainer(Container container) {
public ContainerScreen() {
}

public RecipeHelper recipeHelper = new RecipeHelper(Ourcraft.GAME_INSTANCE);
//public RecipeHelper recipeHelper = new RecipeHelper(Ourcraft.GAME_INSTANCE);

@Override
public void render(RenderWindow window, MatrixStack matrixStack, GraphicsContext graphicsContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static int getSolidChunkFaces(ISubChunk chunk) {
for(int x = 0; x < 16; x++) {
for(int y = 0; y < 16; y++) {
for(int z = 0; z < 16; z++) {
if(chunk.getBlockState(x,y,z).getBlock().blockProperties.transparent) {
if(chunk.getBlockState(null, x,y,z).getBlock().blockProperties.transparent) {
vals[x][y] |= 1 << z;
recursivelyCheck(chunk,vals,x,y,z);
if(checkFace(vals,0,true,false,false) && checkFace(vals,15,true,false,false)) {
Expand Down Expand Up @@ -100,37 +100,37 @@ public static boolean checkFace(short[][] vals, int pos, boolean addX, boolean a

public static void recursivelyCheck(ISubChunk chunk, short[][] vals, int x, int y, int z) {
boolean posZ = false, negZ = false, posX = false, negX = false, posY = false, negY = false;
if(z != 15 && chunk.getBlockState(x,y,z + 1).getBlock().blockProperties.transparent) {
if(z != 15 && chunk.getBlockState(null, x,y,z + 1).getBlock().blockProperties.transparent) {
if ((vals[x][y] & (1 << z + 1)) == 0) {
vals[x][y] |= 1 << z + 1;
posZ = true;
}
}
if(z != 0 && chunk.getBlockState(x,y,z - 1).getBlock().blockProperties.transparent) {
if(z != 0 && chunk.getBlockState(null, x,y,z - 1).getBlock().blockProperties.transparent) {
if ((vals[x][y] & (1 << z - 1)) == 0) {
vals[x][y] |= 1 << z - 1;
negZ = true;
}
}
if(x != 15 && chunk.getBlockState(x + 1,y,z).getBlock().blockProperties.transparent) {
if(x != 15 && chunk.getBlockState(null, x + 1,y,z).getBlock().blockProperties.transparent) {
if ((vals[x + 1][y] & (1 << z)) == 0) {
vals[x + 1][y] |= 1 << z;
posX = true;
}
}
if(x != 0 && chunk.getBlockState(x - 1,y,z).getBlock().blockProperties.transparent) {
if(x != 0 && chunk.getBlockState(null, x - 1,y,z).getBlock().blockProperties.transparent) {
if ((vals[x - 1][y] & (1 << z)) == 0) {
vals[x - 1][y] |= 1 << z;
negX = true;
}
}
if(y != 15 && chunk.getBlockState(x,y + 1,z).getBlock().blockProperties.transparent) {
if(y != 15 && chunk.getBlockState(null, x,y + 1,z).getBlock().blockProperties.transparent) {
if ((vals[x][y + 1] & (1 << z)) == 0) {
vals[x][y + 1] |= 1 << z;
posY = true;
}
}
if(y != 0 && chunk.getBlockState(x,y - 1,z).getBlock().blockProperties.transparent) {
if(y != 0 && chunk.getBlockState(null, x,y - 1,z).getBlock().blockProperties.transparent) {
if ((vals[x][y - 1] & (1 << z)) == 0) {
vals[x][y - 1] |= 1 << z;
negY = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class RenderPipeline implements IRegistryElement, IGraphicsElement {

public String name;
public ModContent modContent;
public RenderWindow window;
// public RenderWindow window;

public ArrayList<RenderTarget> renderTargets = new ArrayList<>();
public ArrayList<RenderTask> renderTasks = new ArrayList<>();
Expand All @@ -25,7 +25,7 @@ public RenderPipeline(String name) {
}

public void render(Client client, MatrixStack worldStack, MatrixStack screenStack, GraphicsContext graphicsContext) {
window.render(graphicsContext, client, worldStack, screenStack);
client.rWindow.render(graphicsContext, client, worldStack, screenStack);
}

public void addRenderTarget(RenderTarget renderTarget) {
Expand Down Expand Up @@ -57,7 +57,7 @@ public void addRenderTarget(RenderTarget renderTarget) {
}

public void build(RenderWindow window) {
this.window = window;
//this.window = window;
}

public void buildTargets(IGraphicsEngine<?, ?, ?> graphicsEngine) {
Expand Down Expand Up @@ -103,7 +103,7 @@ public String getResourceType() {
public String toString() {
return "RenderPipeline{" +
"name='" + name + '\'' +
", window=" + window +
// ", window=" + window +
", renderTargets=" + renderTargets +
", renderTasks=" + renderTasks +
'}';
Expand Down
Loading

0 comments on commit 5a381b4

Please sign in to comment.