Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cascade chunkloading #8

Merged
merged 4 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading