Skip to content

Commit

Permalink
Overwriteifycation
Browse files Browse the repository at this point in the history
  • Loading branch information
Desoroxxx committed Nov 8, 2023
1 parent cadf3ec commit 163883a
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 40 deletions.
6 changes: 6 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Changed

- Lighting engine will now schedule updates no matter if that chunk is loaded, which is different from vanilla but potentially fix areas lighting being weird when going far away
- Made all head-cancels overwrite instead
- All overwrites now make the scope of the overwritten methods `public` to prevent access level conflicts at runtime
- Now depends on Red Core 0.5
- Now depends on MixinBooter 8.6

Expand All @@ -26,6 +28,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Switched to Adoptium
- Moved logo to the root of the resources module

### Removed

- Removed access transformed as overwrites can already do the job on their own

## Alfheim Version 1.0.2 Changelog - 2023-09-12

### Fixed
Expand Down
9 changes: 0 additions & 9 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,6 @@ tasks.processResources.configure {
}
}

val at = project.files("src/main/resources/META-INF/${id}_at.cfg")

tasks.deobfuscateMergedJarToSrg.configure {
accessTransformerFiles.from(at)
}
tasks.srgifyBinpatchedJar.configure {
accessTransformerFiles.from(at)
}

tasks.named<Jar>("jar") {
manifest {
attributes(
Expand Down
24 changes: 16 additions & 8 deletions src/main/java/dev/redstudio/alfheim/mixin/BlockMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
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 org.spongepowered.asm.mixin.injection.callback.LocalCapture;

/**
Expand All @@ -33,10 +33,14 @@ public abstract class BlockMixin implements ILitBlock {

@Shadow @Deprecated public abstract int getLightValue(final IBlockState blockState);

/**
* @reason Part of non-full block lighting fix
* @author Luna Lage (Desoroxxx)
*/
@Overwrite
@SideOnly(Side.CLIENT)
@Inject(method = "getPackedLightmapCoords", at = @At("HEAD"), cancellable = true)
private void getCorrectPackedLightmapCoords(final IBlockState blockState, final IBlockAccess source, final BlockPos blockPos, final CallbackInfoReturnable<Integer> callbackInfoReturnable) {
callbackInfoReturnable.setReturnValue(source.getCombinedLight(blockPos, blockState.getLightValue(source, blockPos)));
public int getPackedLightmapCoords(final IBlockState blockState, final IBlockAccess source, final BlockPos blockPos) {
return source.getCombinedLight(blockPos, blockState.getLightValue(source, blockPos));
}

@Inject(method = "registerBlocks", at = @At(value = "FIELD", target = "Lnet/minecraft/block/Block;useNeighborBrightness:Z", ordinal = 1, shift = At.Shift.BEFORE), locals = LocalCapture.CAPTURE_FAILEXCEPTION)
Expand All @@ -49,15 +53,19 @@ private static void checkForLiquid(final CallbackInfo callbackInfo, @Local(ordin
flag.set(result);
}

/**
* @reason Part of non-full block lighting fix
* @author Luna Lage (Desoroxxx)
*/
@Overwrite
@SideOnly(Side.CLIENT)
@Inject(method = "getAmbientOcclusionLightValue", at = @At(value = "HEAD"), cancellable = true)
private void getCorrectOcclusionLightValue(final IBlockState blockState, final CallbackInfoReturnable<Float> callbackInfoReturnable) {
public float getAmbientOcclusionLightValue(final IBlockState blockState) {
final byte lightValue = (byte) MathUtil.clampMinFirst(blockState.getLightValue() -1, 0, 15);

if (lightValue == 0)
callbackInfoReturnable.setReturnValue(blockState.isBlockNormalCube() ? 0.2F : 1);
return blockState.isBlockNormalCube() ? 0.2F : 1;
else
callbackInfoReturnable.setReturnValue(1F);
return 1F; // Todo: Cleanup
}

@Override
Expand Down
19 changes: 12 additions & 7 deletions src/main/java/dev/redstudio/alfheim/mixin/ChunkCacheMixin.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package dev.redstudio.alfheim.mixin;

import dev.redstudio.alfheim.api.ILightLevelProvider;
import dev.redstudio.alfheim.api.ILightInfoProvider;
import dev.redstudio.alfheim.api.ILightLevelProvider;
import net.minecraft.block.state.IBlockState;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.ChunkCache;
import net.minecraft.world.EnumSkyBlock;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
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.CallbackInfoReturnable;

/**
* @author Luna Lage (Desoroxxx)
Expand All @@ -25,10 +23,17 @@ public abstract class ChunkCacheMixin implements ILightLevelProvider {

@Shadow public abstract IBlockState getBlockState(BlockPos pos);

/**
* Explicitly making this method we overwrite public to prevent access level conflicts at runtime.
* Mods may adjust the visibility of this method to public, causing a crash if our overwritten method has different scope.
*
* @reason Redirect to our lighting engine.
* @author Luna Lage (Desoroxxx)
*/
@Overwrite
@SideOnly(Side.CLIENT)
@Inject(method = "getLightForExt", at = @At("HEAD"), cancellable = true)
private void getLightForExt(final EnumSkyBlock lightType, final BlockPos blockPos, final CallbackInfoReturnable<Integer> callbackInfoReturnable) {
callbackInfoReturnable.setReturnValue(((ILightInfoProvider) getBlockState(blockPos)).alfheim$getLightFor(((ChunkCache) (Object) this), lightType, blockPos));
public int getLightForExt(final EnumSkyBlock lightType, final BlockPos blockPos) {
return ((ILightInfoProvider) getBlockState(blockPos)).alfheim$getLightFor(((ChunkCache) (Object) this), lightType, blockPos);
}

@Override
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/dev/redstudio/alfheim/mixin/ChunkMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ private void setLightForRedirectGenerateSkylightMap(final Chunk chunk, final Enu
}

/**
* Explicitly making this method we overwrite public to prevent access level conflicts at runtime.
* Mods may adjust the visibility of this method to public, causing a crash if our overwritten method has different scope.
*
* @reason Overwrites relightBlock with a more efficient implementation.
* @author Angeline (@jellysquid)
*/
Expand Down Expand Up @@ -202,11 +205,14 @@ public void checkLight() {
}

/**
* Explicitly making this method we overwrite public to prevent access level conflicts at runtime.
* Mods may adjust the visibility of this method to public, causing a crash if our overwritten method has different scope.
*
* @reason Avoids chunk fetches as much as possible.
* @author Angeline (@jellysquid), Luna Lage (Desoroxxx)
*/
@Overwrite
private void recheckGaps(final boolean onlyOne) {
public void recheckGaps(final boolean onlyOne) {
if (!world.isAreaLoaded(new BlockPos((x << 4) + 8, 0, (z << 4) + 8), 16))
return;

Expand Down
35 changes: 21 additions & 14 deletions src/main/java/dev/redstudio/alfheim/mixin/WorldMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
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.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

/**
* @author Luna Lage (Desoroxxx)
Expand Down Expand Up @@ -44,32 +44,39 @@ private void onConstructed(final CallbackInfo callbackInfo) {
alfheim$lightingEngine = new LightingEngine((World) (Object) this);
}

// Todo: We should be the only ones overwriting theses methods, cancelling like that is just a bad overwrite, in Dev 2 try to overwrite them and see if chaos unfolds

/**
* Directs the light update to the lighting engine and always returns a success value.
* @reason Redirect to our lighting engine.
* @author Luna Lage (Desoroxxx)
*/
@Inject(method = "checkLightFor", at = @At(value = "HEAD"), cancellable = true)
private void redirectLightUpdate(final EnumSkyBlock lightType, final BlockPos blockPos, final CallbackInfoReturnable<Boolean> callbackInfoReturnable) {
@Overwrite
public boolean checkLightFor(final EnumSkyBlock lightType, final BlockPos blockPos) {
alfheim$lightingEngine.scheduleLightUpdate(lightType, blockPos);

callbackInfoReturnable.setReturnValue(true);
return true;
}

@Inject(method = "getLight(Lnet/minecraft/util/math/BlockPos;Z)I", at = @At("HEAD"), cancellable = true)
private void getLight(final BlockPos blockPos, final boolean checkNeighbors, final CallbackInfoReturnable<Integer> callbackInfoReturnable) {
/**
* @reason Redirect to our lighting engine.
* @author Luna Lage (Desoroxxx)
*/
@Overwrite
public int getLight(final BlockPos blockPos, final boolean checkNeighbors) {
if (!checkNeighbors)
callbackInfoReturnable.setReturnValue(getLight(blockPos));
return getLight(blockPos);

final IBlockState blockState = getBlockState(blockPos);

callbackInfoReturnable.setReturnValue(Math.max(((ILightInfoProvider) blockState).alfheim$getLightFor(((World) (Object) this), EnumSkyBlock.BLOCK, blockPos), ((ILightInfoProvider) blockState).alfheim$getLightFor(((World) (Object) this), EnumSkyBlock.SKY, blockPos) - skylightSubtracted));
return Math.max(((ILightInfoProvider) blockState).alfheim$getLightFor(((World) (Object) this), EnumSkyBlock.BLOCK, blockPos), ((ILightInfoProvider) blockState).alfheim$getLightFor(((World) (Object) this), EnumSkyBlock.SKY, blockPos) - skylightSubtracted);
}

/**
* @reason Redirect to our lighting engine.
* @author Luna Lage (Desoroxxx)
*/
@Overwrite
@SideOnly(Side.CLIENT)
@Inject(method = "getLightFromNeighborsFor", at = @At("HEAD"), cancellable = true)
private void getLightFromNeighborsFor(final EnumSkyBlock lightType, final BlockPos blockPos, final CallbackInfoReturnable<Integer> callbackInfoReturnable) {
callbackInfoReturnable.setReturnValue(((ILightInfoProvider) getBlockState(blockPos)).alfheim$getLightFor(((World) (Object) this), lightType, blockPos));
public int getLightFromNeighborsFor(final EnumSkyBlock lightType, final BlockPos blockPos) {
return ((ILightInfoProvider) getBlockState(blockPos)).alfheim$getLightFor(((World) (Object) this), lightType, blockPos);
}

@Override
Expand Down
1 change: 0 additions & 1 deletion src/main/resources/META-INF/alfheim_at.cfg

This file was deleted.

0 comments on commit 163883a

Please sign in to comment.