3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-10-03 08:41:05 +02:00

Clean up 1.12 recipe data handling

Supersedes #3880
Dieser Commit ist enthalten in:
Nassim Jahnke 2024-05-26 20:38:27 +02:00
Ursprung 792f52e637
Commit e26f63c703
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: EF6771C01F6EF02F
2 geänderte Dateien mit 32 neuen und 109 gelöschten Zeilen

Datei anzeigen

@ -774,49 +774,52 @@ public class Protocol1_12_2To1_13 extends AbstractProtocol<ClientboundPackets1_1
private void writeDeclareRecipes(PacketWrapper recipesPacket) { private void writeDeclareRecipes(PacketWrapper recipesPacket) {
recipesPacket.write(Types.VAR_INT, RecipeData.recipes.size()); recipesPacket.write(Types.VAR_INT, RecipeData.recipes.size());
for (Map.Entry<String, RecipeData.Recipe> entry : RecipeData.recipes.entrySet()) { for (Map.Entry<String, RecipeData.Recipe> entry : RecipeData.recipes.entrySet()) {
RecipeData.Recipe recipe = entry.getValue();
recipesPacket.write(Types.STRING, entry.getKey()); // Id recipesPacket.write(Types.STRING, entry.getKey()); // Id
recipesPacket.write(Types.STRING, entry.getValue().getType()); recipesPacket.write(Types.STRING, recipe.type());
switch (entry.getValue().getType()) {
// Clone item arrays because array and item are mutable, also make sure the array type is the interface
switch (recipe.type()) {
case "crafting_shapeless": { case "crafting_shapeless": {
recipesPacket.write(Types.STRING, entry.getValue().getGroup()); recipesPacket.write(Types.STRING, recipe.group());
recipesPacket.write(Types.VAR_INT, entry.getValue().getIngredients().length); recipesPacket.write(Types.VAR_INT, recipe.ingredients().length);
for (Item[] ingredient : entry.getValue().getIngredients()) { for (Item[] ingredient : recipe.ingredients()) {
Item[] clone = ingredient.clone(); // Clone because array and item is mutable Item[] clone = new Item[ingredient.length];
for (int i = 0; i < clone.length; i++) { for (int i = 0; i < ingredient.length; i++) {
if (clone[i] == null) continue; if (clone[i] == null) continue;
clone[i] = new DataItem(clone[i]); clone[i] = new DataItem(ingredient[i]);
} }
recipesPacket.write(Types.ITEM1_13_ARRAY, clone); recipesPacket.write(Types.ITEM1_13_ARRAY, clone);
} }
recipesPacket.write(Types.ITEM1_13, new DataItem(entry.getValue().getResult())); recipesPacket.write(Types.ITEM1_13, new DataItem(recipe.result()));
break; break;
} }
case "crafting_shaped": { case "crafting_shaped": {
recipesPacket.write(Types.VAR_INT, entry.getValue().getWidth()); recipesPacket.write(Types.VAR_INT, recipe.width());
recipesPacket.write(Types.VAR_INT, entry.getValue().getHeight()); recipesPacket.write(Types.VAR_INT, recipe.height());
recipesPacket.write(Types.STRING, entry.getValue().getGroup()); recipesPacket.write(Types.STRING, recipe.group());
for (Item[] ingredient : entry.getValue().getIngredients()) { for (Item[] ingredient : recipe.ingredients()) {
Item[] clone = ingredient.clone(); // Clone because array and item is mutable Item[] clone = new Item[ingredient.length];
for (int i = 0; i < clone.length; i++) { for (int i = 0; i < ingredient.length; i++) {
if (clone[i] == null) continue; if (clone[i] == null) continue;
clone[i] = new DataItem(clone[i]); clone[i] = new DataItem(ingredient[i]);
} }
recipesPacket.write(Types.ITEM1_13_ARRAY, clone); recipesPacket.write(Types.ITEM1_13_ARRAY, clone);
} }
recipesPacket.write(Types.ITEM1_13, new DataItem(entry.getValue().getResult())); recipesPacket.write(Types.ITEM1_13, new DataItem(recipe.result()));
break; break;
} }
case "smelting": { case "smelting": {
recipesPacket.write(Types.STRING, entry.getValue().getGroup()); recipesPacket.write(Types.STRING, recipe.group());
Item[] clone = entry.getValue().getIngredient().clone(); // Clone because array and item is mutable Item[] ingredient = new Item[recipe.ingredient().length];
for (int i = 0; i < clone.length; i++) { for (int i = 0; i < ingredient.length; i++) {
if (clone[i] == null) continue; if (recipe.ingredient()[i] == null) continue;
clone[i] = new DataItem(clone[i]); ingredient[i] = new DataItem(recipe.ingredient()[i]);
} }
recipesPacket.write(Types.ITEM1_13_ARRAY, clone); recipesPacket.write(Types.ITEM1_13_ARRAY, ingredient);
recipesPacket.write(Types.ITEM1_13, new DataItem(entry.getValue().getResult())); recipesPacket.write(Types.ITEM1_13, new DataItem(recipe.result()));
recipesPacket.write(Types.FLOAT, entry.getValue().getExperience()); recipesPacket.write(Types.FLOAT, recipe.experience());
recipesPacket.write(Types.VAR_INT, entry.getValue().getCookingTime()); recipesPacket.write(Types.VAR_INT, recipe.cookingTime());
break; break;
} }
} }

Datei anzeigen

@ -25,7 +25,7 @@ import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.Map; import java.util.Map;
public class RecipeData { public final class RecipeData {
public static Map<String, Recipe> recipes; public static Map<String, Recipe> recipes;
public static void init() { public static void init() {
@ -43,87 +43,7 @@ public class RecipeData {
// Ignored // Ignored
} }
public static class Recipe { public record Recipe(String type, String group, int width, int height, float experience, int cookingTime,
private String type; DataItem[] ingredient, DataItem[][] ingredients, DataItem result) {
private String group;
private int width;
private int height;
private float experience;
private int cookingTime;
private DataItem[] ingredient;
private DataItem[][] ingredients;
private DataItem result;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public float getExperience() {
return experience;
}
public void setExperience(float experience) {
this.experience = experience;
}
public int getCookingTime() {
return cookingTime;
}
public void setCookingTime(int cookingTime) {
this.cookingTime = cookingTime;
}
public DataItem[] getIngredient() {
return ingredient;
}
public void setIngredient(DataItem[] ingredient) {
this.ingredient = ingredient;
}
public DataItem[][] getIngredients() {
return ingredients;
}
public void setIngredients(DataItem[][] ingredients) {
this.ingredients = ingredients;
}
public DataItem getResult() {
return result;
}
public void setResult(DataItem result) {
this.result = result;
}
} }
} }