aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/recipes/CraftingRecipe.java
blob: c0729634d4cee8520da1379f3c62625fb1336b4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 * Copyright (C) 2022 NotEnoughUpdates contributors
 *
 * This file is part of NotEnoughUpdates.
 *
 * NotEnoughUpdates is free software: you can redistribute it
 * and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *
 * NotEnoughUpdates 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>.
 */

package io.github.moulberry.notenoughupdates.recipes;

import com.google.common.collect.Sets;
import com.google.gson.JsonObject;
import io.github.moulberry.notenoughupdates.NEUManager;
import io.github.moulberry.notenoughupdates.miscgui.GuiItemRecipe;
import io.github.moulberry.notenoughupdates.util.Utils;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

public class CraftingRecipe implements NeuRecipe {

	public static final ResourceLocation BACKGROUND = new ResourceLocation("notenoughupdates",
		"textures/gui/crafting_table_tall.png");

	private static final int EXTRA_STRING_X = 90;
	private static final int EXTRA_STRING_Y = 35;

	private final NEUManager manager;
	private final Ingredient[] inputs;
	private final String extraText;
	private final Ingredient outputIngredient;
	private List<RecipeSlot> slots;

	public CraftingRecipe(NEUManager manager, Ingredient[] inputs, Ingredient output, String extra) {
		this.manager = manager;
		this.inputs = inputs;
		this.outputIngredient = output;
		this.extraText = extra;
		if (inputs.length != 9)
			throw new IllegalArgumentException("Cannot construct crafting recipe with non standard crafting grid size");
	}

	@Override
	public Set<Ingredient> getIngredients() {
		Set<Ingredient> ingredients = Sets.newHashSet(inputs);
		ingredients.remove(null);
		return ingredients;
	}

	@Override
	public RecipeType getType() {
		return RecipeType.CRAFTING;
	}

	@Override
	public boolean hasVariableCost() {
		return false;
	}

	@Override
	public Set<Ingredient> getOutputs() {
		return Collections.singleton(getOutput());
	}

	public Ingredient getOutput() {
		return outputIngredient;
	}

	public Ingredient[] getInputs() {
		return inputs;
	}

	@Override
	public List<RecipeSlot> getSlots() {
		if (slots != null) return slots;
		slots = new ArrayList<>();
		for (int x = 0; x < 3; x++) {
			for (int y = 0; y < 3; y++) {
				Ingredient input = inputs[x + y * 3];
				if (input == null) continue;
				ItemStack item = input.getItemStack();
				if (item == null) continue;
				slots.add(new RecipeSlot(30 + x * GuiItemRecipe.SLOT_SPACING, 48 + y * GuiItemRecipe.SLOT_SPACING, item));
			}
		}
		slots.add(new RecipeSlot(124, 66, outputIngredient.getItemStack()));
		return slots;
	}

	public String getCraftText() {
		return extraText;
	}

	@Override
	public ResourceLocation getBackground() {
		return BACKGROUND;
	}

	@Override
	public void drawExtraInfo(GuiItemRecipe gui, int mouseX, int mouseY) {
		String craftingText = getCraftText();
		if (craftingText != null)
			Utils.drawStringCenteredScaledMaxWidth(craftingText,
				gui.guiLeft + EXTRA_STRING_X, gui.guiTop + EXTRA_STRING_Y, false, 85, 0x404040
			);
	}

	@Override
	public JsonObject serialize() {
		JsonObject object = new JsonObject();
		object.addProperty("type", "crafting");
		object.addProperty("count", outputIngredient.getCount());
		object.addProperty("overrideOutputId", outputIngredient.getInternalItemId());
		for (int i = 0; i < 9; i++) {
			Ingredient ingredient = inputs[i];
			if (ingredient == null) continue;
			String[] x = {"1", "2", "3"};
			String[] y = {"A", "B", "C"};
			String name = x[i / 3] + y[i % 3];
			object.addProperty(name, ingredient.serialize());
		}
		if (extraText != null)
			object.addProperty("crafttext", extraText);
		return object;
	}

	public static CraftingRecipe parseCraftingRecipe(NEUManager manager, JsonObject recipe, JsonObject outputItem) {
		Ingredient[] craftMatrix = new Ingredient[9];

		String[] x = {"1", "2", "3"};
		String[] y = {"A", "B", "C"};
		for (int i = 0; i < 9; i++) {
			String name = y[i / 3] + x[i % 3];
			if (!recipe.has(name)) continue;
			String item = recipe.get(name).getAsString();
			if (item == null || item.isEmpty()) continue;
			craftMatrix[i] = new Ingredient(manager, item);
		}
		int resultCount = 1;
		if (recipe.has("count"))
			resultCount = recipe.get("count").getAsInt();
		String extra = null;
		if (outputItem.has("crafttext"))
			extra = outputItem.get("crafttext").getAsString();
		if (recipe.has("crafttext"))
			extra = recipe.get("crafttext").getAsString();
		String outputItemId = outputItem.get("internalname").getAsString();
		if (recipe.has("overrideOutputId"))
			outputItemId = recipe.get("overrideOutputId").getAsString();
		return new CraftingRecipe(manager, craftMatrix, new Ingredient(manager, outputItemId, resultCount), extra);
	}
}