Skip to content

Commit

Permalink
Fixes and more minor improvements
Browse files Browse the repository at this point in the history
- Fixed the interaction limited
- Improvements to tnt explosions (non-vanilla)
- Readd the removal of JFR profiler
- Add the ability to remove sounds (WIP)
- FastMath for Vec3d
- More optimizations with FastMath
  • Loading branch information
QPCrummer committed Jun 26, 2024
1 parent 51e4d1f commit bb4beea
Show file tree
Hide file tree
Showing 21 changed files with 287 additions and 37 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ org.gradle.jvmargs=-Xmx1G
loader_version=0.15.11

# Mod Properties
mod_version = 0.0.1-dev.2
mod_version = 0.0.1-dev.3
maven_group = com.github.tatercertified
archives_base_name = potatoptimize

# Dependencies
# check this on https://modmuss50.me/fabric.html
fabric_version=0.100.3+1.21
fabric_version=0.100.4+1.21
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

public class Potatoptimize implements ModInitializer {
public static PotatoptimizeConfig CONFIG;
public static MinecraftServer almightyServerInstance;
public static final ExecutorService clientTickExecutor = Executors.newSingleThreadExecutor();
public static boolean isUnsafeRandomEnabled;
/**
Expand All @@ -21,7 +20,5 @@ public void onInitialize() {
if (CONFIG == null) {
throw new IllegalStateException("The mixin plugin did not initialize the config! Did it not load?");
}

ServerLifecycleEvents.SERVER_STARTING.register(server -> almightyServerInstance = server);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private static <S extends BlockEntity> BlockState noChunkLoading(WorldAccess ins
}

@Inject(method = "toPropertySource", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/WorldAccess;getBlockState(Lnet/minecraft/util/math/BlockPos;)Lnet/minecraft/block/BlockState;", shift = At.Shift.AFTER), cancellable = true)
private static <S extends BlockEntity> void checkIfNull(BlockEntityType<S> blockEntityType, Function<BlockState, DoubleBlockProperties.Type> typeMapper, Function<BlockState, Direction> function, DirectionProperty directionProperty, BlockState state, WorldAccess world, BlockPos pos, BiPredicate<WorldAccess, BlockPos> fallbackTester, CallbackInfoReturnable<DoubleBlockProperties.PropertySource<S>> cir, @Local(ordinal = 0) BlockState blockState, @Local(ordinal = 0) S blockEntity) {
private static <S extends BlockEntity> void checkIfNull(BlockEntityType<S> blockEntityType, Function<BlockState, DoubleBlockProperties.Type> typeMapper, Function<BlockState, Direction> function, DirectionProperty directionProperty, BlockState state, WorldAccess world, BlockPos pos, BiPredicate<WorldAccess, BlockPos> fallbackTester, CallbackInfoReturnable<DoubleBlockProperties.PropertySource<S>> cir, @Local(ordinal = 0, argsOnly = true) BlockState blockState, @Local(ordinal = 0) S blockEntity) {
if (blockState == null) {
cir.setReturnValue(new DoubleBlockProperties.PropertySource.Single<>(blockEntity));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.github.tatercertified.potatoptimize.mixin.entity.halloween;

import com.github.tatercertified.potatoptimize.Potatoptimize;
import com.github.tatercertified.potatoptimize.utils.interfaces.IsHalloweenInterface;
import com.llamalad7.mixinextras.sugar.Local;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.mob.AmbientEntity;
import net.minecraft.entity.passive.BatEntity;
import net.minecraft.world.World;
import net.minecraft.world.WorldAccess;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
Expand All @@ -19,7 +20,7 @@ protected BatEntityMixin(EntityType<? extends AmbientEntity> entityType, World w
}

@Redirect(method = "canSpawn", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/passive/BatEntity;isTodayAroundHalloween()Z"))
private static boolean optimizedIsHalloween() {
return ((IsHalloweenInterface) Potatoptimize.almightyServerInstance).isNearHalloween();
private static boolean optimizedIsHalloween(@Local(ordinal = 0, argsOnly = true) WorldAccess world) {
return ((IsHalloweenInterface) world.getServer()).isNearHalloween();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
@Mixin(PlayerManager.class)
public class SmoothTeleportPlayerManagerMixin {
@Redirect(method = "respawnPlayer", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/network/ServerPlayNetworkHandler;sendPacket(Lnet/minecraft/network/packet/Packet;)V", ordinal = 0))
private void removeSendPacket(ServerPlayNetworkHandler instance, Packet packet, @Local(ordinal = 0) ServerPlayerEntity serverPlayerEntity) {
private void removeSendPacket(ServerPlayNetworkHandler instance, Packet packet, @Local(ordinal = 0, argsOnly = true) ServerPlayerEntity serverPlayerEntity) {
if (!((SmoothTeleportInterface)serverPlayerEntity).shouldSmoothTeleport()) {
instance.sendPacket(packet);
}
}

@Redirect(method = "respawnPlayer", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/network/ServerPlayNetworkHandler;requestTeleport(DDDFF)V"))
private void removeSpawnRequest(ServerPlayNetworkHandler instance, double x, double y, double z, float yaw, float pitch, @Local(ordinal = 0) ServerPlayerEntity serverPlayerEntity) {
private void removeSpawnRequest(ServerPlayNetworkHandler instance, double x, double y, double z, float yaw, float pitch, @Local(ordinal = 0, argsOnly = true) ServerPlayerEntity serverPlayerEntity) {
if (!((SmoothTeleportInterface)serverPlayerEntity).shouldSmoothTeleport()) {
instance.requestTeleport(x, y, z, yaw, pitch);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.github.tatercertified.potatoptimize.mixin.fastmath.general;

import net.minecraft.util.math.Box;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;

import java.util.stream.IntStream;

@Mixin(MathHelper.class)
public class GenericFastMathMixin {

/**
* @author QPCrummer
* @reason Slightly more optimized
*/
@Overwrite
public static boolean method_34945(Vec3d origin, Vec3d direction, Box box) {

double d = (box.minX + box.maxX) * 0.5;
double e = (box.maxX - box.minX) * 0.5;
double f = origin.x - d;
if (Math.abs(f) > e && f * direction.x > 0.0) {
return false;
}

double g = (box.minY + box.maxY) * 0.5;
double h = (box.maxY - box.minY) * 0.5;
double i = origin.y - g;
if (Math.abs(i) > h && i * direction.y >= 0.0) {
return false;
}

double j = (box.minZ + box.maxZ) * 0.5;
double k = (box.maxZ - box.minZ) * 0.5;
double l = origin.z - j;
if (Math.abs(l) > k && l * direction.z >= 0.0) {
return false;
}

double m = Math.abs(direction.x);
double n = Math.abs(direction.y);
double o = Math.abs(direction.z);
double p = direction.y * l - direction.z * i;
if (Math.abs(p) > h * o + k * n || Math.abs(direction.z * f - direction.x * l) > e * o + k * m) {
return false;
}

return Math.abs(direction.x * i - direction.y * f) < e * n + h * m;
}

/**
* @author QPCrummer
* @reason Slightly more optimized
*/
@Overwrite
public static IntStream stream(int seed, int lowerBound, int upperBound, int steps) {

if (steps < 1 || seed < lowerBound || seed > upperBound) {
return IntStream.empty();
}

return IntStream.iterate(seed, i -> {
int nextValue = i + (i <= seed ? steps : -steps);
return nextValue >= lowerBound && nextValue <= upperBound ? nextValue : i;
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.github.tatercertified.potatoptimize.mixin.fastmath.vec;

import net.minecraft.util.math.Vec3d;
import org.spongepowered.asm.mixin.*;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(Vec3d.class)
public class FastMathVec3DMixin {
@Mutable
@Shadow
@Final
public double x;

@Mutable
@Shadow @Final public double y;

@Mutable
@Shadow @Final public double z;
private Vec3d cachedNormalized;

/**
* @author QPCrummer
* @reason Cache normalized Vec
*/
@Overwrite
public Vec3d normalize() {
if (cachedNormalized == null) {
double squaredLength = x * x + y * y + z * z;
if (squaredLength < 1.0E-8) {
cachedNormalized = Vec3d.ZERO;
} else {
double invLength = 1.0 / Math.sqrt(squaredLength);
cachedNormalized = new Vec3d(x * invLength, y * invLength, z * invLength);
}
}
return cachedNormalized;
}

// TODO Figure out why I did this...
@Inject(method = "relativize", at = @At("HEAD"))
private void optimizedRelativize(Vec3d vec, CallbackInfoReturnable<Vec3d> cir) {
this.x -= vec.x;
this.y -= vec.y;
this.z -= vec.z;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.github.tatercertified.potatoptimize.mixin.feature.interaction_limiter;

import com.github.tatercertified.potatoptimize.utils.interfaces.InteractionLimiterInterface;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.block.BlockState;
import net.minecraft.item.BlockItem;
import net.minecraft.item.ItemPlacementContext;
Expand All @@ -15,7 +13,6 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Environment(EnvType.SERVER)
@Mixin(BlockItem.class)
public abstract class LimiterBlockItemMixin {
@Shadow protected abstract @Nullable BlockState getPlacementState(ItemPlacementContext context);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.github.tatercertified.potatoptimize.mixin.feature.interaction_limiter;

import com.github.tatercertified.potatoptimize.utils.interfaces.InteractionLimiterInterface;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.network.packet.s2c.play.BlockUpdateS2CPacket;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.network.ServerPlayerInteractionManager;
Expand All @@ -15,7 +13,6 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Environment(EnvType.SERVER)
@Mixin(ServerPlayerInteractionManager.class)
public abstract class LimiterServerInteractionManagerMixin {
@Shadow @Final protected ServerPlayerEntity player;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package com.github.tatercertified.potatoptimize.mixin.feature.interaction_limiter;

import com.github.tatercertified.potatoptimize.utils.interfaces.InteractionLimiterInterface;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.server.network.ServerPlayerEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Environment(EnvType.SERVER)
@Mixin(ServerPlayerEntity.class)
public class LimiterServerPlayerEntityMixin implements InteractionLimiterInterface {
private int instaBreakCountPerTick = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ public class EulerAngleMixin {
private static float roll;

@Redirect(method = "<init>(FFF)V", at = @At(value = "FIELD", target = "Lnet/minecraft/util/math/EulerAngle;pitch:F"))
private void injectedPitch(EulerAngle instance, float value, @Local(ordinal = 0) float pitch) {
private void injectedPitch(EulerAngle instance, float value, @Local(ordinal = 0, argsOnly = true) float pitch) {
EulerAngleMixin.pitch = pitch;
}

@Redirect(method = "<init>(FFF)V", at = @At(value = "FIELD", target = "Lnet/minecraft/util/math/EulerAngle;yaw:F"))
private void injectedYaw(EulerAngle instance, float value, @Local(ordinal = 0) float yaw) {
private void injectedYaw(EulerAngle instance, float value, @Local(ordinal = 0, argsOnly = true) float yaw) {
EulerAngleMixin.yaw = yaw;
}

@Redirect(method = "<init>(FFF)V", at = @At(value = "FIELD", target = "Lnet/minecraft/util/math/EulerAngle;roll:F"))
private void injectedRoll(EulerAngle instance, float value, @Local(ordinal = 0) float roll) {
private void injectedRoll(EulerAngle instance, float value, @Local(ordinal = 0, argsOnly = true) float roll) {
EulerAngleMixin.roll = roll;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,19 @@
import com.github.tatercertified.potatoptimize.utils.ServerMinionThread;
import net.minecraft.util.Util;
import org.spongepowered.asm.mixin.*;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;

@Mixin(Util.class)
public class UtilClassMixin {
// TODO Determine the hit to performance of this by making it not final
@Shadow @Final @Mutable
private final static ExecutorService MAIN_WORKER_EXECUTOR = createWorker();

@Unique
private static ExecutorService createWorker() {
@Redirect(method = "<clinit>", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/Util;createWorker(Ljava/lang/String;)Ljava/util/concurrent/ExecutorService;"))
private static ExecutorService redirectCreateWorker(String name) {
// TODO Do smart calculations to find the optimal amount of cores to use
int i = Math.min(8, Math.max(Runtime.getRuntime().availableProcessors() - 2, 1));
// PaperMC method below
//i = Integer.getInteger("Paper.WorkerThreadCount", i);
return new java.util.concurrent.ThreadPoolExecutor(i, i,0L, TimeUnit.MILLISECONDS, new java.util.concurrent.LinkedBlockingQueue<>(), target -> new ServerMinionThread(target, "Main", -1)) {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public class AdvancementsMixin {

@Redirect(method = "<init>(Ljava/util/Optional;Ljava/util/Optional;Lnet/minecraft/advancement/AdvancementRewards;Ljava/util/Map;Lnet/minecraft/advancement/AdvancementRequirements;Z)V", at = @At(value = "INVOKE", target = "Ljava/util/Map;copyOf(Ljava/util/Map;)Ljava/util/Map;"))
private static Map<String, AdvancementCriterion<?>> removeCopyOfMap(Map<String, AdvancementCriterion<?>> map, @Local(ordinal = 0) Map<String, AdvancementCriterion<?>> criteria) {
private static Map<String, AdvancementCriterion<?>> removeCopyOfMap(Map<String, AdvancementCriterion<?>> map, @Local(ordinal = 0, argsOnly = true) Map<String, AdvancementCriterion<?>> criteria) {
return criteria;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.github.tatercertified.potatoptimize.mixin.memory.reduce_alloc;

import com.moulberry.mixinconstraints.annotations.IfModAbsent;
import net.minecraft.block.BlockState;
import net.minecraft.entity.Entity;
import net.minecraft.fluid.FluidState;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.BlockView;
import net.minecraft.world.explosion.EntityExplosionBehavior;
import net.minecraft.world.explosion.Explosion;
import net.minecraft.world.explosion.ExplosionBehavior;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;

import java.util.Optional;

/**
* Credit: Leaves Patch #0091
*/
@IfModAbsent(value = "lithium")
@Mixin(EntityExplosionBehavior.class)
public class EntityExplosionBehaviorMixin extends ExplosionBehavior {
@Shadow @Final private Entity entity;

/**
* @author QPCrummer
* @reason Reduce Allocations
*/
@Overwrite
public Optional<Float> getBlastResistance(Explosion explosion, BlockView world, BlockPos pos, BlockState blockState, FluidState fluidState) {
Optional<Float> optionalBlastResistance = super.getBlastResistance(explosion, world, pos, blockState, fluidState);
if (optionalBlastResistance.isPresent()) {
float blastResistance = optionalBlastResistance.get();
float effectiveExplosionResistance = this.entity.getEffectiveExplosionResistance(explosion, world, pos, blockState, fluidState, blastResistance);
if (effectiveExplosionResistance != blastResistance) {
return Optional.of(effectiveExplosionResistance);
}
}
return optionalBlastResistance;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.github.tatercertified.potatoptimize.mixin.remove.profiler;

import net.minecraft.server.command.JfrCommand;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.Text;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;

@Mixin(JfrCommand.class)
public class JFRCommandMixin {
/**
* @author QPCrummer
* @reason Removal
*/
@Overwrite
private static int executeStart(ServerCommandSource source) {
source.sendFeedback(() -> Text.literal("JFR has been removed by Potatoptimize; Use Spark instead!"), false);
return 0;
}

/**
* @author QPCrummer
* @reason Removal
*/
@Overwrite
private static int executeStop(ServerCommandSource source) {
source.sendFeedback(() -> Text.literal("JFR has been removed by Potatoptimize; Use Spark instead!"), false);
return 0;
}
}
Loading

0 comments on commit bb4beea

Please sign in to comment.