Skip to content

Commit 4bdcfb7

Browse files
committed
Fix duplicate loot
1 parent cdb66d9 commit 4bdcfb7

File tree

3 files changed

+49
-38
lines changed

3 files changed

+49
-38
lines changed

common/src/main/java/com/evandev/fieldguide/client/gui/screens/FieldGuideEntryScreen.java

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -379,27 +379,6 @@ private void setupDropWidget(boolean unlocked) {
379379
drops = FieldGuideCobblemonCompat.getCobblemonDrops(entry);
380380
}
381381

382-
if (unlocked) {
383-
List<ItemStack> additionalDrops = new ArrayList<>();
384-
ClientCategoryManager categoryManager = ClientCategoryManager.getInstance();
385-
for (String addition : categoryManager.getLootAdditions()) {
386-
String[] parts = addition.split("\\|", 2);
387-
if (parts.length == 2 && categoryManager.isLootMatch(entry, new ResourceLocation(parts[1]))) {
388-
ResourceLocation itemId = EntryResolver.getRawId(new ResourceLocation(parts[1]));
389-
ItemStack stack = new ItemStack(net.minecraft.core.registries.BuiltInRegistries.ITEM.get(itemId));
390-
if (!stack.isEmpty()) {
391-
stack.getOrCreateTag().putFloat("FieldGuideDropChance", 100.0f);
392-
additionalDrops.add(stack);
393-
}
394-
}
395-
}
396-
if (!additionalDrops.isEmpty()) {
397-
List<ItemStack> combined = new ArrayList<>(drops);
398-
combined.addAll(additionalDrops);
399-
drops = combined;
400-
}
401-
}
402-
403382
if (!drops.isEmpty()) {
404383
int dropItemSize = 20;
405384
this.addRenderableWidget(new PaginatedGridWidget<>(this.leftPageBounds.left() + 2, this.leftPageBounds.bottom() - 33, this.leftPageBounds.width() - 4, dropItemSize, 5, dropItemSize, 0, drops, (graphics, stack, x, y, mouseX, mouseY) -> {

common/src/main/java/com/evandev/fieldguide/server/LootTableHelper.java

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,21 @@ public static Map<ResourceLocation, List<ItemStack>> generateLootMap(ServerLevel
2828
Map<ResourceLocation, List<ItemStack>> lootMap = new HashMap<>();
2929
Set<ResourceLocation> allEntryIds = ServerFieldGuideManager.getInstance().getAllEntryIds();
3030

31+
Set<Object> uniqueEntries = new HashSet<>();
3132
for (ResourceLocation entryId : allEntryIds) {
32-
EntryResolutionHelper.resolveSingleEntry(entryId, null, null).ifPresent(entry -> {
33-
ResourceLocation tableId = null;
34-
if (entry instanceof EntityType<?> type) tableId = type.getDefaultLootTable();
35-
else if (entry instanceof Block block) tableId = block.getLootTable();
36-
else if (entry instanceof CompositeFieldGuideEntry composite) {
37-
Object display = composite.displayEntry();
38-
if (display instanceof EntityType<?> type) tableId = type.getDefaultLootTable();
39-
else if (display instanceof Block block) tableId = block.getLootTable();
40-
}
41-
processEntry(level, entry, tableId, lootMap);
42-
});
33+
EntryResolutionHelper.resolveSingleEntry(entryId, null, null).ifPresent(uniqueEntries::add);
34+
}
35+
36+
for (Object entry : uniqueEntries) {
37+
ResourceLocation tableId = null;
38+
if (entry instanceof EntityType<?> type) tableId = type.getDefaultLootTable();
39+
else if (entry instanceof Block block) tableId = block.getLootTable();
40+
else if (entry instanceof CompositeFieldGuideEntry composite) {
41+
Object display = composite.displayEntry();
42+
if (display instanceof EntityType<?> type) tableId = type.getDefaultLootTable();
43+
else if (display instanceof Block block) tableId = block.getLootTable();
44+
}
45+
processEntry(level, entry, tableId, lootMap);
4346
}
4447
return lootMap;
4548
}
@@ -68,22 +71,51 @@ private static void processEntry(ServerLevel level, Object entry, ResourceLocati
6871
if (!formattedDrops.isEmpty()) {
6972
ResourceLocation id = AutoPopulateRegistry.getEntryId(entry, true);
7073
if (id != null) {
71-
lootMap.computeIfAbsent(id, k -> new ArrayList<>()).addAll(formattedDrops);
74+
List<ItemStack> existing = lootMap.computeIfAbsent(id, k -> new ArrayList<>());
75+
for (ItemStack newStack : formattedDrops) {
76+
boolean found = false;
77+
for (ItemStack s : existing) {
78+
if (ItemStack.isSameItemSameTags(s, newStack)) {
79+
CompoundTag existingTag = s.getOrCreateTag();
80+
CompoundTag newTag = newStack.getOrCreateTag();
81+
82+
float existingChance = existingTag.getFloat("FieldGuideDropChance");
83+
float newChance = newTag.getFloat("FieldGuideDropChance");
84+
existingTag.putFloat("FieldGuideDropChance", Math.min(100.0f, existingChance + newChance));
85+
86+
int existingMin = existingTag.contains("FieldGuideMin") ? existingTag.getInt("FieldGuideMin") : 1;
87+
int newMin = newTag.contains("FieldGuideMin") ? newTag.getInt("FieldGuideMin") : 1;
88+
existingTag.putInt("FieldGuideMin", Math.min(existingMin, newMin));
89+
90+
int existingMax = existingTag.contains("FieldGuideMax") ? existingTag.getInt("FieldGuideMax") : 1;
91+
int newMax = newTag.contains("FieldGuideMax") ? newTag.getInt("FieldGuideMax") : 1;
92+
existingTag.putInt("FieldGuideMax", Math.max(existingMax, newMax));
93+
94+
found = true;
95+
break;
96+
}
97+
}
98+
if (!found) {
99+
existing.add(newStack);
100+
}
101+
}
72102
}
73103
}
74104
}
75105

76106
private static boolean matchesTarget(Object entry, String targetStr) {
77-
ResourceLocation entryId = EntryResolver.getRawId(EntryResolver.getEntryId(entry));
107+
Object coreEntry = entry instanceof CompositeFieldGuideEntry composite ? composite.displayEntry() : entry;
108+
ResourceLocation entryId = EntryResolver.getRawId(EntryResolver.getEntryId(coreEntry));
109+
78110
if (entryId == null) return false;
79111
if (targetStr.startsWith("#")) {
80112
try {
81113
ResourceLocation tagId = new ResourceLocation(targetStr.substring(1));
82-
if (entry instanceof EntityType<?> type) {
114+
if (coreEntry instanceof EntityType<?> type) {
83115
return BuiltInRegistries.ENTITY_TYPE.getHolder(BuiltInRegistries.ENTITY_TYPE.getResourceKey(type).get()).get().is(TagKey.create(Registries.ENTITY_TYPE, tagId));
84-
} else if (entry instanceof Block block) {
116+
} else if (coreEntry instanceof Block block) {
85117
return BuiltInRegistries.BLOCK.getHolder(BuiltInRegistries.BLOCK.getResourceKey(block).get()).get().is(TagKey.create(Registries.BLOCK, tagId));
86-
} else if (entry instanceof Item item) {
118+
} else if (coreEntry instanceof Item item) {
87119
return BuiltInRegistries.ITEM.getHolder(BuiltInRegistries.ITEM.getResourceKey(item).get()).get().is(TagKey.create(Registries.ITEM, tagId));
88120
}
89121
} catch (Exception ignored) {

common/src/main/resources/assets/fieldguide/lang/en_us.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@
203203
"item.fieldguide.page": "Field Guide Page",
204204
"item.fieldguide.page.entry": "Entry: %s",
205205
"item.fieldguide.page.author": "Author: %s",
206-
"item.fieldguide.page.tooltip": "Right-click to restore this entry to your Field Guide",
206+
"item.fieldguide.page.tooltip": "Right-click to add this entry to your Field Guide",
207207

208208
"gui.fieldguide.rip_out.tooltip": "Rip Out Page",
209209

0 commit comments

Comments
 (0)