Skip to content

Commit

Permalink
Fix cascade chunkloading (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pilzinsel64 authored Dec 21, 2024
1 parent f1f7fc9 commit 3e864bc
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import cofh.api.energy.IEnergyContainerItem;
import de.keridos.floodlights.compatability.ModCompatibility;
import de.keridos.floodlights.handler.ConfigHandler;
import de.keridos.floodlights.init.ModBlocks;
import de.keridos.floodlights.reference.Names;
import de.keridos.floodlights.util.BlockPos;
import de.keridos.floodlights.util.GeneralUtil;
Expand All @@ -39,17 +38,7 @@ public void growSource(boolean remove) {
int x = this.xCoord + rotatedCoords[0];
int y = this.yCoord + rotatedCoords[1];
int z = this.zCoord + rotatedCoords[2];
if (remove) {
if (worldObj.getBlock(x, y, z) == ModBlocks.blockPhantomLight) {
TileEntityPhantomLight light = (TileEntityPhantomLight) worldObj.getTileEntity(x, y, z);
light.removeSource(this.xCoord, this.yCoord, this.zCoord);
}
} else if (worldObj.getBlock(x, y, z).isAir(worldObj, x, y, z)) {
setLight(x, y, z);
} else if (worldObj.getBlock(x, y, z) == ModBlocks.blockPhantomLight) {
TileEntityPhantomLight light = (TileEntityPhantomLight) worldObj.getTileEntity(x, y, z);
light.addSource(this.xCoord, this.yCoord, this.zCoord);
}
setLightChecked(x, y, z, remove);
}

public void updateEntity() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;

import de.keridos.floodlights.handler.ConfigHandler;
import de.keridos.floodlights.init.ModBlocks;
Expand Down Expand Up @@ -56,19 +55,18 @@ public void toggleUpdateRun() {
}

public void setLight(int x, int y, int z) {
if (worldObj.getBlock(x, y, z) == ModBlocks.blockUVLightBlock) {
// Ensure we don't get or change a block which chunk hasn't been loaded yet to prevent cascade chunkloading
// and support `WorldServer.loadChunkOnRequest = false`.
if (!worldObj.blockExists(x, y, z) || worldObj.getBlock(x, y, z) == ModBlocks.blockUVLightBlock) {
return;
}
if (worldObj.setBlock(x, y, z, ModBlocks.blockPhantomLight)) {
TileEntity tile = worldObj.getTileEntity(x, y, z);
if (tile instanceof TileEntityPhantomLight) {
TileEntityPhantomLight light = (TileEntityPhantomLight) tile;
light.addSource(this.xCoord, this.yCoord, this.zCoord);
worldObj.markBlockRangeForRenderUpdate(x, y, z, x, y, z);
}
return;
TileEntityPhantomLight light = (TileEntityPhantomLight) worldObj.getTileEntity(x, y, z);
light.addSource(this.xCoord, this.yCoord, this.zCoord);
worldObj.markBlockRangeForRenderUpdate(x, y, z, x, y, z);
} else {
this.toggleUpdateRun();
}
this.toggleUpdateRun();
}

@Override
Expand Down Expand Up @@ -204,17 +202,7 @@ public void straightSource(boolean remove) {
int x = this.xCoord + this.orientation.offsetX * i;
int y = this.yCoord + this.orientation.offsetY * i;
int z = this.zCoord + this.orientation.offsetZ * i;
if (remove) {
if (worldObj.getBlock(x, y, z) == ModBlocks.blockPhantomLight) {
TileEntityPhantomLight light = (TileEntityPhantomLight) worldObj.getTileEntity(x, y, z);
light.removeSource(this.xCoord, this.yCoord, this.zCoord);
}
} else if (worldObj.getBlock(x, y, z).isAir(worldObj, x, y, z)) {
setLight(x, y, z);
} else if (worldObj.getBlock(x, y, z) == ModBlocks.blockPhantomLight) {
TileEntityPhantomLight light = (TileEntityPhantomLight) worldObj.getTileEntity(x, y, z);
light.addSource(this.xCoord, this.yCoord, this.zCoord);
} else if (worldObj.getBlock(x, y, z).isOpaqueCube()) {
if (!setLightChecked(x, y, z, remove)) {
break;
}
}
Expand Down Expand Up @@ -267,17 +255,7 @@ public void wideConeSource(boolean remove) {
int x = this.xCoord + rotatedCoords[0];
int y = this.yCoord + rotatedCoords[1];
int z = this.zCoord + rotatedCoords[2];
if (remove) {
if (worldObj.getBlock(x, y, z) == ModBlocks.blockPhantomLight) {
TileEntityPhantomLight light = (TileEntityPhantomLight) worldObj.getTileEntity(x, y, z);
light.removeSource(this.xCoord, this.yCoord, this.zCoord);
}
} else if (worldObj.getBlock(x, y, z).isAir(worldObj, x, y, z)) {
setLight(x, y, z);
} else if (worldObj.getBlock(x, y, z) == ModBlocks.blockPhantomLight) {
TileEntityPhantomLight light = (TileEntityPhantomLight) worldObj.getTileEntity(x, y, z);
light.addSource(this.xCoord, this.yCoord, this.zCoord);
} else if (worldObj.getBlock(x, y, z).isOpaqueCube()) {
if (!setLightChecked(x, y, z, remove)) {
if (i < 4) { // This is for canceling the long rangs beams
failedBeams[j] = true;
}
Expand Down Expand Up @@ -322,17 +300,7 @@ public void wideConeSource(boolean remove) {
int x = this.xCoord + rotatedCoords[0];
int y = this.yCoord + rotatedCoords[1];
int z = this.zCoord + rotatedCoords[2];
if (remove) {
if (worldObj.getBlock(x, y, z) == ModBlocks.blockPhantomLight) {
TileEntityPhantomLight light = (TileEntityPhantomLight) worldObj.getTileEntity(x, y, z);
light.removeSource(this.xCoord, this.yCoord, this.zCoord);
}
} else if (worldObj.getBlock(x, y, z).isAir(worldObj, x, y, z)) {
setLight(x, y, z);
} else if (worldObj.getBlock(x, y, z) == ModBlocks.blockPhantomLight) {
TileEntityPhantomLight light = (TileEntityPhantomLight) worldObj.getTileEntity(x, y, z);
light.addSource(this.xCoord, this.yCoord, this.zCoord);
} else if (worldObj.getBlock(x, y, z).isOpaqueCube()) {
if (!setLightChecked(x, y, z, remove)) {
break;
}
}
Expand All @@ -350,17 +318,7 @@ public void narrowConeSource(boolean remove) {
int x = this.xCoord + this.orientation.offsetX;
int y = this.yCoord + this.orientation.offsetY;
int z = this.zCoord + this.orientation.offsetZ;
if (remove) {
if (worldObj.getBlock(x, y, z) == ModBlocks.blockPhantomLight) {
TileEntityPhantomLight light = (TileEntityPhantomLight) worldObj.getTileEntity(x, y, z);
light.removeSource(this.xCoord, this.yCoord, this.zCoord);
}
} else if (worldObj.getBlock(x, y, z).isAir(worldObj, x, y, z)) {
setLight(x, y, z);
} else if (worldObj.getBlock(x, y, z) == ModBlocks.blockPhantomLight) {
TileEntityPhantomLight light = (TileEntityPhantomLight) worldObj.getTileEntity(x, y, z);
light.addSource(this.xCoord, this.yCoord, this.zCoord);
} else if (worldObj.getBlock(x, y, z).isOpaqueCube()) {
if (!setLightChecked(x, y, z, remove)) {
return;
}
}
Expand Down Expand Up @@ -402,17 +360,7 @@ public void narrowConeSource(boolean remove) {
int x = this.xCoord + rotatedCoords[0];
int y = this.yCoord + rotatedCoords[1];
int z = this.zCoord + rotatedCoords[2];
if (remove) {
if (worldObj.getBlock(x, y, z) == ModBlocks.blockPhantomLight) {
TileEntityPhantomLight light = (TileEntityPhantomLight) worldObj.getTileEntity(x, y, z);
light.removeSource(this.xCoord, this.yCoord, this.zCoord);
}
} else if (worldObj.getBlock(x, y, z).isAir(worldObj, x, y, z)) {
setLight(x, y, z);
} else if (worldObj.getBlock(x, y, z) == ModBlocks.blockPhantomLight) {
TileEntityPhantomLight light = (TileEntityPhantomLight) worldObj.getTileEntity(x, y, z);
light.addSource(this.xCoord, this.yCoord, this.zCoord);
} else if (worldObj.getBlock(x, y, z).isOpaqueCube()) {
if (!setLightChecked(x, y, z, remove)) {
if (i < 8) { // This is for canceling the long rangs beams
failedBeams[j] = true;
}
Expand Down Expand Up @@ -458,21 +406,33 @@ public void narrowConeSource(boolean remove) {
int x = this.xCoord + rotatedCoords[0];
int y = this.yCoord + rotatedCoords[1];
int z = this.zCoord + rotatedCoords[2];
if (remove) {
if (worldObj.getBlock(x, y, z) == ModBlocks.blockPhantomLight) {
TileEntityPhantomLight light = (TileEntityPhantomLight) worldObj.getTileEntity(x, y, z);
light.removeSource(this.xCoord, this.yCoord, this.zCoord);
}
} else if (worldObj.getBlock(x, y, z).isAir(worldObj, x, y, z)) {
setLight(x, y, z);
} else if (worldObj.getBlock(x, y, z) == ModBlocks.blockPhantomLight) {
TileEntityPhantomLight light = (TileEntityPhantomLight) worldObj.getTileEntity(x, y, z);
light.addSource(this.xCoord, this.yCoord, this.zCoord);
} else if (worldObj.getBlock(x, y, z).isOpaqueCube()) {
if (!setLightChecked(x, y, z, remove)) {
break;
}
}
}
}
}

protected boolean setLightChecked(int x, int y, int z, boolean remove) {
if (!worldObj.blockExists(x, y, z)) {
return false;
}

if (remove) {
if (worldObj.getBlock(x, y, z) == ModBlocks.blockPhantomLight) {
TileEntityPhantomLight light = (TileEntityPhantomLight) worldObj.getTileEntity(x, y, z);
light.removeSource(this.xCoord, this.yCoord, this.zCoord);
}
} else if (worldObj.getBlock(x, y, z).isAir(worldObj, x, y, z)) {
setLight(x, y, z);
} else if (worldObj.getBlock(x, y, z) == ModBlocks.blockPhantomLight) {
TileEntityPhantomLight light = (TileEntityPhantomLight) worldObj.getTileEntity(x, y, z);
light.addSource(this.xCoord, this.yCoord, this.zCoord);
} else if (worldObj.getBlock(x, y, z).isOpaqueCube()) {
return false;
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ public void removeSource(int x, int y, int z) {
}

public void updateAllSources() {
for (int[] source : sources) {
// Create a local copy as we remove sources while iterating them.
// Not best-practive but better then changing public facing methods.
ArrayList<int[]> sourcesCopy = new ArrayList<>(sources);

for (int[] source : sourcesCopy) {
TileEntity te = worldObj.getTileEntity(source[0], source[1], source[2]);
if (te != null && te instanceof TileEntityMetaFloodlight) {
((TileEntityMetaFloodlight) te).toggleUpdateRun();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import cofh.api.energy.IEnergyContainerItem;
import de.keridos.floodlights.compatability.ModCompatibility;
import de.keridos.floodlights.handler.ConfigHandler;
import de.keridos.floodlights.init.ModBlocks;
import de.keridos.floodlights.reference.Names;
import de.keridos.floodlights.util.MathUtil;
import ic2.api.item.ElectricItem;
Expand Down Expand Up @@ -84,17 +83,7 @@ public void smallSource(boolean remove) {
int x = this.xCoord + rotatedCoords[0];
int y = this.yCoord + rotatedCoords[1];
int z = this.zCoord + rotatedCoords[2];
if (remove) {
if (worldObj.getBlock(x, y, z) == ModBlocks.blockPhantomLight) {
TileEntityPhantomLight light = (TileEntityPhantomLight) worldObj.getTileEntity(x, y, z);
light.removeSource(this.xCoord, this.yCoord, this.zCoord);
}
} else if (worldObj.getBlock(x, y, z).isAir(worldObj, x, y, z)) {
setLight(x, y, z);
} else if (worldObj.getBlock(x, y, z) == ModBlocks.blockPhantomLight) {
TileEntityPhantomLight light = (TileEntityPhantomLight) worldObj.getTileEntity(x, y, z);
light.addSource(this.xCoord, this.yCoord, this.zCoord);
}
setLightChecked(x, y, z, remove);
}
}

Expand Down

0 comments on commit 3e864bc

Please sign in to comment.