Skip to content

Commit

Permalink
Merge branch '1.20.1' into 1.19.4
Browse files Browse the repository at this point in the history
# Conflicts:
#	settings.gradle.kts
  • Loading branch information
rfresh2 committed Feb 18, 2025
2 parents ebfc4a0 + e390c8b commit 7822e60
Show file tree
Hide file tree
Showing 22 changed files with 585 additions and 79 deletions.
2 changes: 1 addition & 1 deletion buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ repositories {
dependencies {
implementation("architectury-plugin:architectury-plugin.gradle.plugin:3.4-SNAPSHOT")
implementation("dev.architectury:architectury-loom:1.9-SNAPSHOT")
implementation("com.gradleup.shadow:shadow-gradle-plugin:8.3.5")
implementation("com.gradleup.shadow:shadow-gradle-plugin:8.3.6")
}

Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
public class DirectChunkHighlightDrawFeature implements ChunkHighlightDrawFeature {
private final DirectChunkHighlightProvider chunkHighlightProvider;
private final HighlightDrawBuffer drawBuffer = new HighlightDrawBuffer();
private static final long REFRESH_INTERVAL_MS = 500L;
private int lastRefreshedHighlightCount = 0;
private final boolean refreshEveryTick;

public DirectChunkHighlightDrawFeature(DirectChunkHighlightProvider chunkHighlightProvider) {
public DirectChunkHighlightDrawFeature(DirectChunkHighlightProvider chunkHighlightProvider, boolean refreshEveryTick) {
this.chunkHighlightProvider = chunkHighlightProvider;
this.refreshEveryTick = refreshEveryTick;
}

@Override
Expand All @@ -33,10 +34,16 @@ public Long2LongMap getChunkHighlights() {
@Override
public void render(boolean worldmap) {
Long2LongMap highlights = getChunkHighlights();
if (lastRefreshedHighlightCount != highlights.size()
&& System.currentTimeMillis() - drawBuffer.lastRefreshed > REFRESH_INTERVAL_MS + ThreadLocalRandom.current().nextInt(0, 100)) {
this.invalidateCache();
lastRefreshedHighlightCount = highlights.size();
if (refreshEveryTick) {
if (System.currentTimeMillis() - drawBuffer.lastRefreshed >= 50L) {
this.invalidateCache();
}
} else {
if (lastRefreshedHighlightCount != highlights.size()
&& System.currentTimeMillis() - drawBuffer.lastRefreshed > 500L + ThreadLocalRandom.current().nextInt(0, 100)) {
this.invalidateCache();
lastRefreshedHighlightCount = highlights.size();
}
}
if (drawBuffer.needsRefresh(worldmap)) {
drawBuffer.refresh(highlights, worldmap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ public class DrawFeatureRegistry {
private final List<String> sortedLineKeySet = new ArrayList<>();

public synchronized void registerDirectChunkHighlightProvider(String id, DirectChunkHighlightSupplier chunkHighlightSupplier, IntSupplier colorSupplier) {
registerChunkHighlightDrawFeature(id, new DirectChunkHighlightDrawFeature(new DirectChunkHighlightProvider(chunkHighlightSupplier, colorSupplier)));
registerChunkHighlightDrawFeature(id, new DirectChunkHighlightDrawFeature(new DirectChunkHighlightProvider(chunkHighlightSupplier, colorSupplier), false));
}

// refresh render buffers every tick instead of lazily
public synchronized void registerDirectChunkHighlightProvider(String id, boolean refreshEveryTick, DirectChunkHighlightSupplier chunkHighlightSupplier, IntSupplier colorSupplier) {
registerChunkHighlightDrawFeature(id, new DirectChunkHighlightDrawFeature(new DirectChunkHighlightProvider(chunkHighlightSupplier, colorSupplier), refreshEveryTick));
}

public synchronized void registerAsyncChunkHighlightProvider(String id, AsyncChunkHighlightSupplier chunkHighlightSupplier, IntSupplier colorSupplier) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import com.github.benmanes.caffeine.cache.AsyncLoadingCache;
import com.github.benmanes.caffeine.cache.Caffeine;
import net.minecraft.client.Minecraft;
import xaeroplus.Globals;
import xaeroplus.module.ModuleManager;
import xaeroplus.module.impl.TickTaskExecutor;
import xaeroplus.util.ChunkUtils;

import java.util.Collections;
Expand All @@ -21,7 +22,7 @@ public LineDrawFeature(LineProvider lineProvider, int refreshIntervalMs) {
this.lineRenderCache = Caffeine.newBuilder()
.expireAfterWrite(10, TimeUnit.SECONDS)
.refreshAfterWrite(refreshIntervalMs, TimeUnit.MILLISECONDS)
.executor(Minecraft.getInstance())
.executor(ModuleManager.getModule(TickTaskExecutor.class))
.buildAsync(k -> loadLinesInWindow());
}

Expand Down
2 changes: 2 additions & 0 deletions common/src/main/java/xaeroplus/module/ModuleManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public class ModuleManager {
new Portals(),
new PortalSkipDetection(),
new RenderDistance(),
new SpawnChunks(),
new SpawnChunksPlayer(),
new TickTaskExecutor(),
new WaystoneSync(),
new WorldBorder(),
Expand Down
49 changes: 49 additions & 0 deletions common/src/main/java/xaeroplus/module/impl/SpawnChunks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package xaeroplus.module.impl;

import net.lenni0451.lambdaevents.EventHandler;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.level.GameRules;
import net.minecraft.world.level.Level;
import xaeroplus.XaeroPlus;
import xaeroplus.event.ClientTickEvent;
import xaeroplus.util.ChunkUtils;

public class SpawnChunks extends SpawnChunksBase {

@EventHandler
public void onClientTick(ClientTickEvent.Post event) {
onClientTick();
}

@Override
public ResourceKey<Level> dimension() {
return Level.OVERWORLD;
}

@Override
int getSpawnRadius() {
int spawnChunkRadius = 2;
if (mc.hasSingleplayerServer()) {
try {
spawnChunkRadius = mc.getSingleplayerServer().getLevel(Level.OVERWORLD).getGameRules().getInt(GameRules.RULE_SPAWN_RADIUS);
} catch (final Exception e) {
XaeroPlus.LOGGER.error("Failed to get spawn radius from singleplayer server", e);
}
}
return spawnChunkRadius;
}

@Override
long getSpawnChunkPos() {
var level = mc.level;
if (level == null) return ChunkUtils.chunkPosToLong(0, 0);
ClientLevel.ClientLevelData levelData = level.getLevelData();
if (levelData == null) return ChunkUtils.chunkPosToLong(0, 0);
int spawnBlockX = levelData.getXSpawn();
int spawnBlockZ = levelData.getZSpawn();
int spawnChunkX = ChunkUtils.posToChunkPos(spawnBlockX);
int spawnChunkZ = ChunkUtils.posToChunkPos(spawnBlockZ);
return ChunkUtils.chunkPosToLong(spawnChunkX, spawnChunkZ);
}
}
192 changes: 192 additions & 0 deletions common/src/main/java/xaeroplus/module/impl/SpawnChunksBase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
package xaeroplus.module.impl;

import net.minecraft.resources.ResourceKey;
import net.minecraft.world.level.Level;
import xaeroplus.Globals;
import xaeroplus.feature.render.Line;
import xaeroplus.module.Module;
import xaeroplus.settings.Settings;
import xaeroplus.util.ChunkUtils;
import xaeroplus.util.ColorHelper;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public abstract class SpawnChunksBase extends Module {
final String entityProcessingId = getClass().getName() + "$EntityProcessing";
final String redstoneProcessingId = getClass().getName() + "$RedstoneProcessing";
final String lazyChunkId = getClass().getName() + "$LazyChunk";
final String outerChunksId = getClass().getName() + "$OuterChunks";
final List<Line> entityProcessingCache = new ArrayList<>();
final List<Line> redstoneProcessingCache = new ArrayList<>();
final List<Line> lazyChunksCache = new ArrayList<>();
final List<Line> outerChunksCache = new ArrayList<>();
int alpha = 204;
int entityProcessingColor = ColorHelper.getColor(0, 255, 0, alpha);
int redstoneProcessingColor = ColorHelper.getColor(255, 0, 0, alpha);
int lazyChunksColor = ColorHelper.getColor(0, 0, 255, alpha);
int outerChunksColor = ColorHelper.getColor(255, 255, 0, alpha);
float lineWidth = 0.1f;

public abstract ResourceKey<Level> dimension();

abstract int getSpawnRadius();

abstract long getSpawnChunkPos();

void onClientTick() {
updateCaches();
}

@Override
public void onEnable() {
Globals.drawManager.registry().registerLineProvider(
entityProcessingId,
this::entityProcessing,
this::entityProcessingColor,
this::getLineWidth,
50
);
Globals.drawManager.registry().registerLineProvider(
redstoneProcessingId,
this::redstoneProcessing,
this::redstoneProcessingColor,
this::getLineWidth,
50
);
Globals.drawManager.registry().registerLineProvider(
lazyChunkId,
this::lazyChunks,
this::lazyChunksColor,
this::getLineWidth,
50
);
Globals.drawManager.registry().registerLineProvider(
outerChunksId,
this::outerChunks,
this::outerChunksColor,
this::getLineWidth,
50
);
}

@Override
public void onDisable() {
Globals.drawManager.registry().unregisterLineProvider(entityProcessingId);
Globals.drawManager.registry().unregisterLineProvider(redstoneProcessingId);
Globals.drawManager.registry().unregisterLineProvider(lazyChunkId);
Globals.drawManager.registry().unregisterLineProvider(outerChunksId);
}

public List<Line> entityProcessing(final int windowRegionX, final int windowRegionZ, final int windowRegionSize, ResourceKey<Level> dimension) {
if (dimension != dimension()) return Collections.emptyList();
return entityProcessingCache;
}

public List<Line> redstoneProcessing(final int windowRegionX, final int windowRegionZ, final int windowRegionSize, ResourceKey<Level> dimension) {
if (dimension != dimension()) return Collections.emptyList();
return redstoneProcessingCache;
}

public List<Line> lazyChunks(final int windowRegionX, final int windowRegionZ, final int windowRegionSize, ResourceKey<Level> dimension) {
if (dimension != dimension()) return Collections.emptyList();
return lazyChunksCache;
}

public List<Line> outerChunks(final int windowRegionX, final int windowRegionZ, final int windowRegionSize, ResourceKey<Level> dimension) {
if (dimension != dimension()) return Collections.emptyList();
return outerChunksCache;
}

int entityProcessingColor() {
return entityProcessingColor;
}

int redstoneProcessingColor() {
return redstoneProcessingColor;
}

int lazyChunksColor() {
return lazyChunksColor;
}

int outerChunksColor() {
return outerChunksColor;
}

public void setEntityProcessingColor(final int color) {
entityProcessingColor = ColorHelper.getColorWithAlpha(color, alpha);
}

public void setRedstoneProcessingColor(final int color) {
redstoneProcessingColor = ColorHelper.getColorWithAlpha(color, alpha);
}

public void setLazyChunksColor(final int color) {
lazyChunksColor = ColorHelper.getColorWithAlpha(color, alpha);
}

public void setOuterChunksColor(final int color) {
outerChunksColor = ColorHelper.getColorWithAlpha(color, alpha);
}

public void setAlpha(final int alpha) {
this.alpha = alpha;
setEntityProcessingColor(entityProcessingColor);
setRedstoneProcessingColor(redstoneProcessingColor);
setLazyChunksColor(lazyChunksColor);
setOuterChunksColor(outerChunksColor);
}

public float getLineWidth() {
return lineWidth;
}

public void setLineWidth(final float lineWidth) {
this.lineWidth = lineWidth;
}

void updateCaches() {
int spawnChunkRadius = getSpawnRadius();
clearCaches();
var level = mc.level;
if (level == null) return;
long spawnChunkPosLong = getSpawnChunkPos();
int spawnChunkX = ChunkUtils.longToChunkX(spawnChunkPosLong);
int spawnChunkZ = ChunkUtils.longToChunkZ(spawnChunkPosLong);
int lazyRadius = spawnChunkRadius + 1;
int redstoneRadius = spawnChunkRadius;
int entityProcessingRadius = spawnChunkRadius - 1;
int worldGenRadius = lazyRadius + 11;

populateCache(entityProcessingCache, spawnChunkX, spawnChunkZ, entityProcessingRadius);
if (Settings.REGISTRY.spawnChunksRedstoneProcessingEnabled.get())
populateCache(redstoneProcessingCache, spawnChunkX, spawnChunkZ, redstoneRadius);
populateCache(lazyChunksCache, spawnChunkX, spawnChunkZ, lazyRadius);
if (Settings.REGISTRY.spawnChunksOuterChunksEnabled.get())
populateCache(outerChunksCache, spawnChunkX, spawnChunkZ, worldGenRadius);
}

void populateCache(List<Line> cache, int centerX, int centerZ, int radius) {
int minChunkX = centerX - radius;
int maxChunkX = centerX + radius;
int minChunkZ = centerZ - radius;
int maxChunkZ = centerZ + radius;
int minBlockX = ChunkUtils.chunkCoordToCoord(minChunkX);
int maxBlockX = ChunkUtils.chunkCoordToCoord(maxChunkX + 1);
int minBlockZ = ChunkUtils.chunkCoordToCoord(minChunkZ);
int maxBlockZ = ChunkUtils.chunkCoordToCoord(maxChunkZ + 1);
cache.add(new Line(minBlockX, minBlockZ, maxBlockX, minBlockZ));
cache.add(new Line(minBlockX, minBlockZ, minBlockX, maxBlockZ));
cache.add(new Line(maxBlockX, minBlockZ, maxBlockX, maxBlockZ));
cache.add(new Line(minBlockX, maxBlockZ, maxBlockX, maxBlockZ));
}

void clearCaches() {
entityProcessingCache.clear();
redstoneProcessingCache.clear();
lazyChunksCache.clear();
outerChunksCache.clear();
}
}
32 changes: 32 additions & 0 deletions common/src/main/java/xaeroplus/module/impl/SpawnChunksPlayer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package xaeroplus.module.impl;

import net.lenni0451.lambdaevents.EventHandler;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.level.Level;
import xaeroplus.event.ClientTickEvent;
import xaeroplus.util.ChunkUtils;

public class SpawnChunksPlayer extends SpawnChunksBase {

@EventHandler
public void onClientTick(ClientTickEvent.Post event) {
onClientTick();
}

@Override
public ResourceKey<Level> dimension() {
return ChunkUtils.getActualDimension();
}

@Override
int getSpawnRadius() {
var level = mc.level;
if (level == null) return 0;
return level.getServerSimulationDistance();
}

@Override
long getSpawnChunkPos() {
return ChunkUtils.chunkPosToLong(ChunkUtils.actualPlayerChunkX(), ChunkUtils.actualPlayerChunkZ());
}
}
Loading

0 comments on commit 7822e60

Please sign in to comment.