Skip to content

Commit 7e10c86

Browse files
committed
Loot table reworks
1 parent b9a95a4 commit 7e10c86

17 files changed

Lines changed: 421 additions & 247 deletions

File tree

common/src/main/java/com/evandev/fieldguide/client/ClientFieldGuideManager.java

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import com.evandev.fieldguide.config.ModConfig;
99
import com.evandev.fieldguide.data.Category;
1010
import com.evandev.fieldguide.data.CategoryEntry;
11-
import com.evandev.fieldguide.network.RequestDropsPacket;
12-
import com.evandev.fieldguide.platform.Services;
1311
import com.google.gson.*;
1412
import net.minecraft.client.Minecraft;
1513
import net.minecraft.client.resources.language.I18n;
@@ -65,7 +63,6 @@ public class ClientFieldGuideManager implements ResourceManagerReloadListener {
6563
private final Set<String> seenEntries = new HashSet<>();
6664
private final Map<String, Long> discoveryTimes = new HashMap<>();
6765
private final Map<Object, List<ItemStack>> dropCache = new HashMap<>();
68-
private final Set<Object> requestedDrops = new HashSet<>();
6966
private Path currentSavePath = null;
7067

7168
// Scanning State
@@ -145,6 +142,21 @@ public void updateCategoriesFromServer(List<Category> categories) {
145142
resolveAllEntries();
146143
}
147144

145+
public void updateLootCache(Map<ResourceLocation, List<ItemStack>> lootCache) {
146+
for (Map.Entry<ResourceLocation, List<ItemStack>> entry : lootCache.entrySet()) {
147+
ResourceLocation id = entry.getKey();
148+
List<ItemStack> drops = entry.getValue();
149+
150+
Optional<EntityType<?>> type = BuiltInRegistries.ENTITY_TYPE.getOptional(id);
151+
if (type.isPresent()) {
152+
dropCache.put(type.get(), drops);
153+
} else {
154+
Optional<Block> block = BuiltInRegistries.BLOCK.getOptional(id);
155+
block.ifPresent(b -> dropCache.put(b, drops));
156+
}
157+
}
158+
}
159+
148160
public CategoryVisual getCategoryVisual(ResourceLocation categoryId) {
149161
return categoryVisuals.getOrDefault(categoryId, CategoryVisual.DEFAULT);
150162
}
@@ -717,20 +729,7 @@ public long getDiscoveryTime(Object entry) {
717729
}
718730

719731
public List<ItemStack> getDrops(Object entry) {
720-
if (dropCache.containsKey(entry)) return dropCache.get(entry);
721-
if (!requestedDrops.contains(entry)) {
722-
ResourceLocation id = getEntryId(entry);
723-
if (id != null) {
724-
requestedDrops.add(entry);
725-
Services.NETWORK.sendToServer(new RequestDropsPacket(id));
726-
}
727-
}
728-
return Collections.emptyList();
729-
}
730-
731-
public void setDrops(ResourceLocation entryId, List<ItemStack> drops) {
732-
getValidEntries().stream().filter(e -> Objects.equals(getEntryId(e), entryId))
733-
.findFirst().ifPresent(o -> dropCache.put(o, drops));
732+
return dropCache.getOrDefault(entry, Collections.emptyList());
734733
}
735734

736735
public void onWorldLoad(String serverIdentifier) {

common/src/main/java/com/evandev/fieldguide/config/ModConfig.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class ModConfig {
3939
public List<String> entityBlacklist = new ArrayList<>();
4040

4141
public List<String> lootRemovals = new ArrayList<>();
42-
public List<String> lootAdditions = new ArrayList<>();
42+
public List<String> lootAdditions = getDefaultLootAdditions();
4343

4444
public List<String> biomeRemovals = new ArrayList<>();
4545
public List<String> biomeAdditions = getDefaultBiomeAdditions();
@@ -84,6 +84,12 @@ public static List<String> getDefaultBlacklist() {
8484
return defaults;
8585
}
8686

87+
public static List<String> getDefaultLootAdditions() {
88+
List<String> defaults = new ArrayList<>();
89+
defaults.add("minecraft:wither|minecraft:nether_star");
90+
return defaults;
91+
}
92+
8793
public static List<String> getDefaultBiomeAdditions() {
8894
List<String> defaults = new ArrayList<>();
8995
// Nether Fortress Mobs

common/src/main/java/com/evandev/fieldguide/network/RequestDropsPacket.java

Lines changed: 0 additions & 24 deletions
This file was deleted.

common/src/main/java/com/evandev/fieldguide/network/SyncDropsPacket.java

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.evandev.fieldguide.network;
2+
3+
import net.minecraft.network.FriendlyByteBuf;
4+
import net.minecraft.resources.ResourceLocation;
5+
import net.minecraft.world.item.ItemStack;
6+
7+
import java.util.HashMap;
8+
import java.util.List;
9+
import java.util.Map;
10+
11+
public class SyncLootPacket {
12+
private final Map<ResourceLocation, List<ItemStack>> lootCache;
13+
14+
public SyncLootPacket(Map<ResourceLocation, List<ItemStack>> lootCache) {
15+
this.lootCache = lootCache;
16+
}
17+
18+
public SyncLootPacket(FriendlyByteBuf buf) {
19+
this.lootCache = buf.readMap(
20+
FriendlyByteBuf::readResourceLocation,
21+
b -> b.readList(FriendlyByteBuf::readItem)
22+
);
23+
}
24+
25+
public void encode(FriendlyByteBuf buf) {
26+
buf.writeMap(
27+
lootCache,
28+
FriendlyByteBuf::writeResourceLocation,
29+
(b, list) -> b.writeCollection(list, FriendlyByteBuf::writeItem)
30+
);
31+
}
32+
33+
public Map<ResourceLocation, List<ItemStack>> getLootCache() {
34+
return lootCache;
35+
}
36+
}

common/src/main/java/com/evandev/fieldguide/platform/services/IPlatformHelper.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.evandev.fieldguide.platform.services;
22

3+
import net.minecraft.server.level.ServerLevel;
4+
import net.minecraft.server.level.ServerPlayer;
5+
36
import java.nio.file.Path;
47

58
public interface IPlatformHelper {
@@ -42,4 +45,9 @@ default String getEnvironmentName() {
4245
*/
4346
Path getConfigDirectory();
4447

48+
/**
49+
* Gets a fake player for the given level to simulate player-only loot context.
50+
*/
51+
ServerPlayer getFakePlayer(ServerLevel level);
52+
4553
}

0 commit comments

Comments
 (0)