Skip to content

Commit

Permalink
Misc changes reviewing 1.21.2 update for Paper
Browse files Browse the repository at this point in the history
  • Loading branch information
Spottedleaf committed Oct 22, 2024
1 parent 03784b8 commit 19c96e3
Show file tree
Hide file tree
Showing 15 changed files with 85 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import net.minecraft.world.level.chunk.ChunkAccess;
import net.minecraft.world.level.chunk.LevelChunk;
import net.minecraft.world.level.chunk.ProtoChunk;
import net.minecraft.world.level.chunk.status.ChunkStatusTasks;
import net.minecraft.world.level.chunk.storage.SerializableChunkData;
import net.minecraft.world.level.entity.EntityTypeTest;
import net.minecraft.world.phys.AABB;
Expand Down Expand Up @@ -186,4 +187,24 @@ public boolean hasMainChunkLoadHook() {
public void mainChunkLoad(final ChunkAccess chunk, final SerializableChunkData chunkData) {

}

@Override
public List<Entity> modifySavedEntities(final ServerLevel world, final int chunkX, final int chunkZ, final List<Entity> entities) {
return entities;
}

@Override
public void unloadEntity(final Entity entity) {
entity.setRemoved(Entity.RemovalReason.UNLOADED_TO_CHUNK);
}

@Override
public void postLoadProtoChunk(final ServerLevel world, final ProtoChunk chunk) {
ChunkStatusTasks.postLoadProtoChunk(world, chunk.getEntities());
}

@Override
public int modifyEntityTrackingRange(final Entity entity, final int currentRange) {
return currentRange;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import net.minecraft.world.level.chunk.ImposterProtoChunk;
import net.minecraft.world.level.chunk.LevelChunk;
import net.minecraft.world.level.chunk.ProtoChunk;
import net.minecraft.world.level.chunk.status.ChunkStatusTasks;
import net.minecraft.world.level.chunk.storage.SerializableChunkData;
import net.minecraft.world.level.entity.EntityTypeTest;
import net.minecraft.world.phys.AABB;
Expand Down Expand Up @@ -218,4 +219,24 @@ public boolean hasMainChunkLoadHook() {
public void mainChunkLoad(final ChunkAccess chunk, final SerializableChunkData chunkData) {
NeoForge.EVENT_BUS.post(new ChunkDataEvent.Load(chunk, chunkData));
}

@Override
public List<Entity> modifySavedEntities(final ServerLevel world, final int chunkX, final int chunkZ, final List<Entity> entities) {
return entities;
}

@Override
public void unloadEntity(final Entity entity) {
entity.setRemoved(Entity.RemovalReason.UNLOADED_TO_CHUNK);
}

@Override
public void postLoadProtoChunk(final ServerLevel world, final ProtoChunk chunk) {
ChunkStatusTasks.postLoadProtoChunk(world, chunk.getEntities());
}

@Override
public int modifyEntityTrackingRange(final Entity entity, final int currentRange) {
return currentRange;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ public CompoundTag convertNBT(final DataFixTypes type, final DataFixer dataFixer

public void mainChunkLoad(final ChunkAccess chunk, final SerializableChunkData chunkData);

public List<Entity> modifySavedEntities(final ServerLevel world, final int chunkX, final int chunkZ, final List<Entity> entities);

public void unloadEntity(final Entity entity);

public void postLoadProtoChunk(final ServerLevel world, final ProtoChunk chunk);

public int modifyEntityTrackingRange(final Entity entity, final int currentRange);

public static final class Holder {
private Holder() {
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ca.spottedleaf.moonrise.common.util;

import ca.spottedleaf.moonrise.common.PlatformHooks;
import ca.spottedleaf.moonrise.common.config.adapter.TypeAdapterRegistry;
import ca.spottedleaf.moonrise.common.config.config.YamlConfig;
import ca.spottedleaf.moonrise.common.config.moonrise.MoonriseConfig;
Expand All @@ -13,7 +14,7 @@ public final class ConfigHolder {

private static final Logger LOGGER = LoggerFactory.getLogger(ConfigHolder.class);

private static final File CONFIG_FILE = new File(System.getProperty("Moonrise.ConfigFile", "config/moonrise.yml"));
private static final File CONFIG_FILE = new File(System.getProperty(PlatformHooks.get().getBrand() + ".ConfigFile", "config/moonrise.yml"));
private static final TypeAdapterRegistry CONFIG_ADAPTERS = new TypeAdapterRegistry();
private static final YamlConfig<MoonriseConfig> CONFIG;
static {
Expand All @@ -25,18 +26,18 @@ public final class ConfigHolder {
throw new RuntimeException(ex);
}
}
private static final String CONFIG_HEADER = """
private static final String CONFIG_HEADER = String.format("""
This is the configuration file for Moonrise.
Each configuration option is prefixed with a comment to explain what it does. Additional changes to this file
other than modifying the options, such as adding comments, will be overwritten when Moonrise loads the config.
Below are the Moonrise startup flags. Note that startup flags must be placed in the JVM arguments, not
program arguments.
-DMoonrise.ConfigFile=<file> - Override the config file location. Might be useful for multiple game versions.
-DMoonrise.WorkerThreadCount=<number> - Override the auto configured worker thread counts (worker-threads).
-DMoonrise.MaxViewDistance=<number> - Overrides the maximum view distance, should only use for debugging purposes.
""";
-D%1$s.ConfigFile=<file> - Override the config file location. Might be useful for multiple game versions.
-D%1$s.WorkerThreadCount=<number> - Override the auto configured worker thread counts (worker-threads).
-D%1$s.MaxViewDistance=<number> - Overrides the maximum view distance, should only use for debugging purposes.
""", PlatformHooks.get().getBrand());

static {
reloadConfig();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public final class MoonriseCommon {
@Override
public void accept(Thread thread) {
thread.setDaemon(true);
thread.setName("Moonrise Common Worker #" + this.idGenerator.getAndIncrement());
thread.setName(PlatformHooks.get().getBrand() + " Common Worker #" + this.idGenerator.getAndIncrement());
thread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(final Thread thread, final Throwable throwable) {
Expand All @@ -44,7 +44,7 @@ public static void adjustWorkerThreads(final int configWorkerThreads, final int
} else {
defaultWorkerThreads = defaultWorkerThreads / 2;
}
defaultWorkerThreads = Integer.getInteger("Moonrise.WorkerThreadCount", Integer.valueOf(defaultWorkerThreads));
defaultWorkerThreads = Integer.getInteger(PlatformHooks.get().getBrand() + ".WorkerThreadCount", Integer.valueOf(defaultWorkerThreads));

int workerThreads = configWorkerThreads;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package ca.spottedleaf.moonrise.common.util;

import ca.spottedleaf.moonrise.common.PlatformHooks;

public final class MoonriseConstants {

public static final int MAX_VIEW_DISTANCE = Integer.getInteger("Moonrise.MaxViewDistance", 32);
public static final int MAX_VIEW_DISTANCE = Integer.getInteger(PlatformHooks.get().getBrand() + ".MaxViewDistance", 32);

private MoonriseConstants() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.Property;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Constant;
import org.spongepowered.asm.mixin.injection.Inject;
Expand All @@ -17,6 +18,9 @@ protected BooleanPropertyMixin(String string, Class<Boolean> class_) {
super(string, class_);
}

@Unique
private static final Boolean[] BY_ID = new Boolean[]{ Boolean.FALSE, Boolean.TRUE };

@Override
public final int moonrise$getIdFor(final Boolean value) {
return value.booleanValue() ? 1 : 0;
Expand All @@ -33,6 +37,6 @@ protected BooleanPropertyMixin(String string, Class<Boolean> class_) {
)
)
private void init(final CallbackInfo ci) {
this.moonrise$setById(new Boolean[]{ Boolean.FALSE, Boolean.TRUE });
this.moonrise$setById(BY_ID);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ public ChunkHolderMixin(ChunkPos chunkPos) {
@Unique
private final ReferenceList<ServerPlayer> playersSentChunkTo = new ReferenceList<>(EMPTY_PLAYER_ARRAY);

@Unique
private boolean isMarkedDirtyForPlayers;

@Unique
private ChunkMap getChunkMap() {
return (ChunkMap)this.playerProvider;
Expand Down Expand Up @@ -123,16 +120,6 @@ private ChunkMap getChunkMap() {
return ret;
}

@Override
public final boolean moonrise$isMarkedDirtyForPlayers() {
return this.isMarkedDirtyForPlayers;
}

@Override
public final void moonrise$markDirtyForPlayers(final boolean value) {
this.isMarkedDirtyForPlayers = value;
}

@Unique
private static final ServerPlayer[] EMPTY_PLAYER_ARRAY = new ServerPlayer[0];

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ca.spottedleaf.moonrise.mixin.entity_tracker;

import ca.spottedleaf.moonrise.common.PlatformHooks;
import ca.spottedleaf.moonrise.common.list.ReferenceList;
import ca.spottedleaf.moonrise.common.misc.NearbyPlayers;
import ca.spottedleaf.moonrise.common.util.TickThread;
Expand Down Expand Up @@ -144,8 +145,8 @@ private <E> Set<E> useBetterIdentitySet() {
*/
@Overwrite
public int getEffectiveRange() {
int range = this.range;
final Entity entity = this.entity;
int range = PlatformHooks.get().modifyEntityTrackingRange(entity, this.range);

if (entity.getPassengers() == ImmutableList.<Entity>of()) {
return this.scaledRange(range);
Expand All @@ -154,8 +155,9 @@ public int getEffectiveRange() {
// note: we change to List
final List<Entity> passengers = (List<Entity>)entity.getIndirectPassengers();
for (int i = 0, len = passengers.size(); i < len; ++i) {
final Entity passenger = passengers.get(i);
// note: max should be branchless
range = Math.max(range, passengers.get(i).getType().clientTrackingRange() << 4);
range = Math.max(range, PlatformHooks.get().modifyEntityTrackingRange(passenger, passenger.getType().clientTrackingRange() << 4));
}

return this.scaledRange(range);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ private void injectFluidRegister(
final RegistrationInfo registrationInfo,
final CallbackInfoReturnable<Holder.Reference<T>> cir
) {
if (resourceKey.registryKey() == (Object) Registries.FLUID) {
for (final FluidState possibleState : ((Fluid) object).getStateDefinition().getPossibleStates()) {
((FluidFluidState) (Object) possibleState).moonrise$initCaches();
if (resourceKey.registryKey() == (Object)Registries.FLUID) {
for (final FluidState possibleState : ((Fluid)object).getStateDefinition().getPossibleStates()) {
((FluidFluidState)(Object)possibleState).moonrise$initCaches();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,4 @@ public interface ChunkSystemChunkHolder {

public LevelChunk moonrise$getFullChunk();

public boolean moonrise$isMarkedDirtyForPlayers();

public void moonrise$markDirtyForPlayers(final boolean value);

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ca.spottedleaf.moonrise.patches.chunk_system.level.entity;

import ca.spottedleaf.moonrise.common.PlatformHooks;
import ca.spottedleaf.moonrise.common.list.EntityList;
import ca.spottedleaf.moonrise.patches.chunk_system.level.chunk.ChunkData;
import ca.spottedleaf.moonrise.patches.chunk_system.entity.ChunkSystemEntity;
Expand Down Expand Up @@ -103,7 +104,7 @@ public static CompoundTag saveEntityChunk0(final List<Entity> entities, final Ch
}

final ListTag entitiesTag = new ListTag();
for (final Entity entity : entities) {
for (final Entity entity : PlatformHooks.get().modifySavedEntities(world, chunkPos.x, chunkPos.z, entities)) {
CompoundTag compoundTag = new CompoundTag();
if (entity.save(compoundTag)) {
entitiesTag.add(compoundTag);
Expand Down Expand Up @@ -150,12 +151,12 @@ public boolean unload() {
continue;
}
if (entity.shouldBeSaved()) {
entity.setRemoved(Entity.RemovalReason.UNLOADED_TO_CHUNK);
PlatformHooks.get().unloadEntity(entity);
if (entity.isVehicle()) {
// we cannot assume that these entities are contained within this chunk, because entities can
// desync - so we need to remove them all
for (final Entity passenger : entity.getIndirectPassengers()) {
passenger.setRemoved(Entity.RemovalReason.UNLOADED_TO_CHUNK);
PlatformHooks.get().unloadEntity(passenger);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1057,7 +1057,7 @@ public void onMove() {
@Override
public void onRemove(final Entity.RemovalReason reason) {
final Entity entity = this.entity;
EntityLookup.this.checkThread(entity, "Cannot remove entity off-main"); // Paper - rewrite chunk system
EntityLookup.this.checkThread(entity, "Cannot remove entity off-main");
final Visibility tickingState = EntityLookup.getEntityStatus(entity);

EntityLookup.this.removeEntity(entity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,11 +427,11 @@ private void sendUnloadChunk(final int chunkX, final int chunkZ) {
if (!this.sentChunks.remove(CoordinateUtils.getChunkKey(chunkX, chunkZ))) {
return;
}
PlatformHooks.get().onChunkUnWatch(this.world, new ChunkPos(chunkX, chunkZ), this.player);
this.sendUnloadChunkRaw(chunkX, chunkZ);
}

private void sendUnloadChunkRaw(final int chunkX, final int chunkZ) {
PlatformHooks.get().onChunkUnWatch(this.world, new ChunkPos(chunkX, chunkZ), this.player);
// Note: Check PlayerChunkSender#dropChunk for other logic
// Note: drop isAlive() check so that chunks properly unload client-side when the player dies
((ChunkSystemChunkHolder)((ChunkSystemServerLevel)this.world).moonrise$getChunkTaskScheduler().chunkHolderManager
Expand Down Expand Up @@ -1077,5 +1077,9 @@ void remove() {

// now all tickets should be removed, which is all of our external state
}

public LongOpenHashSet getSentChunksRaw() {
return this.sentChunks;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void run() {
final ServerLevel world = this.world;
final ProtoChunk protoChunk = (ProtoChunk)this.fromChunk;
chunk = new LevelChunk(this.world, protoChunk, (final LevelChunk unused) -> {
ChunkStatusTasks.postLoadProtoChunk(world, protoChunk.getEntities());
PlatformHooks.get().postLoadProtoChunk(world, protoChunk);
});
this.chunkHolder.replaceProtoChunk(new ImposterProtoChunk(chunk, false));
}
Expand Down

0 comments on commit 19c96e3

Please sign in to comment.