From 0b4fc87017d20de2e3443d16e53427d5b24dc102 Mon Sep 17 00:00:00 2001 From: NoJokeFNA Date: Thu, 12 May 2022 02:32:41 +0200 Subject: [PATCH 1/4] feat: added WrapperPlayServerUnlockRecipes --- .../protocol/recipe/UnlockRecipesType.java | 13 + .../recipe/data/MerchantRecipeData.java | 2 +- .../WrapperPlayServerUnlockRecipes.java | 309 ++++++++++++++++++ 3 files changed, 323 insertions(+), 1 deletion(-) create mode 100644 api/src/main/java/com/github/retrooper/packetevents/protocol/recipe/UnlockRecipesType.java create mode 100644 api/src/main/java/com/github/retrooper/packetevents/wrapper/play/server/WrapperPlayServerUnlockRecipes.java diff --git a/api/src/main/java/com/github/retrooper/packetevents/protocol/recipe/UnlockRecipesType.java b/api/src/main/java/com/github/retrooper/packetevents/protocol/recipe/UnlockRecipesType.java new file mode 100644 index 0000000000..f4d4bdf95a --- /dev/null +++ b/api/src/main/java/com/github/retrooper/packetevents/protocol/recipe/UnlockRecipesType.java @@ -0,0 +1,13 @@ +package com.github.retrooper.packetevents.protocol.recipe; + +public enum UnlockRecipesType { + INIT, + ADD, + REMOVE; + + private static final UnlockRecipesType[] VALUES = values(); + + public static UnlockRecipesType getById(int index) { + return VALUES[index]; + } +} diff --git a/api/src/main/java/com/github/retrooper/packetevents/protocol/recipe/data/MerchantRecipeData.java b/api/src/main/java/com/github/retrooper/packetevents/protocol/recipe/data/MerchantRecipeData.java index 58af7055b6..c1ee2af9d2 100644 --- a/api/src/main/java/com/github/retrooper/packetevents/protocol/recipe/data/MerchantRecipeData.java +++ b/api/src/main/java/com/github/retrooper/packetevents/protocol/recipe/data/MerchantRecipeData.java @@ -2,7 +2,7 @@ import com.github.retrooper.packetevents.protocol.item.ItemStack; -public class MerchantRecipeData implements RecipeData{ +public class MerchantRecipeData implements RecipeData { private ItemStack buyItem1; private ItemStack buyItem2; diff --git a/api/src/main/java/com/github/retrooper/packetevents/wrapper/play/server/WrapperPlayServerUnlockRecipes.java b/api/src/main/java/com/github/retrooper/packetevents/wrapper/play/server/WrapperPlayServerUnlockRecipes.java new file mode 100644 index 0000000000..cdc8ece44f --- /dev/null +++ b/api/src/main/java/com/github/retrooper/packetevents/wrapper/play/server/WrapperPlayServerUnlockRecipes.java @@ -0,0 +1,309 @@ +/* + * This file is part of packetevents - https://github.com/retrooper/packetevents + * Copyright (C) 2021 retrooper and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.github.retrooper.packetevents.wrapper.play.server; + +import com.github.retrooper.packetevents.event.PacketSendEvent; +import com.github.retrooper.packetevents.manager.server.ServerVersion; +import com.github.retrooper.packetevents.protocol.packettype.PacketType; +import com.github.retrooper.packetevents.protocol.recipe.UnlockRecipesType; +import com.github.retrooper.packetevents.wrapper.PacketWrapper; +import org.jetbrains.annotations.Nullable; + +import java.util.Arrays; + +public class WrapperPlayServerUnlockRecipes extends PacketWrapper { + private UnlockRecipesType type; + private boolean craftingRecipeBookOpen; + private boolean craftingRecipeBookFilterActive; + private boolean smeltingRecipeBookOpen; + private boolean smeltingRecipeBookFilterActive; + private boolean blastFurnaceRecipeBookOpen; + private boolean blastFurnaceRecipeBookFilterActive; + private boolean smokerRecipeBookOpen; + private boolean smokerRecipeBookFilterActive; + private int elements; + private int @Nullable [] recipeIdsLegacy; + private @Nullable String[] recipeIdsModern; + private int elementsInit; + private int @Nullable [] recipeIdsLegacyInit; + private @Nullable String[] recipeIdsModernInit; + + public WrapperPlayServerUnlockRecipes(PacketSendEvent event) { + super(event); + } + + public WrapperPlayServerUnlockRecipes(UnlockRecipesType type, boolean craftingRecipeBookOpen, boolean craftingRecipeBookFilterActive, int elements, + int @Nullable [] recipeIdsLegacy, int elementsInit, int @Nullable [] recipeIdsLegacyInit) { + super(PacketType.Play.Server.UNLOCK_RECIPES); + this.type = type; + this.craftingRecipeBookOpen = craftingRecipeBookOpen; + this.craftingRecipeBookFilterActive = craftingRecipeBookFilterActive; + this.elements = elements; + this.recipeIdsLegacy = recipeIdsLegacy; + this.elementsInit = elementsInit; + this.recipeIdsLegacyInit = recipeIdsLegacyInit; + } + + public WrapperPlayServerUnlockRecipes(UnlockRecipesType type, boolean craftingRecipeBookOpen, boolean craftingRecipeBookFilterActive, boolean smeltingRecipeBookOpen, + boolean smeltingRecipeBookFilterActive, int elements, String[] recipeIdsModern, int elementsInit, + String[] recipeIdsModernInit) { + super(PacketType.Play.Server.UNLOCK_RECIPES); + this.type = type; + this.craftingRecipeBookOpen = craftingRecipeBookOpen; + this.craftingRecipeBookFilterActive = craftingRecipeBookFilterActive; + this.smeltingRecipeBookOpen = smeltingRecipeBookOpen; + this.smeltingRecipeBookFilterActive = smeltingRecipeBookFilterActive; + this.elements = elements; + this.recipeIdsModern = recipeIdsModern; + this.elementsInit = elementsInit; + this.recipeIdsModernInit = recipeIdsModernInit; + } + + public WrapperPlayServerUnlockRecipes(UnlockRecipesType type, boolean craftingRecipeBookOpen, boolean craftingRecipeBookFilterActive, boolean smeltingRecipeBookOpen, + boolean smeltingRecipeBookFilterActive, boolean blastFurnaceRecipeBookOpen, boolean blastFurnaceRecipeBookFilterActive, + boolean smokerRecipeBookOpen, boolean smokerRecipeBookFilterActive, int elements, String[] recipeIdsModern, + int elementsInit, String[] recipeIdsModernInit) { + super(PacketType.Play.Server.UNLOCK_RECIPES); + this.type = type; + this.craftingRecipeBookOpen = craftingRecipeBookOpen; + this.craftingRecipeBookFilterActive = craftingRecipeBookFilterActive; + this.smeltingRecipeBookOpen = smeltingRecipeBookOpen; + this.smeltingRecipeBookFilterActive = smeltingRecipeBookFilterActive; + this.blastFurnaceRecipeBookOpen = blastFurnaceRecipeBookOpen; + this.blastFurnaceRecipeBookFilterActive = blastFurnaceRecipeBookFilterActive; + this.smokerRecipeBookOpen = smokerRecipeBookOpen; + this.smokerRecipeBookFilterActive = smokerRecipeBookFilterActive; + this.elements = elements; + this.recipeIdsModern = recipeIdsModern; + this.elementsInit = elementsInit; + this.recipeIdsModernInit = recipeIdsModernInit; + } + + @Override + public void read() { + if (serverVersion.isNewerThanOrEquals(ServerVersion.V_1_12)) { + this.type = UnlockRecipesType.getById(readVarInt()); + } else { + this.type = UnlockRecipesType.getById(readShort()); + } + this.craftingRecipeBookOpen = readBoolean(); + this.craftingRecipeBookFilterActive = readBoolean(); + if (serverVersion.isNewerThanOrEquals(ServerVersion.V_1_13)) { + this.smeltingRecipeBookOpen = readBoolean(); + this.smeltingRecipeBookFilterActive = readBoolean(); + } + if (serverVersion.isNewerThanOrEquals(ServerVersion.V_1_16_2)) { + this.blastFurnaceRecipeBookOpen = readBoolean(); + this.blastFurnaceRecipeBookFilterActive = readBoolean(); + this.smokerRecipeBookOpen = readBoolean(); + this.smokerRecipeBookFilterActive = readBoolean(); + } + this.elements = readVarInt(); + if (serverVersion.isNewerThanOrEquals(ServerVersion.V_1_13)) { + this.recipeIdsModern = new String[this.elements]; + for (int i = 0; i < this.elements; i++) { + this.recipeIdsModern[i] = readString(); + } + } else { + this.recipeIdsLegacy = new int[this.elements]; + for (int i = 0; i < this.elements; i++) { + this.recipeIdsLegacy[i] = readVarInt(); + } + } + + if (type == UnlockRecipesType.INIT) { + this.elementsInit = readVarInt(); + if (serverVersion.isNewerThanOrEquals(ServerVersion.V_1_13)) { + this.recipeIdsModernInit = new String[this.elementsInit]; + for (int i = 0; i < this.elementsInit; i++) { + this.recipeIdsModernInit[i] = readString(); + } + } else { + this.recipeIdsLegacyInit = new int[this.elementsInit]; + for (int i = 0; i < this.elementsInit; i++) { + this.recipeIdsLegacyInit[i] = readVarInt(); + } + } + } + } + + @Override + public void write() { + if (serverVersion.isNewerThanOrEquals(ServerVersion.V_1_12)) { + writeVarInt(this.type.ordinal()); + } else { + writeShort(this.type.ordinal()); + } + writeBoolean(this.craftingRecipeBookOpen); + writeBoolean(this.craftingRecipeBookFilterActive); + if (serverVersion.isNewerThanOrEquals(ServerVersion.V_1_13)) { + writeBoolean(this.smeltingRecipeBookOpen); + writeBoolean(this.smeltingRecipeBookFilterActive); + } + if (serverVersion.isNewerThanOrEquals(ServerVersion.V_1_16_2)) { + writeBoolean(this.blastFurnaceRecipeBookOpen); + writeBoolean(this.blastFurnaceRecipeBookFilterActive); + writeBoolean(this.smokerRecipeBookOpen); + writeBoolean(this.smokerRecipeBookFilterActive); + } + writeVarInt(this.elements); + if (serverVersion.isNewerThanOrEquals(ServerVersion.V_1_13)) { + Arrays.stream(this.recipeIdsModern).forEach(this::writeString); + } else { + Arrays.stream(this.recipeIdsLegacy).forEach(this::writeVarInt); + } + if (type == UnlockRecipesType.INIT) { + writeVarInt(this.elementsInit); + if (serverVersion.isNewerThanOrEquals(ServerVersion.V_1_13)) { + Arrays.stream(this.recipeIdsModernInit).forEach(this::writeString); + } else { + Arrays.stream(this.recipeIdsLegacyInit).forEach(this::writeVarInt); + } + } + } + + @Override + public void copy(WrapperPlayServerUnlockRecipes wrapper) { + this.type = wrapper.type; + this.craftingRecipeBookOpen = wrapper.craftingRecipeBookOpen; + this.craftingRecipeBookFilterActive = wrapper.craftingRecipeBookFilterActive; + this.smeltingRecipeBookOpen = wrapper.smeltingRecipeBookOpen; + this.smeltingRecipeBookFilterActive = wrapper.smeltingRecipeBookFilterActive; + this.blastFurnaceRecipeBookOpen = wrapper.blastFurnaceRecipeBookOpen; + this.blastFurnaceRecipeBookFilterActive = wrapper.blastFurnaceRecipeBookFilterActive; + this.smokerRecipeBookOpen = wrapper.smokerRecipeBookOpen; + this.smokerRecipeBookFilterActive = wrapper.smokerRecipeBookFilterActive; + this.elements = wrapper.elements; + this.recipeIdsModern = wrapper.recipeIdsModern; + this.recipeIdsLegacy = wrapper.recipeIdsLegacy; + this.elementsInit = wrapper.elementsInit; + } + + public UnlockRecipesType getType() { + return type; + } + + public void setType(UnlockRecipesType type) { + this.type = type; + } + + public boolean isCraftingRecipeBookOpen() { + return craftingRecipeBookOpen; + } + + public void setCraftingRecipeBookOpen(boolean craftingRecipeBookOpen) { + this.craftingRecipeBookOpen = craftingRecipeBookOpen; + } + + public boolean isCraftingRecipeBookFilterActive() { + return craftingRecipeBookFilterActive; + } + + public void setCraftingRecipeBookFilterActive(boolean craftingRecipeBookFilterActive) { + this.craftingRecipeBookFilterActive = craftingRecipeBookFilterActive; + } + + public boolean isSmeltingRecipeBookOpen() { + return smeltingRecipeBookOpen; + } + + public void setSmeltingRecipeBookOpen(boolean smeltingRecipeBookOpen) { + this.smeltingRecipeBookOpen = smeltingRecipeBookOpen; + } + + public boolean isSmeltingRecipeBookFilterActive() { + return smeltingRecipeBookFilterActive; + } + + public void setSmeltingRecipeBookFilterActive(boolean smeltingRecipeBookFilterActive) { + this.smeltingRecipeBookFilterActive = smeltingRecipeBookFilterActive; + } + + public boolean isBlastFurnaceRecipeBookOpen() { + return blastFurnaceRecipeBookOpen; + } + + public void setBlastFurnaceRecipeBookOpen(boolean blastFurnaceRecipeBookOpen) { + this.blastFurnaceRecipeBookOpen = blastFurnaceRecipeBookOpen; + } + + public boolean isBlastFurnaceRecipeBookFilterActive() { + return blastFurnaceRecipeBookFilterActive; + } + + public void setBlastFurnaceRecipeBookFilterActive(boolean blastFurnaceRecipeBookFilterActive) { + this.blastFurnaceRecipeBookFilterActive = blastFurnaceRecipeBookFilterActive; + } + + public boolean isSmokerRecipeBookOpen() { + return smokerRecipeBookOpen; + } + + public void setSmokerRecipeBookOpen(boolean smokerRecipeBookOpen) { + this.smokerRecipeBookOpen = smokerRecipeBookOpen; + } + + public boolean isSmokerRecipeBookFilterActive() { + return smokerRecipeBookFilterActive; + } + + public void setSmokerRecipeBookFilterActive(boolean smokerRecipeBookFilterActive) { + this.smokerRecipeBookFilterActive = smokerRecipeBookFilterActive; + } + + public int getElements() { + return elements; + } + + public void setElements(int elements) { + this.elements = elements; + } + + public T getRecipeIds() { + return (T) (serverVersion.isNewerThanOrEquals(ServerVersion.V_1_13) ? recipeIdsModern : recipeIdsLegacy); + } + + public void setRecipeIds(T recipeIds) { + if (serverVersion.isNewerThanOrEquals(ServerVersion.V_1_13)) { + this.recipeIdsModern = (String[]) recipeIds; + } else { + this.recipeIdsLegacy = (int[]) recipeIds; + } + } + + public int getElementsInit() { + return elementsInit; + } + + public void setElementsInit(int elementsInit) { + this.elementsInit = elementsInit; + } + + public T getRecipeIdsInit() { + return (T) (serverVersion.isNewerThanOrEquals(ServerVersion.V_1_13) ? recipeIdsModernInit : recipeIdsLegacyInit); + } + + public void setRecipeIdsInit(T recipeIds) { + if (serverVersion.isNewerThanOrEquals(ServerVersion.V_1_13)) { + this.recipeIdsModernInit = (String[]) recipeIds; + } else { + this.recipeIdsLegacyInit = (int[]) recipeIds; + } + } +} From e021f9027965bf2c51440f7a063388a81612f060 Mon Sep 17 00:00:00 2001 From: Julian <53055251+NoJokeFNA@users.noreply.github.com> Date: Mon, 20 Jun 2022 00:18:36 +0200 Subject: [PATCH 2/4] change: changed String to Identifier --- .../WrapperPlayServerUnlockRecipes.java | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/api/src/main/java/com/github/retrooper/packetevents/wrapper/play/server/WrapperPlayServerUnlockRecipes.java b/api/src/main/java/com/github/retrooper/packetevents/wrapper/play/server/WrapperPlayServerUnlockRecipes.java index cdc8ece44f..0fca6e8131 100644 --- a/api/src/main/java/com/github/retrooper/packetevents/wrapper/play/server/WrapperPlayServerUnlockRecipes.java +++ b/api/src/main/java/com/github/retrooper/packetevents/wrapper/play/server/WrapperPlayServerUnlockRecipes.java @@ -22,6 +22,7 @@ import com.github.retrooper.packetevents.manager.server.ServerVersion; import com.github.retrooper.packetevents.protocol.packettype.PacketType; import com.github.retrooper.packetevents.protocol.recipe.UnlockRecipesType; +import com.github.retrooper.packetevents.resources.ResourceLocation; import com.github.retrooper.packetevents.wrapper.PacketWrapper; import org.jetbrains.annotations.Nullable; @@ -39,10 +40,10 @@ public class WrapperPlayServerUnlockRecipes extends PacketWrapper T getRecipeIds() { public void setRecipeIds(T recipeIds) { if (serverVersion.isNewerThanOrEquals(ServerVersion.V_1_13)) { - this.recipeIdsModern = (String[]) recipeIds; + this.recipeIdsModern = (ResourceLocation[]) recipeIds; } else { this.recipeIdsLegacy = (int[]) recipeIds; } @@ -301,7 +302,7 @@ public T getRecipeIdsInit() { public void setRecipeIdsInit(T recipeIds) { if (serverVersion.isNewerThanOrEquals(ServerVersion.V_1_13)) { - this.recipeIdsModernInit = (String[]) recipeIds; + this.recipeIdsModernInit = (ResourceLocation[]) recipeIds; } else { this.recipeIdsLegacyInit = (int[]) recipeIds; } From a5404ce7d0e7e32b9677e0c735b07e2ff96abe9b Mon Sep 17 00:00:00 2001 From: NoJokeFNA Date: Mon, 29 Jan 2024 13:04:30 +0100 Subject: [PATCH 3/4] change: changed handling of recipeIds --- .../protocol/recipe/RecipeId.java | 13 +++ .../WrapperPlayServerUnlockRecipes.java | 103 ++++++------------ 2 files changed, 49 insertions(+), 67 deletions(-) create mode 100644 api/src/main/java/com/github/retrooper/packetevents/protocol/recipe/RecipeId.java diff --git a/api/src/main/java/com/github/retrooper/packetevents/protocol/recipe/RecipeId.java b/api/src/main/java/com/github/retrooper/packetevents/protocol/recipe/RecipeId.java new file mode 100644 index 0000000000..6552429e2c --- /dev/null +++ b/api/src/main/java/com/github/retrooper/packetevents/protocol/recipe/RecipeId.java @@ -0,0 +1,13 @@ +package com.github.retrooper.packetevents.protocol.recipe; + +public class RecipeId { + private final T id; + + public RecipeId(T id) { + this.id = id; + } + + public T getId() { + return id; + } +} diff --git a/api/src/main/java/com/github/retrooper/packetevents/wrapper/play/server/WrapperPlayServerUnlockRecipes.java b/api/src/main/java/com/github/retrooper/packetevents/wrapper/play/server/WrapperPlayServerUnlockRecipes.java index 0fca6e8131..f91460dcdb 100644 --- a/api/src/main/java/com/github/retrooper/packetevents/wrapper/play/server/WrapperPlayServerUnlockRecipes.java +++ b/api/src/main/java/com/github/retrooper/packetevents/wrapper/play/server/WrapperPlayServerUnlockRecipes.java @@ -21,12 +21,13 @@ import com.github.retrooper.packetevents.event.PacketSendEvent; import com.github.retrooper.packetevents.manager.server.ServerVersion; import com.github.retrooper.packetevents.protocol.packettype.PacketType; +import com.github.retrooper.packetevents.protocol.recipe.RecipeId; import com.github.retrooper.packetevents.protocol.recipe.UnlockRecipesType; import com.github.retrooper.packetevents.resources.ResourceLocation; import com.github.retrooper.packetevents.wrapper.PacketWrapper; import org.jetbrains.annotations.Nullable; -import java.util.Arrays; +import java.util.List; public class WrapperPlayServerUnlockRecipes extends PacketWrapper { private UnlockRecipesType type; @@ -39,47 +40,30 @@ public class WrapperPlayServerUnlockRecipes extends PacketWrapper> recipeIds; private int elementsInit; - private int @Nullable [] recipeIdsLegacyInit; - private @Nullable ResourceLocation[] recipeIdsModernInit; + private @Nullable List> recipeIdsInit; public WrapperPlayServerUnlockRecipes(PacketSendEvent event) { super(event); } public WrapperPlayServerUnlockRecipes(UnlockRecipesType type, boolean craftingRecipeBookOpen, boolean craftingRecipeBookFilterActive, int elements, - int @Nullable [] recipeIdsLegacy, int elementsInit, int @Nullable [] recipeIdsLegacyInit) { + @Nullable List> recipeIds, int elementsInit, @Nullable List> recipeIdsInit) { super(PacketType.Play.Server.UNLOCK_RECIPES); this.type = type; this.craftingRecipeBookOpen = craftingRecipeBookOpen; this.craftingRecipeBookFilterActive = craftingRecipeBookFilterActive; this.elements = elements; - this.recipeIdsLegacy = recipeIdsLegacy; + this.recipeIds = recipeIds; this.elementsInit = elementsInit; - this.recipeIdsLegacyInit = recipeIdsLegacyInit; - } - - public WrapperPlayServerUnlockRecipes(UnlockRecipesType type, boolean craftingRecipeBookOpen, boolean craftingRecipeBookFilterActive, boolean smeltingRecipeBookOpen, - boolean smeltingRecipeBookFilterActive, int elements, ResourceLocation[] recipeIdsModern, int elementsInit, - ResourceLocation[] recipeIdsModernInit) { - super(PacketType.Play.Server.UNLOCK_RECIPES); - this.type = type; - this.craftingRecipeBookOpen = craftingRecipeBookOpen; - this.craftingRecipeBookFilterActive = craftingRecipeBookFilterActive; - this.smeltingRecipeBookOpen = smeltingRecipeBookOpen; - this.smeltingRecipeBookFilterActive = smeltingRecipeBookFilterActive; - this.elements = elements; - this.recipeIdsModern = recipeIdsModern; - this.elementsInit = elementsInit; - this.recipeIdsModernInit = recipeIdsModernInit; + this.recipeIdsInit = recipeIdsInit; } public WrapperPlayServerUnlockRecipes(UnlockRecipesType type, boolean craftingRecipeBookOpen, boolean craftingRecipeBookFilterActive, boolean smeltingRecipeBookOpen, boolean smeltingRecipeBookFilterActive, boolean blastFurnaceRecipeBookOpen, boolean blastFurnaceRecipeBookFilterActive, - boolean smokerRecipeBookOpen, boolean smokerRecipeBookFilterActive, int elements, ResourceLocation[] recipeIdsModern, - int elementsInit, ResourceLocation[] recipeIdsModernInit) { + boolean smokerRecipeBookOpen, boolean smokerRecipeBookFilterActive, int elements, @Nullable List> recipeIds, + int elementsInit, @Nullable List> recipeIdsInit) { super(PacketType.Play.Server.UNLOCK_RECIPES); this.type = type; this.craftingRecipeBookOpen = craftingRecipeBookOpen; @@ -91,9 +75,9 @@ public WrapperPlayServerUnlockRecipes(UnlockRecipesType type, boolean craftingRe this.smokerRecipeBookOpen = smokerRecipeBookOpen; this.smokerRecipeBookFilterActive = smokerRecipeBookFilterActive; this.elements = elements; - this.recipeIdsModern = recipeIdsModern; + this.recipeIds = recipeIds; this.elementsInit = elementsInit; - this.recipeIdsModernInit = recipeIdsModernInit; + this.recipeIdsInit = recipeIdsInit; } @Override @@ -116,29 +100,22 @@ public void read() { this.smokerRecipeBookFilterActive = readBoolean(); } this.elements = readVarInt(); - if (serverVersion.isNewerThanOrEquals(ServerVersion.V_1_13)) { - this.recipeIdsModern = new ResourceLocation[this.elements]; - for (int i = 0; i < this.elements; i++) { - this.recipeIdsModern[i] = readIdentifier(); - } - } else { - this.recipeIdsLegacy = new int[this.elements]; - for (int i = 0; i < this.elements; i++) { - this.recipeIdsLegacy[i] = readVarInt(); + for (int i = 0; i < this.elements; i++) { + if (serverVersion.isNewerThanOrEquals(ServerVersion.V_1_13)) { + this.recipeIds.add(new RecipeId<>(readIdentifier())); + } else { + this.recipeIds.add(new RecipeId<>(readVarInt())); } } + if (type == UnlockRecipesType.INIT) { this.elementsInit = readVarInt(); - if (serverVersion.isNewerThanOrEquals(ServerVersion.V_1_13)) { - this.recipeIdsModernInit = new ResourceLocation[this.elementsInit]; - for (int i = 0; i < this.elementsInit; i++) { - this.recipeIdsModernInit[i] = readIdentifier(); - } - } else { - this.recipeIdsLegacyInit = new int[this.elementsInit]; - for (int i = 0; i < this.elementsInit; i++) { - this.recipeIdsLegacyInit[i] = readVarInt(); + for (int i = 0; i < this.elementsInit; i++) { + if (serverVersion.isNewerThanOrEquals(ServerVersion.V_1_13)) { + this.recipeIdsInit.add(new RecipeId<>(readIdentifier())); + } else { + this.recipeIdsInit.add(new RecipeId<>(readVarInt())); } } } @@ -165,16 +142,16 @@ public void write() { } writeVarInt(this.elements); if (serverVersion.isNewerThanOrEquals(ServerVersion.V_1_13)) { - Arrays.stream(this.recipeIdsModern).forEach(this::writeIdentifier); + this.recipeIds.forEach(recipeId -> writeIdentifier(new ResourceLocation(recipeId.getId().toString()))); } else { - Arrays.stream(this.recipeIdsLegacy).forEach(this::writeVarInt); + this.recipeIds.forEach(recipeId -> writeVarInt((int) recipeId.getId())); } if (type == UnlockRecipesType.INIT) { writeVarInt(this.elementsInit); if (serverVersion.isNewerThanOrEquals(ServerVersion.V_1_13)) { - Arrays.stream(this.recipeIdsModernInit).forEach(this::writeIdentifier); + this.recipeIdsInit.forEach(recipeId -> writeIdentifier(new ResourceLocation(recipeId.getId().toString()))); } else { - Arrays.stream(this.recipeIdsLegacyInit).forEach(this::writeVarInt); + this.recipeIdsInit.forEach(recipeId -> writeVarInt((int) recipeId.getId())); } } } @@ -191,8 +168,8 @@ public void copy(WrapperPlayServerUnlockRecipes wrapper) { this.smokerRecipeBookOpen = wrapper.smokerRecipeBookOpen; this.smokerRecipeBookFilterActive = wrapper.smokerRecipeBookFilterActive; this.elements = wrapper.elements; - this.recipeIdsModern = wrapper.recipeIdsModern; - this.recipeIdsLegacy = wrapper.recipeIdsLegacy; + this.recipeIds = wrapper.recipeIds; + this.recipeIdsInit = wrapper.recipeIdsInit; this.elementsInit = wrapper.elementsInit; } @@ -276,16 +253,12 @@ public void setElements(int elements) { this.elements = elements; } - public T getRecipeIds() { - return (T) (serverVersion.isNewerThanOrEquals(ServerVersion.V_1_13) ? recipeIdsModern : recipeIdsLegacy); + public List> getRecipeIds() { + return recipeIds; } - public void setRecipeIds(T recipeIds) { - if (serverVersion.isNewerThanOrEquals(ServerVersion.V_1_13)) { - this.recipeIdsModern = (ResourceLocation[]) recipeIds; - } else { - this.recipeIdsLegacy = (int[]) recipeIds; - } + public void setRecipeIds(List> recipeIds) { + this.recipeIds = recipeIds; } public int getElementsInit() { @@ -296,15 +269,11 @@ public void setElementsInit(int elementsInit) { this.elementsInit = elementsInit; } - public T getRecipeIdsInit() { - return (T) (serverVersion.isNewerThanOrEquals(ServerVersion.V_1_13) ? recipeIdsModernInit : recipeIdsLegacyInit); + public List> getRecipeIdsInit() { + return recipeIdsInit; } - public void setRecipeIdsInit(T recipeIds) { - if (serverVersion.isNewerThanOrEquals(ServerVersion.V_1_13)) { - this.recipeIdsModernInit = (ResourceLocation[]) recipeIds; - } else { - this.recipeIdsLegacyInit = (int[]) recipeIds; - } + public void setRecipeIdsInit(List> recipeIdsInit) { + this.recipeIdsInit = recipeIdsInit; } } From 73d75fc3bdef809aedb87c3d6a3c7bc211ae7a61 Mon Sep 17 00:00:00 2001 From: NoJokeFNA Date: Thu, 8 Feb 2024 11:51:27 +0100 Subject: [PATCH 4/4] feat: added little javadocs --- .../WrapperPlayServerUnlockRecipes.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/api/src/main/java/com/github/retrooper/packetevents/wrapper/play/server/WrapperPlayServerUnlockRecipes.java b/api/src/main/java/com/github/retrooper/packetevents/wrapper/play/server/WrapperPlayServerUnlockRecipes.java index f91460dcdb..3df0079e02 100644 --- a/api/src/main/java/com/github/retrooper/packetevents/wrapper/play/server/WrapperPlayServerUnlockRecipes.java +++ b/api/src/main/java/com/github/retrooper/packetevents/wrapper/play/server/WrapperPlayServerUnlockRecipes.java @@ -253,10 +253,20 @@ public void setElements(int elements) { this.elements = elements; } + /** + * On version 1.13 and above, the recipe ids are read as ResourceLocation, otherwise they are read as integers. + * + * @return The recipe ids + */ public List> getRecipeIds() { return recipeIds; } + /** + * On version 1.13 and above, the recipe ids are written as ResourceLocation, otherwise they are written as integers. + * + * @param recipeIds The recipe ids + */ public void setRecipeIds(List> recipeIds) { this.recipeIds = recipeIds; } @@ -269,10 +279,21 @@ public void setElementsInit(int elementsInit) { this.elementsInit = elementsInit; } + /** + * On version 1.13 and above, the recipe ids are read as ResourceLocation, otherwise they are read as integers. + * + * @return The recipe ids init + */ public List> getRecipeIdsInit() { return recipeIdsInit; } + + /** + * On version 1.13 and above, the recipe ids are written as ResourceLocation, otherwise they are written as integers. + * + * @param recipeIdsInit The recipe ids init + */ public void setRecipeIdsInit(List> recipeIdsInit) { this.recipeIdsInit = recipeIdsInit; }