Skip to content

Commit dffec01

Browse files
Added diminishing workpoints when researcing the same item
1 parent 37a3b9d commit dffec01

File tree

2 files changed

+106
-62
lines changed

2 files changed

+106
-62
lines changed

src/main/java/com/mewo/hbmenhanced/ResearchBlock/Research.java

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class Research {
2121
public void Tier1(TileEntityT1 te) {
2222
if (te.core == null) return;
2323
if (!te.core.canResearch(te)) return;
24+
2425
int mainSlot = te.MAIN_SLOT;
2526
int outputSlot = te.OUTPUT_SLOT;
2627
int fuelSlot = te.FUEL_SLOT;
@@ -36,40 +37,44 @@ public void Tier1(TileEntityT1 te) {
3637
te.isResearching = false;
3738
te.researchProgress = 0;
3839
te.maxResearchProgress = 0;
39-
} else {
40-
te.isResearching = true;
41-
if (te.researchProgress == 0) {
42-
te.maxResearchProgress = getItemValues.getResearchTime(te);
43-
}
40+
return;
4441
}
4542

46-
if (te.isResearching) {
47-
if (te.currentBurnTime <= 0) te.isBurning = false;
48-
if (fuel != null && !te.isBurning) {
49-
te.isBurning = true;
50-
te.totalBurnTime = TileEntityFurnace.getItemBurnTime(fuel);
51-
te.currentBurnTime = TileEntityFurnace.getItemBurnTime(fuel);
52-
fuel.stackSize--;
53-
inventory[fuelSlot] = fuel.stackSize <= 0 ? null : fuel;
54-
}
55-
if (te.currentBurnTime > 0) {
56-
te.currentBurnTime--;
57-
te.researchProgress++;
58-
if (te.researchProgress >= te.maxResearchProgress) {
59-
te.researchProgress = 0;
60-
te.maxResearchProgress = 0;
61-
input.stackSize--;
62-
inventory[mainSlot] = input.stackSize <= 0 ? null : input;
63-
ItemStack itemStack = new ItemStack(hbmenhanced.researchPoint, 1);
64-
ResearchValue researchValue = getItemValues.getItemValue(input.getItem());
65-
getItemValues.setValues(researchValue, itemStack);
66-
output = itemStack;
67-
inventory[outputSlot] = output;
68-
}
43+
te.isResearching = true;
44+
if (te.researchProgress == 0) {
45+
te.maxResearchProgress = getItemValues.getResearchTime(te);
46+
}
47+
48+
if (te.currentBurnTime <= 0) te.isBurning = false;
49+
if (fuel != null && !te.isBurning) {
50+
te.isBurning = true;
51+
te.totalBurnTime = TileEntityFurnace.getItemBurnTime(fuel);
52+
te.currentBurnTime = te.totalBurnTime;
53+
fuel.stackSize--;
54+
inventory[fuelSlot] = (fuel.stackSize <= 0) ? null : fuel;
55+
}
56+
57+
if (te.currentBurnTime > 0) {
58+
te.currentBurnTime--;
59+
te.researchProgress++;
60+
61+
if (te.researchProgress >= te.maxResearchProgress) {
62+
te.researchProgress = 0;
63+
te.maxResearchProgress = 0;
64+
65+
ResearchValue researchValue = getItemValues.getItemValue(input);
66+
67+
input.stackSize--;
68+
inventory[mainSlot] = (input.stackSize <= 0) ? null : input;
69+
70+
ItemStack itemStack = new ItemStack(hbmenhanced.researchPoint, 1);
71+
getItemValues.setValues(researchValue, itemStack, te.team, input);
72+
inventory[outputSlot] = itemStack;
6973
}
7074
}
7175
}
7276

77+
7378
public void Tier2(TileEntityT2 te) {
7479
if (te.core == null) return;
7580
if (!te.core.canResearch(te)) return;
@@ -118,9 +123,9 @@ public void Tier2(TileEntityT2 te) {
118123
if (te.researchProgress >= te.maxResearchProgress) {
119124
System.out.println("Research Finished");
120125
//Award Points
121-
ResearchValue val = getItemValues.getItemValue(input.getItem());
126+
ResearchValue val = getItemValues.getItemValue(input);
122127
ItemStack point = new ItemStack(hbmenhanced.researchPoint);
123-
getItemValues.setValues(val, point);
128+
getItemValues.setValues(val, point, te.team, input);
124129
te.inventory[outputSlot] = point;
125130
te.researchProgress = 0;
126131
te.isResearching = false;
@@ -165,9 +170,9 @@ public void Tier3(TileEntityT3 te) {
165170
te.researchProgress = 0;
166171
te.maxResearchProgress = 0;
167172

168-
ResearchValue val = getItemValues.getItemValue(input.getItem());
173+
ResearchValue val = getItemValues.getItemValue(input);
169174
ItemStack point = new ItemStack(hbmenhanced.researchPoint);
170-
getItemValues.setValues(val, point);
175+
getItemValues.setValues(val, point, te.team, input);
171176
output = point;
172177
te.inventory[output_slot] = output;
173178
te.inventory[main_slot].stackSize--;

src/main/java/com/mewo/hbmenhanced/Util/getItemValues.java

Lines changed: 69 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,21 @@
1313
import net.minecraft.tileentity.TileEntity;
1414

1515
import java.util.*;
16+
1617
public class getItemValues {
1718

19+
public static Map<String, Map<String, Integer>> researchCounts = new HashMap<>();
1820
public static Map<String, ResearchValue> Values = new HashMap<>();
1921

22+
// Creates a truly unique identifier for an item including metadata
23+
private static String getUniqueItemKey(Item item, int meta) {
24+
return Item.itemRegistry.getNameForObject(item) + ":" + meta;
25+
}
26+
27+
private static String getUniqueItemKey(ItemStack stack) {
28+
return getUniqueItemKey(stack.getItem(), stack.getItemDamage());
29+
}
30+
2031
public static boolean isBlackListed(String key) {
2132
String[] keywords = {
2233
"ladder", "plate", "rod", "pipe",
@@ -34,7 +45,8 @@ public static boolean isBlackListed(String key) {
3445
"glass", "bars", "door", "sand",
3546
"axe", "tank", "scaffold", "corner",
3647
"pedestal", "port", "carrot", "tile",
37-
"flint", "furnace"};
48+
"flint", "furnace"
49+
};
3850
for (String keyword : keywords) {
3951
if (key.toLowerCase().trim().contains(keyword)) {
4052
return true;
@@ -46,12 +58,14 @@ public static boolean isBlackListed(String key) {
4658
public static boolean isResearchItem(String name) {
4759
return !isBlackListed(name);
4860
}
49-
public static ResearchValue getItemValue(Item item) {
50-
System.out.println("Item is: " + item.getUnlocalizedName());
51-
if (Values.containsKey(item.getUnlocalizedName().toLowerCase())) {
61+
62+
public static ResearchValue getItemValue(ItemStack stack) {
63+
String key = getUniqueItemKey(stack); // uses damage/meta from stack
64+
System.out.println("Item is: " + key);
65+
if (Values.containsKey(key)) {
5266
System.out.println("Found item in map");
5367
}
54-
return Values.getOrDefault(item.getUnlocalizedName().toLowerCase(), new ResearchValue());
68+
return Values.getOrDefault(key, new ResearchValue());
5569
}
5670

5771
public static int getResearchTime(TileEntity te) {
@@ -78,36 +92,34 @@ public static int getResearchTime(TileEntity te) {
7892
}
7993

8094
private static TileEntityResearchController getCore(TileEntity te) {
81-
TileEntityResearchController core = null;
8295
if (te instanceof TileEntityT1) {
83-
if (((TileEntityT1) te).core != null) {
84-
core = ((TileEntityT1) te).core;
85-
}
96+
return ((TileEntityT1) te).core;
8697
} else if (te instanceof TileEntityT2) {
87-
if (((TileEntityT2) te).core != null) {
88-
core = ((TileEntityT2) te).core;
89-
}
98+
return ((TileEntityT2) te).core;
9099
} else if (te instanceof TileEntityT3) {
91-
if (((TileEntityT3) te).core != null) {
92-
core = ((TileEntityT3) te).core;
93-
}
100+
return ((TileEntityT3) te).core;
94101
}
95-
return core;
102+
return null;
96103
}
97104

98105
public static void init() {
99106
for (Object obj : Item.itemRegistry) {
100-
101107
Item item = (Item) obj;
102-
String name = item.getUnlocalizedName().toLowerCase();
108+
String rawName = item.getUnlocalizedName().toLowerCase();
109+
String normalizedName = rawName
110+
.replace("tile.", "")
111+
.replace("item.", "")
112+
.replace("_", " ")
113+
.trim();
114+
115+
if (!isResearchItem(normalizedName)) continue;
103116

104-
if (!isResearchItem(name)) continue;
105117
ResearchValue value = new ResearchValue();
106118
for (Map.Entry<String, ResearchValue> entry : ResearchMap.keywordMap.entrySet()) {
107119
String keyword = entry.getKey().toLowerCase();
108-
if (name.contains(keyword)) {
109-
System.out.println("Registered " + name);
110-
System.out.println(name + " contains " + keyword);
120+
if (normalizedName.contains(keyword)) {
121+
System.out.println("Registered " + normalizedName);
122+
System.out.println(normalizedName + " contains " + keyword);
111123
ResearchValue toAdd = entry.getValue();
112124
for (Map.Entry<PointManager.ResearchType, Integer> point : toAdd.getAllPoints().entrySet()) {
113125
value.addPoints(point.getKey(), point.getValue());
@@ -116,25 +128,52 @@ public static void init() {
116128
}
117129

118130
if (!value.getAllPoints().isEmpty()) {
119-
System.out.println("Added " + item.getUnlocalizedName());
120-
Values.put(item.getUnlocalizedName().toLowerCase(), value);
131+
String key = getUniqueItemKey(item, 0); // Default meta for base item
132+
System.out.println("Added " + key);
133+
Values.put(key, value);
121134
}
122-
123135
}
124136
System.out.println("VALUES OF RESEARCHMAPIDK: " + Values.keySet());
125137
}
126138

127-
public static void setValues(ResearchValue value, ItemStack stack) {
128-
ItemResearchPoint.setRp(stack, "CHEMICAL", 0);
129-
System.out.println("setValues called for: " + stack);
139+
private static int getDiminishedValues(String itemKey, int base, String team) {
140+
researchCounts.computeIfAbsent(team, t -> new HashMap<>());
141+
int count = researchCounts.get(team).getOrDefault(itemKey, 0);
142+
return Math.max(1, base / (1 + count));
143+
}
144+
145+
public static void setValues(ResearchValue value, ItemStack outStack, String team, ItemStack sourceStack) {
146+
if (!outStack.hasTagCompound()) {
147+
outStack.setTagCompound(new NBTTagCompound());
148+
}
149+
150+
if (sourceStack == null) {
151+
System.out.println("sourceStack is null, cannot apply diminishing correctly");
152+
return;
153+
}
154+
155+
String sourceKey = getUniqueItemKey(sourceStack); // << use this for diminishing
156+
System.out.println("Got source item key for diminishing: " + sourceKey + " for team: " + team);
157+
158+
ItemResearchPoint.setRp(outStack, "CHEMICAL", 0);
159+
System.out.println("setValues called for: " + outStack);
160+
130161
if (value == null) {
131162
System.out.println("value is null!");
132163
return;
133164
}
165+
134166
Map<PointManager.ResearchType, Integer> map = value.getAllPoints();
135167
System.out.println("Map size: " + map.size());
168+
researchCounts.computeIfAbsent(team, t -> new HashMap<>());
169+
136170
for (Map.Entry<PointManager.ResearchType, Integer> entry : map.entrySet()) {
137-
stack.getTagCompound().setInteger(entry.getKey().toString(), entry.getValue());
171+
int diminishedValue = getDiminishedValues(sourceKey, entry.getValue(), team);
172+
outStack.getTagCompound().setInteger(entry.getKey().toString(), diminishedValue);
138173
}
174+
175+
int current = researchCounts.get(team).getOrDefault(sourceKey, 0);
176+
researchCounts.get(team).put(sourceKey, current + 1);
177+
System.out.println("Applied diminishing for source item: " + sourceKey + " for team: " + team);
139178
}
140-
}
179+
}

0 commit comments

Comments
 (0)