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 # dependency versions
registrate_version = MC1.18-1.0.21 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_minecraft_version = 1.18
jei_version = 9.0.0.40 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) { protected int getPackedLight(BeltTileEntity controller, float beltPos) {
int segment = (int) Math.floor(beltPos) * 2; 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 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.AABB;
import net.minecraft.world.phys.Vec3; import net.minecraft.world.phys.Vec3;
import net.minecraftforge.api.distmarker.Dist; 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.IModelData;
import net.minecraftforge.client.model.data.ModelDataMap; import net.minecraftforge.client.model.data.ModelDataMap;
import net.minecraftforge.client.model.data.ModelProperty; 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.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler; import net.minecraftforge.items.IItemHandler;
public class BeltTileEntity extends KineticTileEntity implements LightListener { public class BeltTileEntity extends KineticTileEntity {
public Map<Entity, TransportedEntityInfo> passengers; public Map<Entity, TransportedEntityInfo> passengers;
public Optional<DyeColor> color; public Optional<DyeColor> color;
@ -78,8 +79,8 @@ public class BeltTileEntity extends KineticTileEntity implements LightListener {
public CompoundTag trackerUpdateTag; public CompoundTag trackerUpdateTag;
// client @OnlyIn(Dist.CLIENT)
public byte[] light; public BeltLighter lighter;
public static enum CasingType { public static enum CasingType {
NONE, ANDESITE, BRASS; NONE, ANDESITE, BRASS;
@ -119,11 +120,11 @@ public class BeltTileEntity extends KineticTileEntity implements LightListener {
if (!isController()) if (!isController())
return; return;
if (light == null && level.isClientSide) { DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> {
initializeLight(); if (beltLength > 0 && lighter == null) {
LightUpdater.get(level) lighter = new BeltLighter();
.addListener(this);
} }
});
getInventory().tick(); getInventory().tick();
@ -230,8 +231,11 @@ public class BeltTileEntity extends KineticTileEntity implements LightListener {
trackerUpdateTag = compound; trackerUpdateTag = compound;
index = compound.getInt("Index"); index = compound.getInt("Index");
beltLength = compound.getInt("Length"); beltLength = compound.getInt("Length");
if (prevBeltLength != beltLength) if (prevBeltLength != beltLength) {
light = null; DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> {
lighter = null;
});
}
} }
if (isController()) 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; 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() { public GridAlignedBB getVolume() {
BlockPos endPos = BeltHelper.getPositionForOffset(this, beltLength - 1); BlockPos endPos = BeltHelper.getPositionForOffset(BeltTileEntity.this, beltLength - 1);
GridAlignedBB bb = GridAlignedBB.from(worldPosition, endPos); GridAlignedBB bb = GridAlignedBB.from(worldPosition, endPos);
bb.fixMinMax(); bb.fixMinMax();
return bb; return bb;
@ -549,9 +568,9 @@ public class BeltTileEntity extends KineticTileEntity implements LightListener {
@Override @Override
public void onLightUpdate(LightProvider world, LightLayer type, ImmutableBox changed) { public void onLightUpdate(LightProvider world, LightLayer type, ImmutableBox changed) {
if (this.remove) if (remove)
return; return;
if (this.level == null) if (level == null)
return; return;
GridAlignedBB beltVolume = getVolume(); GridAlignedBB beltVolume = getVolume();
@ -571,7 +590,6 @@ public class BeltTileEntity extends KineticTileEntity implements LightListener {
} }
private void initializeLight() { private void initializeLight() {
if (beltLength > 0) {
light = new byte[beltLength * 2]; light = new byte[beltLength * 2];
Vec3i vec = getBeltFacing().getNormal(); Vec3i vec = getBeltFacing().getNormal();
@ -585,7 +603,6 @@ public class BeltTileEntity extends KineticTileEntity implements LightListener {
pos.move(vec.getX(), verticality, vec.getZ()); pos.move(vec.getX(), verticality, vec.getZ());
} }
} }
}
private void updateBlockLight() { private void updateBlockLight() {
Vec3i vec = getBeltFacing().getNormal(); Vec3i vec = getBeltFacing().getNormal();
@ -612,4 +629,5 @@ public class BeltTileEntity extends KineticTileEntity implements LightListener {
pos.move(vec.getX(), verticality, vec.getZ()); pos.move(vec.getX(), verticality, vec.getZ());
} }
} }
}
} }