diff --git a/build.gradle b/build.gradle index ffc7a0f..8bd9e19 100644 --- a/build.gradle +++ b/build.gradle @@ -3,7 +3,7 @@ plugins { id 'eclipse' id 'idea' id 'maven-publish' - id 'net.neoforged.gradle.userdev' version '7.0.57' + id 'net.neoforged.gradle.userdev' version '7.0.97' } version = mod_version @@ -72,6 +72,13 @@ runs { // Include resources generated by data generators. sourceSets.main.resources { srcDir 'src/generated/resources' } +// Sets up a dependency configuration called 'localRuntime'. +// This configuration should be used instead of 'runtimeOnly' to declare +// a dependency that will be present for runtime testing but that is +// "optional", meaning it will not be pulled by dependents of this mod. +configurations { + runtimeClasspath.extendsFrom localRuntime +} dependencies { // Specify the version of Minecraft to use. @@ -82,11 +89,12 @@ dependencies { // For all intends and purposes: You can treat this dependency as if it is a normal library you would use. implementation "net.neoforged:neoforge:${neo_version}" - // Example mod dependency with JEI + // Example optional mod dependency with JEI // The JEI API is declared for compile time use, while the full JEI artifact is used at runtime // compileOnly "mezz.jei:jei-${mc_version}-common-api:${jei_version}" - // compileOnly "mezz.jei:jei-${mc_version}-forge-api:${jei_version}" - // runtimeOnly "mezz.jei:jei-${mc_version}-forge:${jei_version}" + // compileOnly "mezz.jei:jei-${mc_version}-neoforge-api:${jei_version}" + // We add the full version to localRuntime, not runtimeOnly, so that we do not publish a dependency on it + // localRuntime "mezz.jei:jei-${mc_version}-neoforge:${jei_version}" // Example mod dependency using a mod jar from ./libs with a flat dir repository // This maps to ./libs/coolmod-${mc_version}-${coolmod_version}.jar @@ -110,16 +118,22 @@ dependencies { // See https://docs.gradle.org/current/dsl/org.gradle.language.jvm.tasks.ProcessResources.html tasks.withType(ProcessResources).configureEach { var replaceProperties = [ - minecraft_version : minecraft_version, minecraft_version_range: minecraft_version_range, - neo_version : neo_version, neo_version_range: neo_version_range, - loader_version_range: loader_version_range, - mod_id : mod_id, mod_name: mod_name, mod_license: mod_license, mod_version: mod_version, - mod_authors : mod_authors, mod_description: mod_description, pack_format_number: pack_format_number, + minecraft_version : minecraft_version, + minecraft_version_range: minecraft_version_range, + neo_version : neo_version, + neo_version_range : neo_version_range, + loader_version_range : loader_version_range, + mod_id : mod_id, + mod_name : mod_name, + mod_license : mod_license, + mod_version : mod_version, + mod_authors : mod_authors, + mod_description : mod_description ] inputs.properties replaceProperties - filesMatching(['META-INF/mods.toml', 'pack.mcmeta']) { - expand replaceProperties + [project: project] + filesMatching(['META-INF/mods.toml']) { + expand replaceProperties } } @@ -140,3 +154,11 @@ publishing { tasks.withType(JavaCompile).configureEach { options.encoding = 'UTF-8' // Use the UTF-8 charset for Java compilation } + +// IDEA no longer automatically downloads sources/javadoc jars for dependencies, so we need to explicitly enable the behavior. +idea { + module { + downloadSources = true + downloadJavadoc = true + } +} diff --git a/gradle.properties b/gradle.properties index dc4cbda..08f6140 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,20 +3,24 @@ org.gradle.daemon=false org.gradle.debug=false -## Environment Properties - +#read more on this at https://github.com/neoforged/NeoGradle/blob/NG_7.0/README.md#apply-parchment-mappings +# you can also find the latest versions at: https://parchmentmc.org/docs/getting-started +neogradle.subsystems.parchment.minecraftVersion=1.20.3 +neogradle.subsystems.parchment.mappingsVersion=2023.12.31 +# Environment Properties +# You can find the latest versions here: https://projects.neoforged.net/neoforged/neoforge # The Minecraft version must agree with the Neo version to get a valid artifact -minecraft_version=1.20.2 +minecraft_version=1.20.4 # The Minecraft version range can use any release version of Minecraft as bounds. # Snapshots, pre-releases, and release candidates are not guaranteed to sort properly # as they do not follow standard versioning conventions. -minecraft_version_range=[1.20.2,1.21) +minecraft_version_range=[1.20.4,1.21) # The Neo version must agree with the Minecraft version to get a valid artifact -neo_version=20.2.86 -# The Neo version range can use any version of Neo as bounds or match the loader version range -neo_version_range=[20.2,) -# The loader version range can only use the major version of Neo/FML as bounds -loader_version_range=[1,) +neo_version=20.4.147-beta +# The Neo version range can use any version of Neo as bounds +neo_version_range=[20.4,) +# The loader version range can only use the major version of FML as bounds +loader_version_range=[2,) ## Mod Properties @@ -28,7 +32,7 @@ mod_name=Nature's Compass # The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default. mod_license=CC BY-NC-SA 4.0 # The mod version. See https://semver.org/ -mod_version=1.20.2-3.0.0-neoforge +mod_version=1.20.4-3.0.0-neoforge # The group ID for the mod. It is only important when publishing as an artifact to a Maven repository. # This should match the base package used for the mod sources. # See https://maven.apache.org/guides/mini/guide-naming-conventions.html @@ -38,4 +42,4 @@ mod_authors=ChaosTheDude # The description of the mod. This is a simple multiline text string that is used for display purposes in the mod list. mod_description=Search for a biome and get information about it. # Pack version - this changes each minecraft release, in general. -pack_format_number=18 +pack_format_number=18 \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index 943f0cb..d64cd49 100644 Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 37aef8d..a80b22c 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,7 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip networkTimeout=10000 +validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew index 65dcd68..1aa94a4 100755 --- a/gradlew +++ b/gradlew @@ -83,10 +83,8 @@ done # This is normally unused # shellcheck disable=SC2034 APP_BASE_NAME=${0##*/} -APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit - -# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit # Use the maximum available, or set MAX_FD != -1 to use that value. MAX_FD=maximum @@ -133,10 +131,13 @@ location of your Java installation." fi else JAVACMD=java - which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. Please set the JAVA_HOME variable in your environment to match the location of your Java installation." + fi fi # Increase the maximum file descriptors if we can. @@ -144,7 +145,7 @@ if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then case $MAX_FD in #( max*) # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. - # shellcheck disable=SC3045 + # shellcheck disable=SC2039,SC3045 MAX_FD=$( ulimit -H -n ) || warn "Could not query maximum file descriptor limit" esac @@ -152,7 +153,7 @@ if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then '' | soft) :;; #( *) # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. - # shellcheck disable=SC3045 + # shellcheck disable=SC2039,SC3045 ulimit -n "$MAX_FD" || warn "Could not set maximum file descriptor limit to $MAX_FD" esac @@ -197,11 +198,15 @@ if "$cygwin" || "$msys" ; then done fi -# Collect all arguments for the java command; -# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of -# shell script including quotes and variable substitutions, so put them in -# double quotes to make sure that they get re-expanded; and -# * put everything else in single quotes, so that it's not re-expanded. + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. set -- \ "-Dorg.gradle.appname=$APP_BASE_NAME" \ diff --git a/gradlew.bat b/gradlew.bat index 93e3f59..25da30d 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -43,11 +43,11 @@ set JAVA_EXE=java.exe %JAVA_EXE% -version >NUL 2>&1 if %ERRORLEVEL% equ 0 goto execute -echo. -echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 goto fail @@ -57,11 +57,11 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe if exist "%JAVA_EXE%" goto execute -echo. -echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 goto fail diff --git a/settings.gradle b/settings.gradle index b359a59..ada876e 100644 --- a/settings.gradle +++ b/settings.gradle @@ -7,5 +7,5 @@ pluginManagement { } plugins { - id 'org.gradle.toolchains.foojay-resolver-convention' version '0.5.0' + id 'org.gradle.toolchains.foojay-resolver-convention' version '0.8.0' } diff --git a/src/main/java/com/chaosthedude/naturescompass/NaturesCompass.java b/src/main/java/com/chaosthedude/naturescompass/NaturesCompass.java index c48a04c..cd094b0 100644 --- a/src/main/java/com/chaosthedude/naturescompass/NaturesCompass.java +++ b/src/main/java/com/chaosthedude/naturescompass/NaturesCompass.java @@ -8,7 +8,7 @@ import com.chaosthedude.naturescompass.config.ConfigHandler; import com.chaosthedude.naturescompass.items.NaturesCompassItem; -import com.chaosthedude.naturescompass.network.CompassSearchPacket; +import com.chaosthedude.naturescompass.network.SearchPacket; import com.chaosthedude.naturescompass.network.SyncPacket; import com.chaosthedude.naturescompass.network.TeleportPacket; import com.google.common.collect.ArrayListMultimap; @@ -17,16 +17,17 @@ import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.CreativeModeTabs; import net.minecraft.world.item.ItemStack; +import net.neoforged.api.distmarker.Dist; +import net.neoforged.bus.api.IEventBus; import net.neoforged.bus.api.SubscribeEvent; import net.neoforged.fml.ModLoadingContext; import net.neoforged.fml.common.Mod; import net.neoforged.fml.config.ModConfig; import net.neoforged.fml.event.lifecycle.FMLCommonSetupEvent; -import net.neoforged.fml.javafmlmod.FMLJavaModLoadingContext; import net.neoforged.neoforge.common.NeoForge; import net.neoforged.neoforge.event.BuildCreativeModeTabContentsEvent; -import net.neoforged.neoforge.network.NetworkRegistry.ChannelBuilder; -import net.neoforged.neoforge.network.simple.SimpleChannel; +import net.neoforged.neoforge.network.event.RegisterPayloadHandlerEvent; +import net.neoforged.neoforge.network.registration.IPayloadRegistrar; import net.neoforged.neoforge.server.permission.events.PermissionGatherEvent; import net.neoforged.neoforge.server.permission.nodes.PermissionNode; import net.neoforged.neoforge.server.permission.nodes.PermissionTypes; @@ -40,7 +41,6 @@ public class NaturesCompass { public static final Logger LOGGER = LogManager.getLogger(MODID); - public static SimpleChannel network; public static NaturesCompassItem naturesCompass; public static boolean canTeleport; @@ -49,11 +49,12 @@ public class NaturesCompass { public static NaturesCompass instance; - public NaturesCompass() { + public NaturesCompass(IEventBus bus, Dist dist) { instance = this; - FMLJavaModLoadingContext.get().getModEventBus().addListener(this::preInit); - FMLJavaModLoadingContext.get().getModEventBus().addListener(this::buildCreativeTabContents); + bus.addListener(this::preInit); + bus.addListener(this::buildCreativeTabContents); + bus.addListener(this::registerPayloads); ModLoadingContext.get().registerConfig(ModConfig.Type.COMMON, ConfigHandler.GENERAL_SPEC); ModLoadingContext.get().registerConfig(ModConfig.Type.CLIENT, ConfigHandler.CLIENT_SPEC); @@ -62,16 +63,6 @@ public NaturesCompass() { } private void preInit(FMLCommonSetupEvent event) { - String networkVersion = "1"; - network = ChannelBuilder.named(new ResourceLocation(NaturesCompass.MODID, NaturesCompass.MODID)).networkProtocolVersion(() -> networkVersion).clientAcceptedVersions(networkVersion::equals).serverAcceptedVersions(networkVersion::equals).simpleChannel(); - - // Server packets - network.messageBuilder(CompassSearchPacket.class, 0).encoder(CompassSearchPacket::toBytes).decoder(CompassSearchPacket::new).consumerMainThread(CompassSearchPacket::handle).add(); - network.messageBuilder(TeleportPacket.class, 1).encoder(TeleportPacket::toBytes).decoder(TeleportPacket::new).consumerMainThread(TeleportPacket::handle).add(); - - // Client packet - network.messageBuilder(SyncPacket.class, 2).encoder(SyncPacket::toBytes).decoder(SyncPacket::new).consumerMainThread(SyncPacket::handle).add(); - allowedBiomes = new ArrayList(); dimensionKeysForAllowedBiomeKeys = ArrayListMultimap.create(); } @@ -81,6 +72,13 @@ private void buildCreativeTabContents(BuildCreativeModeTabContentsEvent event) { event.accept(new ItemStack(naturesCompass)); } } + + private void registerPayloads(RegisterPayloadHandlerEvent event) { + final IPayloadRegistrar registrar = event.registrar(MODID); + registrar.play(SearchPacket.ID, SearchPacket::read, handler -> handler.server(SearchPacket::handle)); + registrar.play(TeleportPacket.ID, TeleportPacket::read, handler -> handler.server(TeleportPacket::handle)); + registrar.play(SyncPacket.ID, SyncPacket::read, handler -> handler.client(SyncPacket::handle)); + } @SubscribeEvent public void registerNodes(PermissionGatherEvent.Nodes event) { diff --git a/src/main/java/com/chaosthedude/naturescompass/gui/BiomeSearchList.java b/src/main/java/com/chaosthedude/naturescompass/gui/BiomeSearchList.java index 88d0d13..5ab6ad2 100644 --- a/src/main/java/com/chaosthedude/naturescompass/gui/BiomeSearchList.java +++ b/src/main/java/com/chaosthedude/naturescompass/gui/BiomeSearchList.java @@ -15,8 +15,8 @@ public class BiomeSearchList extends ObjectSelectionList { private final NaturesCompassScreen parentScreen; - public BiomeSearchList(NaturesCompassScreen parentScreen, Minecraft mc, int width, int height, int top, int bottom, int slotHeight) { - super(mc, width, height, top, bottom, slotHeight); + public BiomeSearchList(NaturesCompassScreen parentScreen, Minecraft mc, int width, int height, int y, int itemHeight) { + super(mc, width, height, y, itemHeight); this.parentScreen = parentScreen; refreshList(); } @@ -37,7 +37,7 @@ protected boolean isSelectedItem(int slotIndex) { } @Override - public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTicks) { + public void renderWidget(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTicks) { renderList(guiGraphics, mouseX, mouseY, partialTicks); } @@ -46,10 +46,10 @@ protected void renderList(GuiGraphics guiGraphics, int mouseX, int mouseY, float for (int i = 0; i < getItemCount(); ++i) { int top = getRowTop(i); int bottom = getRowBottom(i); - if (bottom >= y0 && top <= y1) { + if (bottom >= getY() && top <= getBottom()) { BiomeSearchEntry entry = getEntry(i); if (isSelectedItem(i)) { - final int insideLeft = x0 + width / 2 - getRowWidth() / 2 + 2; + final int insideLeft = getX() + width / 2 - getRowWidth() / 2 + 2; guiGraphics.fill(insideLeft - 4, top - 4, insideLeft + getRowWidth() + 4, top + itemHeight, 255 / 2 << 24); } entry.render(guiGraphics, i, top, getRowLeft(), getRowWidth(), itemHeight - 4, mouseX, mouseY, isMouseOver((double) mouseX, (double) mouseY) && Objects.equals(getEntryAtPosition((double) mouseX, (double) mouseY), entry), partialTicks); @@ -59,14 +59,14 @@ protected void renderList(GuiGraphics guiGraphics, int mouseX, int mouseY, float if (getMaxScroll() > 0) { int left = getScrollbarPosition(); int right = left + 6; - int height = (int) ((float) ((y1 - y0) * (y1 - y0)) / (float) getMaxPosition()); - height = Mth.clamp(height, 32, y1 - y0 - 8); - int top = (int) getScrollAmount() * (y1 - y0 - height) / getMaxScroll() + y0; - if (top < y0) { - top = y0; + int height = (int) ((float) ((getBottom() - getY()) * (getBottom() - getY())) / (float) getMaxPosition()); + height = Mth.clamp(height, 32, getBottom() - getY() - 8); + int top = (int) getScrollAmount() * (getBottom() - getY() - height) / getMaxScroll() + getY(); + if (top < getY()) { + top = getY(); } - guiGraphics.fill(left, y0, right, y1, (int) (2.35F * 255.0F) / 2 << 24); + guiGraphics.fill(left, getY(), right, getBottom(), (int) (2.35F * 255.0F) / 2 << 24); guiGraphics.fill(left, top, right, top + height, (int) (1.9F * 255.0F) / 2 << 24); } } diff --git a/src/main/java/com/chaosthedude/naturescompass/gui/NaturesCompassScreen.java b/src/main/java/com/chaosthedude/naturescompass/gui/NaturesCompassScreen.java index a6446f4..1507789 100644 --- a/src/main/java/com/chaosthedude/naturescompass/gui/NaturesCompassScreen.java +++ b/src/main/java/com/chaosthedude/naturescompass/gui/NaturesCompassScreen.java @@ -7,7 +7,7 @@ import com.chaosthedude.naturescompass.NaturesCompass; import com.chaosthedude.naturescompass.items.NaturesCompassItem; -import com.chaosthedude.naturescompass.network.CompassSearchPacket; +import com.chaosthedude.naturescompass.network.SearchPacket; import com.chaosthedude.naturescompass.network.TeleportPacket; import com.chaosthedude.naturescompass.sorting.ISorting; import com.chaosthedude.naturescompass.sorting.NameSorting; @@ -19,6 +19,7 @@ import net.minecraft.client.gui.screens.Screen; import net.minecraft.client.resources.language.I18n; import net.minecraft.network.chat.Component; +import net.minecraft.network.protocol.common.ServerboundCustomPayloadPacket; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; @@ -78,7 +79,7 @@ public void tick() { removeWidget(selectionList); loadAllowedBiomes(NaturesCompass.allowedBiomes); biomesMatchingSearch = new ArrayList(allowedBiomes); - selectionList = new BiomeSearchList(this, minecraft, width + 110, height, 40, height, 45); + selectionList = new BiomeSearchList(this, minecraft, width + 110, height - 40, 40, 45); addRenderableWidget(selectionList); } } @@ -116,13 +117,13 @@ public void selectBiome(BiomeSearchEntry entry) { public void searchForBiome(Biome biome) { if (BiomeUtils.getKeyForBiome(level, biome).isPresent()) { - NaturesCompass.network.send(PacketDistributor.SERVER.noArg(), new CompassSearchPacket(BiomeUtils.getKeyForBiome(level, biome).get(), player.blockPosition())); + minecraft.getConnection().send(new ServerboundCustomPayloadPacket(new SearchPacket(BiomeUtils.getKeyForBiome(level, biome).get(), player.blockPosition()))); } minecraft.setScreen(null); } public void teleport() { - NaturesCompass.network.send(PacketDistributor.SERVER.noArg(), new TeleportPacket()); + minecraft.getConnection().send(new ServerboundCustomPayloadPacket(new TeleportPacket())); minecraft.setScreen(null); } @@ -183,7 +184,7 @@ private void setupWidgets() { searchTextField = addRenderableWidget(new TransparentTextField(font, 130, 10, 140, 20, Component.translatable("string.naturescompass.search"))); if (selectionList == null) { - selectionList = new BiomeSearchList(this, minecraft, width + 110, height, 40, height, 45); + selectionList = new BiomeSearchList(this, minecraft, width + 110, height - 40, 40, 45); } addRenderableWidget(selectionList); } diff --git a/src/main/java/com/chaosthedude/naturescompass/gui/TransparentTextField.java b/src/main/java/com/chaosthedude/naturescompass/gui/TransparentTextField.java index 416210d..4e5aba4 100644 --- a/src/main/java/com/chaosthedude/naturescompass/gui/TransparentTextField.java +++ b/src/main/java/com/chaosthedude/naturescompass/gui/TransparentTextField.java @@ -33,7 +33,7 @@ public TransparentTextField(Font font, int x, int y, int width, int height, Comp } @Override - public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTicks) { + public void renderWidget(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTicks) { if (isVisible()) { if (pseudoEnableBackgroundDrawing) { final int color = (int) (255.0F * 0.55f); diff --git a/src/main/java/com/chaosthedude/naturescompass/items/NaturesCompassItem.java b/src/main/java/com/chaosthedude/naturescompass/items/NaturesCompassItem.java index 263a692..62330d5 100644 --- a/src/main/java/com/chaosthedude/naturescompass/items/NaturesCompassItem.java +++ b/src/main/java/com/chaosthedude/naturescompass/items/NaturesCompassItem.java @@ -3,7 +3,6 @@ import java.util.List; import java.util.Optional; -import com.chaosthedude.naturescompass.NaturesCompass; import com.chaosthedude.naturescompass.config.ConfigHandler; import com.chaosthedude.naturescompass.gui.GuiWrapper; import com.chaosthedude.naturescompass.network.SyncPacket; @@ -14,6 +13,7 @@ import com.chaosthedude.naturescompass.util.PlayerUtils; import net.minecraft.core.BlockPos; +import net.minecraft.network.protocol.common.ClientboundCustomPayloadPacket; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; @@ -25,7 +25,6 @@ import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.Level; import net.minecraft.world.level.biome.Biome; -import net.neoforged.neoforge.network.PacketDistributor; public class NaturesCompassItem extends Item { @@ -48,7 +47,7 @@ public InteractionResultHolder use(Level level, Player player, Intera final ServerPlayer serverPlayer = (ServerPlayer) player; final boolean canTeleport = ConfigHandler.GENERAL.allowTeleport.get() && PlayerUtils.canTeleport(serverPlayer.getServer(), player); final List allowedBiomeKeys = BiomeUtils.getAllowedBiomeKeys(level); - NaturesCompass.network.send(PacketDistributor.PLAYER.with(() -> serverPlayer), new SyncPacket(canTeleport, allowedBiomeKeys, BiomeUtils.getGeneratingDimensionsForAllowedBiomes(serverLevel))); + serverPlayer.connection.send(new ClientboundCustomPayloadPacket(new SyncPacket(canTeleport, allowedBiomeKeys, BiomeUtils.getGeneratingDimensionsForAllowedBiomes(serverLevel)))); } } else { if (worker != null) { diff --git a/src/main/java/com/chaosthedude/naturescompass/network/CompassSearchPacket.java b/src/main/java/com/chaosthedude/naturescompass/network/CompassSearchPacket.java deleted file mode 100644 index 7f5be1f..0000000 --- a/src/main/java/com/chaosthedude/naturescompass/network/CompassSearchPacket.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.chaosthedude.naturescompass.network; - -import com.chaosthedude.naturescompass.items.NaturesCompassItem; -import com.chaosthedude.naturescompass.util.ItemUtils; - -import net.minecraft.core.BlockPos; -import net.minecraft.network.FriendlyByteBuf; -import net.minecraft.resources.ResourceLocation; -import net.minecraft.world.item.ItemStack; -import net.neoforged.neoforge.network.NetworkEvent.ServerCustomPayloadEvent; - -public class CompassSearchPacket { - - private ResourceLocation biomeKey; - private int x; - private int y; - private int z; - - public CompassSearchPacket() {} - - public CompassSearchPacket(ResourceLocation biomeKey, BlockPos pos) { - this.biomeKey = biomeKey; - - this.x = pos.getX(); - this.y = pos.getY(); - this.z = pos.getZ(); - } - - public CompassSearchPacket(FriendlyByteBuf buf) { - biomeKey = buf.readResourceLocation(); - - x = buf.readInt(); - y = buf.readInt(); - z = buf.readInt(); - } - - public void toBytes(FriendlyByteBuf buf) { - buf.writeResourceLocation(biomeKey); - - buf.writeInt(x); - buf.writeInt(y); - buf.writeInt(z); - } - - public static void handle(CompassSearchPacket packet, ServerCustomPayloadEvent.Context ctx) { - ctx.enqueueWork(() -> { - final ItemStack stack = ItemUtils.getHeldNatureCompass(ctx.getSender()); - if (!stack.isEmpty()) { - final NaturesCompassItem natureCompass = (NaturesCompassItem) stack.getItem(); - natureCompass.searchForBiome(ctx.getSender().serverLevel(), ctx.getSender(), packet.biomeKey, new BlockPos(packet.x, packet.y, packet.z), stack); - } - }); - ctx.setPacketHandled(true); - } - -} diff --git a/src/main/java/com/chaosthedude/naturescompass/network/SearchPacket.java b/src/main/java/com/chaosthedude/naturescompass/network/SearchPacket.java new file mode 100644 index 0000000..d2c0cb0 --- /dev/null +++ b/src/main/java/com/chaosthedude/naturescompass/network/SearchPacket.java @@ -0,0 +1,46 @@ +package com.chaosthedude.naturescompass.network; + +import com.chaosthedude.naturescompass.NaturesCompass; +import com.chaosthedude.naturescompass.items.NaturesCompassItem; +import com.chaosthedude.naturescompass.util.ItemUtils; + +import net.minecraft.core.BlockPos; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.world.item.ItemStack; +import net.neoforged.neoforge.network.handling.PlayPayloadContext; + +public record SearchPacket(ResourceLocation biomeKey, BlockPos pos) implements CustomPacketPayload { + + public static final ResourceLocation ID = new ResourceLocation(NaturesCompass.MODID, "search"); + + public static SearchPacket read(FriendlyByteBuf buf) { + return new SearchPacket(buf.readResourceLocation(), buf.readBlockPos()); + } + + @Override + public void write(FriendlyByteBuf buf) { + buf.writeResourceLocation(biomeKey); + buf.writeBlockPos(pos); + } + + public static void handle(SearchPacket packet, PlayPayloadContext context) { + context.workHandler().submitAsync(() -> { + if (context.player().isPresent() && context.level().isPresent()) { + final ItemStack stack = ItemUtils.getHeldNatureCompass(context.player().get()); + if (!stack.isEmpty()) { + final NaturesCompassItem natureCompass = (NaturesCompassItem) stack.getItem(); + natureCompass.searchForBiome((ServerLevel) context.level().get(), context.player().get(), packet.biomeKey, packet.pos, stack); + } + } + }); + } + + @Override + public ResourceLocation id() { + return ID; + } + +} diff --git a/src/main/java/com/chaosthedude/naturescompass/network/SyncPacket.java b/src/main/java/com/chaosthedude/naturescompass/network/SyncPacket.java index 27ff20a..49e1452 100644 --- a/src/main/java/com/chaosthedude/naturescompass/network/SyncPacket.java +++ b/src/main/java/com/chaosthedude/naturescompass/network/SyncPacket.java @@ -8,27 +8,18 @@ import com.google.common.collect.ListMultimap; import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; import net.minecraft.resources.ResourceLocation; -import net.neoforged.neoforge.network.NetworkEvent.ClientCustomPayloadEvent; +import net.neoforged.neoforge.network.handling.PlayPayloadContext; -public class SyncPacket { +public record SyncPacket(boolean canTeleport, List allowedBiomes, ListMultimap dimensionKeysForAllowedBiomeKeys) implements CustomPacketPayload { - private boolean canTeleport; - private List allowedBiomes; - private ListMultimap dimensionKeysForAllowedBiomeKeys; - - public SyncPacket() {} - - public SyncPacket(boolean canTeleport, List allowedBiomes, ListMultimap dimensionKeysForAllowedBiomeKeys) { - this.canTeleport = canTeleport; - this.allowedBiomes = allowedBiomes; - this.dimensionKeysForAllowedBiomeKeys = dimensionKeysForAllowedBiomeKeys; - } - - public SyncPacket(FriendlyByteBuf buf) { - canTeleport = buf.readBoolean(); - allowedBiomes = new ArrayList(); - dimensionKeysForAllowedBiomeKeys = ArrayListMultimap.create(); + public static final ResourceLocation ID = new ResourceLocation(NaturesCompass.MODID, "sync"); + + public static SyncPacket read(FriendlyByteBuf buf) { + boolean canTeleport = buf.readBoolean(); + List allowedBiomes = new ArrayList(); + ListMultimap dimensionKeysForAllowedBiomeKeys = ArrayListMultimap.create(); int size = buf.readInt(); for (int i = 0; i < size; i++) { @@ -44,9 +35,12 @@ public SyncPacket(FriendlyByteBuf buf) { dimensionKeysForAllowedBiomeKeys.putAll(biomeKey, dimensionKeys); } } + + return new SyncPacket(canTeleport, allowedBiomes, dimensionKeysForAllowedBiomeKeys); } - public void toBytes(FriendlyByteBuf buf) { + @Override + public void write(FriendlyByteBuf buf) { buf.writeBoolean(canTeleport); buf.writeInt(allowedBiomes.size()); for (ResourceLocation biomeKey : allowedBiomes) { @@ -59,13 +53,17 @@ public void toBytes(FriendlyByteBuf buf) { } } - public static void handle(SyncPacket packet, ClientCustomPayloadEvent.Context ctx) { - ctx.enqueueWork(() -> { + public static void handle(SyncPacket packet, PlayPayloadContext context) { + context.workHandler().submitAsync(() -> { NaturesCompass.canTeleport = packet.canTeleport; NaturesCompass.allowedBiomes = packet.allowedBiomes; NaturesCompass.dimensionKeysForAllowedBiomeKeys = packet.dimensionKeysForAllowedBiomeKeys; }); - ctx.setPacketHandled(true); + } + + @Override + public ResourceLocation id() { + return ID; } } diff --git a/src/main/java/com/chaosthedude/naturescompass/network/TeleportPacket.java b/src/main/java/com/chaosthedude/naturescompass/network/TeleportPacket.java index 13668e2..06d71eb 100644 --- a/src/main/java/com/chaosthedude/naturescompass/network/TeleportPacket.java +++ b/src/main/java/com/chaosthedude/naturescompass/network/TeleportPacket.java @@ -9,48 +9,52 @@ import net.minecraft.core.BlockPos; import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; +import net.minecraft.resources.ResourceLocation; import net.minecraft.server.level.ServerPlayer; import net.minecraft.tags.BlockTags; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.Level; -import net.neoforged.neoforge.network.NetworkEvent.ServerCustomPayloadEvent; +import net.neoforged.neoforge.network.handling.PlayPayloadContext; -public class TeleportPacket { +public record TeleportPacket() implements CustomPacketPayload { - public TeleportPacket() {} - - public TeleportPacket(FriendlyByteBuf buf) {} - - public void fromBytes(FriendlyByteBuf buf) {} - - public void toBytes(FriendlyByteBuf buf) {} - - public static void handle(TeleportPacket packet, ServerCustomPayloadEvent.Context ctx) { - ctx.enqueueWork(() -> { - final ItemStack stack = ItemUtils.getHeldNatureCompass(ctx.getSender()); - if (!stack.isEmpty()) { - final NaturesCompassItem natureCompass = (NaturesCompassItem) stack.getItem(); - final ServerPlayer player = ctx.getSender(); - if (ConfigHandler.GENERAL.allowTeleport.get() && PlayerUtils.canTeleport(player.getServer(), player)) { - if (natureCompass.getState(stack) == CompassState.FOUND) { - final int x = natureCompass.getFoundBiomeX(stack); - final int z = natureCompass.getFoundBiomeZ(stack); - final int y = packet.findValidTeleportHeight(player.level(), x, z); + public static final ResourceLocation ID = new ResourceLocation(NaturesCompass.MODID, "teleport"); + + public static TeleportPacket read(FriendlyByteBuf buf) { + return new TeleportPacket(); + } - player.stopRiding(); - player.connection.teleport(x, y, z, player.getYRot(), player.getXRot()); + @Override + public void write(FriendlyByteBuf buf) {} - if (!player.isFallFlying()) { - player.setDeltaMovement(player.getDeltaMovement().x(), 0, player.getDeltaMovement().z()); - player.setOnGround(true); + public static void handle(TeleportPacket packet, PlayPayloadContext context) { + context.workHandler().submitAsync(() -> { + if (context.player().isPresent() && context.level().isPresent()) { + final ItemStack stack = ItemUtils.getHeldNatureCompass(context.player().get()); + if (!stack.isEmpty()) { + final NaturesCompassItem natureCompass = (NaturesCompassItem) stack.getItem(); + final ServerPlayer player = (ServerPlayer) context.player().get(); + if (ConfigHandler.GENERAL.allowTeleport.get() && PlayerUtils.canTeleport(player.getServer(), player)) { + if (natureCompass.getState(stack) == CompassState.FOUND) { + final int x = natureCompass.getFoundBiomeX(stack); + final int z = natureCompass.getFoundBiomeZ(stack); + final int y = packet.findValidTeleportHeight(context.level().get(), x, z); + + player.stopRiding(); + player.connection.teleport(x, y, z, player.getYRot(), player.getXRot()); + + if (!player.isFallFlying()) { + player.setDeltaMovement(player.getDeltaMovement().x(), 0, player.getDeltaMovement().z()); + player.setOnGround(true); + } } + } else { + NaturesCompass.LOGGER.warn("Player " + player.getDisplayName().getString() + " tried to teleport but does not have permission."); } - } else { - NaturesCompass.LOGGER.warn("Player " + player.getDisplayName().getString() + " tried to teleport but does not have permission."); } } }); - ctx.setPacketHandled(true); } private int findValidTeleportHeight(Level level, int x, int z) { @@ -78,5 +82,10 @@ private boolean isValidTeleportPosition(Level level, BlockPos pos) { private boolean isFree(Level level, BlockPos pos) { return level.getBlockState(pos).isAir() || level.getBlockState(pos).is(BlockTags.FIRE) || level.getBlockState(pos).liquid() || level.getBlockState(pos).canBeReplaced(); } + + @Override + public ResourceLocation id() { + return ID; + } } diff --git a/src/main/resources/META-INF/mods.toml b/src/main/resources/META-INF/mods.toml index 5f32c8e..19a7472 100644 --- a/src/main/resources/META-INF/mods.toml +++ b/src/main/resources/META-INF/mods.toml @@ -5,7 +5,7 @@ license="CC BY-NC-SA 4.0" [[mods]] modId="naturescompass" - version="1.20.2-3.0.0-neoforge" + version="1.20.4-3.0.0-neoforge" displayName="Nature's Compass" displayURL="https://github.com/MattCzyr/NaturesCompass" authors="ChaosTheDude" @@ -15,13 +15,13 @@ license="CC BY-NC-SA 4.0" [[dependencies.naturescompass]] modId="neoforge" - mandatory=true + type="required" versionRange="${neo_version_range}" ordering="NONE" side="BOTH" [[dependencies.naturescompass]] modId="minecraft" - mandatory=true + type="required" versionRange="${minecraft_version_range}" ordering="NONE" side="BOTH"