aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/recipes/Ingredient.java
blob: a55d4a2eb7910d735021263efc98ebf0b336c4fd (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
/*
 * 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.gson.JsonObject;
import io.github.moulberry.notenoughupdates.NEUManager;
import io.github.moulberry.notenoughupdates.util.ItemUtils;
import net.minecraft.item.ItemStack;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class Ingredient {

	public static final String SKYBLOCK_COIN = "SKYBLOCK_COIN";
	private final double count;
	private final String internalItemId;
	private final NEUManager manager;
	private ItemStack itemStack;

	public Ingredient(NEUManager manager, String ingredientIdentifier) {
		this.manager = manager;
		String[] parts = ingredientIdentifier.split(":");
		internalItemId = parts[0];
		if (parts.length == 2) {
			count = Double.parseDouble(parts[1]);
		} else if (parts.length == 1) {
			count = 1;
		} else {
			throw new IllegalArgumentException("Could not parse ingredient " + ingredientIdentifier);
		}
	}

	public Ingredient(NEUManager manager, String internalItemId, double count) {
		this.manager = manager;
		this.count = count;
		this.internalItemId = internalItemId;
	}

	private Ingredient(NEUManager manager, double coinValue) {
		this.manager = manager;
		this.internalItemId = SKYBLOCK_COIN;
		this.count = coinValue;
	}

	public static Set<Ingredient> mergeIngredients(Iterable<Ingredient> ingredients) {
		Map<String, Ingredient> newIngredients = new HashMap<>();
		for (Ingredient i : ingredients) {
			newIngredients.merge(
				i.getInternalItemId(),
				i,
				(a, b) -> new Ingredient(i.manager, i.internalItemId, a.count + b.count)
			);
		}
		return new HashSet<>(newIngredients.values());
	}

	public static Ingredient coinIngredient(NEUManager manager, int coins) {
		return new Ingredient(manager, coins);
	}

	public boolean isCoins() {
		return "SKYBLOCK_COIN".equals(internalItemId);
	}

	public double getCount() {
		return count;
	}

	public String getInternalItemId() {
		return internalItemId;
	}

	public ItemStack getItemStack() {
		if (itemStack != null) return itemStack;
		if (isCoins()) {
			return ItemUtils.getCoinItemStack(count);
		}
		JsonObject itemInfo = manager.getItemInformation().get(internalItemId);
		itemStack = manager.jsonToStack(itemInfo, false, true);
		itemStack.stackSize = (int) count;
		return itemStack;
	}

	public String serialize() {
		return internalItemId + ":" + count;
	}
}