-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResearchDefinition.java
More file actions
234 lines (208 loc) · 7.87 KB
/
ResearchDefinition.java
File metadata and controls
234 lines (208 loc) · 7.87 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package com.researchcube.research;
import com.researchcube.research.prerequisite.Prerequisite;
import com.researchcube.research.prerequisite.NonePrerequisite;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.RandomSource;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.Optional;
/**
* Immutable definition of a single research entry, loaded from datapack JSON.
*
* JSON structure:
* {
* "tier": "ADVANCED",
* "duration": 6000,
* "category": "circuits",
* "prerequisites": { "type": "AND", "values": [...] },
* "item_costs": [ { "item": "minecraft:redstone", "count": 16 } ],
* "recipe_pool": [ "researchcube:processor_recipe_1", {"id": "researchcube:processor_recipe_2", "weight": 3} ]
* }
*
* The "id" is derived from the datapack file path (e.g., data/researchcube/research/advanced_processor.json
* → "researchcube:advanced_processor").
*/
public class ResearchDefinition {
private final ResourceLocation id;
private final ResearchTier tier;
private final int duration; // in ticks
private final Prerequisite prerequisites;
private final List<ItemCost> itemCosts;
private final List<ResourceLocation> recipePool;
private final List<WeightedRecipe> weightedRecipePool;
@Nullable
private final String name; // human-readable display name (optional)
@Nullable
private final String description; // short description (optional)
@Nullable
private final String category; // optional grouping category (e.g., "circuits", "energy")
@Nullable
private final FluidCost fluidCost; // optional fluid cost for this research
private final Optional<ItemStack> ideaChip; // optional idea chip required to start this research
public ResearchDefinition(ResourceLocation id, ResearchTier tier, int duration,
Prerequisite prerequisites, List<ItemCost> itemCosts,
List<WeightedRecipe> weightedRecipePool,
@Nullable String name, @Nullable String description,
@Nullable String category,
@Nullable FluidCost fluidCost,
Optional<ItemStack> ideaChip) {
this.id = id;
this.tier = tier;
this.duration = duration;
this.prerequisites = prerequisites != null ? prerequisites : NonePrerequisite.INSTANCE;
this.itemCosts = itemCosts != null ? List.copyOf(itemCosts) : List.of();
this.weightedRecipePool = weightedRecipePool != null ? List.copyOf(weightedRecipePool) : List.of();
this.recipePool = this.weightedRecipePool.stream().map(WeightedRecipe::id).toList();
this.name = name;
this.description = description;
this.category = category;
this.fluidCost = fluidCost;
this.ideaChip = ideaChip != null ? ideaChip : Optional.empty();
}
/**
* Constructor without ideaChip (backwards-compatible).
*/
public ResearchDefinition(ResourceLocation id, ResearchTier tier, int duration,
Prerequisite prerequisites, List<ItemCost> itemCosts,
List<WeightedRecipe> weightedRecipePool,
@Nullable String name, @Nullable String description,
@Nullable String category,
@Nullable FluidCost fluidCost) {
this(id, tier, duration, prerequisites, itemCosts, weightedRecipePool,
name, description, category, fluidCost, Optional.empty());
}
/**
* Constructor without fluidCost or ideaChip (backwards-compatible).
*/
public ResearchDefinition(ResourceLocation id, ResearchTier tier, int duration,
Prerequisite prerequisites, List<ItemCost> itemCosts,
List<WeightedRecipe> weightedRecipePool,
@Nullable String name, @Nullable String description,
@Nullable String category) {
this(id, tier, duration, prerequisites, itemCosts, weightedRecipePool,
name, description, category, null);
}
/**
* Backwards-compatible constructor (no name/description/category/fluidCost, plain ResourceLocation pool).
*/
public ResearchDefinition(ResourceLocation id, ResearchTier tier, int duration,
Prerequisite prerequisites, List<ItemCost> itemCosts,
List<ResourceLocation> recipePool) {
this(id, tier, duration, prerequisites, itemCosts,
recipePool.stream().map(rl -> new WeightedRecipe(rl, 1)).toList(),
null, null, null, null, null);
}
public ResourceLocation getId() {
return id;
}
public String getIdString() {
return id.toString();
}
public ResearchTier getTier() {
return tier;
}
/**
* Duration in game ticks.
*/
public int getDuration() {
return duration;
}
public Prerequisite getPrerequisites() {
return prerequisites;
}
public List<ItemCost> getItemCosts() {
return itemCosts;
}
/**
* Pool of recipe ResourceLocations. On completion, one is chosen via weighted random.
*/
public List<ResourceLocation> getRecipePool() {
return recipePool;
}
/**
* Pool of weighted recipe entries for weighted random selection.
*/
public List<WeightedRecipe> getWeightedRecipePool() {
return weightedRecipePool;
}
/**
* Select a recipe from the pool using weighted random selection.
* Returns null if the pool is empty.
*/
@Nullable
public ResourceLocation pickWeightedRecipe(RandomSource random) {
if (weightedRecipePool.isEmpty()) return null;
int totalWeight = 0;
for (WeightedRecipe wr : weightedRecipePool) {
totalWeight += wr.weight();
}
int roll = random.nextInt(totalWeight);
for (WeightedRecipe wr : weightedRecipePool) {
roll -= wr.weight();
if (roll < 0) {
return wr.id();
}
}
// Fallback (should never happen)
return weightedRecipePool.getLast().id();
}
/**
* Optional human-readable name. Falls back to the path of the ID.
*/
@Nullable
public String getName() {
return name;
}
/**
* Returns the display name: the name field if set, otherwise the ID path.
*/
public String getDisplayName() {
return name != null ? name : id.getPath();
}
/**
* Optional short description.
*/
@Nullable
public String getDescription() {
return description;
}
/**
* Optional category for grouping in the UI (e.g., "circuits", "energy").
*/
@Nullable
public String getCategory() {
return category;
}
/**
* Optional fluid cost for this research (e.g., 1000 mB of Thinking Fluid).
*/
@Nullable
public FluidCost getFluidCost() {
return fluidCost;
}
/**
* Optional idea chip required to start this research.
* If present, the player must place a matching item in the idea chip slot.
*/
public Optional<ItemStack> getIdeaChip() {
return ideaChip;
}
/**
* Returns duration as human-readable seconds.
*/
public float getDurationSeconds() {
return duration / 20.0f;
}
/**
* Returns true if this definition has a non-empty recipe pool.
*/
public boolean hasRecipePool() {
return !recipePool.isEmpty();
}
@Override
public String toString() {
return "ResearchDefinition{" + id + ", tier=" + tier + ", duration=" + duration +
", category=" + category + ", costs=" + itemCosts.size() + ", recipes=" + recipePool.size() + "}";
}
}