diff --git a/core/src/main/java/org/geysermc/geyser/inventory/GeyserItemStack.java b/core/src/main/java/org/geysermc/geyser/inventory/GeyserItemStack.java index a67516c5c..4ff8db9f0 100644 --- a/core/src/main/java/org/geysermc/geyser/inventory/GeyserItemStack.java +++ b/core/src/main/java/org/geysermc/geyser/inventory/GeyserItemStack.java @@ -34,6 +34,7 @@ import lombok.Getter; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; import org.cloudburstmc.protocol.bedrock.data.inventory.ItemData; +import org.geysermc.geyser.item.Items; import org.geysermc.geyser.item.type.Item; import org.geysermc.geyser.registry.Registries; import org.geysermc.geyser.registry.type.ItemMapping; @@ -42,7 +43,7 @@ import org.geysermc.geyser.translator.inventory.item.ItemTranslator; @Data public class GeyserItemStack { - public static final GeyserItemStack EMPTY = new GeyserItemStack(0, 0, null); + public static final GeyserItemStack EMPTY = new GeyserItemStack(Items.AIR_ID, 0, null); private final int javaId; private int amount; @@ -64,7 +65,7 @@ public class GeyserItemStack { this.netId = netId; } - public static @NonNull GeyserItemStack from(ItemStack itemStack) { + public static @NonNull GeyserItemStack from(@Nullable ItemStack itemStack) { return itemStack == null ? EMPTY : new GeyserItemStack(itemStack.getId(), itemStack.getAmount(), itemStack.getNbt()); } @@ -122,7 +123,7 @@ public class GeyserItemStack { } public boolean isEmpty() { - return amount <= 0 || javaId == 0; + return amount <= 0 || javaId == Items.AIR_ID; } public GeyserItemStack copy() { diff --git a/core/src/main/java/org/geysermc/geyser/inventory/recipe/GeyserShapedRecipe.java b/core/src/main/java/org/geysermc/geyser/inventory/recipe/GeyserShapedRecipe.java index d61945ad8..05c17cf9f 100644 --- a/core/src/main/java/org/geysermc/geyser/inventory/recipe/GeyserShapedRecipe.java +++ b/core/src/main/java/org/geysermc/geyser/inventory/recipe/GeyserShapedRecipe.java @@ -28,8 +28,9 @@ package org.geysermc.geyser.inventory.recipe; import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack; import com.github.steveice10.mc.protocol.data.game.recipe.Ingredient; import com.github.steveice10.mc.protocol.data.game.recipe.data.ShapedRecipeData; +import org.checkerframework.checker.nullness.qual.Nullable; -public record GeyserShapedRecipe(int width, int height, Ingredient[] ingredients, ItemStack result) implements GeyserRecipe { +public record GeyserShapedRecipe(int width, int height, Ingredient[] ingredients, @Nullable ItemStack result) implements GeyserRecipe { public GeyserShapedRecipe(ShapedRecipeData data) { this(data.getWidth(), data.getHeight(), data.getIngredients(), data.getResult()); diff --git a/core/src/main/java/org/geysermc/geyser/inventory/recipe/GeyserShapelessRecipe.java b/core/src/main/java/org/geysermc/geyser/inventory/recipe/GeyserShapelessRecipe.java index 6c7665bbb..e300e3ec8 100644 --- a/core/src/main/java/org/geysermc/geyser/inventory/recipe/GeyserShapelessRecipe.java +++ b/core/src/main/java/org/geysermc/geyser/inventory/recipe/GeyserShapelessRecipe.java @@ -28,8 +28,9 @@ package org.geysermc.geyser.inventory.recipe; import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack; import com.github.steveice10.mc.protocol.data.game.recipe.Ingredient; import com.github.steveice10.mc.protocol.data.game.recipe.data.ShapelessRecipeData; +import org.checkerframework.checker.nullness.qual.Nullable; -public record GeyserShapelessRecipe(Ingredient[] ingredients, ItemStack result) implements GeyserRecipe { +public record GeyserShapelessRecipe(Ingredient[] ingredients, @Nullable ItemStack result) implements GeyserRecipe { public GeyserShapelessRecipe(ShapelessRecipeData data) { this(data.getIngredients(), data.getResult()); diff --git a/core/src/main/java/org/geysermc/geyser/inventory/recipe/GeyserStonecutterData.java b/core/src/main/java/org/geysermc/geyser/inventory/recipe/GeyserStonecutterData.java index 04a772c31..22163eced 100644 --- a/core/src/main/java/org/geysermc/geyser/inventory/recipe/GeyserStonecutterData.java +++ b/core/src/main/java/org/geysermc/geyser/inventory/recipe/GeyserStonecutterData.java @@ -26,10 +26,11 @@ package org.geysermc.geyser.inventory.recipe; import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack; +import org.checkerframework.checker.nullness.qual.Nullable; /** * @param buttonId the button that needs to be pressed for Java Edition to accept this item. * @param output the expected output of this item when cut. */ -public record GeyserStonecutterData(int buttonId, ItemStack output) { +public record GeyserStonecutterData(int buttonId, @Nullable ItemStack output) { } diff --git a/core/src/main/java/org/geysermc/geyser/item/Items.java b/core/src/main/java/org/geysermc/geyser/item/Items.java index 92c446f68..e84315fd8 100644 --- a/core/src/main/java/org/geysermc/geyser/item/Items.java +++ b/core/src/main/java/org/geysermc/geyser/item/Items.java @@ -1351,6 +1351,8 @@ public final class Items { public static final Item TRIAL_SPAWNER = register(new BlockItem("trial_spawner", builder())); public static final Item TRIAL_KEY = register(new Item("trial_key", builder())); + public static final int AIR_ID = AIR.javaId(); + private static T register(T item) { return register(item, Registries.JAVA_ITEMS.get().size()); } diff --git a/core/src/main/java/org/geysermc/geyser/item/type/Item.java b/core/src/main/java/org/geysermc/geyser/item/type/Item.java index a7a59f6af..94bb3324e 100644 --- a/core/src/main/java/org/geysermc/geyser/item/type/Item.java +++ b/core/src/main/java/org/geysermc/geyser/item/type/Item.java @@ -40,6 +40,7 @@ import org.geysermc.geyser.text.ChatColor; import org.geysermc.geyser.text.MinecraftLocale; import org.geysermc.geyser.translator.inventory.item.ItemTranslator; import org.geysermc.geyser.translator.text.MessageTranslator; +import org.geysermc.geyser.util.InventoryUtils; import java.util.ArrayList; import java.util.List; @@ -82,7 +83,7 @@ public class Item { /* Translation methods to Bedrock and back */ public ItemData.Builder translateToBedrock(ItemStack itemStack, ItemMapping mapping, ItemMappings mappings) { - if (itemStack == null) { + if (InventoryUtils.isEmpty(itemStack)) { // Return, essentially, air return ItemData.builder(); } diff --git a/core/src/main/java/org/geysermc/geyser/registry/type/ItemMappings.java b/core/src/main/java/org/geysermc/geyser/registry/type/ItemMappings.java index 704d5c211..40359b437 100644 --- a/core/src/main/java/org/geysermc/geyser/registry/type/ItemMappings.java +++ b/core/src/main/java/org/geysermc/geyser/registry/type/ItemMappings.java @@ -83,7 +83,7 @@ public class ItemMappings implements DefinitionRegistry { * @return an item entry from the given java edition identifier */ @NonNull - public ItemMapping getMapping(ItemStack itemStack) { + public ItemMapping getMapping(@NonNull ItemStack itemStack) { return this.getMapping(itemStack.getId()); } diff --git a/core/src/main/java/org/geysermc/geyser/translator/inventory/item/ItemTranslator.java b/core/src/main/java/org/geysermc/geyser/translator/inventory/item/ItemTranslator.java index 856b22c9b..d1a256551 100644 --- a/core/src/main/java/org/geysermc/geyser/translator/inventory/item/ItemTranslator.java +++ b/core/src/main/java/org/geysermc/geyser/translator/inventory/item/ItemTranslator.java @@ -67,6 +67,7 @@ import org.geysermc.geyser.registry.Registries; import org.geysermc.geyser.text.ChatColor; import org.geysermc.geyser.text.MinecraftLocale; import org.geysermc.geyser.translator.text.MessageTranslator; +import org.geysermc.geyser.util.InventoryUtils; import java.text.DecimalFormat; import java.util.*; @@ -89,7 +90,7 @@ public final class ItemTranslator { */ public static ItemStack translateToJava(ItemData data, ItemMappings mappings) { if (data == null) { - return new ItemStack(0); + return new ItemStack(Items.AIR_ID); } ItemMapping bedrockItem = mappings.getMapping(data); @@ -118,7 +119,7 @@ public final class ItemTranslator { @NonNull public static ItemData translateToBedrock(GeyserSession session, ItemStack stack) { - if (stack == null) { + if (InventoryUtils.isEmpty(stack)) { return ItemData.AIR; } diff --git a/core/src/main/java/org/geysermc/geyser/translator/protocol/bedrock/BedrockBlockPickRequestTranslator.java b/core/src/main/java/org/geysermc/geyser/translator/protocol/bedrock/BedrockBlockPickRequestTranslator.java index dd1a259fd..381adf2b7 100644 --- a/core/src/main/java/org/geysermc/geyser/translator/protocol/bedrock/BedrockBlockPickRequestTranslator.java +++ b/core/src/main/java/org/geysermc/geyser/translator/protocol/bedrock/BedrockBlockPickRequestTranslator.java @@ -56,7 +56,7 @@ public class BedrockBlockPickRequestTranslator extends PacketTranslator true") public static boolean isEmpty(@Nullable ItemStack itemStack) { - return itemStack == null || itemStack.getId() == Items.AIR.javaId() || itemStack.getAmount() <= 0; + return itemStack == null || itemStack.getId() == Items.AIR_ID || itemStack.getAmount() <= 0; } /** @@ -233,11 +235,10 @@ public class InventoryUtils { * @param itemStack the item to try to find a match for. NBT will also be accounted for. */ public static void findOrCreateItem(GeyserSession session, ItemStack itemStack) { - PlayerInventory inventory = session.getPlayerInventory(); - - if (itemStack == null || itemStack.getId() == 0) { + if (isEmpty(itemStack)) { return; } + PlayerInventory inventory = session.getPlayerInventory(); // Check hotbar for item for (int i = 36; i < 45; i++) { diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 22332880a..faacaca2a 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -14,7 +14,7 @@ protocol-connection = "3.0.0.Beta1-20231107.190703-112" raknet = "1.0.0.CR1-20230703.195238-9" blockstateupdater="1.20.50-20231106.161340-1" mcauthlib = "d9d773e" -mcprotocollib = "00bce42" +mcprotocollib = "c6ba2fa" adventure = "4.14.0" adventure-platform = "4.3.0" junit = "5.9.2"