From d33cef15dc65b54ab86f73491cc15a3ab0580069 Mon Sep 17 00:00:00 2001 From: MikhailTapio Date: Thu, 18 Jan 2024 19:22:14 +0800 Subject: [PATCH] Rollback to NeoForge 20.4.2 --- build.gradle | 5 +- gradle.properties | 4 +- .../java/team/dovecotmc/glasses/Glasses.java | 2 + .../client/keybinding/KeyBindingRef.java | 3 +- .../network/handler/NetworkHandler.java | 46 +++++++++---------- .../common/network/msg/GlassesUseMessage.java | 33 ++++++++----- 6 files changed, 51 insertions(+), 42 deletions(-) diff --git a/build.gradle b/build.gradle index 1716e9d..0537d39 100644 --- a/build.gradle +++ b/build.gradle @@ -17,7 +17,7 @@ repositories { } base { - archivesName = mod_id + archivesName = "${mod_name}-NeoForge-${neo_version}" } // Mojang ships Java 17 to end users in 1.18+, so your mod should target Java 17. @@ -78,8 +78,7 @@ sourceSets.main.resources { srcDir 'src/generated/resources' } dependencies { implementation "net.neoforged:neoforge:${neo_version}" - compileOnly "top.theillusivec4.curios:curios-neoforge:${project.curios_version}" - // Curios is not updated to nf 20.4.80 + runtimeOnly "top.theillusivec4.curios:curios-neoforge:${project.curios_version}" compileOnly "top.theillusivec4.curios:curios-neoforge:${project.curios_version}:api" } diff --git a/gradle.properties b/gradle.properties index b347b0a..b01992f 100644 --- a/gradle.properties +++ b/gradle.properties @@ -11,9 +11,9 @@ neogradle.subsystems.parchment.mappingsVersion=2023.12.31 # The Minecraft version must agree with the Neo version to get a valid artifact minecraft_version=1.20.4 # The Neo version must agree with the Minecraft version to get a valid artifact -neo_version=20.4.80-beta +neo_version=20.4.2-beta # The loader version range can only use the major version of FML as bounds -loader_version_range=[2,) +loader_version_range=[1,2) ## Mod Properties # The unique mod identifier for the mod. Must be lowercase in English locale. Must fit the regex [a-z][a-z0-9_]{1,63} # Must match the String constant located in the main mod class annotated with @Mod. diff --git a/src/main/java/team/dovecotmc/glasses/Glasses.java b/src/main/java/team/dovecotmc/glasses/Glasses.java index 2e05798..b050297 100644 --- a/src/main/java/team/dovecotmc/glasses/Glasses.java +++ b/src/main/java/team/dovecotmc/glasses/Glasses.java @@ -6,6 +6,7 @@ import net.neoforged.fml.common.Mod; import org.slf4j.Logger; import team.dovecotmc.glasses.common.init.RegistryHandler; +import team.dovecotmc.glasses.common.network.handler.NetworkHandler; @Mod(Glasses.MODID) public class Glasses { @@ -16,6 +17,7 @@ public class Glasses { public Glasses(IEventBus bus) { curiosLoaded = ModList.get().isLoaded("curios"); RegistryHandler.init(bus); + NetworkHandler.registerMessage(); } public static boolean isCuriosLoaded() { diff --git a/src/main/java/team/dovecotmc/glasses/client/keybinding/KeyBindingRef.java b/src/main/java/team/dovecotmc/glasses/client/keybinding/KeyBindingRef.java index 5db08fa..5ecccdc 100644 --- a/src/main/java/team/dovecotmc/glasses/client/keybinding/KeyBindingRef.java +++ b/src/main/java/team/dovecotmc/glasses/client/keybinding/KeyBindingRef.java @@ -8,6 +8,7 @@ import net.neoforged.neoforge.client.settings.KeyModifier; import team.dovecotmc.glasses.Glasses; import team.dovecotmc.glasses.client.integration.curios.GlassesRenderer; +import team.dovecotmc.glasses.common.network.handler.NetworkHandler; import team.dovecotmc.glasses.common.network.msg.GlassesUseMessage; import team.dovecotmc.glasses.util.client.ClientUtilities; import team.dovecotmc.glasses.util.common.CommonUtilities; @@ -26,7 +27,7 @@ public enum KeyBindingRef implements Supplier { final LocalPlayer player = mc.player; if (player == null) return; CommonUtilities.getMatchedWearingItem(player, CommonUtilities.GLASSES) - .ifPresent($ -> player.connection.send(new GlassesUseMessage())); + .ifPresent($ -> NetworkHandler.INSTANCE.sendToServer(new GlassesUseMessage())); }, false), GLASSES_OFFSET_X_PLUS(new KeyMapping("key.glasses.offset.x_plus", KeyConflictContext.IN_GAME, diff --git a/src/main/java/team/dovecotmc/glasses/common/network/handler/NetworkHandler.java b/src/main/java/team/dovecotmc/glasses/common/network/handler/NetworkHandler.java index e54cf17..f0d8b36 100644 --- a/src/main/java/team/dovecotmc/glasses/common/network/handler/NetworkHandler.java +++ b/src/main/java/team/dovecotmc/glasses/common/network/handler/NetworkHandler.java @@ -1,33 +1,31 @@ package team.dovecotmc.glasses.common.network.handler; -import net.minecraft.server.level.ServerPlayer; -import net.neoforged.bus.api.SubscribeEvent; -import net.neoforged.fml.common.Mod; -import net.neoforged.neoforge.network.event.RegisterPayloadHandlerEvent; -import net.neoforged.neoforge.network.registration.IPayloadRegistrar; +import net.minecraft.resources.ResourceLocation; +import net.neoforged.neoforge.network.NetworkRegistry; +import net.neoforged.neoforge.network.simple.SimpleChannel; import team.dovecotmc.glasses.Glasses; -import team.dovecotmc.glasses.common.item.base.GlassesItem; import team.dovecotmc.glasses.common.network.msg.GlassesUseMessage; -import team.dovecotmc.glasses.util.common.CommonUtilities; -@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD) public class NetworkHandler { - @SubscribeEvent - public static void registerPayload(RegisterPayloadHandlerEvent event) { - final IPayloadRegistrar registrar = event.registrar(Glasses.MODID); - registrar.play(GlassesUseMessage.ID, GlassesUseMessage::new, h -> h - .server((d, ctx) -> ctx.workHandler().submitAsync(() -> ctx.player().ifPresent(p -> { - if (!(p instanceof ServerPlayer player)) return; - CommonUtilities.getMatchedWearingItem(player, CommonUtilities.GLASSES).ifPresent(m -> { - if (m.getItem() instanceof GlassesItem g - && (g.getProperties().packetAction() != null - || g.getProperties().canUse())) - g.onReceivePacket(player, m); - }); - })).exceptionally(e -> { - Glasses.LOGGER.error("Error occurred while handling 'use' message for glasses", e); - return null; - })) + public static SimpleChannel INSTANCE; + public static final String VERSION = "1"; + private static int id = 0; + + public static int nextId() { + return id++; + } + + public static void registerMessage() { + INSTANCE = NetworkRegistry.newSimpleChannel( + new ResourceLocation(Glasses.MODID, "use"), + () -> VERSION, + VERSION::equals, + VERSION::equals ); + INSTANCE.messageBuilder(GlassesUseMessage.class, nextId()) + .encoder(GlassesUseMessage::toBytes) + .decoder(GlassesUseMessage::new) + .consumerMainThread(GlassesUseMessage::handler) + .add(); } } diff --git a/src/main/java/team/dovecotmc/glasses/common/network/msg/GlassesUseMessage.java b/src/main/java/team/dovecotmc/glasses/common/network/msg/GlassesUseMessage.java index 44938cb..a7c1972 100644 --- a/src/main/java/team/dovecotmc/glasses/common/network/msg/GlassesUseMessage.java +++ b/src/main/java/team/dovecotmc/glasses/common/network/msg/GlassesUseMessage.java @@ -1,25 +1,34 @@ package team.dovecotmc.glasses.common.network.msg; import net.minecraft.network.FriendlyByteBuf; -import net.minecraft.network.protocol.common.custom.CustomPacketPayload; -import net.minecraft.resources.ResourceLocation; -import team.dovecotmc.glasses.Glasses; - -public class GlassesUseMessage implements CustomPacketPayload { - public static final ResourceLocation ID = new ResourceLocation(Glasses.MODID, "use"); +import net.minecraft.server.level.ServerPlayer; +import net.neoforged.neoforge.network.NetworkEvent; +import team.dovecotmc.glasses.common.item.base.GlassesItem; +import team.dovecotmc.glasses.util.common.CommonUtilities; +public class GlassesUseMessage { public GlassesUseMessage(FriendlyByteBuf buf) { } public GlassesUseMessage() { } - @Override - public void write(FriendlyByteBuf pBuffer) { + public void toBytes(FriendlyByteBuf buf) { + } - @Override - public ResourceLocation id() { - return ID; + public void handler(NetworkEvent.Context ctx) { + ctx.enqueueWork(() -> { + final ServerPlayer player = ctx.getSender(); + if (player == null) return; + CommonUtilities.getMatchedWearingItem(player, CommonUtilities.GLASSES).ifPresent(m -> { + if (m.getItem() instanceof GlassesItem g + && (g.getProperties().packetAction() != null + || g.getProperties().canUse())) + g.onReceivePacket(player, m); + }); + }); + ctx.setPacketHandled(true); } -} + +} \ No newline at end of file