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 #1918 - RBMKs turning off when chunks unload, a world is quit, or a dimension unloads #1958

Merged
merged 3 commits into from
Feb 27, 2025
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
74 changes: 37 additions & 37 deletions src/main/java/com/hbm/blocks/machine/rbmk/RBMKBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,31 +58,31 @@ public int[] getDimensions() {
public int getOffset() {
return 0;
}

public boolean openInv(World world, int x, int y, int z, EntityPlayer player) {

if(world.isRemote) {
return true;
}

int[] pos = this.findCore(world, x, y, z);

if(pos == null)
return false;

TileEntity te = world.getTileEntity(pos[0], pos[1], pos[2]);

if(!(te instanceof TileEntityRBMKBase))
return false;

TileEntityRBMKBase rbmk = (TileEntityRBMKBase) te;

if(player.getHeldItem() != null && player.getHeldItem().getItem() instanceof ItemRBMKLid) {

if(!rbmk.hasLid())
return false;
}

if(!player.isSneaking()) {
FMLNetworkHandler.openGui(player, MainRegistry.instance, 0, world, pos[0], pos[1], pos[2]);
return true;
Expand All @@ -93,27 +93,27 @@ public boolean openInv(World world, int x, int y, int z, EntityPlayer player) {

@Override
public AxisAlignedBB getCollisionBoundingBoxFromPool(World world, int x, int y, int z) {

float height = 0.0F;

int[] pos = this.findCore(world, x, y, z);

if(pos != null) {
TileEntity te = world.getTileEntity(pos[0], pos[1], pos[2]);

if(te instanceof TileEntityRBMKBase) {

TileEntityRBMKBase rbmk = (TileEntityRBMKBase) te;

if(rbmk.hasLid()) {
height += 0.25F;
}
}
}

return AxisAlignedBB.getBoundingBox(x + this.minX, y + this.minY, z + this.minZ, x + this.maxX, y + this.maxY + height, z + this.maxZ);
}

/*
* NORTH: no cover
* EAST: concrete cover
Expand All @@ -130,70 +130,70 @@ public void fillSpace(World world, int x, int y, int z, ForgeDirection dir, int
MultiblockHandlerXR.fillSpace(world, x + dir.offsetX * o, y + dir.offsetY * o, z + dir.offsetZ * o, getDimensions(world), this, dir);
this.makeExtra(world, x, y + RBMKDials.getColumnHeight(world), z);
}

@Override
protected ForgeDirection getDirModified(ForgeDirection dir) {
return DIR_NO_LID;
}

public int[] getDimensions(World world) {
return new int[] {RBMKDials.getColumnHeight(world), 0, 0, 0, 0, 0};
}

@Override
public void breakBlock(World world, int x, int y, int z, Block b, int i) {

if(!world.isRemote && dropLids) {

if(i == DIR_NORMAL_LID.ordinal() + offset) {
world.spawnEntityInWorld(new EntityItem(world, x + 0.5, y + 0.5 + RBMKDials.getColumnHeight(world), z + 0.5, new ItemStack(ModItems.rbmk_lid)));
}
if(i == DIR_GLASS_LID.ordinal() + offset) {
world.spawnEntityInWorld(new EntityItem(world, x + 0.5, y + 0.5 + RBMKDials.getColumnHeight(world), z + 0.5, new ItemStack(ModItems.rbmk_lid_glass)));
}
}

super.breakBlock(world, x, y, z, b, i);
}

@Override
public boolean onScrew(World world, EntityPlayer player, int x, int y, int z, int side, float fX, float fY, float fZ, ToolType tool) {

if(tool != ToolType.SCREWDRIVER)
return false;

int[] pos = this.findCore(world, x, y, z);

if(pos != null) {
TileEntity te = world.getTileEntity(pos[0], pos[1], pos[2]);

if(te instanceof TileEntityRBMKBase) {

TileEntityRBMKBase rbmk = (TileEntityRBMKBase) te;
int i = rbmk.getBlockMetadata();

if(rbmk.hasLid() && rbmk.isLidRemovable()) {

RBMKNeutronNode node = (RBMKNeutronNode) NeutronNodeWorld.getNode(new BlockPos(te));
if (node != null)
RBMKNeutronNode node = (RBMKNeutronNode) NeutronNodeWorld.getNode(world, new BlockPos(te));
if(node != null)
node.removeLid();

if(!world.isRemote) {
if(i == DIR_NORMAL_LID.ordinal() + offset) {
world.spawnEntityInWorld(new EntityItem(world, pos[0] + 0.5, pos[1] + 0.5 + RBMKDials.getColumnHeight(world), pos[2] + 0.5, new ItemStack(ModItems.rbmk_lid)));
}
if(i == DIR_GLASS_LID.ordinal() + offset) {
world.spawnEntityInWorld(new EntityItem(world, pos[0] + 0.5, pos[1] + 0.5 + RBMKDials.getColumnHeight(world), pos[2] + 0.5, new ItemStack(ModItems.rbmk_lid_glass)));
}
world.setBlockMetadataWithNotify(pos[0], pos[1], pos[2], DIR_NO_LID.ordinal() + this.offset, 3);

world.setBlockMetadataWithNotify(pos[0], pos[1], pos[2], DIR_NO_LID.ordinal() + offset, 3);
}

return true;
}
}
}

return false;
}

Expand Down
50 changes: 10 additions & 40 deletions src/main/java/com/hbm/handler/neutron/NeutronHandler.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package com.hbm.handler.neutron;

import com.hbm.tileentity.machine.rbmk.RBMKDials;
import com.hbm.util.fauxpointtwelve.BlockPos;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.TickEvent;
import net.minecraft.world.World;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


Expand All @@ -21,20 +18,16 @@ public void onServerTick(TickEvent.ServerTickEvent event) {
if(event.phase != TickEvent.Phase.START)
return;

// Remove `StreamWorld` objects if they have no streams.
{ // aflghdkljghlkbhfjkghgilurbhlkfjghkffdjgn
List<World> toRemove = new ArrayList<>();
NeutronNodeWorld.streamWorlds.forEach((world, streamWorld) -> {
if (streamWorld.streams.isEmpty())
toRemove.add(world);
});
// Freshen the node cache every `cacheTime` ticks to prevent huge RAM usage from idle nodes.
int cacheTime = 20;
boolean cacheClear = ticks >= cacheTime;
if(cacheClear) ticks = 0;
ticks++;

for (World world : toRemove) {
NeutronNodeWorld.streamWorlds.remove(world);
}
}
// Remove `StreamWorld` objects if they have no streams.
NeutronNodeWorld.removeEmptyWorlds();

for (Map.Entry<World, NeutronNodeWorld.StreamWorld> world : NeutronNodeWorld.streamWorlds.entrySet()) {
for(Map.Entry<World, NeutronNodeWorld.StreamWorld> world : NeutronNodeWorld.streamWorlds.entrySet()) {

// Gamerule caching because this apparently is kinda slow?
// meh, good enough
Expand All @@ -48,33 +41,10 @@ public void onServerTick(TickEvent.ServerTickEvent event) {
RBMKNeutronHandler.columnHeight = RBMKDials.getColumnHeight(world.getKey()) + 1;
RBMKNeutronHandler.fluxRange = RBMKDials.getFluxRange(world.getKey());

for (NeutronStream stream : world.getValue().streams) {
stream.runStreamInteraction(world.getKey());
}
world.getValue().runStreamInteractions(world.getKey());
world.getValue().removeAllStreams();
}

// Freshen the node cache every `cacheTime` ticks to prevent huge RAM usage from idle nodes.
int cacheTime = 20;
if (ticks >= cacheTime) {
ticks = 0;
List<BlockPos> toRemove = new ArrayList<>();
for (NeutronNode cachedNode : NeutronNodeWorld.nodeCache.values()) {
if (cachedNode.type == NeutronStream.NeutronType.RBMK) {
RBMKNeutronHandler.RBMKNeutronNode node = (RBMKNeutronHandler.RBMKNeutronNode) cachedNode;
toRemove.addAll(node.checkNode());
}
/* TODO: actually do this and uncache pile nodes
if (cachedNode.type == NeutronStream.NeutronType.PILE) {
PileNeutronNode node = (PileNeutronNode) cachedNode;
toRemove.addAll(node.checkNode());
}
*/
}

toRemove.forEach(NeutronNodeWorld::removeNode);

if(cacheClear) world.getValue().cleanNodes();
}
ticks++;
}
}
87 changes: 63 additions & 24 deletions src/main/java/com/hbm/handler/neutron/NeutronNodeWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,55 +8,94 @@
import java.util.List;

public class NeutronNodeWorld {
// HashMap of all neutron nodes and their positions.
protected static HashMap<BlockPos, NeutronNode> nodeCache = new HashMap<>();

public static void addNode(NeutronNode node) {
nodeCache.put(node.pos, node);
// List of all stream worlds.
public static HashMap<World, StreamWorld> streamWorlds = new HashMap<>();

public static NeutronNode getNode(World world, BlockPos pos) {
StreamWorld streamWorld = streamWorlds.get(world);
return streamWorld != null ? streamWorld.nodeCache.get(pos) : null;
}

public static void removeNode(BlockPos position) {
nodeCache.remove(position);
public static void addNode(World world, NeutronNode node) {
StreamWorld streamWorld = getOrAddWorld(world);
streamWorld.nodeCache.put(node.pos, node);
}

public static NeutronNode getNode(BlockPos position) {
return nodeCache.get(position);
public static void removeNode(World world, BlockPos pos) {
StreamWorld streamWorld = streamWorlds.get(world);
if(streamWorld == null) return;
streamWorld.removeNode(pos);
}

public static void removeAllNodes() {
nodeCache.clear();
public static StreamWorld getOrAddWorld(World world) {
StreamWorld streamWorld = streamWorlds.get(world);
if(streamWorld == null) {
streamWorld = new StreamWorld();
streamWorlds.put(world, streamWorld);
}
return streamWorld;
}

// List of all stream worlds.
public static HashMap<World, StreamWorld> streamWorlds = new HashMap<>();
public static void removeAllWorlds() {
streamWorlds.clear();
}

public static void removeEmptyWorlds() {
streamWorlds.values().removeIf((streamWorld) -> {
return streamWorld.streams.isEmpty();
});
}

public static class StreamWorld {

List<NeutronStream> streams;
private List<NeutronStream> streams;
private HashMap<BlockPos, NeutronNode> nodeCache = new HashMap<>();

public StreamWorld() {
streams = new ArrayList<>();
}

public void runStreamInteractions(World world) {
for(NeutronStream stream : streams) {
stream.runStreamInteraction(world);
}
}

public void addStream(NeutronStream stream) {
this.streams.add(stream);
streams.add(stream);
}

public void removeAllStreams() {
this.streams.clear();
streams.clear();
}

public void removeAllStreamsOfType(NeutronStream.NeutronType type) {
List<NeutronStream> toRemove = new ArrayList<>();
for (NeutronStream stream : streams) {
if (stream.type == type)
toRemove.add(stream);
public void cleanNodes() {
List<BlockPos> toRemove = new ArrayList<>();
for(NeutronNode cachedNode : nodeCache.values()) {
if(cachedNode.type == NeutronStream.NeutronType.RBMK) {
RBMKNeutronHandler.RBMKNeutronNode node = (RBMKNeutronHandler.RBMKNeutronNode) cachedNode;
toRemove.addAll(node.checkNode());
}
/* TODO: actually do this and uncache pile nodes
if(cachedNode.type == NeutronStream.NeutronType.PILE) {
PileNeutronNode node = (PileNeutronNode) cachedNode;
toRemove.addAll(node.checkNode());
}
*/
}

for(BlockPos pos : toRemove) {
nodeCache.remove(pos);
}
toRemove.forEach((stream) -> streams.remove(stream));
}
}

public static void removeAllWorlds() {
streamWorlds.clear();
public void removeNode(BlockPos pos) {
nodeCache.remove(pos);
}

public void removeAllStreamsOfType(NeutronStream.NeutronType type) {
streams.removeIf(stream -> stream.type == type);
}
}
}
10 changes: 2 additions & 8 deletions src/main/java/com/hbm/handler/neutron/NeutronStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.hbm.util.fauxpointtwelve.BlockPos;
import net.minecraft.util.Vec3;
import net.minecraft.world.World;
import com.hbm.handler.neutron.NeutronNodeWorld.StreamWorld;

import java.util.Iterator;

Expand Down Expand Up @@ -43,13 +42,8 @@ public NeutronStream(NeutronNode origin, Vec3 vector, double flux, double ratio,
this.fluxQuantity = flux;
this.fluxRatio = ratio;
this.type = type;
World worldObj = origin.tile.getWorldObj();
if (NeutronNodeWorld.streamWorlds.get(worldObj) == null) {
StreamWorld world = new StreamWorld();
world.addStream(this);
NeutronNodeWorld.streamWorlds.put(worldObj, world);
} else
NeutronNodeWorld.streamWorlds.get(worldObj).addStream(this);

NeutronNodeWorld.getOrAddWorld(origin.tile.getWorldObj()).addStream(this);
}

protected BlockPos posInstance;
Expand Down
Loading