Skip to content
This repository has been archived by the owner on Jan 18, 2023. It is now read-only.

Commit

Permalink
Fixed MixinClientWorld loading on server bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Tri0de committed Jan 15, 2021
1 parent 39ec822 commit c7017ce
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 46 deletions.
64 changes: 18 additions & 46 deletions src/main/java/org/valkyrienskies/mixin/world/MixinClientWorld.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package org.valkyrienskies.mixin.world;

import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.EnumSkyBlock;
import net.minecraft.world.World;
import org.joml.Vector3d;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.joml.Vector3dc;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
Expand All @@ -17,10 +15,10 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.valkyrienskies.mod.common.config.VSConfig;
import org.valkyrienskies.mod.common.ships.ship_world.IWorldVS;
import org.valkyrienskies.mod.common.ships.ship_world.PhysicsObject;
import org.valkyrienskies.mod.common.util.JOML;
import org.valkyrienskies.mod.common.util.ValkyrienUtils;
import org.valkyrienskies.mod.fixes.FixAccurateRain;
import valkyrienwarfare.api.TransformType;

import java.util.HashMap;
Expand Down Expand Up @@ -50,58 +48,32 @@ private void onPreTick(CallbackInfo ci) {
*
* @author DaPorkchop_, rubydesic
*/
@SideOnly(Side.CLIENT)
@Overwrite
public BlockPos getPrecipitationHeight(BlockPos input) {
// Try to use the precipitationHeightCache
if (precipitationHeightCache.containsKey(input)) {
final BlockPos cached = precipitationHeightCache.get(input);
if (cached != null) {
return cached;
}
}

final World thisAsWorld = World.class.cast(this);

final BlockPos originalHeight = thisAsWorld.getChunk(input).getPrecipitationHeight(input);

if (VSConfig.accurateRain && Minecraft.getMinecraft().player != null) {
final AxisAlignedBB boundingBox = new AxisAlignedBB(input.getX() - .5, 0, input.getZ() - .5, input.getX() + .5, 255, input.getZ() + .5);
final List<PhysicsObject> physicsObjectList = ValkyrienUtils.getPhysObjWorld(thisAsWorld).getPhysObjectsInAABB(boundingBox);

final Vec3d traceStart = new Vec3d(originalHeight.getX() + .5, Minecraft.getMinecraft().player.posY + 50, originalHeight.getZ() + .5);
final Vec3d traceEnd = new Vec3d(originalHeight.getX() + .5, originalHeight.getY() + .5, originalHeight.getZ() + .5);

if (traceStart.y < traceEnd.y) {
precipitationHeightCache.put(input, originalHeight);
return originalHeight;
}

for (final PhysicsObject physicsObject : physicsObjectList) {
final RayTraceResult result = ((IWorldVS) thisAsWorld).rayTraceBlocksInShip(traceStart, traceEnd, true, true, false, physicsObject);

//noinspection ConstantConditions
if (result != null && result.getBlockPos() != null && result.typeOfHit != RayTraceResult.Type.MISS) {
Vector3d blockPosVector = JOML.convertDouble(result.getBlockPos())
.add(.5, .5, .5);

physicsObject
.getShipTransformationManager()
.getCurrentTickTransform()
.getSubspaceToGlobal()
.transformPosition(blockPosVector);

final BlockPos modifiedHeight = new BlockPos(originalHeight.getX(), blockPosVector.y(), originalHeight.getZ());
precipitationHeightCache.put(input, modifiedHeight);
return modifiedHeight;
// Only do accurate rain on client side
if (thisAsWorld.isRemote) {
// Try to use the precipitationHeightCache
if (precipitationHeightCache.containsKey(input)) {
final BlockPos cached = precipitationHeightCache.get(input);
if (cached != null) {
return cached;
}
}
final BlockPos rainPosWithShips = FixAccurateRain.getRainPosFromShips(thisAsWorld, originalHeight);
precipitationHeightCache.put(input, rainPosWithShips);
return rainPosWithShips;
} else {
precipitationHeightCache.put(input, originalHeight);
return originalHeight;
}

precipitationHeightCache.put(input, originalHeight);
return originalHeight;
}


@SideOnly(Side.CLIENT)
@Inject(method = "getCombinedLight(Lnet/minecraft/util/math/BlockPos;I)I", at = @At("HEAD"),
cancellable = true)
private void preGetCombinedLight(BlockPos pos, int lightValue,
Expand Down
57 changes: 57 additions & 0 deletions src/main/java/org/valkyrienskies/mod/fixes/FixAccurateRain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.valkyrienskies.mod.fixes;

import net.minecraft.client.Minecraft;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import org.joml.Vector3d;
import org.valkyrienskies.mod.common.config.VSConfig;
import org.valkyrienskies.mod.common.ships.ship_world.IWorldVS;
import org.valkyrienskies.mod.common.ships.ship_world.PhysicsObject;
import org.valkyrienskies.mod.common.util.JOML;
import org.valkyrienskies.mod.common.util.ValkyrienUtils;

import java.util.List;

/**
* Why not just inline this code into MixinClientWorld? There's a strange mixin bug that causes MixinClientWorld to get
* loaded on servers. However by using World.isRemote we can prevent this code from ever being loaded on server side
* therefore preventing an incorrect side crash.
*/
public class FixAccurateRain {

public static BlockPos getRainPosFromShips(final World world, final BlockPos originalHeight) {
if (VSConfig.accurateRain && Minecraft.getMinecraft().player != null) {
final AxisAlignedBB boundingBox = new AxisAlignedBB(originalHeight.getX() - .5, 0, originalHeight.getZ() - .5, originalHeight.getX() + .5, 255, originalHeight.getZ() + .5);
final List<PhysicsObject> physicsObjectList = ValkyrienUtils.getPhysObjWorld(world).getPhysObjectsInAABB(boundingBox);

final Vec3d traceStart = new Vec3d(originalHeight.getX() + .5, Minecraft.getMinecraft().player.posY + 50, originalHeight.getZ() + .5);
final Vec3d traceEnd = new Vec3d(originalHeight.getX() + .5, originalHeight.getY() + .5, originalHeight.getZ() + .5);

if (traceStart.y < traceEnd.y) {
return originalHeight;
}

for (final PhysicsObject physicsObject : physicsObjectList) {
final RayTraceResult result = ((IWorldVS) world).rayTraceBlocksInShip(traceStart, traceEnd, true, true, false, physicsObject);

//noinspection ConstantConditions
if (result != null && result.getBlockPos() != null && result.typeOfHit != RayTraceResult.Type.MISS) {
Vector3d blockPosVector = JOML.convertDouble(result.getBlockPos())
.add(.5, .5, .5);

physicsObject
.getShipTransformationManager()
.getCurrentTickTransform()
.getSubspaceToGlobal()
.transformPosition(blockPosVector);

return new BlockPos(originalHeight.getX(), blockPosVector.y(), originalHeight.getZ());
}
}
}
return originalHeight;
}
}

0 comments on commit c7017ce

Please sign in to comment.