Skip to content

Commit

Permalink
change: cleaned up classes
Browse files Browse the repository at this point in the history
  • Loading branch information
NoJokeFNA committed Jun 19, 2022
1 parent 113b32f commit 1a0b13b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -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];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<WrapperPlayServerUnlockRecipes> {
private Action action;
private UnlockRecipesType type;
private boolean craftingRecipeBookOpen;
private boolean craftingRecipeBookFilterActive;
private boolean smeltingRecipeBookOpen;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -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];
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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() {
Expand Down Expand Up @@ -275,20 +275,16 @@ public void setElements(int elements) {
this.elements = elements;
}

public Optional<int[]> getRecipeIdsLegacy() {
return Optional.ofNullable(recipeIdsLegacy);
public <T> T getRecipeIds() {
return (T) (serverVersion.isNewerThanOrEquals(ServerVersion.V_1_13) ? recipeIdsModern : recipeIdsLegacy);
}

public void setRecipeIdsLegacy(int @Nullable [] recipeIdsLegacy) {
this.recipeIdsLegacy = recipeIdsLegacy;
}

public Optional<String[]> getRecipeIdsModern() {
return Optional.ofNullable(recipeIdsModern);
}

public void setRecipeIdsModern(String @Nullable [] recipeIdsModern) {
this.recipeIdsModern = recipeIdsModern;
public <T> void setRecipeIds(T recipeIds) {
if (serverVersion.isNewerThanOrEquals(ServerVersion.V_1_13)) {
this.recipeIdsModern = (String[]) recipeIds;
} else {
this.recipeIdsLegacy = (int[]) recipeIds;
}
}

public int getElementsInit() {
Expand All @@ -299,27 +295,15 @@ public void setElementsInit(int elementsInit) {
this.elementsInit = elementsInit;
}

public Optional<int[]> getRecipeIdsLegacyInit() {
return Optional.ofNullable(recipeIdsLegacyInit);
public <T> T getRecipeIdsInit() {
return (T) (serverVersion.isNewerThanOrEquals(ServerVersion.V_1_13) ? recipeIdsModernInit : recipeIdsLegacyInit);
}

public void setRecipeIdsLegacyInit(int @Nullable [] recipeIdsLegacyInit) {
this.recipeIdsLegacyInit = recipeIdsLegacyInit;
}

public Optional<String[]> 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 <T> void setRecipeIdsInit(T recipeIds) {
if (serverVersion.isNewerThanOrEquals(ServerVersion.V_1_13)) {
this.recipeIdsModernInit = (String[]) recipeIds;
} else {
this.recipeIdsLegacyInit = (int[]) recipeIds;
}
}
}

0 comments on commit 1a0b13b

Please sign in to comment.