Skip to content

Commit cb17b98

Browse files
committed
Bug fixes, WIP multiblock scan
1 parent 3d9adf6 commit cb17b98

3 files changed

Lines changed: 102 additions & 11 deletions

File tree

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

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.evandev.fieldguide.util.EntryResolver;
1515
import net.minecraft.client.resources.language.I18n;
1616
import net.minecraft.core.registries.BuiltInRegistries;
17+
import net.minecraft.nbt.CompoundTag;
1718
import net.minecraft.network.chat.Component;
1819
import net.minecraft.resources.ResourceLocation;
1920
import net.minecraft.server.packs.resources.ResourceManager;
@@ -178,7 +179,6 @@ public void updateCategoriesFromServer(List<Category> categories) {
178179
resolveAllEntries();
179180
}
180181

181-
182182
public void updateLootCache(Map<ResourceLocation, List<ItemStack>> lootCache) {
183183
this.dropCache.clear();
184184

@@ -261,9 +261,26 @@ private void loadVisuals(ResourceManager mgr, String folder, BiConsumer<Resource
261261
private void resolveAllEntries() {
262262
resolvedCategoryEntries.clear();
263263
ModConfig config = ModConfig.get();
264+
Set<Object> allCompositeComponents = new HashSet<>();
265+
264266
syncedCategories.values().forEach(category -> {
265-
resolvedCategoryEntries.put(category.getId(), EntryResolver.resolveCategoryEntries(category, config));
267+
List<Object> entries = EntryResolver.resolveCategoryEntries(category, config);
268+
for (Object entry : entries) {
269+
if (entry instanceof CompositeFieldGuideEntry composite) {
270+
if (composite.components() != null) {
271+
allCompositeComponents.addAll(composite.components());
272+
}
273+
if (composite.displayEntry() != null) {
274+
allCompositeComponents.add(composite.displayEntry());
275+
}
276+
}
277+
}
278+
resolvedCategoryEntries.put(category.getId(), entries);
266279
});
280+
281+
for (List<Object> entries : resolvedCategoryEntries.values()) {
282+
entries.removeIf(entry -> !(entry instanceof CompositeFieldGuideEntry) && allCompositeComponents.contains(entry));
283+
}
267284
}
268285

269286
public List<Object> getEntriesForCategory(Category category) {
@@ -291,25 +308,45 @@ public List<Object> searchEntries(String query) {
291308
}
292309

293310
public List<ItemStack> getDrops(Object entry) {
294-
List<ItemStack> rawDrops;
311+
List<ItemStack> rawDrops = new ArrayList<>();
295312
if (entry instanceof CompositeFieldGuideEntry composite) {
296-
rawDrops = new ArrayList<>(dropCache.getOrDefault(composite.displayEntry(), Collections.emptyList()));
297-
for (Object comp : composite.components()) {
313+
Set<Object> uniqueComponents = new HashSet<>();
314+
if (composite.displayEntry() != null) uniqueComponents.add(composite.displayEntry());
315+
if (composite.components() != null) uniqueComponents.addAll(composite.components());
316+
for (Object comp : uniqueComponents) {
298317
rawDrops.addAll(dropCache.getOrDefault(comp, Collections.emptyList()));
299318
}
300319
} else {
301-
rawDrops = dropCache.getOrDefault(entry, Collections.emptyList());
320+
rawDrops.addAll(dropCache.getOrDefault(entry, Collections.emptyList()));
302321
}
303322

304323
List<ItemStack> distinct = new ArrayList<>();
305324
for (ItemStack stack : rawDrops) {
306-
if (distinct.stream().noneMatch(s -> ItemStack.isSameItemSameTags(s, stack))) {
325+
if (distinct.stream().noneMatch(s -> isSameLootItem(s, stack))) {
307326
distinct.add(stack);
308327
}
309328
}
310329
return distinct;
311330
}
312331

332+
private boolean isSameLootItem(ItemStack a, ItemStack b) {
333+
if (!ItemStack.isSameItem(a, b)) return false;
334+
if (a.getTag() == b.getTag()) return true;
335+
if (a.getTag() == null || b.getTag() == null) return false;
336+
337+
CompoundTag tagA = a.getTag().copy();
338+
tagA.remove("FieldGuideDropChance");
339+
tagA.remove("FieldGuideMin");
340+
tagA.remove("FieldGuideMax");
341+
342+
CompoundTag tagB = b.getTag().copy();
343+
tagB.remove("FieldGuideDropChance");
344+
tagB.remove("FieldGuideMin");
345+
tagB.remove("FieldGuideMax");
346+
347+
return tagA.equals(tagB);
348+
}
349+
313350
public void onClientTick(net.minecraft.client.Minecraft minecraft) {
314351
FieldGuideScanner.getInstance().onClientTick(minecraft);
315352
}

common/src/main/java/com/evandev/fieldguide/client/render/ScanOverlayRenderer.java

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
import net.minecraft.util.Mth;
2222
import net.minecraft.util.RandomSource;
2323
import net.minecraft.world.entity.Entity;
24-
import net.minecraft.world.level.block.Block;
25-
import net.minecraft.world.level.block.RenderShape;
24+
import net.minecraft.world.level.block.*;
2625
import net.minecraft.world.level.block.state.BlockState;
2726
import net.minecraft.world.phys.Vec3;
2827
import net.minecraft.world.phys.shapes.CollisionContext;
@@ -79,14 +78,34 @@ public static void render(PoseStack poseStack, float partialTick, Camera camera,
7978
}
8079
}
8180

81+
private static boolean isMultiblockPlant(Block block) {
82+
return block instanceof CactusBlock ||
83+
block instanceof SugarCaneBlock ||
84+
block instanceof BambooStalkBlock ||
85+
block instanceof KelpBlock ||
86+
block instanceof KelpPlantBlock ||
87+
block instanceof TallGrassBlock ||
88+
block instanceof DoublePlantBlock ||
89+
block instanceof VineBlock ||
90+
block instanceof WeepingVinesBlock ||
91+
block instanceof WeepingVinesPlantBlock ||
92+
block instanceof TwistingVinesBlock ||
93+
block instanceof TwistingVinesPlantBlock ||
94+
block instanceof CaveVinesBlock ||
95+
block instanceof CaveVinesPlantBlock ||
96+
block instanceof ChorusPlantBlock ||
97+
block instanceof ChorusFlowerBlock;
98+
}
99+
82100
private static void renderBlockOverlay(PoseStack poseStack, float partialTick, Vec3 camPos, MultiBufferSource.BufferSource bufferSource, BlockPos targetBlock, FieldGuideScanner scanner, Minecraft mc, float red, float green, float blue, float alpha) {
83101
boolean isOutOfRange = scanner.getOutOfRangePos() != null && targetBlock == scanner.getOutOfRangePos();
84102
float progress = isOutOfRange ? 1.0f : (scanner.getScanningTarget() != null ? scanner.getScanProgress(partialTick) : scanner.getFadeProgress(partialTick));
85103
if (progress <= 0.0f) return;
86104

87105
float fillHeight = scanner.getScanningTarget() != null ? progress : 1.0f;
88106

89-
Object entry = ClientFieldGuideManager.getInstance().getEntryForTarget(mc.level.getBlockState(targetBlock).getBlock());
107+
Block targetBlockType = mc.level.getBlockState(targetBlock).getBlock();
108+
Object entry = ClientFieldGuideManager.getInstance().getEntryForTarget(targetBlockType);
90109
Set<BlockPos> blocksToRender = new HashSet<>();
91110
blocksToRender.add(targetBlock);
92111

@@ -108,6 +127,24 @@ private static void renderBlockOverlay(PoseStack poseStack, float partialTick, V
108127
}
109128
}
110129
}
130+
} else if (isMultiblockPlant(targetBlockType)) {
131+
Queue<BlockPos> queue = new LinkedList<>();
132+
queue.add(targetBlock);
133+
int maxBlocks = 100;
134+
135+
while (!queue.isEmpty() && blocksToRender.size() < maxBlocks) {
136+
BlockPos pos = queue.poll();
137+
for (Direction dir : Direction.values()) {
138+
BlockPos neighbor = pos.relative(dir);
139+
if (!blocksToRender.contains(neighbor)) {
140+
Block neighborBlock = mc.level.getBlockState(neighbor).getBlock();
141+
if (neighborBlock == targetBlockType) {
142+
blocksToRender.add(neighbor);
143+
queue.add(neighbor);
144+
}
145+
}
146+
}
147+
}
111148
}
112149

113150
int minY = Integer.MAX_VALUE;

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,25 @@ public void syncToPlayer(ServerPlayer player) {
8383
private void resolveAllCategories() {
8484
resolvedCategoryEntries.clear();
8585
ModConfig config = ModConfig.get();
86+
Set<Object> allCompositeComponents = new HashSet<>();
87+
8688
for (Category cat : categories.values()) {
87-
resolvedCategoryEntries.put(cat.getId(), EntryResolver.resolveCategoryEntries(cat, config));
89+
List<Object> entries = EntryResolver.resolveCategoryEntries(cat, config);
90+
for (Object entry : entries) {
91+
if (entry instanceof CompositeFieldGuideEntry composite) {
92+
if (composite.components() != null) {
93+
allCompositeComponents.addAll(composite.components());
94+
}
95+
if (composite.displayEntry() != null) {
96+
allCompositeComponents.add(composite.displayEntry());
97+
}
98+
}
99+
}
100+
resolvedCategoryEntries.put(cat.getId(), entries);
101+
}
102+
103+
for (List<Object> entries : resolvedCategoryEntries.values()) {
104+
entries.removeIf(entry -> !(entry instanceof CompositeFieldGuideEntry) && allCompositeComponents.contains(entry));
88105
}
89106
}
90107

0 commit comments

Comments
 (0)