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,8 +539,23 @@ public class BeltTileEntity extends KineticTileEntity implements LightListener {
return state != null && state.hasProperty(BeltBlock.PART) && state.getValue(BeltBlock.PART) == BeltPart.START;
}
/**
* Hide this behavior in an inner class to avoid loading LightListener on servers.
*/
@OnlyIn(Dist.CLIENT)
class BeltLighter implements LightListener {
// client
public byte[] light;
public BeltLighter() {
initializeLight();
LightUpdater.get(level)
.addListener(this);
}
@Override
public GridAlignedBB getVolume() {
BlockPos endPos = BeltHelper.getPositionForOffset(this, beltLength - 1);
BlockPos endPos = BeltHelper.getPositionForOffset(BeltTileEntity.this, beltLength - 1);
GridAlignedBB bb = GridAlignedBB.from(worldPosition, endPos);
bb.fixMinMax();
return bb;
@ -549,9 +568,9 @@ public class BeltTileEntity extends KineticTileEntity implements LightListener {
@Override
public void onLightUpdate(LightProvider world, LightLayer type, ImmutableBox changed) {
if (this.remove)
if (remove)
return;
if (this.level == null)
if (level == null)
return;
GridAlignedBB beltVolume = getVolume();
@ -571,7 +590,6 @@ public class BeltTileEntity extends KineticTileEntity implements LightListener {
}
private void initializeLight() {
if (beltLength > 0) {
light = new byte[beltLength * 2];
Vec3i vec = getBeltFacing().getNormal();
@ -585,7 +603,6 @@ public class BeltTileEntity extends KineticTileEntity implements LightListener {
pos.move(vec.getX(), verticality, vec.getZ());
}
}
}
private void updateBlockLight() {
Vec3i vec = getBeltFacing().getNormal();
@ -612,4 +629,5 @@ public class BeltTileEntity extends KineticTileEntity implements LightListener {
pos.move(vec.getX(), verticality, vec.getZ());
}
}
}
}