Skip to content

Commit

Permalink
Net2 Work Continued
Browse files Browse the repository at this point in the history
I am currently hopped up on pain meds after my wisdom teeth so this is probably not my best code

Signed-off-by: Siuolplex <lmeitrodt@gmail.com>
  • Loading branch information
Siuolplex committed Jul 16, 2024
1 parent 24fe011 commit 028c9c1
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 199 deletions.
5 changes: 5 additions & 0 deletions src/main/java/org/violetmoon/zeta/net2/NetworkPhase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.violetmoon.zeta.net2;

public enum NetworkPhase {
PLAY, LOGIN, ANY
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.violetmoon.zeta.net2.event.load;

import com.mojang.serialization.Codec;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import org.violetmoon.zeta.event.bus.IZetaLoadEvent;
import org.violetmoon.zeta.net2.NetworkPhase;

public interface ZRegisterPayloadHandlers extends IZetaLoadEvent {
<T> void registerServerboundPayload(CustomPacketPayload.Type type, StreamCodec codec, T handler, NetworkPhase phase);
<T> void registerClientboundPayload(CustomPacketPayload.Type type, StreamCodec codec, T handler, NetworkPhase phase);
<T> void registerBidirectionalPayload(CustomPacketPayload.Type type, StreamCodec codec, T handler, NetworkPhase phase);
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

22 changes: 22 additions & 0 deletions src/main/java/org/violetmoon/zeta/net2/payload/SyncPayload.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.violetmoon.zeta.net2.payload;

import io.netty.buffer.ByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
import org.violetmoon.zeta.Zeta;

public record SyncPayload(byte[] flags, int expectedLength, int expectedHash) implements ZetaPacketPayload {
public static final CustomPacketPayload.Type<SyncPayload> TYPE = new Type<>(ResourceLocation.fromNamespaceAndPath(Zeta.ZETA_ID, "handshake_payload"));
public static final StreamCodec<ByteBuf, SyncPayload> STREAM_CODEC = StreamCodec.composite(
ByteBufCodecs.BYTE_ARRAY, SyncPayload::flags,
ByteBufCodecs.INT, SyncPayload::expectedLength,
ByteBufCodecs.INT, SyncPayload::expectedHash,
SyncPayload::new
);
@Override
public Type<? extends CustomPacketPayload> type() {
return TYPE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.violetmoon.zeta.net2.payload;

import net.minecraft.network.protocol.common.custom.CustomPacketPayload;

public interface ZetaPacketPayload extends CustomPacketPayload {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package org.violetmoon.zetaimplforge.net2.event.load;

import com.mojang.serialization.Codec;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.neoforged.neoforge.network.event.RegisterPayloadHandlersEvent;
import net.neoforged.neoforge.network.registration.PayloadRegistrar;
import org.violetmoon.zeta.Zeta;
import org.violetmoon.zeta.net2.NetworkPhase;
import org.violetmoon.zeta.net2.event.load.ZRegisterPayloadHandlers;
import org.violetmoon.zetaimplforge.net2.handlers.ZetaPayloadHandler;

public class ForgeZRegisterPayloadHandlers implements ZRegisterPayloadHandlers {
private final RegisterPayloadHandlersEvent e;

public ForgeZRegisterPayloadHandlers(RegisterPayloadHandlersEvent event) {
this.e = event;
}

@Override
public <T> void registerServerboundPayload(CustomPacketPayload.Type type, StreamCodec codec, T handler, NetworkPhase phase) {
if (handler instanceof ZetaPayloadHandler zetaPayloadHandler) {
final PayloadRegistrar registrar = e.registrar("1");
switch (phase) {
case PLAY: {
registrar.playToServer(type, codec, zetaPayloadHandler);
}
case LOGIN: {
registrar.configurationToServer(type, codec, zetaPayloadHandler);
}
case ANY: {
registrar.commonToServer(type, codec, zetaPayloadHandler);
}
}
} else {
Zeta.GLOBAL_LOG.fatal("THE PACKETS HAVE FAILED, OH GOD. PLEASE REPLACE ME WITH AN ACTUAL ERROR MESSAGE"); //todo: Replace this with an actual error message
}
}

@Override
public <T> void registerClientboundPayload(CustomPacketPayload.Type type, StreamCodec codec, T handler, NetworkPhase phase) {
if (handler instanceof ZetaPayloadHandler zetaPayloadHandler) {
final PayloadRegistrar registrar = e.registrar("1");
switch (phase) {
case PLAY: {
registrar.playToClient(type, codec, zetaPayloadHandler);
}
case LOGIN: {
registrar.configurationToClient(type, codec, zetaPayloadHandler);
}
case ANY: {
registrar.commonToClient(type, codec, zetaPayloadHandler);
}
}
} else {
Zeta.GLOBAL_LOG.fatal("THE PACKETS HAVE FAILED, OH GOD. PLEASE REPLACE ME WITH AN ACTUAL ERROR MESSAGE"); //todo: Replace this with an actual error message
}
}

@Override
public <T> void registerBidirectionalPayload(CustomPacketPayload.Type type, StreamCodec codec, T handler, NetworkPhase phase) {
if (handler instanceof ZetaPayloadHandler zetaPayloadHandler) {
final PayloadRegistrar registrar = e.registrar("1");
switch (phase) {
case PLAY: {
registrar.playBidirectional(type, codec, zetaPayloadHandler);
}
case LOGIN: {
registrar.configurationBidirectional(type, codec, zetaPayloadHandler);
}
case ANY: {
registrar.commonBidirectional(type, codec, zetaPayloadHandler);
}
}
} else {
Zeta.GLOBAL_LOG.fatal("THE PACKETS HAVE FAILED, OH GOD. PLEASE REPLACE ME WITH AN ACTUAL ERROR MESSAGE"); //todo: Replace this with an actual error message
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.violetmoon.zetaimplforge.net2.handlers;

import net.neoforged.neoforge.network.handling.IPayloadHandler;

//todo: Im putting this in for now, but it will probably go byebye
public interface ZetaPayloadHandler extends IPayloadHandler {}

0 comments on commit 028c9c1

Please sign in to comment.