Skip to content

Commit 8303467

Browse files
committed
ESP and Datapack Improvements
1 parent b2f03bf commit 8303467

File tree

3 files changed

+98
-21
lines changed

3 files changed

+98
-21
lines changed

src/main/java/dev/xpple/seedmapper/datapack/DatapackStructureManager.java

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -489,15 +489,21 @@ public DimensionContext getDimensionContext(int dimensionId) {
489489
return context;
490490
}
491491

492-
public StructureResult resolveStructure(CustomStructureSet set, DimensionContext context, ChunkPos chunkPos, WorldgenRandom selectionRandom) {
492+
public StructureResult resolveStructure(CustomStructureSet set, DimensionContext context, ChunkPos chunkPos, WorldgenRandom selectionRandom, Predicate<StructureSetEntry> entryFilter) {
493+
if (entryFilter == null) {
494+
entryFilter = entry -> true;
495+
}
493496
String cacheKey = set.id() + ":" + context.dimensionId();
494497
Map<Long, StructureResult> cache = this.structureCache.computeIfAbsent(cacheKey, _ -> new HashMap<>());
495498
long chunkKey = chunkPos.toLong();
496499
StructureResult cached = cache.get(chunkKey);
497500
if (cached != null) {
498-
return cached;
501+
StructureSetEntry cachedEntry = cached.entry();
502+
if (cachedEntry == null || entryFilter.test(cachedEntry)) {
503+
return cached;
504+
}
499505
}
500-
StructureSetEntry entry = set.selectEntry(selectionRandom);
506+
StructureSetEntry entry = set.selectEntry(selectionRandom, entryFilter);
501507
if (entry == null) {
502508
cache.put(chunkKey, StructureResult.EMPTY);
503509
return StructureResult.EMPTY;
@@ -632,18 +638,43 @@ public List<StructureSetEntry> entries() {
632638
}
633639

634640
public StructureSetEntry selectEntry(WorldgenRandom random) {
641+
return selectEntry(random, entry -> true);
642+
}
643+
644+
public StructureSetEntry selectEntry(WorldgenRandom random, Predicate<StructureSetEntry> filter) {
635645
if (this.totalWeight <= 0) {
636646
return null;
637647
}
638-
int roll = random.nextInt(this.totalWeight);
648+
if (filter == null) {
649+
filter = entry -> true;
650+
}
651+
int filteredSum = 0;
652+
for (StructureSetEntry entry : this.entries) {
653+
if (filter.test(entry)) {
654+
filteredSum += entry.weight();
655+
}
656+
}
657+
if (filteredSum <= 0) {
658+
return null;
659+
}
660+
int roll = random.nextInt(filteredSum);
639661
int accumulator = 0;
640662
for (StructureSetEntry entry : this.entries) {
663+
if (!filter.test(entry)) {
664+
continue;
665+
}
641666
accumulator += entry.weight();
642667
if (roll < accumulator) {
643668
return entry;
644669
}
645670
}
646-
return this.entries.get(this.entries.size() - 1);
671+
for (int i = this.entries.size() - 1; i >= 0; i--) {
672+
StructureSetEntry entry = this.entries.get(i);
673+
if (filter.test(entry)) {
674+
return entry;
675+
}
676+
}
677+
return null;
647678
}
648679

649680
public RandomSpreadCandidate sampleRandomSpread(long seed, int regionX, int regionZ) {

src/main/java/dev/xpple/seedmapper/seedmap/LootTableScreen.java

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ public final class LootTableScreen extends Screen {
8686
private final Map<LootExportHelper.LootEntry, Double> entryDistanceSq = new HashMap<>();
8787
private List<LootExportHelper.LootEntry> filteredEntries = new ArrayList<>();
8888
private final List<Button> rowButtons = new ArrayList<>();
89-
private final Map<String, Long> activeHighlights = new HashMap<>();
9089
private final Map<String, String> activeWaypoints = new HashMap<>();
90+
private static final Map<String, HighlightRecord> GLOBAL_HIGHLIGHTS = new HashMap<>();
9191

9292
private EditBox searchField;
9393
private Button scrollUpButton;
@@ -266,14 +266,14 @@ private void rebuildRowButtons() {
266266
private void toggleHighlight(LootExportHelper.LootEntry entry) {
267267
String key = highlightKey(entry);
268268
if (isHighlightActive(entry)) {
269-
activeHighlights.remove(key);
270-
RenderManager.clearHighlight(getActionPos(entry));
269+
removeHighlightState(key);
271270
rebuildRowButtons();
272271
return;
273272
}
274-
RenderManager.drawBoxes(List.of(getActionPos(entry)), Configs.BlockHighlightESP, 0xFFFFCC00);
273+
BlockPos highlightPos = getActionPos(entry);
274+
RenderManager.drawBoxes(List.of(highlightPos), Configs.BlockHighlightESP, 0xFFFFCC00);
275275
long timeoutMs = (long)Math.max(0.0, Configs.EspTimeoutMinutes * 60_000.0);
276-
activeHighlights.put(key, System.currentTimeMillis() + timeoutMs);
276+
GLOBAL_HIGHLIGHTS.put(key, new HighlightRecord(highlightPos, System.currentTimeMillis() + timeoutMs));
277277
rebuildRowButtons();
278278
}
279279

@@ -381,35 +381,56 @@ private String waypointKey(LootExportHelper.LootEntry entry) {
381381
}
382382

383383
private boolean isHighlightActive(LootExportHelper.LootEntry entry) {
384-
Long expiresAt = activeHighlights.get(highlightKey(entry));
385-
if (expiresAt == null) {
384+
return isHighlightKeyActive(highlightKey(entry));
385+
}
386+
387+
private static boolean isHighlightKeyActive(String key) {
388+
HighlightRecord record = GLOBAL_HIGHLIGHTS.get(key);
389+
if (record == null) {
386390
return false;
387391
}
388-
if (expiresAt <= System.currentTimeMillis()) {
389-
activeHighlights.remove(highlightKey(entry));
392+
if (record.expiresAt <= System.currentTimeMillis()) {
393+
removeHighlightState(key);
390394
return false;
391395
}
392396
return true;
393397
}
394398

395-
private boolean pruneHighlightStates() {
399+
private static boolean pruneHighlightStates() {
396400
boolean changed = false;
397401
long now = System.currentTimeMillis();
398402
List<String> expired = new ArrayList<>();
399-
for (Map.Entry<String, Long> entry : activeHighlights.entrySet()) {
400-
if (entry.getValue() == null || entry.getValue() <= now) {
403+
for (Map.Entry<String, HighlightRecord> entry : GLOBAL_HIGHLIGHTS.entrySet()) {
404+
if (entry.getValue() == null || entry.getValue().expiresAt <= now) {
401405
expired.add(entry.getKey());
402406
}
403407
}
404408
if (!expired.isEmpty()) {
405409
for (String key : expired) {
406-
activeHighlights.remove(key);
410+
removeHighlightState(key);
407411
}
408412
changed = true;
409413
}
410414
return changed;
411415
}
412416

417+
private static void removeHighlightState(String key) {
418+
HighlightRecord record = GLOBAL_HIGHLIGHTS.remove(key);
419+
if (record != null) {
420+
RenderManager.clearHighlight(record.pos);
421+
}
422+
}
423+
424+
private static final class HighlightRecord {
425+
final BlockPos pos;
426+
final long expiresAt;
427+
428+
HighlightRecord(BlockPos pos, long expiresAt) {
429+
this.pos = pos;
430+
this.expiresAt = expiresAt;
431+
}
432+
}
433+
413434
private boolean isWaypointActive(LootExportHelper.LootEntry entry) {
414435
String key = waypointKey(entry);
415436
if (activeWaypoints.containsKey(key)) {

src/main/java/dev/xpple/seedmapper/seedmap/SeedMapScreen.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@
143143
import java.util.Objects;
144144
import java.util.Optional;
145145
import java.util.OptionalInt;
146+
import java.util.function.Predicate;
146147
import java.util.function.ToIntBiFunction;
147148
import java.util.stream.IntStream;
148149
import java.util.stream.Stream;
@@ -3606,7 +3607,22 @@ private java.util.List<CustomStructureMarker> buildCustomStructureTile(
36063607
if (!this.tileIntersectsWorldBorder(minChunkX, maxChunkX, minChunkZ, maxChunkZ)) {
36073608
return markers;
36083609
}
3610+
java.util.Set<String> disabledStructureIds = Configs.getDatapackStructureDisabled(this.structureCompletionKey);
3611+
Predicate<DatapackStructureManager.StructureSetEntry> entryFilter = entry ->
3612+
entry != null && entry.custom() && !disabledStructureIds.contains(entry.id());
3613+
java.util.List<DatapackStructureManager.CustomStructureSet> enabledSets = new java.util.ArrayList<>();
36093614
for (DatapackStructureManager.CustomStructureSet set : sets) {
3615+
for (DatapackStructureManager.StructureSetEntry entry : set.entries()) {
3616+
if (entryFilter.test(entry)) {
3617+
enabledSets.add(set);
3618+
break;
3619+
}
3620+
}
3621+
}
3622+
if (enabledSets.isEmpty()) {
3623+
return markers;
3624+
}
3625+
for (DatapackStructureManager.CustomStructureSet set : enabledSets) {
36103626
this.customStructureLoadingLabel.set(set.id());
36113627
StructurePlacement placement = set.placement();
36123628
if (placement instanceof RandomSpreadStructurePlacement randomPlacement) {
@@ -3626,11 +3642,14 @@ private java.util.List<CustomStructureMarker> buildCustomStructureTile(
36263642
if (!placement.isStructureChunk(context.structureState(), chunkPos.x, chunkPos.z)) {
36273643
continue;
36283644
}
3629-
DatapackStructureManager.StructureResult result = worldgen.resolveStructure(set, context, chunkPos, candidate.random());
3645+
DatapackStructureManager.StructureResult result = worldgen.resolveStructure(set, context, chunkPos, candidate.random(), entryFilter);
36303646
if (result == null || !result.isPresent()) {
36313647
continue;
36323648
}
36333649
DatapackStructureManager.StructureSetEntry entry = result.entry();
3650+
if (!entryFilter.test(entry)) {
3651+
continue;
3652+
}
36343653
if (entry == null || !entry.custom()) {
36353654
continue;
36363655
}
@@ -3652,11 +3671,14 @@ private java.util.List<CustomStructureMarker> buildCustomStructureTile(
36523671
continue;
36533672
}
36543673
WorldgenRandom random = DatapackStructureManager.createSelectionRandom(this.seed, chunkPos.x, chunkPos.z, placement);
3655-
DatapackStructureManager.StructureResult result = worldgen.resolveStructure(set, context, chunkPos, random);
3674+
DatapackStructureManager.StructureResult result = worldgen.resolveStructure(set, context, chunkPos, random, entryFilter);
36563675
if (result == null || !result.isPresent()) {
36573676
continue;
36583677
}
36593678
DatapackStructureManager.StructureSetEntry entry = result.entry();
3679+
if (!entryFilter.test(entry)) {
3680+
continue;
3681+
}
36603682
if (entry == null || !entry.custom()) {
36613683
continue;
36623684
}
@@ -3675,11 +3697,14 @@ private java.util.List<CustomStructureMarker> buildCustomStructureTile(
36753697
}
36763698
ChunkPos chunkPos = new ChunkPos(chunkX, chunkZ);
36773699
WorldgenRandom random = DatapackStructureManager.createSelectionRandom(this.seed, chunkX, chunkZ, placement);
3678-
DatapackStructureManager.StructureResult result = worldgen.resolveStructure(set, context, chunkPos, random);
3700+
DatapackStructureManager.StructureResult result = worldgen.resolveStructure(set, context, chunkPos, random, entryFilter);
36793701
if (result == null || !result.isPresent()) {
36803702
continue;
36813703
}
36823704
DatapackStructureManager.StructureSetEntry entry = result.entry();
3705+
if (!entryFilter.test(entry)) {
3706+
continue;
3707+
}
36833708
if (entry == null || !entry.custom()) {
36843709
continue;
36853710
}

0 commit comments

Comments
 (0)