Implemented a generic proxy for dealing with different logical sides.
This commit is contained in:
@ -1,10 +1,9 @@
|
||||
package dev.micle.loginprotection;
|
||||
|
||||
import dev.micle.loginprotection.data.ProtectedPlayers;
|
||||
import dev.micle.loginprotection.setup.Config;
|
||||
import dev.micle.loginprotection.setup.Registration;
|
||||
import dev.micle.loginprotection.proxy.IProxy;
|
||||
import dev.micle.loginprotection.proxy.Proxy;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.fml.DistExecutor;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import net.minecraftforge.network.NetworkRegistry;
|
||||
import net.minecraftforge.network.simple.SimpleChannel;
|
||||
@ -21,11 +20,16 @@ public class LoginProtection {
|
||||
PROTOCOL_VERSION::equals,
|
||||
PROTOCOL_VERSION::equals
|
||||
);
|
||||
private static IProxy proxy;
|
||||
|
||||
public LoginProtection() {
|
||||
Registration.register();
|
||||
Config.init();
|
||||
proxy = DistExecutor.safeRunForDist(
|
||||
() -> Proxy.Client::new,
|
||||
() -> Proxy.Server::new
|
||||
);
|
||||
}
|
||||
|
||||
MinecraftForge.EVENT_BUS.register(this);
|
||||
public static IProxy getProxy() {
|
||||
return proxy;
|
||||
}
|
||||
}
|
||||
|
||||
11
src/main/java/dev/micle/loginprotection/proxy/IProxy.java
Normal file
11
src/main/java/dev/micle/loginprotection/proxy/IProxy.java
Normal file
@ -0,0 +1,11 @@
|
||||
package dev.micle.loginprotection.proxy;
|
||||
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.level.Level;
|
||||
|
||||
public interface IProxy {
|
||||
MinecraftServer getServer();
|
||||
Player getClientPlayer();
|
||||
Level getClientWorld();
|
||||
}
|
||||
112
src/main/java/dev/micle/loginprotection/proxy/Proxy.java
Normal file
112
src/main/java/dev/micle/loginprotection/proxy/Proxy.java
Normal file
@ -0,0 +1,112 @@
|
||||
package dev.micle.loginprotection.proxy;
|
||||
|
||||
import dev.micle.loginprotection.data.ProtectedPlayerManager;
|
||||
import dev.micle.loginprotection.setup.Config;
|
||||
import dev.micle.loginprotection.setup.Registration;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.event.AddReloadListenerEvent;
|
||||
import net.minecraftforge.event.server.ServerStartedEvent;
|
||||
import net.minecraftforge.event.server.ServerStoppingEvent;
|
||||
import net.minecraftforge.eventbus.api.IEventBus;
|
||||
import net.minecraftforge.fml.event.lifecycle.*;
|
||||
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
|
||||
|
||||
public class Proxy implements IProxy {
|
||||
// Initialize variables
|
||||
private static MinecraftServer server = null;
|
||||
|
||||
// Common setup
|
||||
public Proxy() {
|
||||
// Initialize setup
|
||||
Registration.register();
|
||||
Config.init();
|
||||
|
||||
// Initialize other
|
||||
ProtectedPlayerManager.init();
|
||||
|
||||
// Register mod event bus listeners
|
||||
IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();
|
||||
modEventBus.addListener(Proxy::setup);
|
||||
modEventBus.addListener(Proxy::imcEnqueue);
|
||||
modEventBus.addListener(Proxy::imcProcess);
|
||||
|
||||
// Register event but listeners
|
||||
MinecraftForge.EVENT_BUS.addListener(Proxy::onAddReloadListeners);
|
||||
MinecraftForge.EVENT_BUS.addListener(Proxy::serverStarted);
|
||||
MinecraftForge.EVENT_BUS.addListener(Proxy::serverStopping);
|
||||
}
|
||||
|
||||
private static void setup(FMLCommonSetupEvent event) {}
|
||||
|
||||
private static void imcEnqueue(InterModEnqueueEvent event) {}
|
||||
|
||||
private static void imcProcess(InterModProcessEvent event) {}
|
||||
|
||||
private static void onAddReloadListeners(AddReloadListenerEvent event) {}
|
||||
|
||||
private static void serverStarted(ServerStartedEvent event) {
|
||||
server = event.getServer();
|
||||
}
|
||||
|
||||
private static void serverStopping(ServerStoppingEvent event) {
|
||||
server = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MinecraftServer getServer() {
|
||||
return server;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Player getClientPlayer() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Level getClientWorld() {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Client setup
|
||||
public static class Client extends Proxy {
|
||||
public Client() {
|
||||
// Register mod event bus listeners
|
||||
IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();
|
||||
modEventBus.addListener(Client::setup);
|
||||
modEventBus.addListener(Client::postSetup);
|
||||
}
|
||||
|
||||
private static void setup(FMLClientSetupEvent event) {}
|
||||
|
||||
private static void postSetup(FMLLoadCompleteEvent event) {}
|
||||
|
||||
@Override
|
||||
@OnlyIn(Dist.CLIENT)
|
||||
public Player getClientPlayer() {
|
||||
return Minecraft.getInstance().player;
|
||||
}
|
||||
|
||||
@Override
|
||||
@OnlyIn(Dist.DEDICATED_SERVER)
|
||||
public Level getClientWorld() {
|
||||
return Minecraft.getInstance().level;
|
||||
}
|
||||
}
|
||||
|
||||
// Server setup
|
||||
public static class Server extends Proxy {
|
||||
public Server() {
|
||||
// Register mod event bus listeners
|
||||
IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();
|
||||
modEventBus.addListener(Server::setup);
|
||||
}
|
||||
|
||||
private static void setup(FMLDedicatedServerSetupEvent event) {}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user