From 1a0b13b5d72bd3948d4aaa7315e2687006d20b41 Mon Sep 17 00:00:00 2001 From: Julian <53055251+NoJokeFNA@users.noreply.github.com> Date: Sun, 19 Jun 2022 19:32:43 +0200 Subject: [PATCH] change: cleaned up classes --- .../protocol/recipe/UnlockRecipesType.java | 13 +++ .../recipe/data/MerchantRecipeData.java | 2 +- .../WrapperPlayServerUnlockRecipes.java | 86 ++++++++----------- 3 files changed, 49 insertions(+), 52 deletions(-) create mode 100644 api/src/main/java/com/github/retrooper/packetevents/protocol/recipe/UnlockRecipesType.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 index 819cccbae7..cdc8ece44f 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,14 +21,14 @@ 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; -import java.util.Optional; public class WrapperPlayServerUnlockRecipes extends PacketWrapper { - private Action action; + private UnlockRecipesType type; private boolean craftingRecipeBookOpen; private boolean craftingRecipeBookFilterActive; private boolean smeltingRecipeBookOpen; @@ -48,10 +48,10 @@ public WrapperPlayServerUnlockRecipes(PacketSendEvent event) { super(event); } - public WrapperPlayServerUnlockRecipes(Action action, boolean craftingRecipeBookOpen, boolean craftingRecipeBookFilterActive, int elements, + 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.action = action; + this.type = type; this.craftingRecipeBookOpen = craftingRecipeBookOpen; this.craftingRecipeBookFilterActive = craftingRecipeBookFilterActive; this.elements = elements; @@ -60,11 +60,11 @@ public WrapperPlayServerUnlockRecipes(Action action, boolean craftingRecipeBookO this.recipeIdsLegacyInit = recipeIdsLegacyInit; } - public WrapperPlayServerUnlockRecipes(Action action, boolean craftingRecipeBookOpen, boolean craftingRecipeBookFilterActive, boolean smeltingRecipeBookOpen, + 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.action = action; + this.type = type; this.craftingRecipeBookOpen = craftingRecipeBookOpen; this.craftingRecipeBookFilterActive = craftingRecipeBookFilterActive; this.smeltingRecipeBookOpen = smeltingRecipeBookOpen; @@ -75,12 +75,12 @@ public WrapperPlayServerUnlockRecipes(Action action, boolean craftingRecipeBookO this.recipeIdsModernInit = recipeIdsModernInit; } - public WrapperPlayServerUnlockRecipes(Action action, boolean craftingRecipeBookOpen, boolean craftingRecipeBookFilterActive, boolean smeltingRecipeBookOpen, + 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.action = action; + this.type = type; this.craftingRecipeBookOpen = craftingRecipeBookOpen; this.craftingRecipeBookFilterActive = craftingRecipeBookFilterActive; this.smeltingRecipeBookOpen = smeltingRecipeBookOpen; @@ -98,9 +98,9 @@ public WrapperPlayServerUnlockRecipes(Action action, boolean craftingRecipeBookO @Override public void read() { if (serverVersion.isNewerThanOrEquals(ServerVersion.V_1_12)) { - this.action = Action.VALUES[readVarInt()]; + this.type = UnlockRecipesType.getById(readVarInt()); } else { - this.action = Action.VALUES[readShort()]; + this.type = UnlockRecipesType.getById(readShort()); } this.craftingRecipeBookOpen = readBoolean(); this.craftingRecipeBookFilterActive = readBoolean(); @@ -127,7 +127,7 @@ public void read() { } } - if (action == Action.INIT) { + if (type == UnlockRecipesType.INIT) { this.elementsInit = readVarInt(); if (serverVersion.isNewerThanOrEquals(ServerVersion.V_1_13)) { this.recipeIdsModernInit = new String[this.elementsInit]; @@ -146,9 +146,9 @@ public void read() { @Override public void write() { if (serverVersion.isNewerThanOrEquals(ServerVersion.V_1_12)) { - writeVarInt(this.action.ordinal()); + writeVarInt(this.type.ordinal()); } else { - writeShort(this.action.ordinal()); + writeShort(this.type.ordinal()); } writeBoolean(this.craftingRecipeBookOpen); writeBoolean(this.craftingRecipeBookFilterActive); @@ -168,7 +168,7 @@ public void write() { } else { Arrays.stream(this.recipeIdsLegacy).forEach(this::writeVarInt); } - if (action == Action.INIT) { + if (type == UnlockRecipesType.INIT) { writeVarInt(this.elementsInit); if (serverVersion.isNewerThanOrEquals(ServerVersion.V_1_13)) { Arrays.stream(this.recipeIdsModernInit).forEach(this::writeString); @@ -180,7 +180,7 @@ public void write() { @Override public void copy(WrapperPlayServerUnlockRecipes wrapper) { - this.action = wrapper.action; + this.type = wrapper.type; this.craftingRecipeBookOpen = wrapper.craftingRecipeBookOpen; this.craftingRecipeBookFilterActive = wrapper.craftingRecipeBookFilterActive; this.smeltingRecipeBookOpen = wrapper.smeltingRecipeBookOpen; @@ -195,12 +195,12 @@ public void copy(WrapperPlayServerUnlockRecipes wrapper) { this.elementsInit = wrapper.elementsInit; } - public Action getAction() { - return action; + public UnlockRecipesType getType() { + return type; } - public void setAction(Action action) { - this.action = action; + public void setType(UnlockRecipesType type) { + this.type = type; } public boolean isCraftingRecipeBookOpen() { @@ -275,20 +275,16 @@ public void setElements(int elements) { this.elements = elements; } - public Optional getRecipeIdsLegacy() { - return Optional.ofNullable(recipeIdsLegacy); + public T getRecipeIds() { + return (T) (serverVersion.isNewerThanOrEquals(ServerVersion.V_1_13) ? recipeIdsModern : recipeIdsLegacy); } - public void setRecipeIdsLegacy(int @Nullable [] recipeIdsLegacy) { - this.recipeIdsLegacy = recipeIdsLegacy; - } - - public Optional getRecipeIdsModern() { - return Optional.ofNullable(recipeIdsModern); - } - - public void setRecipeIdsModern(String @Nullable [] recipeIdsModern) { - this.recipeIdsModern = recipeIdsModern; + public void setRecipeIds(T recipeIds) { + if (serverVersion.isNewerThanOrEquals(ServerVersion.V_1_13)) { + this.recipeIdsModern = (String[]) recipeIds; + } else { + this.recipeIdsLegacy = (int[]) recipeIds; + } } public int getElementsInit() { @@ -299,27 +295,15 @@ public void setElementsInit(int elementsInit) { this.elementsInit = elementsInit; } - public Optional getRecipeIdsLegacyInit() { - return Optional.ofNullable(recipeIdsLegacyInit); + public T getRecipeIdsInit() { + return (T) (serverVersion.isNewerThanOrEquals(ServerVersion.V_1_13) ? recipeIdsModernInit : recipeIdsLegacyInit); } - public void setRecipeIdsLegacyInit(int @Nullable [] recipeIdsLegacyInit) { - this.recipeIdsLegacyInit = recipeIdsLegacyInit; - } - - public Optional getRecipeIdsModernInit() { - return Optional.ofNullable(recipeIdsModernInit); - } - - public void setRecipeIdsModernInit(String[] recipeIdsModernInit) { - this.recipeIdsModernInit = recipeIdsModernInit; - } - - public enum Action { - INIT, - ADD, - REMOVE; - - public static final Action[] VALUES = values(); + public void setRecipeIdsInit(T recipeIds) { + if (serverVersion.isNewerThanOrEquals(ServerVersion.V_1_13)) { + this.recipeIdsModernInit = (String[]) recipeIds; + } else { + this.recipeIdsLegacyInit = (int[]) recipeIds; + } } }