Fixed a force field registry crash (regression since ???)

This commit is contained in:
Unknown 2018-05-06 21:10:21 +02:00 committed by unknown
parent 7b89fff017
commit d4d4cd1144

View file

@ -7,7 +7,6 @@ import cr0s.warpdrive.config.WarpDriveConfig;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
@ -16,17 +15,18 @@ import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.WorldServer;
/**
* Registry of all known forcefield in the loaded worlds, grouped by frequency
* Registry of all known force field blocks in the loaded worlds, grouped by frequency
*
* @author LemADEC
*/
public class ForceFieldRegistry {
private static final HashMap<Integer, CopyOnWriteArraySet<GlobalPosition>> registry = new HashMap<>();
private static int countAdd = 0;
private static int countRemove = 0;
private static int countRead = 0;
public static Set<TileEntity> getTileEntities(final int beamFrequency, WorldServer worldSource, final int x, final int y, final int z) {
public static Set<TileEntity> getTileEntities(final int beamFrequency, final WorldServer worldSource, final int x, final int y, final int z) {
countRead++;
if (WarpDriveConfig.LOGGING_FORCEFIELD_REGISTRY) {
if (countRead % 1000 == 0) {
@ -123,7 +123,7 @@ public class ForceFieldRegistry {
return setEntries;
}
public static void updateInRegistry(IBeamFrequency tileEntity) {
public static void updateInRegistry(final IBeamFrequency tileEntity) {
assert(tileEntity instanceof TileEntity);
countRead++;
@ -131,7 +131,7 @@ public class ForceFieldRegistry {
if (setGlobalPositions == null) {
setGlobalPositions = new CopyOnWriteArraySet<>();
}
for (GlobalPosition globalPosition : setGlobalPositions) {
for (final GlobalPosition globalPosition : setGlobalPositions) {
if (globalPosition.equals(tileEntity)) {
// already registered
return;
@ -139,7 +139,7 @@ public class ForceFieldRegistry {
}
// not found => add
countAdd++;
setGlobalPositions.add(new GlobalPosition((TileEntity)tileEntity));
setGlobalPositions.add(new GlobalPosition((TileEntity) tileEntity));
registry.put(tileEntity.getBeamFrequency(), setGlobalPositions);
if (WarpDriveConfig.LOGGING_FORCEFIELD_REGISTRY) {
printRegistry("added");
@ -155,12 +155,11 @@ public class ForceFieldRegistry {
// noting to remove
return;
}
for (final Iterator<GlobalPosition> iterator = setGlobalPositions.iterator(); iterator.hasNext();) {
GlobalPosition globalPosition = iterator.next();
for (final GlobalPosition globalPosition : setGlobalPositions) {
if (globalPosition.equals(tileEntity)) {
// found it, remove and exit
countRemove++;
iterator.remove();
setGlobalPositions.remove(globalPosition);
return;
}
}
@ -168,17 +167,20 @@ public class ForceFieldRegistry {
}
public static void printRegistry(final String trigger) {
WarpDrive.logger.info("Forcefield registry (" + registry.size() + " entries after " + trigger + "):");
WarpDrive.logger.info("Force field registry (" + registry.size() + " entries after " + trigger + "):");
for (final Map.Entry<Integer, CopyOnWriteArraySet<GlobalPosition>> entry : registry.entrySet()) {
String message = "";
final StringBuilder message = new StringBuilder();
for (GlobalPosition globalPosition : entry.getValue()) {
if (!message.isEmpty()) {
message += ", ";
if (message.length() > 0) {
message.append(", ");
}
message += globalPosition.dimensionId + ": " + globalPosition.x + " " + globalPosition.y + " " + globalPosition.z;
message.append(globalPosition.dimensionId).append(": ").append(globalPosition.x).append(" ").append(globalPosition.y).append(" ").append(globalPosition.z);
}
WarpDrive.logger.info("- " + entry.getValue().size() + " entries at frequency " + entry.getKey() + ": " + message);
WarpDrive.logger.info(String.format("- %d entries at frequency %d : %s",
entry.getValue().size(),
entry.getKey(),
message));
}
}
}