[MAJOR] Implement new NOJANG modding system
This commit is contained in:
@@ -1,17 +1,18 @@
|
||||
package com.hypherionmc.craterlib;
|
||||
|
||||
import com.hypherionmc.craterlib.api.event.client.LateInitEvent;
|
||||
import com.hypherionmc.craterlib.api.events.client.LateInitEvent;
|
||||
import com.hypherionmc.craterlib.common.NeoForgeServerEvents;
|
||||
import com.hypherionmc.craterlib.core.event.CraterEventBus;
|
||||
import com.hypherionmc.craterlib.core.networking.CraterPacketNetwork;
|
||||
import com.hypherionmc.craterlib.core.networking.PacketRegistry;
|
||||
import com.hypherionmc.craterlib.core.networking.data.PacketSide;
|
||||
import com.hypherionmc.craterlib.network.CraterNeoForgeNetworkHandler;
|
||||
import com.hypherionmc.craterlib.nojang.client.BridgedMinecraft;
|
||||
import com.hypherionmc.craterlib.nojang.client.BridgedOptions;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.neoforged.bus.api.IEventBus;
|
||||
import net.neoforged.fml.common.Mod;
|
||||
import net.neoforged.fml.event.lifecycle.FMLCommonSetupEvent;
|
||||
import net.neoforged.fml.javafmlmod.FMLJavaModLoadingContext;
|
||||
import net.neoforged.fml.loading.FMLEnvironment;
|
||||
import net.neoforged.fml.loading.FMLLoader;
|
||||
import net.neoforged.neoforge.common.NeoForge;
|
||||
@@ -23,14 +24,14 @@ public class CraterLib {
|
||||
|
||||
public CraterLib(IEventBus eventBus) {
|
||||
NeoForge.EVENT_BUS.register(new NeoForgeServerEvents());
|
||||
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::commonSetup);
|
||||
eventBus.addListener(this::commonSetup);
|
||||
handler = new CraterNeoForgeNetworkHandler(FMLLoader.getDist().isClient() ? PacketSide.CLIENT : PacketSide.SERVER);
|
||||
}
|
||||
|
||||
public void commonSetup(FMLCommonSetupEvent evt) {
|
||||
new CraterPacketNetwork(handler);
|
||||
if (FMLEnvironment.dist.isClient()) {
|
||||
LateInitEvent event = new LateInitEvent(Minecraft.getInstance(), Minecraft.getInstance().options);
|
||||
LateInitEvent event = new LateInitEvent(new BridgedMinecraft(), BridgedOptions.of(Minecraft.getInstance().options));
|
||||
CraterEventBus.INSTANCE.postEvent(event);
|
||||
}
|
||||
}
|
||||
|
@@ -1,8 +1,9 @@
|
||||
package com.hypherionmc.craterlib.client;
|
||||
|
||||
import com.hypherionmc.craterlib.CraterConstants;
|
||||
import com.hypherionmc.craterlib.api.event.client.CraterClientTickEvent;
|
||||
import com.hypherionmc.craterlib.api.events.client.CraterClientTickEvent;
|
||||
import com.hypherionmc.craterlib.core.event.CraterEventBus;
|
||||
import com.hypherionmc.craterlib.nojang.client.multiplayer.BridgedClientLevel;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.neoforged.api.distmarker.Dist;
|
||||
import net.neoforged.bus.api.SubscribeEvent;
|
||||
@@ -14,7 +15,7 @@ public class NeoForgeClientEvents {
|
||||
|
||||
@SubscribeEvent
|
||||
public static void clientTick(TickEvent.LevelTickEvent event) {
|
||||
CraterClientTickEvent craterClientTickEvent = new CraterClientTickEvent(Minecraft.getInstance().level);
|
||||
CraterClientTickEvent craterClientTickEvent = new CraterClientTickEvent(BridgedClientLevel.of(Minecraft.getInstance().level));
|
||||
CraterEventBus.INSTANCE.postEvent(craterClientTickEvent);
|
||||
}
|
||||
|
||||
|
@@ -1,6 +1,9 @@
|
||||
package com.hypherionmc.craterlib.client;
|
||||
|
||||
import com.hypherionmc.craterlib.core.platform.ClientPlatform;
|
||||
import com.hypherionmc.craterlib.nojang.client.BridgedMinecraft;
|
||||
import com.hypherionmc.craterlib.nojang.client.multiplayer.BridgedClientLevel;
|
||||
import com.hypherionmc.craterlib.nojang.world.entity.player.BridgedPlayer;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.network.Connection;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
@@ -10,7 +13,6 @@ import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @author HypherionSA
|
||||
* @date 16/06/2022
|
||||
*/
|
||||
public class NeoForgeClientHelper implements ClientPlatform {
|
||||
|
||||
@@ -18,18 +20,18 @@ public class NeoForgeClientHelper implements ClientPlatform {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Minecraft getClientInstance() {
|
||||
return Minecraft.getInstance();
|
||||
public BridgedMinecraft getClientInstance() {
|
||||
return new BridgedMinecraft();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Player getClientPlayer() {
|
||||
return Minecraft.getInstance().player;
|
||||
public BridgedPlayer getClientPlayer() {
|
||||
return BridgedPlayer.of(Minecraft.getInstance().player);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Level getClientLevel() {
|
||||
return Minecraft.getInstance().level;
|
||||
public BridgedClientLevel getClientLevel() {
|
||||
return BridgedClientLevel.of(Minecraft.getInstance().level);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -3,6 +3,8 @@ package com.hypherionmc.craterlib.common;
|
||||
import com.hypherionmc.craterlib.core.network.CraterNetworkHandler;
|
||||
import com.hypherionmc.craterlib.core.platform.CommonPlatform;
|
||||
import com.hypherionmc.craterlib.network.NeoForgeNetworkHandler;
|
||||
import com.hypherionmc.craterlib.nojang.client.BridgedMinecraft;
|
||||
import com.hypherionmc.craterlib.nojang.server.BridgedMinecraftServer;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.world.item.CreativeModeTab;
|
||||
@@ -16,19 +18,11 @@ import java.util.Map;
|
||||
*/
|
||||
public class NeoForgeCommonHelper implements CommonPlatform {
|
||||
|
||||
public static Map<ResourceLocation, CreativeModeTab> TABS = new HashMap<>();
|
||||
|
||||
public NeoForgeCommonHelper() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public CraterNetworkHandler createPacketHandler(String modid, boolean requiredClient, boolean requiredServer) {
|
||||
return new NeoForgeNetworkHandler(modid, requiredClient, requiredServer);
|
||||
//return NeoForgeNetworkHandler.of(modid, requiredClient, requiredServer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MinecraftServer getMCServer() {
|
||||
return ServerLifecycleHooks.getCurrentServer();
|
||||
public BridgedMinecraftServer getMCServer() {
|
||||
return BridgedMinecraftServer.of(ServerLifecycleHooks.getCurrentServer());
|
||||
}
|
||||
}
|
||||
|
@@ -1,8 +1,10 @@
|
||||
package com.hypherionmc.craterlib.common;
|
||||
|
||||
import com.hypherionmc.craterlib.api.event.server.CraterRegisterCommandEvent;
|
||||
import com.hypherionmc.craterlib.api.event.server.CraterServerLifecycleEvent;
|
||||
import com.hypherionmc.craterlib.api.events.server.CraterRegisterCommandEvent;
|
||||
import com.hypherionmc.craterlib.api.events.server.CraterServerLifecycleEvent;
|
||||
import com.hypherionmc.craterlib.core.event.CraterEventBus;
|
||||
import com.hypherionmc.craterlib.nojang.commands.CommandsRegistry;
|
||||
import com.hypherionmc.craterlib.nojang.server.BridgedMinecraftServer;
|
||||
import net.neoforged.bus.api.SubscribeEvent;
|
||||
import net.neoforged.neoforge.event.RegisterCommandsEvent;
|
||||
import net.neoforged.neoforge.event.server.ServerStartedEvent;
|
||||
@@ -14,27 +16,28 @@ public class NeoForgeServerEvents {
|
||||
|
||||
@SubscribeEvent
|
||||
public void serverStarting(ServerStartingEvent event) {
|
||||
CraterEventBus.INSTANCE.postEvent(new CraterServerLifecycleEvent.Starting(event.getServer()));
|
||||
CraterEventBus.INSTANCE.postEvent(new CraterServerLifecycleEvent.Starting(BridgedMinecraftServer.of(event.getServer())));
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void serverStarted(ServerStartedEvent event) {
|
||||
CraterEventBus.INSTANCE.postEvent(new CraterServerLifecycleEvent.Started());
|
||||
CraterEventBus.INSTANCE.postEvent(new CraterServerLifecycleEvent.Started(BridgedMinecraftServer.of(event.getServer())));
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void serverStopping(ServerStoppingEvent event) {
|
||||
CraterEventBus.INSTANCE.postEvent(new CraterServerLifecycleEvent.Stopping());
|
||||
CraterEventBus.INSTANCE.postEvent(new CraterServerLifecycleEvent.Stopping(BridgedMinecraftServer.of(event.getServer())));
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void serverStopped(ServerStoppedEvent event) {
|
||||
CraterEventBus.INSTANCE.postEvent(new CraterServerLifecycleEvent.Stopped());
|
||||
CraterEventBus.INSTANCE.postEvent(new CraterServerLifecycleEvent.Stopped(BridgedMinecraftServer.of(event.getServer())));
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void onCommandRegister(RegisterCommandsEvent event) {
|
||||
CraterEventBus.INSTANCE.postEvent(new CraterRegisterCommandEvent(event.getDispatcher()));
|
||||
CraterEventBus.INSTANCE.postEvent(new CraterRegisterCommandEvent());
|
||||
CommandsRegistry.INSTANCE.registerCommands(event.getDispatcher());
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -25,8 +25,6 @@ public class ConfigScreenHandlerMixin {
|
||||
/**
|
||||
* Inject Auto Generated config Screens into forge
|
||||
*
|
||||
* @param selectedMod
|
||||
* @param cir
|
||||
*/
|
||||
@Inject(at = @At("RETURN"), method = "getScreenFactoryFor", cancellable = true, remap = false)
|
||||
private static void injectConfigScreen(IModInfo selectedMod, CallbackInfoReturnable<Optional<BiFunction<Minecraft, Screen, Screen>>> cir) {
|
||||
|
@@ -0,0 +1,36 @@
|
||||
package com.hypherionmc.craterlib.mixin;
|
||||
|
||||
import com.hypherionmc.craterlib.api.events.server.CraterServerChatEvent;
|
||||
import com.hypherionmc.craterlib.core.event.CraterEventBus;
|
||||
import com.hypherionmc.craterlib.nojang.world.entity.player.BridgedPlayer;
|
||||
import com.hypherionmc.craterlib.utils.ChatUtils;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.network.chat.PlayerChatMessage;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.server.network.FilteredText;
|
||||
import net.minecraft.server.network.ServerGamePacketListenerImpl;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.Shadow;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
|
||||
@Mixin(value = ServerGamePacketListenerImpl.class, priority = Integer.MIN_VALUE)
|
||||
public class ServerGamePacketListenerImplMixin {
|
||||
|
||||
@Shadow
|
||||
public ServerPlayer player;
|
||||
|
||||
@Inject(
|
||||
method = "lambda$handleChat$6",
|
||||
at = @At("HEAD"),
|
||||
cancellable = true
|
||||
)
|
||||
private void injectChatEvent(Component component, PlayerChatMessage arg, FilteredText p_296589_, CallbackInfo ci) {
|
||||
CraterServerChatEvent event = new CraterServerChatEvent(BridgedPlayer.of(this.player), arg.decoratedContent().getString(), ChatUtils.mojangToAdventure(arg.decoratedContent()));
|
||||
CraterEventBus.INSTANCE.postEvent(event);
|
||||
if (event.wasCancelled())
|
||||
ci.cancel();
|
||||
}
|
||||
|
||||
}
|
@@ -5,6 +5,8 @@ import com.hypherionmc.craterlib.core.networking.PacketRegistry;
|
||||
import com.hypherionmc.craterlib.core.networking.data.PacketContext;
|
||||
import com.hypherionmc.craterlib.core.networking.data.PacketHolder;
|
||||
import com.hypherionmc.craterlib.core.networking.data.PacketSide;
|
||||
import com.hypherionmc.craterlib.nojang.network.BridgedFriendlyByteBuf;
|
||||
import com.hypherionmc.craterlib.nojang.world.entity.player.BridgedPlayer;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
@@ -17,6 +19,7 @@ import net.neoforged.neoforge.network.registration.IPayloadRegistrar;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
@@ -48,9 +51,9 @@ public class CraterNeoForgeNetworkHandler extends PacketRegistry {
|
||||
if (PACKETS.get(container.messageType()) == null) {
|
||||
var packetContainer = new NeoForgePacketContainer<>(
|
||||
container.messageType(),
|
||||
container.packetId(),
|
||||
container.encoder(),
|
||||
decoder(container.decoder()),
|
||||
container.packetId().toMojang(),
|
||||
mojangEncoder(container.encoder()),
|
||||
decoder(mojangDecoder(container.decoder())),
|
||||
buildHandler(container.handler())
|
||||
);
|
||||
|
||||
@@ -78,11 +81,14 @@ public class CraterNeoForgeNetworkHandler extends PacketRegistry {
|
||||
}
|
||||
}
|
||||
|
||||
public <T> void sendToClient(T packet, ServerPlayer player) {
|
||||
public <T> void sendToClient(T packet, BridgedPlayer player) {
|
||||
NeoForgePacketContainer<T> container = PACKETS.get(packet.getClass());
|
||||
try {
|
||||
if (player.connection.isConnected(container.packetId())) {
|
||||
PacketDistributor.PLAYER.with(player).send(new NeoForgePacket<>(container, packet));
|
||||
if (player.getConnection() == null)
|
||||
return;
|
||||
|
||||
if (player.getConnection().isConnected(container.packetId())) {
|
||||
PacketDistributor.PLAYER.with(player.toMojangServerPlayer()).send(new NeoForgePacket<>(container, packet));
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
CraterConstants.LOG.error("{} packet not registered on the server, this is needed.", packet.getClass(), t);
|
||||
@@ -94,10 +100,18 @@ public class CraterNeoForgeNetworkHandler extends PacketRegistry {
|
||||
try {
|
||||
PacketSide side = ctx.flow().getReceptionSide().equals(LogicalSide.SERVER) ? PacketSide.SERVER : PacketSide.CLIENT;
|
||||
Player player = ctx.player().orElse(null);
|
||||
handler.accept(new PacketContext<>(player, payload.packet(), side));
|
||||
handler.accept(new PacketContext<>(BridgedPlayer.of(player), payload.packet(), side));
|
||||
} catch (Throwable t) {
|
||||
CraterConstants.LOG.error("Error handling packet: {} -> ", payload.packet().getClass(), t);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private <T> Function<FriendlyByteBuf, T> mojangDecoder(Function<BridgedFriendlyByteBuf, T> handler) {
|
||||
return byteBuf -> handler.apply(BridgedFriendlyByteBuf.of(byteBuf));
|
||||
}
|
||||
|
||||
private <T> BiConsumer<T, FriendlyByteBuf> mojangEncoder(BiConsumer<T, BridgedFriendlyByteBuf> handler) {
|
||||
return ((t, byteBuf) -> handler.accept(t, BridgedFriendlyByteBuf.of(byteBuf)));
|
||||
}
|
||||
}
|
@@ -3,6 +3,7 @@ package com.hypherionmc.craterlib.network;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* Based on https://github.com/mysticdrew/common-networking/tree/1.20.4
|
||||
@@ -10,13 +11,13 @@ import net.minecraft.resources.ResourceLocation;
|
||||
public record NeoForgePacket<T>(NeoForgePacketContainer<T> container, T packet) implements CustomPacketPayload {
|
||||
|
||||
@Override
|
||||
public void write(FriendlyByteBuf buff)
|
||||
public void write(@NotNull FriendlyByteBuf buff)
|
||||
{
|
||||
container().encoder().accept(packet(), buff);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResourceLocation id()
|
||||
public @NotNull ResourceLocation id()
|
||||
{
|
||||
return container().packetId();
|
||||
}
|
||||
|
@@ -9,6 +9,7 @@
|
||||
"ConfigScreenHandlerMixin"
|
||||
],
|
||||
"server": [
|
||||
"ServerGamePacketListenerImplMixin"
|
||||
],
|
||||
"injectors": {
|
||||
"defaultRequire": 1
|
||||
|
Reference in New Issue
Block a user