refactor and block_entity_retrieval mixin

This commit is contained in:
Taiyou06
2024-07-06 08:24:41 +03:00
parent 8314c4b05f
commit bc1b851a4e
31 changed files with 190 additions and 43 deletions

View File

@@ -1,10 +1,28 @@
// Nitori Copyright (C) 2024 Gensokyo Reimagined
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package net.gensokyoreimagined.nitori.common.world;
import net.minecraft.core.BlockPos;
import net.minecraft.core.BlockPos.MutableBlockPos;
import net.minecraft.world.level.Level;
// Taken from Lithium
// https://github.com/CaffeineMC/lithium-fabric/blob/427dd75ffc922cc1858c1db4b283cc54744567e0/src/main/java/me/jellysquid/mods/lithium/common/world/ChunkRandomSource.java
public interface ChunkRandomSource {
void lithium$getRandomPosInChunk(int x, int y, int z, int mask, BlockPos.MutableBlockPos out);
}
/**
* Alternative implementation of {@link net.minecraft.server.level.ServerLevel#getBlockRandomPos(int, int, int, int)} which does not allocate
* a new {@link BlockPos}.
*/
void nitori$getRandomPosInChunk(int x, int y, int z, int mask, BlockPos.MutableBlockPos out);
}

View File

@@ -0,0 +1,8 @@
package net.gensokyoreimagined.nitori.common.world.blockentity;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.core.BlockPos;
public interface BlockEntityGetter {
BlockEntity lithium$getLoadedExistingBlockEntity(BlockPos pos);
}

View File

@@ -0,0 +1,65 @@
// Nitori Copyright (C) 2024 Gensokyo Reimagined
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package net.gensokyoreimagined.nitori.core;
import net.gensokyoreimagined.nitori.common.world.ChunkRandomSource;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.RandomSource;
import net.minecraft.world.level.block.state.BlockState;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
// Taken from Lithium
// https://github.com/CaffeineMC/lithium-fabric/blob/427dd75ffc922cc1858c1db4b283cc54744567e0/src/main/java/me/jellysquid/mods/lithium/mixin/alloc/chunk_random/ServerWorldMixin.java#L24
@Mixin(ServerLevel.class)
public abstract class MixinServerWorld {
@Unique
private final BlockPos.MutableBlockPos nitori$randomPosInChunkCachedPos = new BlockPos.MutableBlockPos();
/**
* @reason Avoid allocating BlockPos every invocation through using our allocation-free variant
*/
@Redirect(
method = "tickChunk",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/server/level/ServerLevel;getBlockRandomPos(IIII)Lnet/minecraft/core/BlockPos;"
)
)
private BlockPos redirectTickGetRandomPosInChunk(ServerLevel serverWorld, int x, int y, int z, int mask) {
((ChunkRandomSource) serverWorld).nitori$getRandomPosInChunk(x, y, z, mask, this.nitori$randomPosInChunkCachedPos);
return this.nitori$randomPosInChunkCachedPos;
}
/**
* @reason Ensure an immutable block position is passed on block tick
*/
@Redirect(
method = "tickChunk",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/world/level/block/state/BlockState;randomTick(Lnet/minecraft/server/level/ServerLevel;Lnet/minecraft/core/BlockPos;Lnet/minecraft/util/RandomSource;)V"
)
)
private void redirectBlockStateTick(BlockState blockState, ServerLevel world, BlockPos pos, RandomSource rand) {
blockState.randomTick(world, pos.immutable(), rand);
}
}

View File

@@ -12,7 +12,7 @@
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package net.gensokyoreimagined.nitori.core;
package net.gensokyoreimagined.nitori.mixin;
import com.destroystokyo.paper.util.misc.PooledLinkedHashSets;
import it.unimi.dsi.fastutil.ints.Int2ObjectLinkedOpenHashMap;

View File

@@ -12,7 +12,7 @@
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package net.gensokyoreimagined.nitori.core;
package net.gensokyoreimagined.nitori.mixin;
import net.minecraft.core.Direction;
import net.minecraft.world.phys.AABB;

View File

@@ -12,7 +12,7 @@
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package net.gensokyoreimagined.nitori.core;
package net.gensokyoreimagined.nitori.mixin;
import net.minecraft.core.Direction;
import net.minecraft.world.level.block.Block;

View File

@@ -12,7 +12,7 @@
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package net.gensokyoreimagined.nitori.core;
package net.gensokyoreimagined.nitori.mixin;
import it.unimi.dsi.fastutil.longs.LongList;
import net.gensokyoreimagined.nitori.cached_blockpos_iteration.IterateOutwardsCache;

View File

@@ -12,7 +12,7 @@
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package net.gensokyoreimagined.nitori.core;
package net.gensokyoreimagined.nitori.mixin;
import com.destroystokyo.paper.util.maplist.EntityList;
import io.papermc.paper.world.ChunkEntitySlices;

View File

@@ -12,7 +12,7 @@
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package net.gensokyoreimagined.nitori.core;
package net.gensokyoreimagined.nitori.mixin;
import net.minecraft.Util;
import net.minecraft.world.entity.player.Player;

View File

@@ -12,7 +12,7 @@
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package net.gensokyoreimagined.nitori.core;
package net.gensokyoreimagined.nitori.mixin;
import net.minecraft.core.Direction;
import net.minecraft.util.RandomSource;

View File

@@ -12,7 +12,7 @@
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package net.gensokyoreimagined.nitori.core;
package net.gensokyoreimagined.nitori.mixin;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.Entity;

View File

@@ -12,7 +12,7 @@
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package net.gensokyoreimagined.nitori.core;
package net.gensokyoreimagined.nitori.mixin;
import com.llamalad7.mixinextras.sugar.Local;
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;

View File

@@ -12,7 +12,7 @@
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package net.gensokyoreimagined.nitori.core;
package net.gensokyoreimagined.nitori.mixin;
import com.google.common.collect.ImmutableMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;

View File

@@ -12,12 +12,11 @@
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package net.gensokyoreimagined.nitori.core;
package net.gensokyoreimagined.nitori.mixin;
import io.papermc.paper.util.maplist.IteratorSafeOrderedReferenceSet;
import net.gensokyoreimagined.nitori.access.IMixinIteratorSafeOrderedReferenceSetAccess;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.gen.Accessor;
@Mixin(IteratorSafeOrderedReferenceSet.class)

View File

@@ -12,10 +12,12 @@
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package net.gensokyoreimagined.nitori.core;
package net.gensokyoreimagined.nitori.mixin;
import com.google.common.collect.Lists;
import net.gensokyoreimagined.nitori.common.util.collections.HashedReferenceList;
import net.gensokyoreimagined.nitori.common.world.ChunkRandomSource;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.TickingBlockEntity;
import org.spongepowered.asm.mixin.Final;
@@ -29,7 +31,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import java.util.List;
@Mixin(Level.class)
public class MixinLevel {
public class MixinLevel implements ChunkRandomSource {
// Implementation of 0006-lithium-HashedReferenceList.patch
@Mutable
@Final @Shadow
@@ -40,10 +42,20 @@ public class MixinLevel {
@Final @Shadow
private List<TickingBlockEntity> pendingBlockEntityTickers;
@Shadow
protected int randValue;
// Implementation of 0006-lithium-HashedReferenceList.patch
@Inject(method = "<init>", at = @At("RETURN"))
private void onInit(CallbackInfo ci) {
this.blockEntityTickers = new HashedReferenceList<>(Lists.newArrayList());
this.pendingBlockEntityTickers = new HashedReferenceList<>(Lists.newArrayList());
}
@Override
public void nitori$getRandomPosInChunk(int x, int y, int z, int mask, BlockPos.MutableBlockPos out) {
this.randValue = this.randValue * 3 + 1013904223;
int rand = this.randValue >> 2;
out.set(x + (rand & 15), y + (rand >> 16 & mask), z + (rand >> 8 & 15));
}
}

View File

@@ -12,7 +12,7 @@
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package net.gensokyoreimagined.nitori.core;
package net.gensokyoreimagined.nitori.mixin;
import net.minecraft.Util;
import net.minecraft.nbt.CompoundTag;

View File

@@ -12,7 +12,7 @@
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package net.gensokyoreimagined.nitori.core;
package net.gensokyoreimagined.nitori.mixin;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.EntityType;

View File

@@ -12,7 +12,7 @@
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package net.gensokyoreimagined.nitori.core;
package net.gensokyoreimagined.nitori.mixin;
import net.minecraft.util.Mth;
import org.spongepowered.asm.mixin.Mixin;

View File

@@ -12,7 +12,7 @@
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package net.gensokyoreimagined.nitori.core;
package net.gensokyoreimagined.nitori.mixin;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;

View File

@@ -12,7 +12,7 @@
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package net.gensokyoreimagined.nitori.core;
package net.gensokyoreimagined.nitori.mixin;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.Entity;

View File

@@ -12,7 +12,7 @@
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package net.gensokyoreimagined.nitori.core;
package net.gensokyoreimagined.nitori.mixin;
import net.minecraft.Util;
import net.minecraft.server.players.PlayerList;

View File

@@ -12,7 +12,7 @@
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package net.gensokyoreimagined.nitori.core;
package net.gensokyoreimagined.nitori.mixin;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;

View File

@@ -12,7 +12,7 @@
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package net.gensokyoreimagined.nitori.core;
package net.gensokyoreimagined.nitori.mixin;
import com.google.common.collect.Sets;
import net.minecraft.server.level.ServerBossEvent;

View File

@@ -12,7 +12,7 @@
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package net.gensokyoreimagined.nitori.core;
package net.gensokyoreimagined.nitori.mixin;
import com.google.common.collect.Lists;
import com.llamalad7.mixinextras.sugar.Local;

View File

@@ -12,7 +12,7 @@
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package net.gensokyoreimagined.nitori.core;
package net.gensokyoreimagined.nitori.mixin;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;

View File

@@ -12,7 +12,7 @@
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package net.gensokyoreimagined.nitori.core;
package net.gensokyoreimagined.nitori.mixin;
import net.gensokyoreimagined.nitori.common.util.Pos;
import net.minecraft.core.BlockPos;

View File

@@ -1,4 +1,4 @@
package net.gensokyoreimagined.nitori.core;
package net.gensokyoreimagined.nitori.mixin.alloc;
import net.minecraft.server.level.ChunkHolder;
import net.minecraft.server.level.ServerChunkCache;

View File

@@ -1,4 +1,4 @@
package net.gensokyoreimagined.nitori.core;
package net.gensokyoreimagined.nitori.mixin.alloc;
import com.google.common.collect.Table;
import net.gensokyoreimagined.nitori.common.state.FastImmutableTable;

View File

@@ -1,4 +1,4 @@
package net.gensokyoreimagined.nitori.core;
package net.gensokyoreimagined.nitori.mixin.alloc;
import net.gensokyoreimagined.nitori.common.world.ChunkRandomSource;
import net.minecraft.core.BlockPos;

View File

@@ -0,0 +1,42 @@
package net.gensokyoreimagined.nitori.mixin.util;
import net.gensokyoreimagined.nitori.common.world.blockentity.BlockEntityGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.Containers;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.chunk.ChunkAccess;
import net.minecraft.world.level.chunk.LevelChunk;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.core.BlockPos;
import net.minecraft.core.SectionPos;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
@Mixin(Level.class)
public abstract class BlockEntityRetrieval_WorldMixin implements BlockEntityGetter, Containers {
@Final
public boolean isClient;
@Final
private Thread thread;
public abstract LevelChunk getChunk(int i, int j);
@Nullable
public abstract ChunkPos getChunk(int chunkX, int chunkZ, ChunkAccess leastStatus, boolean create);
@Override
public BlockEntity lithium$getLoadedExistingBlockEntity(BlockPos pos) {
if (!this.isOutOfHeightLimit(pos)) {
if (this.isClient || Thread.currentThread() == this.thread) {
ChunkPos chunk = this.getChunk(SectionPos.posToSectionCoord(pos.getX()), SectionPos.posToSectionCoord(pos.getZ()), ChunkAccess.FULL, false);
if (chunk != null) {
return chunk.BlockEntityType.getBlockEntity(pos);
}
}
}
return null;
}
}

View File

@@ -1,37 +1,40 @@
{
"required": true,
"minVersion": "0.8.5",
"package": "net.gensokyoreimagined.nitori.core",
"package": "net.gensokyoreimagined.nitori.mixin",
"plugin": "net.gensokyoreimagined.nitori.plugins.CorePlugin",
"target": "@env(DEFAULT)",
"compatibilityLevel": "JAVA_17",
"server": [
"ChunkMapMixin",
"ChunkMapMixin$TrackedEntity",
"MixinAABB",
"MixinBlock",
"MixinBlockPos",
"MixinChunkEntitySlices",
"MixinCraftPlayer",
"MixinDirection",
"MixinEntity",
"MixinEntitySectionStorage",
"MixinGameRules",
"MixinIteratorSafeOrderedReferenceSet",
"MixinLevel",
"MixinLevelStorageAccess",
"MixinMob",
"MixinMth",
"MixinNoiseBasedChunkGenerator",
"MixinPlayer",
"MixinPlayerList",
"MixinReobfServer",
"MixinServerBossEvent",
"MixinServerEntity",
"MixinSpongeSIMD",
"MixinWorldGenRegion",
"ChunkMapMixin",
"ChunkMapMixin$TrackedEntity",
"MixinReobfServer",
"MixinLevelStorageAccess",
"MixinPlayerList",
"MixinCraftPlayer",
"StateMixin",
"WorldMixin",
"ServerChunkManagerMixin"
"alloc.ServerChunkManagerMixin",
"alloc.StateMixin",
"alloc.WorldMixin"
],
"mixins": [
"util.BlockEntityRetrieval_WorldMixin"
]
}