Skip to content

Commit

Permalink
Fixed networking
Browse files Browse the repository at this point in the history
  • Loading branch information
MisterJulsen committed Apr 5, 2024
1 parent 3a1a181 commit ff7c1e5
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/main/java/de/mrjulsen/crn/network/NetworkManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import de.mrjulsen.crn.network.packets.stc.TrackStationResponsePacket;
import de.mrjulsen.crn.network.packets.stc.TrainDataResponsePacket;
import de.mrjulsen.mcdragonlib.network.IPacketBase;
import de.mrjulsen.mcdragonlib.network.NetworkManagerBase;

public class NetworkManager extends NetworkManagerBase<NetworkManager> {

Expand All @@ -34,7 +33,7 @@ public NetworkManager(String modid, String channelName, String protocolVersion)
}

public static void create() {
instance = NetworkManagerBase.create(NetworkManager.class, ModMain.MOD_ID, "crn_network", "5");
instance = NetworkManagerBase.create(NetworkManager.class, ModMain.MOD_ID, "createrailwaysnavigator_network", "v6");
}

public static NetworkManager getInstance() {
Expand Down
97 changes: 97 additions & 0 deletions src/main/java/de/mrjulsen/crn/network/NetworkManagerBase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package de.mrjulsen.crn.network;

import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.Optional;
import java.util.function.Supplier;

import de.mrjulsen.mcdragonlib.DragonLib;
import de.mrjulsen.mcdragonlib.network.IPacketBase;
import net.minecraft.network.Connection;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.network.NetworkDirection;
import net.minecraftforge.network.NetworkEvent;
import net.minecraftforge.network.NetworkRegistry;
import net.minecraftforge.network.simple.SimpleChannel;

public abstract class NetworkManagerBase<N extends NetworkManagerBase<N>> {

public final String PROTOCOL_VERSION;
private static int currentId = 0;

public final SimpleChannel MOD_CHANNEL;

protected NetworkManagerBase(String modid, String channelName, String protocolVersion) {
PROTOCOL_VERSION = protocolVersion;
MOD_CHANNEL = NetworkRegistry.ChannelBuilder.named(new ResourceLocation(modid, channelName)).networkProtocolVersion(() -> PROTOCOL_VERSION).clientAcceptedVersions(PROTOCOL_VERSION::equals).serverAcceptedVersions(PROTOCOL_VERSION::equals).simpleChannel();

registerPackets(packets());
}

public <T extends IPacketBase<T>> void registerPackets(Collection<Class<? extends IPacketBase<?>>> classes) {
for (Class<? extends IPacketBase<?>> clazz : classes) {
registerPacketHelper(clazz);
}
}

@SuppressWarnings("unchecked")
private <T extends IPacketBase<T>> void registerPacketHelper(Class<?> clazz) {
registerPacket((Class<T>)clazz);
}


public static <T extends NetworkManagerBase<T>> T create(Class<T> networkManager, String modid, String channelName, String protocolVersion) {
try {
return networkManager.getConstructor(String.class, String.class, String.class).newInstance(modid, channelName, protocolVersion);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
e.printStackTrace();
DragonLib.LOGGER.error("Unable to create NetworkManager.", e);
return null;
}
}

public abstract Collection<Class<? extends IPacketBase<?>>> packets();

public SimpleChannel getPlayChannel() {
return MOD_CHANNEL;
}

private <T extends IPacketBase<T>> void registerPacket(Class<T> clazz) {
try {
T packet = clazz.getConstructor().newInstance();
currentId++;
final int id = currentId;
MOD_CHANNEL.registerMessage(id, clazz, packet::encode, packet::decode, packet::handle, Optional.of(packet.getDirection()));
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
e.printStackTrace();
DragonLib.LOGGER.error("Unable to register packet.", e);
}
}

public <T> void sendToClient(IPacketBase<?> o, ServerPlayer player) {
MOD_CHANNEL.sendTo(o, player.connection.connection, NetworkDirection.PLAY_TO_CLIENT);
}

public <T> void sendToServer(Connection connection, IPacketBase<?> o) {
MOD_CHANNEL.sendTo(o, connection, NetworkDirection.PLAY_TO_SERVER);
}

public static void executeOnClient(Runnable task) {
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> task.run());
}

public static <T extends IPacketBase<?>> void handlePacket(T packet, Supplier<NetworkEvent.Context> context, Runnable run) {
context.get().enqueueWork(() -> {
if (packet.getDirection() == NetworkDirection.PLAY_TO_SERVER || packet.getDirection() == NetworkDirection.LOGIN_TO_SERVER) {
run.run();
} else if (packet.getDirection() == NetworkDirection.PLAY_TO_CLIENT || packet.getDirection() == NetworkDirection.LOGIN_TO_CLIENT) {
executeOnClient(run);
}
});
context.get().setPacketHandled(true);
}
}

0 comments on commit ff7c1e5

Please sign in to comment.