micro optimizations (mirai patches)
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package net.gensokyoreimagined.nitori.mixin.entity.navigation;
|
||||
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.entity.ai.navigation.PathNavigation;
|
||||
import net.minecraft.world.level.pathfinder.Path;
|
||||
import net.minecraft.world.entity.Mob;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import org.spongepowered.asm.mixin.Final;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.Shadow;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
@Mixin(PathNavigation.class)
|
||||
public class CancelNavigationMixin {
|
||||
@Shadow @Final protected Mob mob;
|
||||
|
||||
@Inject(method = "tick", at = @At("HEAD"), cancellable = true)
|
||||
private void cancelTick(CallbackInfo ci) {
|
||||
if (mob.isPassenger()) {
|
||||
ci.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
@Inject(method = "createPath(Ljava/util/Set;Lnet/minecraft/world/entity/Entity;IZIF)Lnet/minecraft/world/level/pathfinder/Path;", at = @At(value = "INVOKE", target = "Ljava/util/Set;isEmpty()Z", shift = At.Shift.AFTER), cancellable = true)
|
||||
private void cancelFindPath(Set<BlockPos> positions, Entity target, int range, boolean useHeadPos, int distance, float followRange, CallbackInfoReturnable<Path> cir) {
|
||||
if (mob.isPassenger()) {
|
||||
cir.setReturnValue(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package net.gensokyoreimagined.nitori.mixin.entity.speed;
|
||||
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.Shadow;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Redirect;
|
||||
|
||||
// Credit Mirai patch #0046
|
||||
@Mixin(Entity.class)
|
||||
public abstract class EntitySpeedMixin {
|
||||
@Shadow public abstract Vec3 getDeltaMovement();
|
||||
|
||||
@Shadow protected abstract float getBlockSpeedFactor();
|
||||
|
||||
@Redirect(method = "move", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/Entity;getBlockSpeedFactor()F"))
|
||||
private float redirectMultiplier(Entity instance) {
|
||||
if (this.getDeltaMovement().x == 0 && this.getDeltaMovement().z == 0) {
|
||||
return 1;
|
||||
} else {
|
||||
return this.getBlockSpeedFactor();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package net.gensokyoreimagined.nitori.mixin.network.block_breaking;
|
||||
|
||||
import com.llamalad7.mixinextras.sugar.Local;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.protocol.game.ClientboundBlockDestructionPacket;
|
||||
import net.minecraft.server.network.ServerGamePacketListenerImpl;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.core.BlockPos;
|
||||
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.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.Redirect;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
|
||||
// Credit Mirai patch #0061
|
||||
@Mixin(ServerLevel.class)
|
||||
public class CacheBlockBreakPacketMixin {
|
||||
@Unique
|
||||
private ClientboundBlockDestructionPacket packet;
|
||||
|
||||
@Inject(method = "destroyBlockProgress", at = @At("HEAD"))
|
||||
private void setPacketNull(int entityId, BlockPos pos, int progress, CallbackInfo ci) {
|
||||
this.packet = null;
|
||||
}
|
||||
|
||||
@Redirect(method = "destroyBlockProgress", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/network/ServerGamePacketListenerImpl;send(Lnet/minecraft/network/protocol/Packet;)V"))
|
||||
private void redirectPacketSent(ServerGamePacketListenerImpl instance, Packet<?> packetBad, @Local(ordinal = 0, argsOnly = true) int entityId, @Local(ordinal = 0, argsOnly = true) BlockPos pos, @Local(ordinal = 0, argsOnly = true) int progress, @Local(ordinal = 0)ServerPlayer serverPlayerEntity) {
|
||||
if (this.packet == null) this.packet = new ClientboundBlockDestructionPacket(entityId, pos, progress);
|
||||
serverPlayerEntity.connection.sendPacket(this.packet);
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@ import org.spongepowered.asm.mixin.injection.Inject;
|
||||
@Mixin(BlockBehaviour.BlockStateBase.class)
|
||||
public class AbstractBlockStateMixin implements BlockStateFlagHolder {
|
||||
@Unique
|
||||
private int flags;
|
||||
private int nitori$flags;
|
||||
|
||||
@Inject(method = "initCache", at = @At("RETURN"))
|
||||
private void init(CallbackInfo ci) {
|
||||
@@ -34,11 +34,11 @@ public class AbstractBlockStateMixin implements BlockStateFlagHolder {
|
||||
}
|
||||
}
|
||||
|
||||
this.flags = flags;
|
||||
this.nitori$flags = flags;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int lithium$getAllFlags() {
|
||||
return this.flags;
|
||||
return this.nitori$flags;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
package net.gensokyoreimagined.nitori.mixin.world.biome_access;
|
||||
|
||||
import net.minecraft.core.Holder;
|
||||
import net.minecraft.util.Mth;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.world.level.biome.Biome;
|
||||
import net.minecraft.world.level.biome.BiomeManager;
|
||||
import net.minecraft.util.LinearCongruentialGenerator;
|
||||
import org.spongepowered.asm.mixin.*;
|
||||
|
||||
/**
|
||||
* Credit to FXMorin (Unmerged PR for Lithium)
|
||||
*/
|
||||
@Mixin(BiomeManager.class)
|
||||
public class BiomeAccessMixin {
|
||||
@Shadow
|
||||
@Final
|
||||
private BiomeManager.NoiseBiomeSource noiseBiomeSource;
|
||||
|
||||
@Shadow
|
||||
@Final
|
||||
private long biomeZoomSeed;
|
||||
|
||||
@Shadow
|
||||
private static double getFiddle(long l) {return 0;}
|
||||
|
||||
@Unique
|
||||
private static final double nitori$maxOffset = 0.4500000001D;
|
||||
|
||||
/**
|
||||
* @author FX - PR0CESS
|
||||
* @reason I wanted to make it faster
|
||||
*/
|
||||
@Overwrite
|
||||
public Holder<Biome> getBiome(BlockPos pos) {
|
||||
int xMinus2 = pos.getX() - 2;
|
||||
int yMinus2 = pos.getY() - 2;
|
||||
int zMinus2 = pos.getZ() - 2;
|
||||
int x = xMinus2 >> 2;
|
||||
int y = yMinus2 >> 2;
|
||||
int z = zMinus2 >> 2;
|
||||
double quartX = (double)(xMinus2 & 3) / 4.0D;
|
||||
double quartY = (double)(yMinus2 & 3) / 4.0D;
|
||||
double quartZ = (double)(zMinus2 & 3) / 4.0D;
|
||||
int smallestX = 0;
|
||||
double smallestDist = Double.POSITIVE_INFINITY;
|
||||
for(int biomeX = 0; biomeX < 8; ++biomeX) {
|
||||
boolean everyOtherQuad = (biomeX & 4) == 0;
|
||||
boolean everyOtherPair = (biomeX & 2) == 0;
|
||||
boolean everyOther = (biomeX & 1) == 0;
|
||||
double quartXX = everyOtherQuad ? quartX : quartX - 1.0D;
|
||||
double quartYY = everyOtherPair ? quartY : quartY - 1.0D;
|
||||
double quartZZ = everyOther ? quartZ : quartZ - 1.0D;
|
||||
|
||||
double maxQuartYY = 0.0D,maxQuartZZ = 0.0D;
|
||||
if (biomeX != 0) {
|
||||
maxQuartYY = Mth.square(Math.max(quartYY + nitori$maxOffset, Math.abs(quartYY - nitori$maxOffset)));
|
||||
maxQuartZZ = Mth.square(Math.max(quartZZ + nitori$maxOffset, Math.abs(quartZZ - nitori$maxOffset)));
|
||||
double maxQuartXX = Mth.square(Math.max(quartXX + nitori$maxOffset, Math.abs(quartXX - nitori$maxOffset)));
|
||||
if (smallestDist < maxQuartXX + maxQuartYY + maxQuartZZ) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
int xx = everyOtherQuad ? x : x + 1;
|
||||
int yy = everyOtherPair ? y : y + 1;
|
||||
int zz = everyOther ? z : z + 1;
|
||||
|
||||
long seed = LinearCongruentialGenerator.next(this.biomeZoomSeed, xx);
|
||||
seed = LinearCongruentialGenerator.next(seed, yy);
|
||||
seed = LinearCongruentialGenerator.next(seed, zz);
|
||||
seed = LinearCongruentialGenerator.next(seed, xx);
|
||||
seed = LinearCongruentialGenerator.next(seed, yy);
|
||||
seed = LinearCongruentialGenerator.next(seed, zz);
|
||||
double offsetX = getFiddle(seed);
|
||||
double sqrX = Mth.square(quartXX + offsetX);
|
||||
if (biomeX != 0 && smallestDist < sqrX + maxQuartYY + maxQuartZZ) {
|
||||
continue;
|
||||
}
|
||||
seed = LinearCongruentialGenerator.next(seed, this.biomeZoomSeed);
|
||||
double offsetY = getFiddle(seed);
|
||||
double sqrY = Mth.square(quartYY + offsetY);
|
||||
if (biomeX != 0 && smallestDist < sqrX + sqrY + maxQuartZZ) {
|
||||
continue;
|
||||
}
|
||||
seed = LinearCongruentialGenerator.next(seed, this.biomeZoomSeed);
|
||||
double offsetZ = getFiddle(seed);
|
||||
double biomeDist = sqrX + sqrY + Mth.square(quartZZ + offsetZ);
|
||||
|
||||
if (smallestDist > biomeDist) {
|
||||
smallestX = biomeX;
|
||||
smallestDist = biomeDist;
|
||||
}
|
||||
}
|
||||
|
||||
int biomeX = (smallestX & 4) == 0 ? x : x + 1;
|
||||
int biomeY = (smallestX & 2) == 0 ? y : y + 1;
|
||||
int biomeZ = (smallestX & 1) == 0 ? z : z + 1;
|
||||
return this.noiseBiomeSource.getNoiseBiome(biomeX, biomeY, biomeZ);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package net.gensokyoreimagined.nitori.mixin.world.farmland;
|
||||
|
||||
import net.minecraft.world.level.block.FarmBlock;
|
||||
import net.minecraft.tags.FluidTags;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.world.level.LevelReader;
|
||||
import net.minecraft.world.level.chunk.LevelChunk;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.Overwrite;
|
||||
|
||||
/**
|
||||
* Credit to PaperMC patch #0682 and EinS4ckZwiebeln
|
||||
*/
|
||||
@Mixin(FarmBlock.class)
|
||||
public abstract class FarmlandBlockMixin {
|
||||
|
||||
/**
|
||||
* @author QPCrummer
|
||||
* @reason Optimize FarlandBlock nearby water lookup
|
||||
*/
|
||||
@Overwrite
|
||||
private static boolean isNearWater(LevelReader world, BlockPos pos) {
|
||||
int posX = pos.getX();
|
||||
int posY = pos.getY();
|
||||
int posZ = pos.getZ();
|
||||
|
||||
for (int dz = -4; dz <= 4; ++dz) {
|
||||
int z = dz + posZ;
|
||||
for (int dx = -4; dx <= 4; ++dx) {
|
||||
int x = posX + dx;
|
||||
for (int dy = 0; dy <= 1; ++dy) {
|
||||
net.minecraft.world.level.chunk.LevelChunk chunk = (LevelChunk) world.getChunk(x >> 4, z >> 4);
|
||||
net.minecraft.world.level.material.FluidState fluid = chunk.getBlockState(new BlockPos(x, dy + posY, z)).getFluidState();
|
||||
if (fluid.is(FluidTags.WATER)) return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -47,6 +47,8 @@
|
||||
"entity.fall_damage.NoFallDamageMixin",
|
||||
"entity.sprinting_particles.EntityMixin",
|
||||
"entity.collisions.unpushable_cramming.BoatEntityMixin",
|
||||
"entity.speed.EntitySpeedMixin",
|
||||
"entity.navigation.CancelNavigationMixin",
|
||||
"logic.fast_bits_blockpos.OptimizedBlockPosBitsMixin",
|
||||
"math.fast_blockops.BlockPosMixin",
|
||||
"math.fast_blockops.DirectionMixin",
|
||||
@@ -63,6 +65,7 @@
|
||||
"math.random.RandomMixin",
|
||||
"network.microopt.VarIntsMixin",
|
||||
"network.microopt.StringEncodingMixin",
|
||||
"network.block_breaking.CacheBlockBreakPacketMixin",
|
||||
"vmp.playerwatching.MixinChunkFilter",
|
||||
"vmp.playerwatching.MixinServerPlayerEntity",
|
||||
"vmp.playerwatching.optimize_nearby_player_lookups.MixinMobEntity",
|
||||
@@ -87,6 +90,8 @@
|
||||
"world.block_entity_ticking.support_cache.DirectBlockEntityTickInvokerMixin",
|
||||
"world.block_entity_ticking.support_cache.WorldChunkMixin",
|
||||
"world.portal_checks.DisablePortalChecksMixin",
|
||||
"world.blending.BlendMixin"
|
||||
"world.blending.BlendMixin",
|
||||
"world.farmland.FarmlandBlockMixin",
|
||||
"world.biome_access.BiomeAccessMixin"
|
||||
]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user