Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Light emission per blockstate #72

Merged
merged 5 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public enum Blocks {
CUSTOM_MODEL_BLOCK("farlands_block", "farlands_block", id -> new ModdedModelBlock(id, Material.DIRT).setHardness(1)),
FREEZER("freezer", "freezer", id -> new BlockFreezer(id).setHardness(2.5F).setSounds(TemplateBlockBase.STONE_SOUNDS)),
ALTAR("altar", "altar", id -> new BlockAltar(id, Material.STONE).setHardness(3)),
VARIATION_BLOCK("variation_block", "variationBlock", id -> new VariationBlock(id, Material.STONE).setHardness(.5F).setSounds(BlockBase.STONE_SOUNDS).disableAutomaticBlockItemRegistration());
VARIATION_BLOCK("variation_block", "variationBlock", id -> new VariationBlock(id, Material.STONE).setHardness(.5F).setSounds(BlockBase.STONE_SOUNDS).disableAutomaticBlockItemRegistration()),
EMISSION_CHECKER("emission_checker", "emissionChecker", LampBlock::new);

private final Runnable register;
private BlockBase block;
Expand Down
52 changes: 52 additions & 0 deletions src/test/java/net/modificationstation/sltest/block/LampBlock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package net.modificationstation.sltest.block;

import net.minecraft.block.BlockBase;
import net.minecraft.block.material.Material;
import net.minecraft.entity.player.PlayerBase;
import net.minecraft.level.BlockView;
import net.minecraft.level.Level;
import net.modificationstation.stationapi.api.block.BlockState;
import net.modificationstation.stationapi.api.registry.Identifier;
import net.modificationstation.stationapi.api.state.StateManager.Builder;
import net.modificationstation.stationapi.api.state.property.BooleanProperty;
import net.modificationstation.stationapi.api.template.block.TemplateBlockBase;
import net.modificationstation.stationapi.api.world.BlockStateView;

public class LampBlock extends TemplateBlockBase {
private static final BooleanProperty ACTIVE = BooleanProperty.of("active");

public LampBlock(Identifier id) {
super(id, Material.WOOD);
setLuminance(state -> state.get(ACTIVE) ? 15 : 0);
setTranslationKey(id);
setSounds(WOOD_SOUNDS);
setDefaultState(getDefaultState().with(ACTIVE, false));
}

@Override
public void appendProperties(Builder<BlockBase, BlockState> builder) {
super.appendProperties(builder);
builder.add(ACTIVE);
}

@Override
public int getTextureForSide(BlockView view, int x, int y, int z, int side) {
if (view instanceof BlockStateView stateView) {
return stateView.getBlockState(x, y, z).get(ACTIVE) ? STILL_LAVA.texture : OBSIDIAN.texture;
}
return OBSIDIAN.texture;
}

@Override
public int getTextureForSide(int side) {
return OBSIDIAN.texture;
}

@Override
public boolean canUse(Level level, int x, int y, int z, PlayerBase player) {
BlockState state = level.getBlockState(x, y, z);
state = state.with(ACTIVE, !state.get(ACTIVE));
level.setBlockState(x, y, z, state);
return super.canUse(level, x, y, z, player);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import net.modificationstation.stationapi.api.state.property.Property;
import net.modificationstation.stationapi.api.tag.TagKey;
import net.modificationstation.stationapi.api.util.math.MathHelper;
import net.modificationstation.stationapi.impl.block.StationFlatteningBlockInternal;

import java.util.function.Predicate;
import java.util.stream.Stream;
Expand All @@ -26,6 +27,7 @@ public abstract class AbstractBlockState extends State<BlockBase, BlockState> {
private final MaterialColour materialColor;
private final boolean toolRequired;
private final boolean opaque;
private int luminance = -1;

protected AbstractBlockState(BlockBase block, ImmutableMap<Property<?>, Comparable<?>> propertyMap, MapCodec<BlockState> mapCodec) {
super(block, propertyMap, mapCodec);
Expand All @@ -48,7 +50,9 @@ public Material getMaterial() {
* Returns the light level emitted by this block state.
*/
public int getLuminance() {
return BlockBase.EMITTANCE[owner.id];
return luminance == -1 ?
luminance = ((StationFlatteningBlockInternal) owner).stationapi_getLuminanceProvider().applyAsInt(asBlockState()) :
luminance;
}

public boolean isAir() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.jetbrains.annotations.ApiStatus;

import java.util.List;
import java.util.function.ToIntFunction;

public interface StationFlatteningBlock extends
RemappableRawIdHolder,
Expand Down Expand Up @@ -103,4 +104,8 @@ default boolean canReplace(BlockState state, ItemPlacementContext context) {
default void onBlockPlaced(Level world, int x, int y, int z, BlockState replacedState) {
Util.assertImpl();
}

default BlockBase setLuminance(ToIntFunction<BlockState> provider) {
return Util.assertImpl();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.modificationstation.stationapi.impl.block;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;

@Environment(EnvType.CLIENT)
public class BlockBrightness {
public static int light;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.modificationstation.stationapi.impl.block;

import net.modificationstation.stationapi.api.block.BlockState;

import java.util.function.ToIntFunction;

public interface StationFlatteningBlockInternal {
ToIntFunction<BlockState> stationapi_getLuminanceProvider();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.modificationstation.stationapi.mixin.flattening;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.block.BlockBase;
import net.minecraft.block.material.Material;
import net.minecraft.entity.player.PlayerBase;
Expand All @@ -19,16 +21,19 @@
import net.modificationstation.stationapi.api.registry.RegistryEntry;
import net.modificationstation.stationapi.api.registry.sync.trackers.*;
import net.modificationstation.stationapi.api.state.StateManager;
import net.modificationstation.stationapi.impl.block.BlockBrightness;
import net.modificationstation.stationapi.impl.block.BlockDropListImpl;
import net.modificationstation.stationapi.impl.block.StationFlatteningBlockInternal;
import org.objectweb.asm.Opcodes;
import org.spongepowered.asm.mixin.*;
import org.spongepowered.asm.mixin.injection.*;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.List;
import java.util.function.ToIntFunction;

@Mixin(BlockBase.class)
public abstract class MixinBlockBase implements StationFlatteningBlock {
public abstract class MixinBlockBase implements StationFlatteningBlock, StationFlatteningBlockInternal {

@Shadow public abstract void beforeDestroyedByExplosion(Level arg, int i, int j, int k, int l, float f);

Expand Down Expand Up @@ -318,4 +323,28 @@ private int ensureCapacity(int rawId) {
private static ItemBase onlyUnderShift(ItemBase[] array, int index) {
return index < ItemRegistry.ID_SHIFT ? array[index] : null;
}

@Unique private ToIntFunction<BlockState> stationapi_luminance = state -> BlockBase.EMITTANCE[state.getBlock().id];

@Override
public BlockBase setLuminance(ToIntFunction<BlockState> provider) {
stationapi_luminance = provider;

// Need for proper functionality of LevelMixin
BlockBase.EMITTANCE[id] = 15;

return BlockBase.class.cast(this);
}

@Override
@Unique
public ToIntFunction<BlockState> stationapi_getLuminanceProvider() {
return stationapi_luminance;
}

@Environment(value= EnvType.CLIENT)
@ModifyArg(method = "getBrightness", at = @At(value = "INVOKE", target = "Lnet/minecraft/level/BlockView;method_1784(IIII)F"), index = 3)
private int getStateBrightness(int original) {
return BlockBrightness.light;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package net.modificationstation.stationapi.mixin.flattening;

import com.llamalad7.mixinextras.sugar.Local;
import net.minecraft.class_417;
import net.minecraft.level.Level;
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.Constant;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyConstant;
import org.spongepowered.asm.mixin.injection.*;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(class_417.class)
Expand All @@ -21,7 +19,6 @@ private void method_1869(Level level, CallbackInfo info) {
minBlock = (short) level.getBottomY();
}

@SuppressWarnings("MixinAnnotationTarget")
@ModifyConstant(method = "method_1402(Lnet/minecraft/level/Level;)V", constant = @Constant(expandZeroConditions = Constant.Condition.GREATER_THAN_OR_EQUAL_TO_ZERO, ordinal = 0))
private int changeMinHeight(int value) {
return minBlock;
Expand All @@ -41,4 +38,16 @@ private int changeMaxHeight(int value) {
private int changeMaxHeightFallback(int value) {
return maxBlock - 1;
}

@ModifyVariable(
method = "method_1402(Lnet/minecraft/level/Level;)V",
at = @At(
value = "STORE",
ordinal = 2
),
index = 20
)
private int getStateLuminance(int original, @Local Level level, @Local(index = 10) int x, @Local(index = 15) int y, @Local(index = 11) int z) {
return level.getBlockState(x, y, z).getLuminance();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,17 @@ public int getBottomY() {
private BlockBase accountForAirBlock(BlockBase value) {
return value == States.AIR.get().getBlock() ? null : value;
}

@ModifyVariable(
method = "method_165(Lnet/minecraft/level/LightType;IIII)V",
at = @At(
value = "STORE",
ordinal = 1
),
index = 5,
argsOnly = true
)
private int getStateLuminance(int original, @Local(index = 2) int x, @Local(index = 3) int y, @Local(index = 4) int z, @Local(index = 5) int light) {
return Math.max(getBlockState(x, y, z).getLuminance(), light);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package net.modificationstation.stationapi.mixin.flattening.client;

import net.minecraft.block.BlockBase;
import net.minecraft.client.render.block.BlockRenderer;
import net.minecraft.level.BlockView;
import net.modificationstation.stationapi.api.block.BlockState;
import net.modificationstation.stationapi.api.world.BlockStateView;
import net.modificationstation.stationapi.impl.block.BlockBrightness;
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.CallbackInfoReturnable;

@Mixin(value = BlockRenderer.class, priority = 500)
public class MixinBlockRenderer {
@Shadow private BlockView blockView;

@Inject(method = "render", at = @At("HEAD"))
private void captureLightEmission(BlockBase block, int x, int y, int z, CallbackInfoReturnable<Boolean> info) {
if (blockView instanceof BlockStateView stateView) {
BlockState state = stateView.getBlockState(x, y, z);
BlockBrightness.light = state.getLuminance();
}
else BlockBrightness.light = BlockBase.EMITTANCE[block.id];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@
"RegionFileAccessor"
],
"client": [
"client.MixinBaseClientInteractionManager",
"client.MixinBlockRenderer",
"client.MixinClientLevel",
"client.MixinInGame",
"client.MixinMinecraft",
"client.MixinMultiplayerChunkCache",
"client.MixinMultiPlayerClientInteractionManager",
"client.MixinPlayerRenderer",
"client.MixinSinglePlayerClientInteractionManager",
"client.MixinWorldRenderer",
"client.MixinBaseClientInteractionManager"
"client.MixinWorldRenderer"
],
"injectors": {
"defaultRequire": 1
Expand Down