diff --git a/build.gradle b/build.gradle index a233fb5..3312b15 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ plugins { - id 'fabric-loom' version '1.4-SNAPSHOT' + id 'fabric-loom' version '1.6-SNAPSHOT' id 'maven-publish' } @@ -18,6 +18,18 @@ repositories { // for more information about repositories. } +loom { + splitEnvironmentSourceSets() + + mods { + "modid" { + sourceSet sourceSets.main + sourceSet sourceSets.client + } + } + +} + dependencies { // To change the versions see the gradle.properties file minecraft "com.mojang:minecraft:${project.minecraft_version}" @@ -27,10 +39,6 @@ dependencies { // Fabric API. This is technically optional, but you probably want it anyway. modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" - // Uncomment the following line to enable the deprecated Fabric API modules. - // These are included in the Fabric API production distribution and allow you to update your mod to the latest modules at a later more convenient time. - - // modImplementation "net.fabricmc.fabric-api:fabric-api-deprecated:${project.fabric_version}" } processResources { @@ -42,7 +50,7 @@ processResources { } tasks.withType(JavaCompile).configureEach { - it.options.release = 17 + it.options.release = 21 } java { @@ -51,8 +59,8 @@ java { // If you remove this line, sources will not be generated. withSourcesJar() - sourceCompatibility = JavaVersion.VERSION_17 - targetCompatibility = JavaVersion.VERSION_17 + sourceCompatibility = JavaVersion.VERSION_21 + targetCompatibility = JavaVersion.VERSION_21 } jar { @@ -64,7 +72,8 @@ jar { // configure the maven publication publishing { publications { - mavenJava(MavenPublication) { + create("mavenJava", MavenPublication) { + artifactId = project.archives_base_name from components.java } } diff --git a/gradle.properties b/gradle.properties index 3ca346d..6375d97 100644 --- a/gradle.properties +++ b/gradle.properties @@ -4,14 +4,14 @@ org.gradle.parallel=true # Fabric Properties # check these on https://fabricmc.net/develop -minecraft_version=1.20.4 -yarn_mappings=1.20.4+build.1 -loader_version=0.15.0 +minecraft_version=1.20.5 +yarn_mappings=1.20.5+build.1 +loader_version=0.15.10 # Mod Properties -mod_version=1.20.4-2.2.3-fabric +mod_version=1.20.5-2.2.4-fabric maven_group=com.chaosthedude.naturescompass archives_base_name=naturescompass # Dependencies -fabric_version=0.91.1+1.20.4 \ No newline at end of file +fabric_version=0.97.5+1.20.5 \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index 033e24c..e644113 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 1af9e09..b82aa23 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip networkTimeout=10000 validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME 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/src/main/java/com/chaosthedude/naturescompass/NaturesCompass.java b/src/main/java/com/chaosthedude/naturescompass/NaturesCompass.java index 58b770e..e84e1c8 100644 --- a/src/main/java/com/chaosthedude/naturescompass/NaturesCompass.java +++ b/src/main/java/com/chaosthedude/naturescompass/NaturesCompass.java @@ -9,14 +9,19 @@ import com.chaosthedude.naturescompass.config.NaturesCompassConfig; import com.chaosthedude.naturescompass.items.NaturesCompassItem; import com.chaosthedude.naturescompass.network.SearchPacket; +import com.chaosthedude.naturescompass.network.SyncPacket; import com.chaosthedude.naturescompass.network.TeleportPacket; import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.ListMultimap; +import com.mojang.serialization.Codec; import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents; +import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry; import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; +import net.minecraft.component.DataComponentType; import net.minecraft.item.ItemGroups; +import net.minecraft.network.codec.PacketCodecs; import net.minecraft.registry.Registries; import net.minecraft.registry.Registry; import net.minecraft.util.Identifier; @@ -29,6 +34,14 @@ public class NaturesCompass implements ModInitializer { public static final NaturesCompassItem NATURES_COMPASS_ITEM = new NaturesCompassItem(); + public static final DataComponentType BIOME_ID_COMPONENT = DataComponentType.builder().codec(Codec.STRING).packetCodec(PacketCodecs.STRING).build(); + public static final DataComponentType COMPASS_STATE_COMPONENT = DataComponentType.builder().codec(Codec.INT).packetCodec(PacketCodecs.INTEGER).build(); + public static final DataComponentType FOUND_X_COMPONENT = DataComponentType.builder().codec(Codec.INT).packetCodec(PacketCodecs.INTEGER).build(); + public static final DataComponentType FOUND_Z_COMPONENT = DataComponentType.builder().codec(Codec.INT).packetCodec(PacketCodecs.INTEGER).build(); + public static final DataComponentType SEARCH_RADIUS_COMPONENT = DataComponentType.builder().codec(Codec.INT).packetCodec(PacketCodecs.INTEGER).build(); + public static final DataComponentType SAMPLES_COMPONENT = DataComponentType.builder().codec(Codec.INT).packetCodec(PacketCodecs.INTEGER).build(); + public static final DataComponentType DISPLAY_COORDS_COMPONENT = DataComponentType.builder().codec(Codec.BOOL).packetCodec(PacketCodecs.BOOL).build(); + public static boolean canTeleport; public static List allowedBiomes; public static ListMultimap dimensionIDsForAllowedBiomeIDs; @@ -38,11 +51,23 @@ public void onInitialize() { NaturesCompassConfig.load(); Registry.register(Registries.ITEM, new Identifier(MODID, "naturescompass"), NATURES_COMPASS_ITEM); + + Registry.register(Registries.DATA_COMPONENT_TYPE, new Identifier(MODID, "biome_id"), BIOME_ID_COMPONENT); + Registry.register(Registries.DATA_COMPONENT_TYPE, new Identifier(MODID, "compass_state"), COMPASS_STATE_COMPONENT); + Registry.register(Registries.DATA_COMPONENT_TYPE, new Identifier(MODID, "found_x"), FOUND_X_COMPONENT); + Registry.register(Registries.DATA_COMPONENT_TYPE, new Identifier(MODID, "found_z"), FOUND_Z_COMPONENT); + Registry.register(Registries.DATA_COMPONENT_TYPE, new Identifier(MODID, "search_radius"), SEARCH_RADIUS_COMPONENT); + Registry.register(Registries.DATA_COMPONENT_TYPE, new Identifier(MODID, "samples"), SAMPLES_COMPONENT); + Registry.register(Registries.DATA_COMPONENT_TYPE, new Identifier(MODID, "display_coords"), DISPLAY_COORDS_COMPONENT); ItemGroupEvents.modifyEntriesEvent(ItemGroups.TOOLS).register(entries -> entries.add(NATURES_COMPASS_ITEM)); - - ServerPlayNetworking.registerGlobalReceiver(SearchPacket.ID, SearchPacket::apply); - ServerPlayNetworking.registerGlobalReceiver(TeleportPacket.ID, TeleportPacket::apply); + + PayloadTypeRegistry.playC2S().register(SearchPacket.PACKET_ID, SearchPacket.PACKET_CODEC); + PayloadTypeRegistry.playC2S().register(TeleportPacket.PACKET_ID, TeleportPacket.PACKET_CODEC); + PayloadTypeRegistry.playS2C().register(SyncPacket.PACKET_ID, SyncPacket.PACKET_CODEC); + + ServerPlayNetworking.registerGlobalReceiver(SearchPacket.PACKET_ID, SearchPacket::apply); + ServerPlayNetworking.registerGlobalReceiver(TeleportPacket.PACKET_ID, TeleportPacket::apply); allowedBiomes = new ArrayList(); dimensionIDsForAllowedBiomeIDs = ArrayListMultimap.create(); diff --git a/src/main/java/com/chaosthedude/naturescompass/NaturesCompassClient.java b/src/main/java/com/chaosthedude/naturescompass/NaturesCompassClient.java index bd2819c..6d05f8a 100644 --- a/src/main/java/com/chaosthedude/naturescompass/NaturesCompassClient.java +++ b/src/main/java/com/chaosthedude/naturescompass/NaturesCompassClient.java @@ -21,7 +21,7 @@ public class NaturesCompassClient implements ClientModInitializer { @Override public void onInitializeClient() { - ClientPlayNetworking.registerGlobalReceiver(SyncPacket.ID, SyncPacket::apply); + ClientPlayNetworking.registerGlobalReceiver(SyncPacket.PACKET_ID, SyncPacket::apply); ModelPredicateProviderRegistry.register(NaturesCompass.NATURES_COMPASS_ITEM, new Identifier("angle"), new ClampedModelPredicateProvider() { private double rotation; diff --git a/src/main/java/com/chaosthedude/naturescompass/gui/BiomeInfoScreen.java b/src/main/java/com/chaosthedude/naturescompass/gui/BiomeInfoScreen.java index 1d99e76..2d0f78e 100644 --- a/src/main/java/com/chaosthedude/naturescompass/gui/BiomeInfoScreen.java +++ b/src/main/java/com/chaosthedude/naturescompass/gui/BiomeInfoScreen.java @@ -65,6 +65,8 @@ public void init() { @Override public void render(DrawContext context, int mouseX, int mouseY, float partialTicks) { + super.render(context, mouseX, mouseY, partialTicks); + context.drawText(textRenderer, BiomeUtils.getBiomeNameForDisplay(parentScreen.world, biome), (width / 2) - (textRenderer.getWidth(BiomeUtils.getBiomeNameForDisplay(parentScreen.world, biome)) / 2), 20, 0xffffff, false); context.drawText(textRenderer, Text.translatable("string.naturescompass.source"), width / 2 - 100, 40, 0xffffff, false); @@ -90,8 +92,6 @@ public void render(DrawContext context, int mouseX, int mouseY, float partialTic //context.drawText(textRenderer, Text.translatable("string.naturescompass.highHumidity"), width / 2 + 40, 100, 0xffffff, false); //context.drawText(textRenderer, highHumidity, width / 2 + 40, 110, 0x808080, false); - - super.render(context, mouseX, mouseY, partialTicks); } private void setupButtons() { diff --git a/src/main/java/com/chaosthedude/naturescompass/gui/BiomeSearchList.java b/src/main/java/com/chaosthedude/naturescompass/gui/BiomeSearchList.java index 1d0156e..a853e7d 100644 --- a/src/main/java/com/chaosthedude/naturescompass/gui/BiomeSearchList.java +++ b/src/main/java/com/chaosthedude/naturescompass/gui/BiomeSearchList.java @@ -21,16 +21,16 @@ public BiomeSearchList(NaturesCompassScreen guiNaturesCompass, MinecraftClient m this.guiNaturesCompass = guiNaturesCompass; refreshList(); } - - @Override - protected int getScrollbarPositionX() { - return super.getScrollbarPositionX() + 20; - } - + @Override public int getRowWidth() { return super.getRowWidth() + 50; } + + @Override + protected int getDefaultScrollbarX() { + return getRowLeft() + getRowWidth() - 2; + } @Override protected boolean isSelectedEntry(int slotIndex) { @@ -44,15 +44,18 @@ public void renderWidget(DrawContext context, int mouseX, int mouseY, float par3 @Override protected void renderList(DrawContext context, int mouseX, int mouseY, float par5) { + context.fill(getRowLeft() - 4, getY(), getRowLeft() + getRowWidth() + 4, getY() + getHeight() + 4, 255 / 2 << 24); + + enableScissor(context); int i = getEntryCount(); for (int j = 0; j < i; ++j) { int k = getRowTop(j); int l = getRowBottom(j); if (l >= getY() && k <= getBottom()) { - int j1 = this.itemHeight - 4; + int j1 = itemHeight - 4; BiomeSearchEntry e = getEntry(j); int k1 = getRowWidth(); - if (/*renderSelection*/ true && isSelectedEntry(j)) { + if (isSelectedEntry(j)) { final int insideLeft = getX() + width / 2 - getRowWidth() / 2 + 2; context.fill(insideLeft - 4, k - 4, insideLeft + getRowWidth() + 4, k + itemHeight, 255 / 2 << 24); } @@ -61,9 +64,10 @@ protected void renderList(DrawContext context, int mouseX, int mouseY, float par e.render(context, j, k, j2, k1, j1, mouseX, mouseY, isMouseOver((double) mouseX, (double) mouseY) && Objects .equals(getEntryAtPosition((double) mouseX, (double) mouseY), e), par5); } } + context.disableScissor(); if (getMaxScroll() > 0) { - int left = getScrollbarPositionX(); + int left = getScrollbarX(); int right = left + 6; int height = (int) ((float) ((getBottom() - getY()) * (getBottom() - getY())) / (float) getMaxPosition()); height = MathHelper.clamp(height, 32, getBottom() - getY() - 8); @@ -76,7 +80,13 @@ protected void renderList(DrawContext context, int mouseX, int mouseY, float par context.fill(left, scrollbarTop, right, scrollbarTop + height, (int) (1.9F * 255.0F) / 2 << 24); } } + + @Override + protected void enableScissor(DrawContext context) { + context.enableScissor(getX(), getY(), getRight(), getBottom()); + } + @Override protected int getRowBottom(int index) { return getRowTop(index) + itemHeight; } diff --git a/src/main/java/com/chaosthedude/naturescompass/gui/NaturesCompassScreen.java b/src/main/java/com/chaosthedude/naturescompass/gui/NaturesCompassScreen.java index dd54f76..285de30 100644 --- a/src/main/java/com/chaosthedude/naturescompass/gui/NaturesCompassScreen.java +++ b/src/main/java/com/chaosthedude/naturescompass/gui/NaturesCompassScreen.java @@ -20,10 +20,12 @@ import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.widget.ButtonWidget; +import net.minecraft.client.gui.widget.TextFieldWidget; import net.minecraft.client.resource.language.I18n; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemStack; import net.minecraft.text.Text; +import net.minecraft.util.Colors; import net.minecraft.util.Identifier; import net.minecraft.world.World; import net.minecraft.world.biome.Biome; @@ -41,7 +43,7 @@ public class NaturesCompassScreen extends Screen { private ButtonWidget teleportButton; private ButtonWidget cancelButton; private ButtonWidget sortByButton; - private TransparentTextField searchTextField; + private TextFieldWidget searchTextField; private BiomeSearchList selectionList; private ISorting sortingCategory; @@ -91,8 +93,8 @@ public void tick() { @Override public void render(DrawContext context, int mouseX, int mouseY, float partialTicks) { - context.drawCenteredTextWithShadow(textRenderer, I18n.translate("string.naturescompass.selectBiome"), 65, 15, 0xffffff); super.render(context, mouseX, mouseY, partialTicks); + context.drawCenteredTextWithShadow(textRenderer, I18n.translate("string.naturescompass.selectBiome"), 65, 15, Colors.WHITE); } @Override @@ -121,12 +123,12 @@ public void selectBiome(BiomeSearchEntry entry) { } public void searchForBiome(Biome biome) { - ClientPlayNetworking.send(SearchPacket.ID, new SearchPacket(BiomeUtils.getIdentifierForBiome(world, biome), player.getBlockPos())); + ClientPlayNetworking.send(new SearchPacket(BiomeUtils.getIdentifierForBiome(world, biome), player.getBlockPos())); client.setScreen(null); } public void teleport() { - ClientPlayNetworking.send(TeleportPacket.ID, new TeleportPacket()); + ClientPlayNetworking.send(new TeleportPacket()); client.setScreen(null); } @@ -185,8 +187,7 @@ private void setupButtons() { } private void setupTextFields() { - searchTextField = new TransparentTextField(textRenderer, 130, 10, 140, 20, Text.translatable("string.naturescompass.search")); - addDrawableChild(searchTextField); + searchTextField = addDrawableChild(new TransparentTextField(textRenderer, 130, 10, 140, 20, Text.translatable("string.naturescompass.search"))); } private void loadAllowedBiomes(List allowedBiomeIDs) { diff --git a/src/main/java/com/chaosthedude/naturescompass/items/NaturesCompassItem.java b/src/main/java/com/chaosthedude/naturescompass/items/NaturesCompassItem.java index b6cd8b1..fa50e9e 100644 --- a/src/main/java/com/chaosthedude/naturescompass/items/NaturesCompassItem.java +++ b/src/main/java/com/chaosthedude/naturescompass/items/NaturesCompassItem.java @@ -3,6 +3,7 @@ import java.util.List; import java.util.Optional; +import com.chaosthedude.naturescompass.NaturesCompass; import com.chaosthedude.naturescompass.config.NaturesCompassConfig; import com.chaosthedude.naturescompass.gui.GuiWrapper; import com.chaosthedude.naturescompass.network.SyncPacket; @@ -12,7 +13,6 @@ import com.chaosthedude.naturescompass.utils.PlayerUtils; import com.chaosthedude.naturescompass.workers.BiomeSearchWorker; -import net.fabricmc.fabric.api.item.v1.FabricItemSettings; import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.Item; @@ -31,7 +31,7 @@ public class NaturesCompassItem extends Item { private BiomeSearchWorker worker; public NaturesCompassItem() { - super(new FabricItemSettings().maxCount(1)); + super(new Settings().maxCount(1)); } @Override @@ -45,7 +45,7 @@ public TypedActionResult use(World world, PlayerEntity player, Hand h final ServerPlayerEntity serverPlayer = (ServerPlayerEntity) player; final boolean canTeleport = NaturesCompassConfig.allowTeleport && PlayerUtils.canTeleport(player); final List allowedBiomeIDs = BiomeUtils.getAllowedBiomeIDs(world); - ServerPlayNetworking.send(serverPlayer, SyncPacket.ID, new SyncPacket(canTeleport, allowedBiomeIDs, BiomeUtils.getGeneratingDimensionsForAllowedBiomes(serverWorld))); + ServerPlayNetworking.send(serverPlayer, new SyncPacket(canTeleport, allowedBiomeIDs, BiomeUtils.getGeneratingDimensionsForAllowedBiomes(serverWorld))); } } else { if (worker != null) { @@ -81,7 +81,7 @@ public void fail(ItemStack stack, PlayerEntity player, int searchRadius, int sam } public boolean isActive(ItemStack stack) { - if (ItemUtils.verifyNBT(stack)) { + if (ItemUtils.isCompass(stack)) { return getState(stack) != CompassState.INACTIVE; } @@ -89,136 +89,136 @@ public boolean isActive(ItemStack stack) { } public void setSearching(ItemStack stack, Identifier biomeID, PlayerEntity player) { - if (ItemUtils.verifyNBT(stack)) { - stack.getNbt().putString("BiomeID", biomeID.toString()); - stack.getNbt().putInt("State", CompassState.SEARCHING.getID()); - stack.getNbt().putInt("SearchRadius", 0); + if (ItemUtils.isCompass(stack)) { + stack.set(NaturesCompass.BIOME_ID_COMPONENT, biomeID.toString()); + stack.set(NaturesCompass.COMPASS_STATE_COMPONENT, CompassState.SEARCHING.getID()); + stack.set(NaturesCompass.SEARCH_RADIUS_COMPONENT, 0); } } public void setFound(ItemStack stack, int x, int z, int samples, PlayerEntity player) { - if (ItemUtils.verifyNBT(stack)) { - stack.getNbt().putInt("State", CompassState.FOUND.getID()); - stack.getNbt().putInt("FoundX", x); - stack.getNbt().putInt("FoundZ", z); - stack.getNbt().putInt("Samples", samples); + if (ItemUtils.isCompass(stack)) { + stack.set(NaturesCompass.COMPASS_STATE_COMPONENT, CompassState.FOUND.getID()); + stack.set(NaturesCompass.FOUND_X_COMPONENT, x); + stack.set(NaturesCompass.FOUND_Z_COMPONENT, z); + stack.set(NaturesCompass.SAMPLES_COMPONENT, samples); } } public void setNotFound(ItemStack stack, PlayerEntity player, int searchRadius, int samples) { - if (ItemUtils.verifyNBT(stack)) { - stack.getNbt().putInt("State", CompassState.NOT_FOUND.getID()); - stack.getNbt().putInt("SearchRadius", searchRadius); - stack.getNbt().putInt("Samples", samples); + if (ItemUtils.isCompass(stack)) { + stack.set(NaturesCompass.COMPASS_STATE_COMPONENT, CompassState.NOT_FOUND.getID()); + stack.set(NaturesCompass.SEARCH_RADIUS_COMPONENT, searchRadius); + stack.set(NaturesCompass.SAMPLES_COMPONENT, samples); } } public void setInactive(ItemStack stack, PlayerEntity player) { - if (ItemUtils.verifyNBT(stack)) { - stack.getNbt().putInt("State", CompassState.INACTIVE.getID()); + if (ItemUtils.isCompass(stack)) { + stack.set(NaturesCompass.COMPASS_STATE_COMPONENT, CompassState.INACTIVE.getID()); } } public void setState(ItemStack stack, BlockPos pos, CompassState state, PlayerEntity player) { - if (ItemUtils.verifyNBT(stack)) { - stack.getNbt().putInt("State", state.getID()); + if (ItemUtils.isCompass(stack)) { + stack.set(NaturesCompass.COMPASS_STATE_COMPONENT, state.getID()); } } public void setDisplayCoordinates(ItemStack stack, boolean displayPosition) { - if (ItemUtils.verifyNBT(stack)) { - stack.getNbt().putBoolean("DisplayCoordinates", displayPosition); + if (ItemUtils.isCompass(stack)) { + stack.set(NaturesCompass.DISPLAY_COORDS_COMPONENT, displayPosition); } } public void setFoundBiomeX(ItemStack stack, int x, PlayerEntity player) { - if (ItemUtils.verifyNBT(stack)) { - stack.getNbt().putInt("FoundX", x); + if (ItemUtils.isCompass(stack)) { + stack.set(NaturesCompass.FOUND_X_COMPONENT, x); } } public void setFoundBiomeZ(ItemStack stack, int z, PlayerEntity player) { - if (ItemUtils.verifyNBT(stack)) { - stack.getNbt().putInt("FoundZ", z); + if (ItemUtils.isCompass(stack)) { + stack.set(NaturesCompass.FOUND_Z_COMPONENT, z); } } public void setBiomeID(ItemStack stack, Identifier biomeID, PlayerEntity player) { - if (ItemUtils.verifyNBT(stack)) { - stack.getNbt().putString("BiomeID", biomeID.toString()); + if (ItemUtils.isCompass(stack)) { + stack.set(NaturesCompass.BIOME_ID_COMPONENT, biomeID.toString()); } } public void setSearchRadius(ItemStack stack, int searchRadius, PlayerEntity player) { - if (ItemUtils.verifyNBT(stack)) { - stack.getNbt().putInt("SearchRadius", searchRadius); + if (ItemUtils.isCompass(stack)) { + stack.set(NaturesCompass.SEARCH_RADIUS_COMPONENT, searchRadius); } } public void setSamples(ItemStack stack, int samples, PlayerEntity player) { - if (ItemUtils.verifyNBT(stack)) { - stack.getNbt().putInt("Samples", samples); + if (ItemUtils.isCompass(stack)) { + stack.set(NaturesCompass.SAMPLES_COMPONENT, samples); } } public CompassState getState(ItemStack stack) { - if (ItemUtils.verifyNBT(stack)) { - return CompassState.fromID(stack.getNbt().getInt("State")); + if (ItemUtils.isCompass(stack) && stack.contains(NaturesCompass.COMPASS_STATE_COMPONENT)) { + return CompassState.fromID(stack.get(NaturesCompass.COMPASS_STATE_COMPONENT)); } return null; } public int getFoundBiomeX(ItemStack stack) { - if (ItemUtils.verifyNBT(stack)) { - return stack.getNbt().getInt("FoundX"); + if (ItemUtils.isCompass(stack) && stack.contains(NaturesCompass.FOUND_X_COMPONENT)) { + return stack.get(NaturesCompass.FOUND_X_COMPONENT); } return 0; } public int getFoundBiomeZ(ItemStack stack) { - if (ItemUtils.verifyNBT(stack)) { - return stack.getNbt().getInt("FoundZ"); + if (ItemUtils.isCompass(stack) && stack.contains(NaturesCompass.FOUND_Z_COMPONENT)) { + return stack.get(NaturesCompass.FOUND_Z_COMPONENT); } return 0; } public Identifier getBiomeID(ItemStack stack) { - if (ItemUtils.verifyNBT(stack)) { - return new Identifier(stack.getNbt().getString("BiomeID")); + if (ItemUtils.isCompass(stack) && stack.contains(NaturesCompass.BIOME_ID_COMPONENT)) { + return new Identifier(stack.get(NaturesCompass.BIOME_ID_COMPONENT)); } return new Identifier(""); } public int getSearchRadius(ItemStack stack) { - if (ItemUtils.verifyNBT(stack)) { - return stack.getNbt().getInt("SearchRadius"); + if (ItemUtils.isCompass(stack) && stack.contains(NaturesCompass.SEARCH_RADIUS_COMPONENT)) { + return stack.get(NaturesCompass.SEARCH_RADIUS_COMPONENT); } return -1; } public int getSamples(ItemStack stack) { - if (ItemUtils.verifyNBT(stack)) { - return stack.getNbt().getInt("Samples"); + if (ItemUtils.isCompass(stack) && stack.contains(NaturesCompass.SAMPLES_COMPONENT)) { + return stack.get(NaturesCompass.SAMPLES_COMPONENT); } return -1; } - - public int getDistanceToBiome(PlayerEntity player, ItemStack stack) { - return BiomeUtils.getDistanceToBiome(player, getFoundBiomeX(stack), getFoundBiomeZ(stack)); - } public boolean shouldDisplayCoordinates(ItemStack stack) { - if (ItemUtils.verifyNBT(stack) && stack.getNbt().contains("DisplayCoordinates")) { - return stack.getNbt().getBoolean("DisplayCoordinates"); + if (ItemUtils.isCompass(stack) && stack.contains(NaturesCompass.DISPLAY_COORDS_COMPONENT)) { + return stack.get(NaturesCompass.DISPLAY_COORDS_COMPONENT); } return true; } + public int getDistanceToBiome(PlayerEntity player, ItemStack stack) { + return BiomeUtils.getDistanceToBiome(player, getFoundBiomeX(stack), getFoundBiomeZ(stack)); + } + } diff --git a/src/main/java/com/chaosthedude/naturescompass/network/SearchPacket.java b/src/main/java/com/chaosthedude/naturescompass/network/SearchPacket.java index 330094e..10f8bf6 100644 --- a/src/main/java/com/chaosthedude/naturescompass/network/SearchPacket.java +++ b/src/main/java/com/chaosthedude/naturescompass/network/SearchPacket.java @@ -4,37 +4,42 @@ import com.chaosthedude.naturescompass.items.NaturesCompassItem; import com.chaosthedude.naturescompass.utils.ItemUtils; -import io.netty.buffer.Unpooled; -import net.fabricmc.fabric.api.networking.v1.PacketSender; +import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; import net.minecraft.item.ItemStack; -import net.minecraft.network.PacketByteBuf; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.network.ServerPlayNetworkHandler; -import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.network.RegistryByteBuf; +import net.minecraft.network.codec.PacketCodec; +import net.minecraft.network.packet.CustomPayload; import net.minecraft.server.world.ServerWorld; import net.minecraft.util.Identifier; import net.minecraft.util.math.BlockPos; -public class SearchPacket extends PacketByteBuf { +public record SearchPacket(Identifier biomeID, BlockPos pos) implements CustomPayload { - public static final Identifier ID = new Identifier(NaturesCompass.MODID, "search"); + public static final CustomPayload.Id PACKET_ID = new CustomPayload.Id<>(new Identifier(NaturesCompass.MODID, "search")); - public SearchPacket(Identifier biomeID, BlockPos pos) { - super(Unpooled.buffer()); - writeIdentifier(biomeID); - writeBlockPos(pos); + public static final PacketCodec PACKET_CODEC = PacketCodec.of(SearchPacket::write, SearchPacket::read); + + public static SearchPacket read(RegistryByteBuf buf) { + return new SearchPacket(buf.readIdentifier(), buf.readBlockPos()); + } + + public void write(RegistryByteBuf buf) { + buf.writeIdentifier(biomeID); + buf.writeBlockPos(pos); } + + @Override + public Id getId() { + return PACKET_ID; + } - public static void apply(MinecraftServer server, ServerPlayerEntity player, ServerPlayNetworkHandler handler, PacketByteBuf buf, PacketSender responseSender) { - final Identifier biomeID = buf.readIdentifier(); - final BlockPos pos = buf.readBlockPos(); - - server.execute(() -> { - final ItemStack stack = ItemUtils.getHeldNatureCompass(player); + public static void apply(SearchPacket packet, ServerPlayNetworking.Context context) { + context.player().getServer().execute(() -> { + final ItemStack stack = ItemUtils.getHeldNatureCompass(context.player()); if (!stack.isEmpty()) { final NaturesCompassItem natureCompass = (NaturesCompassItem) stack.getItem(); - final ServerWorld world = player.getServerWorld(); - natureCompass.searchForBiome(world, player, biomeID, pos, stack); + final ServerWorld world = context.player().getServerWorld(); + natureCompass.searchForBiome(world, context.player(), packet.biomeID(), packet.pos(), stack); } }); } diff --git a/src/main/java/com/chaosthedude/naturescompass/network/SyncPacket.java b/src/main/java/com/chaosthedude/naturescompass/network/SyncPacket.java index 29b1eeb..c15a444 100644 --- a/src/main/java/com/chaosthedude/naturescompass/network/SyncPacket.java +++ b/src/main/java/com/chaosthedude/naturescompass/network/SyncPacket.java @@ -7,35 +7,22 @@ import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.ListMultimap; -import io.netty.buffer.Unpooled; -import net.fabricmc.fabric.api.networking.v1.PacketSender; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.network.ClientPlayNetworkHandler; -import net.minecraft.network.PacketByteBuf; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; +import net.minecraft.network.RegistryByteBuf; +import net.minecraft.network.codec.PacketCodec; +import net.minecraft.network.packet.CustomPayload; import net.minecraft.util.Identifier; -public class SyncPacket extends PacketByteBuf { +public record SyncPacket(boolean canTeleport, List allowedBiomeIDs, ListMultimap dimensionIDsForAllowedBiomeIDs) implements CustomPayload { - public static final Identifier ID = new Identifier(NaturesCompass.MODID, "sync"); + public static final CustomPayload.Id PACKET_ID = new CustomPayload.Id<>(new Identifier(NaturesCompass.MODID, "sync")); + + public static final PacketCodec PACKET_CODEC = PacketCodec.of(SyncPacket::write, SyncPacket::read); - public SyncPacket(boolean canTeleport, List allowedBiomeIDs, ListMultimap dimensionIDsForAllowedBiomeIDs) { - super(Unpooled.buffer()); - writeBoolean(canTeleport); - writeInt(allowedBiomeIDs.size()); - for (Identifier biomeID : allowedBiomeIDs) { - writeIdentifier(biomeID); - List dimensionIDs = dimensionIDsForAllowedBiomeIDs.get(biomeID); - writeInt(dimensionIDs.size()); - for (Identifier dimensionID : dimensionIDs) { - writeIdentifier(dimensionID); - } - } - } - - public static void apply(MinecraftClient client, ClientPlayNetworkHandler handler, PacketByteBuf buf, PacketSender responseSender) { - final boolean canTeleport = buf.readBoolean(); - final List allowedBiomeIDs = new ArrayList(); - final ListMultimap dimensionIDsForAllowedBiomeIDs = ArrayListMultimap.create(); + public static SyncPacket read(RegistryByteBuf buf) { + boolean canTeleport = buf.readBoolean(); + ListallowedBiomeIDs = new ArrayList(); + ListMultimap dimensionIDsForAllowedBiomeIDs = ArrayListMultimap.create(); int size = buf.readInt(); for (int i = 0; i < size; i++) { Identifier biomeID = buf.readIdentifier(); @@ -51,10 +38,32 @@ public static void apply(MinecraftClient client, ClientPlayNetworkHandler handle } } - client.execute(() -> { - NaturesCompass.canTeleport = canTeleport; - NaturesCompass.allowedBiomes = allowedBiomeIDs; - NaturesCompass.dimensionIDsForAllowedBiomeIDs = dimensionIDsForAllowedBiomeIDs; + return new SyncPacket(canTeleport, allowedBiomeIDs, dimensionIDsForAllowedBiomeIDs); + } + + public void write(RegistryByteBuf buf) { + buf.writeBoolean(canTeleport); + buf.writeInt(allowedBiomeIDs.size()); + for (Identifier biomeID : allowedBiomeIDs) { + buf.writeIdentifier(biomeID); + List dimensionIDs = dimensionIDsForAllowedBiomeIDs.get(biomeID); + buf.writeInt(dimensionIDs.size()); + for (Identifier dimensionID : dimensionIDs) { + buf.writeIdentifier(dimensionID); + } + } + } + + @Override + public Id getId() { + return PACKET_ID; + } + + public static void apply(SyncPacket packet, ClientPlayNetworking.Context context) { + context.client().execute(() -> { + NaturesCompass.canTeleport = packet.canTeleport(); + NaturesCompass.allowedBiomes = packet.allowedBiomeIDs(); + NaturesCompass.dimensionIDsForAllowedBiomeIDs = packet.dimensionIDsForAllowedBiomeIDs(); }); } diff --git a/src/main/java/com/chaosthedude/naturescompass/network/TeleportPacket.java b/src/main/java/com/chaosthedude/naturescompass/network/TeleportPacket.java index 8c07535..1bac345 100644 --- a/src/main/java/com/chaosthedude/naturescompass/network/TeleportPacket.java +++ b/src/main/java/com/chaosthedude/naturescompass/network/TeleportPacket.java @@ -9,51 +9,59 @@ import com.chaosthedude.naturescompass.utils.ItemUtils; import com.chaosthedude.naturescompass.utils.PlayerUtils; -import io.netty.buffer.Unpooled; -import net.fabricmc.fabric.api.networking.v1.PacketSender; +import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; import net.minecraft.item.ItemStack; -import net.minecraft.network.PacketByteBuf; +import net.minecraft.network.RegistryByteBuf; +import net.minecraft.network.codec.PacketCodec; +import net.minecraft.network.packet.CustomPayload; import net.minecraft.registry.tag.BlockTags; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.network.ServerPlayNetworkHandler; -import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.util.Identifier; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; -public class TeleportPacket extends PacketByteBuf { +public record TeleportPacket() implements CustomPayload { - public static final Identifier ID = new Identifier(NaturesCompass.MODID, "teleport"); +public static final CustomPayload.Id PACKET_ID = new CustomPayload.Id<>(new Identifier(NaturesCompass.MODID, "teleport")); + + public static final PacketCodec PACKET_CODEC = PacketCodec.of(TeleportPacket::write, TeleportPacket::read); - public TeleportPacket() { - super(Unpooled.buffer()); + public static TeleportPacket read(RegistryByteBuf buf) { + return new TeleportPacket(); + } + + public void write(RegistryByteBuf buf) { } - public static void apply(MinecraftServer server, ServerPlayerEntity player, ServerPlayNetworkHandler handler, PacketByteBuf buf, PacketSender responseSender) { - server.execute(() -> { - final ItemStack stack = ItemUtils.getHeldNatureCompass(player); + public static void apply(TeleportPacket packet, ServerPlayNetworking.Context context) { + context.player().getServer().execute(() -> { + final ItemStack stack = ItemUtils.getHeldNatureCompass(context.player()); if (!stack.isEmpty()) { final NaturesCompassItem natureCompass = (NaturesCompassItem) stack.getItem(); - if (NaturesCompassConfig.allowTeleport && PlayerUtils.canTeleport(player)) { + if (NaturesCompassConfig.allowTeleport && PlayerUtils.canTeleport(context.player())) { if (natureCompass.getState(stack) == CompassState.FOUND) { final int x = natureCompass.getFoundBiomeX(stack); final int z = natureCompass.getFoundBiomeZ(stack); - final int y = findValidTeleportHeight(player.getEntityWorld(), x, z); + final int y = findValidTeleportHeight(context.player().getEntityWorld(), x, z); - player.stopRiding(); - ((ServerPlayerEntity) player).networkHandler.requestTeleport(x, y, z, player.getYaw(), player.getPitch(), Collections.emptySet()); + context.player().stopRiding(); + context.player().networkHandler.requestTeleport(x, y, z, context.player().getYaw(), context.player().getPitch(), Collections.emptySet()); - if (!player.isFallFlying()) { - player.setVelocity(player.getVelocity().getX(), 0, player.getVelocity().getZ()); - player.setOnGround(true); + if (!context.player().isFallFlying()) { + context.player().setVelocity(context.player().getVelocity().getX(), 0, context.player().getVelocity().getZ()); + context.player().setOnGround(true); } } } else { - NaturesCompass.LOGGER.warn("Player " + player.getDisplayName().getString() + " tried to teleport but does not have permission."); + NaturesCompass.LOGGER.warn("Player " + context.player().getDisplayName().getString() + " tried to teleport but does not have permission."); } } }); } + + @Override + public Id getId() { + return PACKET_ID; + } private static int findValidTeleportHeight(World world, int x, int z) { int upY = world.getSeaLevel(); diff --git a/src/main/java/com/chaosthedude/naturescompass/utils/ItemUtils.java b/src/main/java/com/chaosthedude/naturescompass/utils/ItemUtils.java index 1ea1e89..8630c48 100644 --- a/src/main/java/com/chaosthedude/naturescompass/utils/ItemUtils.java +++ b/src/main/java/com/chaosthedude/naturescompass/utils/ItemUtils.java @@ -5,18 +5,11 @@ import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NbtCompound; public class ItemUtils { - public static boolean verifyNBT(ItemStack stack) { - if (stack.isEmpty() || stack.getItem() != NaturesCompass.NATURES_COMPASS_ITEM) { - return false; - } else if (!stack.hasNbt()) { - stack.setNbt(new NbtCompound()); - } - - return true; + public static boolean isCompass(ItemStack stack) { + return !stack.isEmpty() || stack.getItem() == NaturesCompass.NATURES_COMPASS_ITEM; } public static ItemStack getHeldNatureCompass(PlayerEntity player) { diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 3d79fbe..1445e29 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -31,9 +31,9 @@ "accessWidener" : "naturescompass.accesswidener", "depends": { "fabricloader": ">=0.15.0", - "fabric": "*", - "minecraft": "~1.20.4", - "java": ">=17" + "fabric-api": "*", + "minecraft": "~1.20.5", + "java": ">=21" }, "suggests": { "explorerscompass": "*" diff --git a/src/main/resources/naturescompass.mixins.json b/src/main/resources/naturescompass.mixins.json index a604f00..42550a2 100644 --- a/src/main/resources/naturescompass.mixins.json +++ b/src/main/resources/naturescompass.mixins.json @@ -1,7 +1,7 @@ { "required": true, "package": "com.chaosthedude.naturescompass.mixins", - "compatibilityLevel": "JAVA_8", + "compatibilityLevel": "JAVA_21", "mixins": [ "ServerMixin" ],