Skip to content

Commit

Permalink
simpler ItemLightSource getLuminance(ItemStack stack, BlockState stat…
Browse files Browse the repository at this point in the history
…e) code
  • Loading branch information
BoBkiNN committed Jun 30, 2024
1 parent 1c4bf2f commit 4dab47e
Showing 1 changed file with 4 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.BlockStateComponent;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
Expand Down Expand Up @@ -163,26 +164,11 @@ public int getLuminance(ItemStack stack) {
}

static int getLuminance(ItemStack stack, BlockState state) {
var blockStates = stack.get(DataComponentTypes.BLOCK_STATE);

if (blockStates != null) {
var properties = blockStates.properties();
var stateManager = state.getBlock().getStateManager();

for (var key : properties.keySet()) {
var property = stateManager.getProperty(key);
if (property != null) {
var value = properties.get(key);
state = with(state, property, value);
}
}
var blockState = stack.getComponents().getOrDefault(DataComponentTypes.BLOCK_STATE, BlockStateComponent.DEFAULT);
if (!blockState.isEmpty()){
state = blockState.apply(state);
}

return state.getLuminance();
}

private static <T extends Comparable<T>> BlockState with(BlockState state, Property<T> property, String name) {
return property.parse(name).map(value -> state.with(property, value)).orElse(state);
}
}
}

0 comments on commit 4dab47e

Please sign in to comment.