Skip to content

Commit

Permalink
Fixed networking for new engine loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Hilligans committed Feb 27, 2024
1 parent 402b94b commit 8d9b981
Show file tree
Hide file tree
Showing 13 changed files with 187 additions and 189 deletions.
11 changes: 11 additions & 0 deletions src/main/java/dev/hilligans/ourcraft/GameInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,17 @@ public void copyRegistries() {
LAYOUT_ENGINES = (Registry<ILayoutEngine<?>>) REGISTRIES.getExcept("ourcraft:layout_engine");
}

public void finishBuild() {
for (Registry<?> registry : REGISTRIES.ELEMENTS) {
for (Object o : registry.ELEMENTS) {
if (o instanceof IRegistryElement) {
((IRegistryElement) o).load(this);
}
}
}
buildBlockStates();
}

public void buildBlockStates() {
BLOCK_STATES = new ArrayList<>(BLOCKS.ELEMENTS.size());
int offset = 0;
Expand Down
272 changes: 136 additions & 136 deletions src/main/java/dev/hilligans/ourcraft/Ourcraft.java

Large diffs are not rendered by default.

25 changes: 7 additions & 18 deletions src/main/java/dev/hilligans/ourcraft/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
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.packet.client.CCloseScreen;
import dev.hilligans.ourcraft.network.packet.client.CDropItem;
Expand Down Expand Up @@ -119,7 +120,6 @@ public Client setGraphicsEngine(IGraphicsEngine<?,?,?> graphicsEngine) {
}

public void setupClient() {
Thread.startVirtualThread(() -> network = new ClientNetwork(gameInstance.PROTOCOLS.get("Play")).debug(argumentContainer.getBoolean("--packetTrace", false)));
/*authNetwork = new ClientNetwork(gameInstance.PROTOCOLS.get("Auth"));
CompoundNBTTag tag = WorldLoader.loadTag("clientData.dat");
Expand Down Expand Up @@ -177,9 +177,14 @@ public void tick(ThreadContext threadContext) {
newClientWorld.tick();
}
try(var $0 = threadContext.getSection().startSection("process_packets")) {
network.processPackets();
if(network != null) {
network.processPackets();
}
}
if(transition) {
Thread.startVirtualThread(() -> {
network = new ClientNetwork(gameInstance.PROTOCOLS.get("ourcraft:Play")).debug(argumentContainer.getBoolean("--packetTrace", false));
});
client.gameInstance.build(client.graphicsEngine, null);
transition = false;
rWindow.setRenderPipeline("ourcraft:menu_pipeline");
Expand Down Expand Up @@ -264,22 +269,6 @@ public void openScreen(String screenName) {

public void registerKeyHandlers() {

/*
KeyHandler.register(new KeyPress() {
@Override
public void onPress() {
clientWorld.reloadChunks();
}
},KeyHandler.GLFW_KEY_F9);
KeyHandler.register(new KeyPress() {
@Override
public void onPress() {
ResourceManager.reload();
clientWorld.reloadChunks();
}
},KeyHandler.GLFW_KEY_F8);
*/

KeyHandler.register(new KeyPress() {
@Override
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/dev/hilligans/ourcraft/client/input/Input.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dev.hilligans.ourcraft.client.rendering.graphics.RenderWindow;
import dev.hilligans.ourcraft.client.rendering.graphics.api.IInputProvider;
import dev.hilligans.ourcraft.mod.handler.content.ModContainer;
import dev.hilligans.ourcraft.mod.handler.content.ModContent;
import dev.hilligans.ourcraft.util.registry.IRegistryElement;
import it.unimi.dsi.fastutil.ints.IntArrayList;
Expand All @@ -23,8 +24,6 @@ public class Input implements IRegistryElement {

public String displayName;

public ModContent modContent;

public Input(String defaultBind) {
bind(defaultBind);
}
Expand Down Expand Up @@ -83,9 +82,9 @@ public String getResourceType() {
return "key_bind";
}

public void setModContent(ModContent modContent) {
this.modContent = modContent;
this.modID = modContent.getModID();
@Override
public void assignOwner(ModContainer owner) {
this.modID = owner.getModID();
}

public String getDisplay() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public void draw(RenderWindow window, GraphicsContext graphicsContext, IGraphics
screenStack.setColor(1.0f,1.0f,1.0f);
engine.getDefaultImpl().uploadMatrix(graphicsContext, screenStack, Textures.BACKFILL.shaderSource);

rend.drawStringInternal(window, graphicsContext, screenStack, stage.getTypeA(), 20, 29, 0.5f);
rend.drawStringInternal(window, graphicsContext, screenStack, STR."\{index} of \{gameInstance.loaderPipeline.section.subsectionLength}", 20, 29 * 2, 0.5f);
rend.drawCenteredStringInternal(window, graphicsContext, screenStack, stage.getTypeA(), 29, 0.5f);
rend.drawCenteredStringInternal(window, graphicsContext, screenStack, STR."\{index} of \{gameInstance.loaderPipeline.section.subsectionLength}", 29 * 2, 0.5f);
screenStack.pop();
} catch (Exception e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,13 @@ public void drawCenteredStringInternal(RenderWindow window, GraphicsContext grap
if(textureAtlas == null) {
return;
}
PrimitiveBuilder primitiveBuilder = primitiveBuilders.get(val);
primitiveBuilder.translate(x - finalWidth / 2f,0,0);
impl.bindPipeline(graphicsContext,shaderSource.program);
impl.bindTexture(graphicsContext,textureAtlas.glTextureId);
impl.drawAndDestroyMesh(graphicsContext,matrixStack,primitiveBuilder.toVertexMesh());
if(textureAtlas.glTextureId != -1) {
PrimitiveBuilder primitiveBuilder = primitiveBuilders.get(val);
primitiveBuilder.translate(x - finalWidth / 2f, 0, 0);
impl.bindPipeline(graphicsContext, shaderSource.program);
impl.bindTexture(graphicsContext, textureAtlas.glTextureId);
impl.drawAndDestroyMesh(graphicsContext, matrixStack, primitiveBuilder.toVertexMesh());
}

});
} catch (Exception ignored) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public void recursivelyLoad(String modID) {


public String[] getModList() {
String[] modList = new String[mods.size() - 1];
String[] modList = new String[mods.size()];
int x = 0;
for(String string : mods.keySet()) {
if(!string.equals("ourcraft")) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.hilligans.ourcraft.mod.handler.content;

import dev.hilligans.ourcraft.GameInstance;
import dev.hilligans.ourcraft.client.input.Input;
import dev.hilligans.ourcraft.client.input.InputHandlerProvider;
import dev.hilligans.ourcraft.client.rendering.Texture;
import dev.hilligans.ourcraft.client.rendering.graphics.*;
Expand Down Expand Up @@ -57,6 +58,10 @@ public void registerResourceLoader(ResourceLoader<?>... resourceLoaders) {
container.registerCore("ourcraft:resource_loader", resourceLoaders);
}

public void registerKeybinds(Input... inputs) {
container.registerCore("ourcraft:key_bind", inputs);
}

public void registerTexture(Texture... textures) {
container.registerCore("ourcraft:texture", textures);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public void registerModel(IModel... models) {
public final void registerPacket(Supplier<PacketBase>... packets) {
ModContainer self = this;
for(Supplier<PacketBase> packet : packets) {
Protocol protocol = protocolRegistry.computeIfAbsent("Play", (s -> new Protocol(s).setSource(self)));
Protocol protocol = protocolRegistry.computeIfAbsent("ourcraft:Play", (s -> new Protocol(s.split(":")[1]).setSource(self)));
protocol.register(packet);
}
}
Expand All @@ -198,7 +198,7 @@ public final void registerPacket(Supplier<PacketBase>... packets) {
public final void registerPacket(String protocolName, Supplier<PacketBase>... packets) {
ModContainer self = this;
for(Supplier<PacketBase> packet : packets) {
Protocol protocol = protocolRegistry.computeIfAbsent(protocolName, (s -> new Protocol(s).setSource(self)));
Protocol protocol = protocolRegistry.computeIfAbsent(protocolName, (s -> new Protocol(s.split(":")[1]).setSource(self)));
protocol.register(packet);
}
}
Expand All @@ -207,7 +207,7 @@ public final void registerPacket(String protocolName, Supplier<PacketBase>... pa
public final void registerPacket(String protocolName, int id, Supplier<PacketBase>... packets) {
ModContainer self = this;
for(Supplier<PacketBase> packet : packets) {
Protocol protocol = protocolRegistry.computeIfAbsent(protocolName, (s -> new Protocol(s).setSource(self)));
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
Expand Up @@ -67,15 +67,7 @@ public static InstanceLoaderPipeline get(GameInstance gameInstance) {
//this will copy all the loaded content to the game instance in a predictable way
pipeline.addStage("Copy Content Back", (pipeline12, section) -> pipeline12.getModList().foreach(modContainer -> pipeline12.getGameInstance().REGISTRIES.putFrom(modContainer.registries)));

pipeline.addStage("Build Content For Game Instance", (pipeline15, section) -> {
for (Registry<?> registry : pipeline15.getGameInstance().REGISTRIES.ELEMENTS) {
for (Object o : registry.ELEMENTS) {
if (o instanceof IRegistryElement) {
((IRegistryElement) o).load(pipeline15.getGameInstance());
}
}
}
});
pipeline.addStage("Build Content For Game Instance", (pipeline15, section) -> pipeline15.getGameInstance().finishBuild());

pipeline.addStage("Post Hooks", (pipeline1, section) -> pipeline1.runPostHooks());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public ClientNetwork debug(boolean debug) {

@Override
public void sendPacket(PacketBase packetBase) {
System.out.println("Sending packet:" + packetBase.getClass());
// System.out.println("Sending packet:" + packetBase.getClass());
if(networkHandler != null && ((ClientNetworkHandler)networkHandler).enabled) {
packetBase.packetId = sendProtocol.packetMap.get(packetBase.getClass());
sendPacketDirect(packetBase);
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/dev/hilligans/ourcraft/network/Protocols.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ public static void register(ModContainer modContent) {
modContent.registerPacket(SSendModContentPacket::new);
modContent.registerPacket(CRequestContent::new);

modContent.registerPacket("Auth", SAccountPacket::new);
modContent.registerPacket("Auth", SSendToken::new);
modContent.registerPacket("Auth", STokenValid::new);
modContent.registerPacket("Auth", SSendLoginToken::new);
modContent.registerPacket("ourcraft:Auth", SAccountPacket::new);
modContent.registerPacket("ourcraft:Auth", SSendToken::new);
modContent.registerPacket("ourcraft:Auth", STokenValid::new);
modContent.registerPacket("ourcraft:Auth", SSendLoginToken::new);

modContent.registerPacket("Auth", CCreateAccount::new);
modContent.registerPacket("Auth", CGetToken::new);
modContent.registerPacket("Auth", CTokenValid::new);
modContent.registerPacket("Auth", CLogin::new);
modContent.registerPacket("ourcraft:Auth", CCreateAccount::new);
modContent.registerPacket("ourcraft:Auth", CGetToken::new);
modContent.registerPacket("ourcraft:Auth", CTokenValid::new);
modContent.registerPacket("ourcraft:Auth", CLogin::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void startServer(String port) {
playerHandler.scheduleAtFixedRate(new PlayerHandler(this), 0, 10, TimeUnit.MILLISECONDS);
// ConsoleReader consoleReader = new ConsoleReader(this::executeCommand);

serverNetwork = new ServerNetwork(gameInstance.PROTOCOLS.get("Play"), this).debug(Ourcraft.getArgumentContainer().getBoolean("--packetTrace", false));
serverNetwork = new ServerNetwork(gameInstance.PROTOCOLS.get("ourcraft:Play"), this).debug(Ourcraft.getArgumentContainer().getBoolean("--packetTrace", false));
try {
serverNetwork.startServer(port);
} catch (Exception e) {
Expand Down

0 comments on commit 8d9b981

Please sign in to comment.