[MAJOR] Implement new NOJANG modding system
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
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.ForgeServerEvents;
|
||||
import com.hypherionmc.craterlib.core.event.CraterEventBus;
|
||||
import com.hypherionmc.craterlib.core.networking.CraterPacketNetwork;
|
||||
import com.hypherionmc.craterlib.core.networking.data.PacketSide;
|
||||
import com.hypherionmc.craterlib.network.CraterForgeNetworkHandler;
|
||||
import com.hypherionmc.craterlib.nojang.client.BridgedMinecraft;
|
||||
import com.hypherionmc.craterlib.nojang.client.BridgedOptions;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
@@ -26,7 +28,7 @@ public class CraterLib {
|
||||
public void commonSetup(FMLCommonSetupEvent evt) {
|
||||
new CraterPacketNetwork(new CraterForgeNetworkHandler(FMLLoader.getDist().isClient() ? PacketSide.CLIENT : PacketSide.SERVER));
|
||||
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> {
|
||||
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.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.event.TickEvent;
|
||||
@@ -14,7 +15,7 @@ public class ForgeClientEvents {
|
||||
|
||||
@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;
|
||||
@@ -18,18 +21,18 @@ public class ForgeClientHelper 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
|
||||
|
@@ -1,10 +1,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.ForgeNetworkHandler;
|
||||
import com.hypherionmc.craterlib.nojang.server.BridgedMinecraftServer;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.world.item.CreativeModeTab;
|
||||
import net.minecraftforge.server.ServerLifecycleHooks;
|
||||
|
||||
@@ -22,12 +20,7 @@ public class ForgeCommonHelper implements CommonPlatform {
|
||||
}
|
||||
|
||||
@Override
|
||||
public CraterNetworkHandler createPacketHandler(String modid, boolean requiredClient, boolean requiredServer) {
|
||||
return ForgeNetworkHandler.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.minecraftforge.event.RegisterCommandsEvent;
|
||||
import net.minecraftforge.event.server.ServerStartedEvent;
|
||||
import net.minecraftforge.event.server.ServerStartingEvent;
|
||||
@@ -14,27 +16,28 @@ public class ForgeServerEvents {
|
||||
|
||||
@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,9 +5,13 @@ 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.client.Minecraft;
|
||||
import net.minecraft.network.Connection;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.server.network.ServerGamePacketListenerImpl;
|
||||
import net.minecraftforge.event.network.CustomPayloadEvent;
|
||||
import net.minecraftforge.network.ChannelBuilder;
|
||||
import net.minecraftforge.network.PacketDistributor;
|
||||
@@ -17,6 +21,7 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* Based on https://github.com/mysticdrew/common-networking/tree/1.20.4
|
||||
@@ -31,15 +36,15 @@ public class CraterForgeNetworkHandler extends PacketRegistry {
|
||||
protected <T> void registerPacket(PacketHolder<T> holder) {
|
||||
if (CHANNELS.get(holder.messageType()) == null) {
|
||||
SimpleChannel channel = ChannelBuilder
|
||||
.named(holder.packetId())
|
||||
.named(holder.packetId().toMojang())
|
||||
.clientAcceptedVersions((a, b) -> true)
|
||||
.serverAcceptedVersions((a, b) -> true)
|
||||
.networkProtocolVersion(1)
|
||||
.simpleChannel();
|
||||
|
||||
channel.messageBuilder(holder.messageType())
|
||||
.decoder(holder.decoder())
|
||||
.encoder(holder.encoder())
|
||||
.decoder(mojangDecoder(holder.decoder()))
|
||||
.encoder(mojangEncoder(holder.encoder()))
|
||||
.consumerNetworkThread(buildHandler(holder.handler()))
|
||||
.add();
|
||||
|
||||
@@ -61,21 +66,31 @@ public class CraterForgeNetworkHandler extends PacketRegistry {
|
||||
}
|
||||
}
|
||||
|
||||
public <T> void sendToClient(T packet, ServerPlayer player) {
|
||||
public <T> void sendToClient(T packet, BridgedPlayer player) {
|
||||
SimpleChannel channel = CHANNELS.get(packet.getClass());
|
||||
Connection connection = player.connection.getConnection();
|
||||
if (channel.isRemotePresent(connection)) {
|
||||
channel.send(packet, PacketDistributor.PLAYER.with(player));
|
||||
ServerGamePacketListenerImpl connection = player.getConnection();
|
||||
if (connection == null)
|
||||
return;
|
||||
|
||||
if (channel.isRemotePresent(connection.getConnection())) {
|
||||
channel.send(packet, PacketDistributor.PLAYER.with(player.toMojangServerPlayer()));
|
||||
}
|
||||
}
|
||||
|
||||
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)));
|
||||
}
|
||||
|
||||
private <T> BiConsumer<T, CustomPayloadEvent.Context> buildHandler(Consumer<PacketContext<T>> handler) {
|
||||
return (message, ctx) -> {
|
||||
ctx.enqueueWork(() -> {
|
||||
PacketSide side = ctx.getDirection().getReceptionSide().isServer() ? PacketSide.SERVER : PacketSide.CLIENT;
|
||||
ServerPlayer player = ctx.getSender();
|
||||
handler.accept(new PacketContext<>(player, message, side));
|
||||
handler.accept(new PacketContext<>(BridgedPlayer.of(player), message, side));
|
||||
});
|
||||
ctx.setPacketHandled(true);
|
||||
};
|
||||
|
@@ -93,7 +93,7 @@ public class ForgeNetworkHandler implements CraterNetworkHandler {
|
||||
sup.enqueueWork(() -> {
|
||||
Player player;
|
||||
if (packetDirection == PacketDirection.TO_CLIENT) {
|
||||
player = ClientPlatform.INSTANCE.getClientPlayer();
|
||||
player = ClientPlatform.INSTANCE.getClientPlayer().toMojangServerPlayer();
|
||||
} else {
|
||||
player = sup.getSender();
|
||||
}
|
||||
|
@@ -9,6 +9,7 @@
|
||||
"ConfigScreenHandlerMixin"
|
||||
],
|
||||
"server": [
|
||||
"ServerGamePacketListenerImplMixin"
|
||||
],
|
||||
"injectors": {
|
||||
"defaultRequire": 1
|
||||
|
Reference in New Issue
Block a user