Move Belt LightListener to inner class

This commit is contained in:
Jozufozu 2022-01-04 14:11:42 -08:00
parent 5f81b31fe4
commit 5910ad5a03
3 changed files with 83 additions and 65 deletions

View File

@ -19,7 +19,7 @@ parchment_version = 2021.12.19
# dependency versions
registrate_version = MC1.18-1.0.21
flywheel_version = 1.18-0.5.1.38
flywheel_version = 1.18-0.5.1.39
jei_minecraft_version = 1.18
jei_version = 9.0.0.40

View File

@ -309,10 +309,10 @@ public class BeltRenderer extends SafeTileEntityRenderer<BeltTileEntity> {
protected int getPackedLight(BeltTileEntity controller, float beltPos) {
int segment = (int) Math.floor(beltPos) * 2;
if (controller.light == null || segment >= controller.light.length || segment < 0)
if (controller.lighter.light == null || segment >= controller.lighter.light.length || segment < 0)
return 0;
return LightTexture.pack(controller.light[segment], controller.light[segment + 1]);
return LightTexture.pack(controller.lighter.light[segment], controller.lighter.light[segment + 1]);
}
}

View File

@ -54,6 +54,7 @@ import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.client.model.data.IModelData;
import net.minecraftforge.client.model.data.ModelDataMap;
import net.minecraftforge.client.model.data.ModelProperty;
@ -63,7 +64,7 @@ import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
public class BeltTileEntity extends KineticTileEntity implements LightListener {
public class BeltTileEntity extends KineticTileEntity {
public Map<Entity, TransportedEntityInfo> passengers;
public Optional<DyeColor> color;
@ -78,8 +79,8 @@ public class BeltTileEntity extends KineticTileEntity implements LightListener {
public CompoundTag trackerUpdateTag;
// client
public byte[] light;
@OnlyIn(Dist.CLIENT)
public BeltLighter lighter;
public static enum CasingType {
NONE, ANDESITE, BRASS;
@ -119,11 +120,11 @@ public class BeltTileEntity extends KineticTileEntity implements LightListener {
if (!isController())
return;
if (light == null && level.isClientSide) {
initializeLight();
LightUpdater.get(level)
.addListener(this);
}
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> {
if (beltLength > 0 && lighter == null) {
lighter = new BeltLighter();
}
});
getInventory().tick();
@ -230,8 +231,11 @@ public class BeltTileEntity extends KineticTileEntity implements LightListener {
trackerUpdateTag = compound;
index = compound.getInt("Index");
beltLength = compound.getInt("Length");
if (prevBeltLength != beltLength)
light = null;
if (prevBeltLength != beltLength) {
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> {
lighter = null;
});
}
}
if (isController())
@ -535,43 +539,57 @@ public class BeltTileEntity extends KineticTileEntity implements LightListener {
return state != null && state.hasProperty(BeltBlock.PART) && state.getValue(BeltBlock.PART) == BeltPart.START;
}
public GridAlignedBB getVolume() {
BlockPos endPos = BeltHelper.getPositionForOffset(this, beltLength - 1);
GridAlignedBB bb = GridAlignedBB.from(worldPosition, endPos);
bb.fixMinMax();
return bb;
}
/**
* Hide this behavior in an inner class to avoid loading LightListener on servers.
*/
@OnlyIn(Dist.CLIENT)
class BeltLighter implements LightListener {
// client
public byte[] light;
@Override
public ListenerStatus status() {
return remove ? ListenerStatus.REMOVE : ListenerStatus.OKAY;
}
@Override
public void onLightUpdate(LightProvider world, LightLayer type, ImmutableBox changed) {
if (this.remove)
return;
if (this.level == null)
return;
GridAlignedBB beltVolume = getVolume();
if (beltVolume.intersects(changed)) {
if (light == null) {
initializeLight();
return;
}
if (type == LightLayer.BLOCK)
updateBlockLight();
if (type == LightLayer.SKY)
updateSkyLight();
public BeltLighter() {
initializeLight();
LightUpdater.get(level)
.addListener(this);
}
}
private void initializeLight() {
if (beltLength > 0) {
@Override
public GridAlignedBB getVolume() {
BlockPos endPos = BeltHelper.getPositionForOffset(BeltTileEntity.this, beltLength - 1);
GridAlignedBB bb = GridAlignedBB.from(worldPosition, endPos);
bb.fixMinMax();
return bb;
}
@Override
public ListenerStatus status() {
return remove ? ListenerStatus.REMOVE : ListenerStatus.OKAY;
}
@Override
public void onLightUpdate(LightProvider world, LightLayer type, ImmutableBox changed) {
if (remove)
return;
if (level == null)
return;
GridAlignedBB beltVolume = getVolume();
if (beltVolume.intersects(changed)) {
if (light == null) {
initializeLight();
return;
}
if (type == LightLayer.BLOCK)
updateBlockLight();
if (type == LightLayer.SKY)
updateSkyLight();
}
}
private void initializeLight() {
light = new byte[beltLength * 2];
Vec3i vec = getBeltFacing().getNormal();
@ -585,31 +603,31 @@ public class BeltTileEntity extends KineticTileEntity implements LightListener {
pos.move(vec.getX(), verticality, vec.getZ());
}
}
}
private void updateBlockLight() {
Vec3i vec = getBeltFacing().getNormal();
BeltSlope slope = getBlockState().getValue(BeltBlock.SLOPE);
int verticality = slope == BeltSlope.DOWNWARD ? -1 : slope == BeltSlope.UPWARD ? 1 : 0;
private void updateBlockLight() {
Vec3i vec = getBeltFacing().getNormal();
BeltSlope slope = getBlockState().getValue(BeltBlock.SLOPE);
int verticality = slope == BeltSlope.DOWNWARD ? -1 : slope == BeltSlope.UPWARD ? 1 : 0;
MutableBlockPos pos = new MutableBlockPos(controller.getX(), controller.getY(), controller.getZ());
for (int i = 0; i < beltLength * 2; i += 2) {
light[i] = (byte) level.getBrightness(LightLayer.BLOCK, pos);
MutableBlockPos pos = new MutableBlockPos(controller.getX(), controller.getY(), controller.getZ());
for (int i = 0; i < beltLength * 2; i += 2) {
light[i] = (byte) level.getBrightness(LightLayer.BLOCK, pos);
pos.move(vec.getX(), verticality, vec.getZ());
pos.move(vec.getX(), verticality, vec.getZ());
}
}
}
private void updateSkyLight() {
Vec3i vec = getBeltFacing().getNormal();
BeltSlope slope = getBlockState().getValue(BeltBlock.SLOPE);
int verticality = slope == BeltSlope.DOWNWARD ? -1 : slope == BeltSlope.UPWARD ? 1 : 0;
private void updateSkyLight() {
Vec3i vec = getBeltFacing().getNormal();
BeltSlope slope = getBlockState().getValue(BeltBlock.SLOPE);
int verticality = slope == BeltSlope.DOWNWARD ? -1 : slope == BeltSlope.UPWARD ? 1 : 0;
MutableBlockPos pos = new MutableBlockPos(controller.getX(), controller.getY(), controller.getZ());
for (int i = 1; i < beltLength * 2; i += 2) {
light[i] = (byte) level.getBrightness(LightLayer.SKY, pos);
MutableBlockPos pos = new MutableBlockPos(controller.getX(), controller.getY(), controller.getZ());
for (int i = 1; i < beltLength * 2; i += 2) {
light[i] = (byte) level.getBrightness(LightLayer.SKY, pos);
pos.move(vec.getX(), verticality, vec.getZ());
pos.move(vec.getX(), verticality, vec.getZ());
}
}
}
}