Cleaned up TotemOfReviving by moving most setup code to the new SideProxy.
This commit is contained in:
13
src/main/java/dev/micle/totemofreviving/ISideProxy.java
Normal file
13
src/main/java/dev/micle/totemofreviving/ISideProxy.java
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package dev.micle.totemofreviving;
|
||||||
|
|
||||||
|
import net.minecraft.client.world.ClientWorld;
|
||||||
|
import net.minecraft.entity.player.PlayerEntity;
|
||||||
|
import net.minecraft.server.MinecraftServer;
|
||||||
|
|
||||||
|
public interface ISideProxy {
|
||||||
|
MinecraftServer getServer();
|
||||||
|
|
||||||
|
PlayerEntity getClientPlayer();
|
||||||
|
|
||||||
|
ClientWorld getClientWorld();
|
||||||
|
}
|
100
src/main/java/dev/micle/totemofreviving/SideProxy.java
Normal file
100
src/main/java/dev/micle/totemofreviving/SideProxy.java
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
package dev.micle.totemofreviving;
|
||||||
|
|
||||||
|
import dev.micle.totemofreviving.config.Config;
|
||||||
|
import dev.micle.totemofreviving.data.DataGenerators;
|
||||||
|
import dev.micle.totemofreviving.network.Network;
|
||||||
|
import dev.micle.totemofreviving.setup.Registration;
|
||||||
|
import net.minecraft.client.Minecraft;
|
||||||
|
import net.minecraft.client.world.ClientWorld;
|
||||||
|
import net.minecraft.entity.player.PlayerEntity;
|
||||||
|
import net.minecraft.server.MinecraftServer;
|
||||||
|
import net.minecraftforge.common.MinecraftForge;
|
||||||
|
import net.minecraftforge.event.AddReloadListenerEvent;
|
||||||
|
import net.minecraftforge.eventbus.api.IEventBus;
|
||||||
|
import net.minecraftforge.fml.event.lifecycle.*;
|
||||||
|
import net.minecraftforge.fml.event.server.FMLServerStartedEvent;
|
||||||
|
import net.minecraftforge.fml.event.server.FMLServerStoppingEvent;
|
||||||
|
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
|
||||||
|
|
||||||
|
class SideProxy implements ISideProxy {
|
||||||
|
private static MinecraftServer server = null;
|
||||||
|
|
||||||
|
// Common setup
|
||||||
|
SideProxy() {
|
||||||
|
Registration.register();
|
||||||
|
Config.init();
|
||||||
|
Network.init();
|
||||||
|
|
||||||
|
IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();
|
||||||
|
modEventBus.addListener(DataGenerators::gatherData);
|
||||||
|
modEventBus.addListener(SideProxy::setup);
|
||||||
|
modEventBus.addListener(SideProxy::imcEnqueue);
|
||||||
|
modEventBus.addListener(SideProxy::imcProcess);
|
||||||
|
|
||||||
|
MinecraftForge.EVENT_BUS.addListener(SideProxy::onAddReloadListeners);
|
||||||
|
MinecraftForge.EVENT_BUS.addListener(SideProxy::serverStarted);
|
||||||
|
MinecraftForge.EVENT_BUS.addListener(SideProxy::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(FMLServerStartedEvent event) {
|
||||||
|
server = event.getServer();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void serverStopping(FMLServerStoppingEvent event) {
|
||||||
|
server = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public MinecraftServer getServer() {
|
||||||
|
return server;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PlayerEntity getClientPlayer() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ClientWorld getClientWorld() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Client setup
|
||||||
|
static class Client extends SideProxy {
|
||||||
|
Client() {
|
||||||
|
FMLJavaModLoadingContext.get().getModEventBus().addListener(Client::setup);
|
||||||
|
FMLJavaModLoadingContext.get().getModEventBus().addListener(Client::postSetup);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void setup(FMLClientSetupEvent event) {}
|
||||||
|
|
||||||
|
private static void postSetup(FMLLoadCompleteEvent event) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PlayerEntity getClientPlayer() {
|
||||||
|
return Minecraft.getInstance().player;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ClientWorld getClientWorld() {
|
||||||
|
return Minecraft.getInstance().level;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Server setup
|
||||||
|
static class Server extends SideProxy {
|
||||||
|
Server() {
|
||||||
|
FMLJavaModLoadingContext.get().getModEventBus().addListener(Server::setup);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void setup(FMLDedicatedServerSetupEvent event) {}
|
||||||
|
}
|
||||||
|
}
|
@ -1,53 +1,39 @@
|
|||||||
package dev.micle.totemofreviving;
|
package dev.micle.totemofreviving;
|
||||||
|
|
||||||
import dev.micle.totemofreviving.network.C2SRequestPlayerRevive;
|
|
||||||
import dev.micle.totemofreviving.network.C2SRequestTotemCharge;
|
|
||||||
import dev.micle.totemofreviving.network.C2SRequestTotemTarget;
|
|
||||||
import dev.micle.totemofreviving.setup.Registration;
|
|
||||||
import net.minecraft.server.management.PlayerList;
|
|
||||||
import net.minecraft.util.ResourceLocation;
|
import net.minecraft.util.ResourceLocation;
|
||||||
import net.minecraftforge.common.MinecraftForge;
|
import net.minecraftforge.fml.DistExecutor;
|
||||||
import net.minecraftforge.fml.common.Mod;
|
import net.minecraftforge.fml.common.Mod;
|
||||||
import net.minecraftforge.fml.network.NetworkRegistry;
|
|
||||||
import net.minecraftforge.fml.network.simple.SimpleChannel;
|
import org.apache.logging.log4j.LogManager;
|
||||||
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
@Mod(TotemOfReviving.MOD_ID)
|
@Mod(TotemOfReviving.MOD_ID)
|
||||||
public class TotemOfReviving {
|
public final class TotemOfReviving {
|
||||||
public static final String MOD_ID = "totemofreviving";
|
public static final String MOD_ID = "totemofreviving";
|
||||||
public static PlayerList players;
|
public static final String MOD_NAME = "Micle's Totem of Reviving";
|
||||||
|
|
||||||
private static final String PROTOCOL_VERSION = "1";
|
public static final String RESOURCE_PREFIX = MOD_ID + ':';
|
||||||
public static final SimpleChannel INSTANCE = NetworkRegistry.newSimpleChannel(
|
|
||||||
new ResourceLocation(TotemOfReviving.MOD_ID, "main"),
|
public static final Random RANDOM = new Random();
|
||||||
() -> PROTOCOL_VERSION,
|
public static final Logger LOGGER = LogManager.getLogger(MOD_NAME);
|
||||||
PROTOCOL_VERSION::equals,
|
|
||||||
PROTOCOL_VERSION::equals
|
public static TotemOfReviving INSTANCE;
|
||||||
);
|
public static SideProxy SIDE_PROXY;
|
||||||
|
|
||||||
public TotemOfReviving() {
|
public TotemOfReviving() {
|
||||||
Registration.register();
|
INSTANCE = this;
|
||||||
|
SIDE_PROXY = DistExecutor.safeRunForDist(
|
||||||
int id = 0;
|
() -> SideProxy.Client::new,
|
||||||
INSTANCE.registerMessage(id++,
|
() -> SideProxy.Server::new
|
||||||
C2SRequestPlayerRevive.class,
|
);
|
||||||
C2SRequestPlayerRevive::encode,
|
|
||||||
C2SRequestPlayerRevive::decode,
|
|
||||||
C2SRequestPlayerRevive::handle
|
|
||||||
);
|
|
||||||
INSTANCE.registerMessage(id++,
|
|
||||||
C2SRequestTotemTarget.class,
|
|
||||||
C2SRequestTotemTarget::encode,
|
|
||||||
C2SRequestTotemTarget::decode,
|
|
||||||
C2SRequestTotemTarget::handle
|
|
||||||
);
|
|
||||||
INSTANCE.registerMessage(id++,
|
|
||||||
C2SRequestTotemCharge.class,
|
|
||||||
C2SRequestTotemCharge::encode,
|
|
||||||
C2SRequestTotemCharge::decode,
|
|
||||||
C2SRequestTotemCharge::handle
|
|
||||||
);
|
|
||||||
|
|
||||||
// Register ourselves for server and other game events we are interested in
|
|
||||||
MinecraftForge.EVENT_BUS.register(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static ResourceLocation createResourceLocation(String name) {
|
||||||
|
if (name.contains(":")) {
|
||||||
|
throw new IllegalArgumentException("name containes namespace");
|
||||||
|
}
|
||||||
|
return new ResourceLocation(MOD_ID, name);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user