Fix level F3 loggers not being removed

This commit is contained in:
James Seibel
2023-05-18 07:56:17 -05:00
parent aee6c84dae
commit 428a0cd10b
4 changed files with 34 additions and 45 deletions
@@ -105,6 +105,8 @@ public class DhClientLevel extends AbstractDhClientLevel implements IDhClientLev
@Override
public void close()
{
this.f3Message.close();
this.baseClose();
LOGGER.info("Closed "+DhClientLevel.class.getSimpleName()+" for "+this.clientLevelWrapper);
}
@@ -216,6 +216,7 @@ public class DhClientServerLevel extends AbstractDhClientLevel implements IDhCli
public void close()
{
super.baseClose();
this.f3Message.close();
WorldGenState worldGenState = this.worldGenStateRef.get();
if (worldGenState != null)
@@ -2,6 +2,7 @@ package com.seibel.lod.core.logging.f3;
import com.seibel.lod.coreapi.ModInfo;
import java.io.Closeable;
import java.lang.ref.WeakReference;
import java.util.Arrays;
import java.util.Iterator;
@@ -11,22 +12,19 @@ import java.util.function.Supplier;
public class F3Screen
{
public static boolean renderCustomF3 = true;
private static final String[] DEFAULT_STR = {
"",
private static final String[] DEFAULT_STRING = {
"", // blank line for spacing
ModInfo.READABLE_NAME + " version: " + ModInfo.VERSION
};
private static final LinkedList<WeakReference<Message>> selfUpdateMessages = new LinkedList<>();
private static final LinkedList<Message> SELF_UPDATE_MESSAGE_LIST = new LinkedList<>();
public static void addStringToDisplay(List<String> list)
{
list.addAll(Arrays.asList(DEFAULT_STR));
Iterator<WeakReference<Message>> iterator = selfUpdateMessages.iterator();
list.addAll(Arrays.asList(DEFAULT_STRING));
Iterator<Message> iterator = SELF_UPDATE_MESSAGE_LIST.iterator();
while (iterator.hasNext())
{
WeakReference<Message> ref = iterator.next();
Message message = ref.get();
{
Message message = iterator.next();
if (message == null)
{
iterator.remove();
@@ -44,72 +42,59 @@ public class F3Screen
// helper classes //
//================//
public static abstract class Message
// we are using Closeable instead of AutoCloseable because the close method should never throw exceptions
// and because this class shouldn't be used in a try {} block.
public static abstract class Message implements Closeable
{
protected Message()
{
selfUpdateMessages.add(new WeakReference<>(this));
}
protected Message() { SELF_UPDATE_MESSAGE_LIST.add(this); }
public abstract void printTo(List<String> output);
@Override
public void close() { SELF_UPDATE_MESSAGE_LIST.remove(this); }
}
@SuppressWarnings("unused")
public static class StaticMessage extends Message
{
private final String[] lines;
public StaticMessage(String... lines)
{
this.lines = lines;
}
public StaticMessage(String... lines) { this.lines = lines; }
@Override
public void printTo(List<String> output)
{
output.addAll(Arrays.asList(lines));
}
public void printTo(List<String> output) { output.addAll(Arrays.asList(this.lines)); }
}
@SuppressWarnings("unused")
public static class DynamicMessage extends Message
{
private final Supplier<String> supplier;
public DynamicMessage(Supplier<String> message)
{
this.supplier = message;
}
public DynamicMessage(Supplier<String> message) { this.supplier = message; }
public void printTo(List<String> list)
{
String msg = supplier.get();
if (msg != null)
String message = this.supplier.get();
if (message != null)
{
list.add(msg);
list.add(message);
}
}
}
@SuppressWarnings("unused")
public static class MultiDynamicMessage extends Message
{
private final Supplier<String>[] supplier;
private final Supplier<String>[] supplierList;
@SafeVarargs
public MultiDynamicMessage(Supplier<String>... messages)
{
this.supplier = messages;
}
public MultiDynamicMessage(Supplier<String>... suppliers) { this.supplierList = suppliers; }
public void printTo(List<String> list)
{
for (Supplier<String> s : supplier)
for (Supplier<String> supplier : this.supplierList)
{
String msg = s.get();
if (msg != null)
String message = supplier.get();
if (message != null)
{
list.add(msg);
list.add(message);
}
}
}
@@ -126,10 +111,10 @@ public class F3Screen
public void printTo(List<String> list)
{
String[] msg = supplier.get();
if (msg != null)
String[] message = this.supplier.get();
if (message != null)
{
list.addAll(Arrays.asList(msg));
list.addAll(Arrays.asList(message));
}
}
}
@@ -130,6 +130,7 @@ public class DhClientServerWorld extends AbstractDhWorld implements IDhClientWor
{
// at this point the levels are probably unloaded, so this save call usually generally won't do anything
this.saveAndFlush().join();
this.f3Message.close();
for (DhClientServerLevel level : this.dhLevels)
{