Initial Network System implementation
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
package me.hypherionmc.craterlib.common;
|
||||
|
||||
import me.hypherionmc.craterlib.network.CraterNetworkHandler;
|
||||
import me.hypherionmc.craterlib.network.CraterPacket;
|
||||
import me.hypherionmc.craterlib.network.FabricNetworkHandler;
|
||||
import me.hypherionmc.craterlib.platform.services.LibCommonHelper;
|
||||
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
|
||||
import net.fabricmc.fabric.api.networking.v1.PacketSender;
|
||||
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.multiplayer.ClientPacketListener;
|
||||
import net.minecraft.network.Connection;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.server.network.ServerGamePacketListenerImpl;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.level.Level;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* @author HypherionSA
|
||||
* @date 24/09/2022
|
||||
*/
|
||||
public class FabricCommonHelper implements LibCommonHelper {
|
||||
|
||||
public static MinecraftServer server;
|
||||
|
||||
@Override
|
||||
public CraterNetworkHandler createPacketHandler(String modid) {
|
||||
return FabricNetworkHandler.of(modid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Minecraft getClientInstance() {
|
||||
return Minecraft.getInstance();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Player getClientPlayer() {
|
||||
return Minecraft.getInstance().player;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Level getClientLevel() {
|
||||
return Minecraft.getInstance().level;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Connection getClientConnection() {
|
||||
return Minecraft.getInstance().getConnection().getConnection();
|
||||
}
|
||||
|
||||
@Override
|
||||
public MinecraftServer getMCServer() {
|
||||
return server;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerClientReceiver(ResourceLocation channelName, Function<FriendlyByteBuf, CraterPacket<?>> factory) {
|
||||
ClientPlayNetworking.registerGlobalReceiver(channelName, (Minecraft client, ClientPacketListener handler, FriendlyByteBuf buf, PacketSender responseSender) -> {
|
||||
CraterPacket<?> packet = factory.apply(buf);
|
||||
client.execute(() -> packet.handle(client.player, client));
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerServerReceiver(ResourceLocation channelName, Function<FriendlyByteBuf, CraterPacket<?>> factory) {
|
||||
ServerPlayNetworking.registerGlobalReceiver(channelName, (MinecraftServer server, ServerPlayer player, ServerGamePacketListenerImpl handler, FriendlyByteBuf buf, PacketSender responseSender) -> {
|
||||
CraterPacket<?> packet = factory.apply(buf);
|
||||
server.execute(() -> packet.handle(player, server));
|
||||
});
|
||||
}
|
||||
}
|
@@ -0,0 +1,71 @@
|
||||
package me.hypherionmc.craterlib.common;
|
||||
|
||||
import me.hypherionmc.craterlib.platform.services.Environment;
|
||||
import me.hypherionmc.craterlib.platform.services.ILoaderHelper;
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
import net.minecraft.SharedConstants;
|
||||
import net.minecraft.client.Minecraft;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
/**
|
||||
* @author HypherionSA
|
||||
* @date 07/08/2022
|
||||
*/
|
||||
public class FabricLoaderHelper implements ILoaderHelper {
|
||||
|
||||
@Override
|
||||
public boolean isFabric() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isForge() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getGameVersion() {
|
||||
return SharedConstants.VERSION_STRING;
|
||||
}
|
||||
|
||||
@Override
|
||||
public File getGameFolder() {
|
||||
return Minecraft.getInstance().gameDirectory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public File getConfigFolder() {
|
||||
return FabricLoader.getInstance().getConfigDir().toFile();
|
||||
}
|
||||
|
||||
@Override
|
||||
public File getModsFolder() {
|
||||
return new File(FabricLoader.getInstance().getGameDir().toString() + File.separator + "mods");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Environment getEnvironment() {
|
||||
switch (FabricLoader.getInstance().getEnvironmentType()) {
|
||||
case SERVER -> {
|
||||
return Environment.SERVER;
|
||||
}
|
||||
case CLIENT -> {
|
||||
return Environment.CLIENT;
|
||||
}
|
||||
}
|
||||
return Environment.UNKNOWN;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isModLoaded(String modid) {
|
||||
return FabricLoader.getInstance().isModLoaded(modid);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDevEnv() {
|
||||
return FabricLoader.getInstance().isDevelopmentEnvironment();
|
||||
}
|
||||
}
|
@@ -1,26 +0,0 @@
|
||||
package me.hypherionmc.craterlib.common;
|
||||
|
||||
import me.hypherionmc.craterlib.platform.services.IPlatformHelper;
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
|
||||
/**
|
||||
* @author HypherionSA
|
||||
* @date 07/08/2022
|
||||
*/
|
||||
public class FabricPlatformHelper implements IPlatformHelper {
|
||||
|
||||
@Override
|
||||
public String getPlatformName() {
|
||||
return "Fabric";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isModLoaded(String modId) {
|
||||
return FabricLoader.getInstance().isModLoaded(modId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDevelopmentEnvironment() {
|
||||
return FabricLoader.getInstance().isDevelopmentEnvironment();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user