Skip to content

Commit

Permalink
Move field initializations out of constructor injects
Browse files Browse the repository at this point in the history
They should be less fragile for breakages.

Additionally, modify PoiManagerMixin to work around an issue where
our getOrCreate() method is not being invoked _because_ its return
type was PoiSection and not Object. Apparently, the JVM will search
the superclass when a direct match (params+return type) is not found.
  • Loading branch information
Spottedleaf committed Jun 20, 2024
1 parent 044a23f commit 30cb658
Show file tree
Hide file tree
Showing 18 changed files with 44 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public ChunkHolderMixin(ChunkPos chunkPos) {
private NewChunkHolder newChunkHolder;

@Unique
private ReferenceList<ServerPlayer> playersSentChunkTo;
private final ReferenceList<ServerPlayer> playersSentChunkTo = new ReferenceList<>(EMPTY_PLAYER_ARRAY, 0);

@Unique
private ChunkMap getChunkMap() {
Expand Down Expand Up @@ -134,8 +134,6 @@ private ChunkMap getChunkMap() {
)
)
private void initFields(final CallbackInfo ci) {
this.playersSentChunkTo = new ReferenceList<>(EMPTY_PLAYER_ARRAY, 0);

this.fullChunkFuture = null;
this.tickingChunkFuture = null;
this.entityTickingChunkFuture = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ public abstract class ChunkStatusMixin implements ChunkSystemChunkStatus {
private boolean emptyLoadTask;

@Unique
private int writeRadius;
private int writeRadius = -1;

@Unique
private ChunkStatus nextStatus;
private ChunkStatus nextStatus = (ChunkStatus)(Object)this;

@Unique
private AtomicBoolean warnedAboutNoImmediateComplete;
private final AtomicBoolean warnedAboutNoImmediateComplete = new AtomicBoolean();

@Override
public final boolean moonrise$isParallelCapable() {
Expand Down Expand Up @@ -90,12 +90,8 @@ public abstract class ChunkStatusMixin implements ChunkSystemChunkStatus {
)
)
private void initFields(ChunkStatus prevStatus, EnumSet<Heightmap.Types> enumSet, ChunkType chunkType, CallbackInfo ci) {
this.isParallelCapable = false;
this.writeRadius = -1;
this.nextStatus = (ChunkStatus)(Object)this;
if (prevStatus != null) {
((ChunkStatusMixin)(Object)prevStatus).nextStatus = (ChunkStatus)(Object)this;
}
this.warnedAboutNoImmediateComplete = new AtomicBoolean();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ public abstract class EntityMixin implements ChunkSystemEntity {
private FullChunkStatus chunkStatus;

@Unique
private int sectionX;
private int sectionX = Integer.MIN_VALUE;

@Unique
private int sectionY;
private int sectionY = Integer.MIN_VALUE;

@Unique
private int sectionZ;
private int sectionZ = Integer.MIN_VALUE;

@Unique
private boolean updatingSectionStatus;
Expand Down Expand Up @@ -124,20 +124,6 @@ public abstract class EntityMixin implements ChunkSystemEntity {
return this.getIndirectPassengersStream().anyMatch((entity) -> entity instanceof Player);
}

/**
* @reason Initialise fields
* @author Spottedleaf
*/
@Inject(
method = "<init>",
at = @At(
value = "RETURN"
)
)
private void initHook(final CallbackInfo ci) {
this.sectionX = this.sectionY = this.sectionZ = Integer.MIN_VALUE;
}

/**
* @reason Stop bad mods from moving entities during section status updates, which otherwise would cause CMEs
* @author Spottedleaf
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public abstract class EntityTickListMixin {
private Int2ObjectMap<Entity> passive;

@Unique
private IteratorSafeOrderedReferenceSet<Entity> entities;
private final IteratorSafeOrderedReferenceSet<Entity> entities = new IteratorSafeOrderedReferenceSet<>();

/**
* @reason Initialise new fields and destroy old state
Expand All @@ -41,7 +41,6 @@ public abstract class EntityTickListMixin {
private void initHook(final CallbackInfo ci) {
this.active = null;
this.passive = null;
this.entities = new IteratorSafeOrderedReferenceSet<>();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,7 @@ public abstract class LevelChunkTicksMixin<T> implements ChunkSystemLevelChunkTi
private boolean dirty;

@Unique
private long lastSaved;

/**
* @reason Hook to init fields
* @author Spottedleaf
*/
@Inject(
method = "<init>()V",
at = @At(
value = "RETURN"
)
)
private void init(final CallbackInfo ci) {
this.lastSaved = Long.MIN_VALUE;
}
private long lastSaved = Long.MIN_VALUE;

@Override
public final boolean moonrise$isDirty(final long tick) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Optional;
Expand All @@ -44,15 +43,17 @@
import java.util.stream.Stream;

@Mixin(PoiManager.class)
public abstract class PoiManagerMixin extends SectionStorage<PoiSection> implements ChunkSystemPoiManager {
// Declare the generic type as Object so that our Overrides match the method signature of the superclass
// Specifically, getOrCreate must return Object so that existing invokes do not route to the superclass
public abstract class PoiManagerMixin extends SectionStorage<Object> implements ChunkSystemPoiManager {

@Shadow
abstract boolean isVillageCenter(long l);

@Shadow
public abstract void checkConsistencyWithBlocks(SectionPos sectionPos, LevelChunkSection levelChunkSection);

public PoiManagerMixin(SimpleRegionStorage simpleRegionStorage, Function<Runnable, Codec<PoiSection>> function, Function<Runnable, PoiSection> function2, RegistryAccess registryAccess, ChunkIOErrorReporter chunkIOErrorReporter, LevelHeightAccessor levelHeightAccessor) {
public PoiManagerMixin(SimpleRegionStorage simpleRegionStorage, Function<Runnable, Codec<Object>> function, Function<Runnable, Object> function2, RegistryAccess registryAccess, ChunkIOErrorReporter chunkIOErrorReporter, LevelHeightAccessor levelHeightAccessor) {
super(simpleRegionStorage, function, function2, registryAccess, chunkIOErrorReporter, levelHeightAccessor);
}

Expand All @@ -62,7 +63,7 @@ public PoiManagerMixin(SimpleRegionStorage simpleRegionStorage, Function<Runnabl
// the vanilla tracker needs to be replaced because it does not support level removes, and we need level removes
// to support poi unloading
@Unique
private Delayed26WayDistancePropagator3D villageDistanceTracker;
private final Delayed26WayDistancePropagator3D villageDistanceTracker = new Delayed26WayDistancePropagator3D();

@Unique
private static final int POI_DATA_SOURCE = 7;
Expand Down Expand Up @@ -95,7 +96,6 @@ private void initHook(RegionStorageInfo regionStorageInfo, Path path, DataFixer
RegistryAccess registryAccess, ChunkIOErrorReporter chunkIOErrorReporter,
LevelHeightAccessor levelHeightAccessor, CallbackInfo ci) {
this.world = (ServerLevel)levelHeightAccessor;
this.villageDistanceTracker = new Delayed26WayDistancePropagator3D();
}

/**
Expand Down Expand Up @@ -145,7 +145,7 @@ public void onSectionLoad(final long pos) {
}

@Override
public Optional<PoiSection> get(final long pos) {
public Optional<Object> get(final long pos) {
final int chunkX = CoordinateUtils.getChunkSectionX(pos);
final int chunkY = CoordinateUtils.getChunkSectionY(pos);
final int chunkZ = CoordinateUtils.getChunkSectionZ(pos);
Expand All @@ -155,11 +155,11 @@ public Optional<PoiSection> get(final long pos) {
final ChunkHolderManager manager = ((ChunkSystemServerLevel)this.world).moonrise$getChunkTaskScheduler().chunkHolderManager;
final PoiChunk ret = manager.getPoiChunkIfLoaded(chunkX, chunkZ, true);

return ret == null ? Optional.empty() : ret.getSectionForVanilla(chunkY);
return ret == null ? Optional.empty() : (Optional)ret.getSectionForVanilla(chunkY);
}

@Override
public Optional<PoiSection> getOrLoad(final long pos) {
public Optional<Object> getOrLoad(final long pos) {
final int chunkX = CoordinateUtils.getChunkSectionX(pos);
final int chunkY = CoordinateUtils.getChunkSectionY(pos);
final int chunkZ = CoordinateUtils.getChunkSectionZ(pos);
Expand All @@ -171,17 +171,17 @@ public Optional<PoiSection> getOrLoad(final long pos) {
if (chunkY >= WorldUtil.getMinSection(this.world) && chunkY <= WorldUtil.getMaxSection(this.world)) {
final PoiChunk ret = manager.getPoiChunkIfLoaded(chunkX, chunkZ, true);
if (ret != null) {
return ret.getSectionForVanilla(chunkY);
return (Optional)ret.getSectionForVanilla(chunkY);
} else {
return manager.loadPoiChunk(chunkX, chunkZ).getSectionForVanilla(chunkY);
return (Optional)manager.loadPoiChunk(chunkX, chunkZ).getSectionForVanilla(chunkY);
}
}
// retain vanilla behavior: do not load section if out of bounds!
return Optional.empty();
}

@Override
protected PoiSection getOrCreate(final long pos) {
protected Object getOrCreate(final long pos) {
final int chunkX = CoordinateUtils.getChunkSectionX(pos);
final int chunkY = CoordinateUtils.getChunkSectionY(pos);
final int chunkZ = CoordinateUtils.getChunkSectionZ(pos);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,7 @@ public abstract class PoiSectionMixin implements ChunkSystemPoiSection {


@Unique
private Optional<PoiSection> noAllocOptional;

/**
* @reason Initialise fields
* @author Spottedleaf
*/
@Inject(
method = "<init>(Ljava/lang/Runnable;ZLjava/util/List;)V",
at = @At(
value = "RETURN"
)
)
private void init(final CallbackInfo ci) {
this.noAllocOptional = Optional.of((PoiSection)(Object)this);
}
private final Optional<PoiSection> noAllocOptional = Optional.of((PoiSection)(Object)this);;

@Override
public final boolean moonrise$isEmpty() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ protected ServerLevelMixin(WritableLevelData writableLevelData, ResourceKey<Leve
private EntityDataController entityDataController;

@Unique
private PoiDataController poiDataController;
private final PoiDataController poiDataController = new PoiDataController((ServerLevel)(Object)this);

@Unique
private ChunkDataController chunkDataController;
private final ChunkDataController chunkDataController = new ChunkDataController((ServerLevel)(Object)this);

@Unique
private ChunkTaskScheduler chunkTaskScheduler;
Expand Down Expand Up @@ -115,8 +115,6 @@ private void init(MinecraftServer minecraftServer, Executor executor,
minecraftServer.forceSynchronousWrites()
)
);
this.poiDataController = new PoiDataController((ServerLevel)(Object)this);
this.chunkDataController = new ChunkDataController((ServerLevel)(Object)this);
this.moonrise$setEntityLookup(new ServerEntityLookup((ServerLevel)(Object)this, ((ServerLevel)(Object)this).new EntityCallbacks()));
this.chunkTaskScheduler = new ChunkTaskScheduler((ServerLevel)(Object)this, MoonriseCommon.WORKER_POOL);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,7 @@ public ServerPlayerMixin(Level level, BlockPos blockPos, float f, GameProfile ga
private RegionizedPlayerChunkLoader.PlayerChunkLoaderData chunkLoader;

@Unique
private RegionizedPlayerChunkLoader.ViewDistanceHolder viewDistanceHolder;

/**
* @reason Initialise fields
* @author Spottedleaf
*/
@Inject(
method = "<init>",
at = @At(
value = "RETURN"
)
)
private void init(final CallbackInfo ci) {
this.viewDistanceHolder = new RegionizedPlayerChunkLoader.ViewDistanceHolder();
}
private RegionizedPlayerChunkLoader.ViewDistanceHolder viewDistanceHolder = new RegionizedPlayerChunkLoader.ViewDistanceHolder();

@Override
public final boolean moonrise$isRealPlayer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ protected BlockStateBaseMixin(Block object, Reference2ObjectArrayMap<Property<?>
private static final AtomicInteger ID_GENERATOR = new AtomicInteger();

@Unique
private int id1, id2;
private final int id1 = HashCommon.murmurHash3(HashCommon.murmurHash3(ID_GENERATOR.getAndIncrement() + RANDOM_OFFSET) + RANDOM_OFFSET);
@Unique
private final int id2 = HashCommon.murmurHash3(HashCommon.murmurHash3(ID_GENERATOR.getAndIncrement() + RANDOM_OFFSET) + RANDOM_OFFSET);

@Unique
private boolean occludesFullBlock;
Expand All @@ -72,18 +74,6 @@ private static void initCaches(final VoxelShape shape) {
}
}

@Inject(
method = "<init>",
at = @At(
value = "RETURN"
)
)
private void init(final CallbackInfo ci) {
// note: murmurHash3 has an inverse, so the field is still unique
this.id1 = HashCommon.murmurHash3(HashCommon.murmurHash3(ID_GENERATOR.getAndIncrement() + RANDOM_OFFSET) + RANDOM_OFFSET);
this.id2 = HashCommon.murmurHash3(HashCommon.murmurHash3(ID_GENERATOR.getAndIncrement() + RANDOM_OFFSET) + RANDOM_OFFSET);
}

/**
* @reason Init collision state only after cache is set up
* @author Spottedleaf
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@Mixin(DiscreteVoxelShape.class)
public abstract class DiscreteVoxelShapeMixin implements CollisionDiscreteVoxelShape {

// ignore race conditions here: the shape is static, so it doesn't matter
// ignore race conditions on field read/write: the shape is static, so it doesn't matter
@Unique
private CachedShapeData cachedShapeData;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ public abstract class ExplosionMixin {
@Final
private boolean fire;

@Shadow @Final private DamageSource damageSource;
@Shadow
@Final private DamageSource damageSource;


@Unique
private static final double[] CACHED_RAYS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ private static Stream<Pair<Holder<PoiType>, BlockPos>> aaa(PoiManager poiManager
final List<Pair<Holder<PoiType>, BlockPos>> ret = new ArrayList<>();

PoiAccess.findNearestPoiPositions(
poiManager, predicate, predicate2, blockPos, i, Double.MAX_VALUE, occup, true, 5, ret
poiManager, predicate, predicate2, blockPos, i, Double.MAX_VALUE, occup, PoiAccess.LOAD_FOR_SEARCHING, 5, ret
);

return ret.stream();
Expand Down
Loading

0 comments on commit 30cb658

Please sign in to comment.