Skip to content

Commit

Permalink
Support modded light blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeregorix committed Apr 13, 2023
1 parent 7834309 commit 3ec7db1
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.block.state.BlockState;
import org.spongepowered.common.bridge.world.level.block.state.BlockStateBridge;
import org.spongepowered.common.event.tracking.context.transaction.pipeline.BlockPipeline;
import org.spongepowered.common.event.tracking.context.transaction.pipeline.PipelineCursor;
import org.spongepowered.common.world.SpongeBlockChangeFlag;
Expand All @@ -51,7 +52,6 @@ public EffectResult processSideEffect(
if (!flag.updateLighting()) {
return EffectResult.NULL_PASS;
}
final int originalOpactiy = oldState.opacity;
final ServerLevel serverWorld = pipeline.getServerWorld();
final BlockState currentState = pipeline.getAffectedChunk().getBlockState(oldState.pos);
// local variable notes:
Expand All @@ -68,8 +68,8 @@ public EffectResult processSideEffect(
// )
// ) {
if (oldState.state != currentState
&& (currentState.getLightBlock(serverWorld, oldState.pos) != originalOpactiy
|| currentState.getLightEmission() != oldState.state.getLightEmission()
&& (currentState.getLightBlock(serverWorld, oldState.pos) != oldState.opacity
|| ((BlockStateBridge) currentState).bridge$getLightValue(serverWorld, oldState.pos) != oldState.light
|| currentState.useShapeForLightOcclusion()
|| oldState.state.useShapeForLightOcclusion()
)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import net.minecraft.world.level.chunk.LevelChunkSection;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.common.bridge.world.level.block.state.BlockStateBridge;
import org.spongepowered.common.event.tracking.PhaseContext;
import org.spongepowered.common.event.tracking.context.transaction.EffectTransactor;
import org.spongepowered.common.event.tracking.context.transaction.ResultingTransactionBySideEffect;
Expand Down Expand Up @@ -111,10 +112,11 @@ public LevelChunkSection getAffectedSection() {
return null;
}
final ServerLevel serverWorld = this.serverWorld.get();
final int oldLight = ((BlockStateBridge) currentState).bridge$getLightValue(serverWorld, pos);
final int oldOpacity = currentState.getLightBlock(serverWorld, pos);
final SpongeBlockChangeFlag flag = this.transaction.getBlockChangeFlag();
final @Nullable BlockEntity existing = this.chunkSupplier.get().getBlockEntity(pos, LevelChunk.EntityCreationType.CHECK);
PipelineCursor formerState = new PipelineCursor(currentState, oldOpacity, pos, existing, (Entity) null, limit);
PipelineCursor formerState = new PipelineCursor(currentState, oldLight, oldOpacity, pos, existing, (Entity) null, limit);

for (final ResultingTransactionBySideEffect effect : this.chunkEffects) {
try (final EffectTransactor ignored = context.getTransactor().pushEffect(effect)) {
Expand All @@ -129,7 +131,7 @@ public LevelChunkSection getAffectedSection() {
return result.resultingState;
}
if (formerState.drops.isEmpty() && !result.drops.isEmpty()) {
formerState = new PipelineCursor(currentState, oldOpacity, pos, existing, null, result.drops, limit);
formerState = new PipelineCursor(currentState, oldLight, oldOpacity, pos, existing, null, result.drops, limit);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,20 @@

public final class PipelineCursor {
public final BlockState state;
public final int light;
public final int opacity;
public final BlockPos pos;
public final @Nullable BlockEntity tileEntity;
public final @Nullable Entity destroyer;
public final List<ItemStack> drops;
public final int limit;

public PipelineCursor(final BlockState state, final int opacity, final BlockPos pos,
public PipelineCursor(final BlockState state, final int light, final int opacity, final BlockPos pos,
final @Nullable BlockEntity tileEntity,
final @Nullable Entity destroyer, final int limit
) {
this.state = state;
this.light = light;
this.opacity = opacity;
this.pos = pos;
this.tileEntity = tileEntity;
Expand All @@ -57,13 +59,14 @@ public PipelineCursor(final BlockState state, final int opacity, final BlockPos
this.limit = limit;
}

public PipelineCursor(final BlockState state, final int opacity, final BlockPos pos,
public PipelineCursor(final BlockState state, final int light, final int opacity, final BlockPos pos,
final @Nullable BlockEntity tileEntity,
final @Nullable Entity destroyer,
final List<ItemStack> drops,
final int limit
) {
this.state = state;
this.light = light;
this.opacity = opacity;
this.pos = pos;
this.tileEntity = tileEntity;
Expand All @@ -74,12 +77,9 @@ public PipelineCursor(final BlockState state, final int opacity, final BlockPos

@Override
public String toString() {
return new StringJoiner(
", ",
PipelineCursor.class.getSimpleName() + "[",
"]"
)
return new StringJoiner(", ", PipelineCursor.class.getSimpleName() + "[", "]")
.add("state=" + this.state)
.add("light=" + this.light)
.add("opacity=" + this.opacity)
.add("pos=" + this.pos)
.add("tileEntity=" + this.tileEntity)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public boolean processEffects(final PhaseContext<?> context, final PipelineCurso
if (result.resultingState != currentCursor.state) {
currentCursor = new PipelineCursor(
result.resultingState,
currentCursor.light,
currentCursor.opacity,
currentCursor.pos,
currentCursor.tileEntity,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import net.minecraft.world.level.chunk.LevelChunkSection;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.common.bridge.world.level.block.state.BlockStateBridge;
import org.spongepowered.common.event.tracking.PhaseContext;
import org.spongepowered.common.event.tracking.context.transaction.EffectTransactor;
import org.spongepowered.common.event.tracking.context.transaction.ResultingTransactionBySideEffect;
Expand Down Expand Up @@ -98,8 +99,9 @@ public boolean processEffects(final PhaseContext<?> context, final BlockState cu
if (oldState == null) {
return false;
}
final int oldLight = ((BlockStateBridge) oldState).bridge$getLightValue(serverWorld, pos);
final int oldOpacity = oldState.getLightBlock(serverWorld, pos);
PipelineCursor formerState = new PipelineCursor(oldState, oldOpacity, pos, existing, destroyer, limit);
PipelineCursor formerState = new PipelineCursor(oldState, oldLight, oldOpacity, pos, existing, destroyer, limit);

for (final ResultingTransactionBySideEffect effect : this.worldEffects) {
try (final EffectTransactor ignored = context.getTransactor().pushEffect(effect)) {
Expand All @@ -114,7 +116,7 @@ public boolean processEffects(final PhaseContext<?> context, final BlockState cu
return result.resultingState != null;
}
if (formerState.drops.isEmpty() && !result.drops.isEmpty()) {
formerState = new PipelineCursor(oldState, oldOpacity, pos, existing, formerState.destroyer, result.drops, limit);
formerState = new PipelineCursor(oldState, oldLight, oldOpacity, pos, existing, formerState.destroyer, result.drops, limit);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,8 @@ public boolean destroyBlock(final BlockPos pos, final boolean doDrops, @Nullable
.addEffect(RemoveTileEntityFromWorldEffect.getInstance())
.addEffect(RemoveTileEntityFromChunkEffect.getInstance())
.build();
pipeline.processEffects(current, new PipelineCursor(tileentity.getBlockState(), 0,immutable, tileentity, (Entity) null, Constants.World.DEFAULT_BLOCK_CHANGE_LIMIT));
pipeline.processEffects(current, new PipelineCursor(tileentity.getBlockState(), 0, 0, immutable, tileentity, (Entity) null,
Constants.World.DEFAULT_BLOCK_CHANGE_LIMIT));
return;
}
super.shadow$removeBlockEntity(immutable);
Expand Down Expand Up @@ -670,7 +671,7 @@ public boolean destroyBlock(final BlockPos pos, final boolean doDrops, @Nullable
.addEffect(AddTileEntityToTickableListEffect.getInstance())
.addEffect(TileOnLoadDuringAddToWorldEffect.getInstance())
.build();
return pipeline.processEffects(current, new PipelineCursor(tileEntity.getBlockState(), 0, immutable, tileEntity,
return pipeline.processEffects(current, new PipelineCursor(tileEntity.getBlockState(), 0, 0, immutable, tileEntity,
(Entity) null,
Constants.World.DEFAULT_BLOCK_CHANGE_LIMIT));
}
Expand Down Expand Up @@ -707,7 +708,8 @@ public boolean destroyBlock(final BlockPos pos, final boolean doDrops, @Nullable
.addEffect(RemoveProposedTileEntitiesDuringSetIfWorldProcessingEffect.getInstance())
.addEffect(ReplaceTileEntityInWorldEffect.getInstance())
.build();
pipeline.processEffects(current, new PipelineCursor(proposed.getBlockState(), 0,immutable, proposed, (Entity) null, Constants.World.DEFAULT_BLOCK_CHANGE_LIMIT));
pipeline.processEffects(current, new PipelineCursor(proposed.getBlockState(), 0, 0, immutable, proposed, (Entity) null,
Constants.World.DEFAULT_BLOCK_CHANGE_LIMIT));
return;
}
}
Expand Down

0 comments on commit 3ec7db1

Please sign in to comment.