From 58e8bf9fac7224b4fa7acc3d8bcf457ec17b9287 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Mon, 31 May 2021 15:10:32 +0200 Subject: [PATCH] Improved error handling Signed-off-by: Lixfel --- .../de/steamwar/fightsystem/FightSystem.java | 6 ++ .../fightsystem/record/GlobalRecorder.java | 56 ++++++++----------- .../steamwar/fightsystem/record/Recorder.java | 4 +- 3 files changed, 30 insertions(+), 36 deletions(-) diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/FightSystem.java b/FightSystem_Main/src/de/steamwar/fightsystem/FightSystem.java index b0ff4bf..0db7f9c 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/FightSystem.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/FightSystem.java @@ -30,6 +30,7 @@ import de.steamwar.fightsystem.fight.IFight; import de.steamwar.fightsystem.listener.Shutdown; import de.steamwar.fightsystem.listener.*; import de.steamwar.fightsystem.record.FileRecorder; +import de.steamwar.fightsystem.record.GlobalRecorder; import de.steamwar.fightsystem.record.LiveRecorder; import de.steamwar.fightsystem.record.PacketProcessor; import de.steamwar.fightsystem.states.FightState; @@ -158,6 +159,11 @@ public class FightSystem extends JavaPlugin { } } + @Override + public void onDisable() { + GlobalRecorder.getInstance().close(); + } + public static void setPreLeaderState() { FightState.setFightState(FightState.PRE_LEADER_SETUP); diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/record/GlobalRecorder.java b/FightSystem_Main/src/de/steamwar/fightsystem/record/GlobalRecorder.java index 2a3664d..b0e9824 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/record/GlobalRecorder.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/record/GlobalRecorder.java @@ -19,12 +19,14 @@ package de.steamwar.fightsystem.record; +import org.bukkit.Bukkit; + import java.io.DataOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; -import java.util.Iterator; import java.util.List; +import java.util.logging.Level; public class GlobalRecorder extends OutputStream implements Recorder { @@ -34,19 +36,19 @@ public class GlobalRecorder extends OutputStream implements Recorder { return recorder; } - private final List streams = new ArrayList<>(); + private final List recorders = new ArrayList<>(); private final DataOutputStream outputStream = new DataOutputStream(this); - public void add(OutputStream stream){ - streams.add(stream); + public void add(Recorder recorder){ + recorders.add(recorder); } - public void remove(OutputStream stream){ - streams.remove(stream); + public void remove(Recorder recorder){ + recorders.remove(recorder); } public boolean recording(){ - return !streams.isEmpty(); + return !recorders.isEmpty(); } @Override @@ -55,49 +57,35 @@ public class GlobalRecorder extends OutputStream implements Recorder { } @Override - public void write(int i) throws IOException { + public void write(int i) { foreach(stream -> stream.write(i)); } @Override - public void write(byte[] bytes, int i, int i1) throws IOException { + public void write(byte[] bytes, int i, int i1) { foreach(stream -> stream.write(bytes, i, i1)); } @Override - public void flush() throws IOException { + public void flush() { foreach(OutputStream::flush); } @Override - public void close() throws IOException { + public void close() { foreach(OutputStream::close); } - private void foreach(IOThrower thrower) throws IOException { - //Custom iterator to allow removal of current element during iteration in underlying list - Iterator it = new Iterator() { - - private int pos = 0; - private int size = streams.size(); - - @Override - public boolean hasNext() { - return pos < streams.size(); + private void foreach(IOThrower thrower) { + for(int i = 0; i < recorders.size(); i++) { + Recorder stream = recorders.get(i); + try{ + thrower.run(stream.getStream()); + }catch (IOException e){ + Bukkit.getLogger().log(Level.SEVERE, "Could not operate on OutputStream", e); + stream.disable(); + i--; // Recorder was removed from recorders } - - @Override - public OutputStream next() { - if(size > streams.size()){ - pos--; - size--; - } - return streams.get(pos++); - } - }; - - while(it.hasNext()) { - thrower.run(it.next()); } } diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/record/Recorder.java b/FightSystem_Main/src/de/steamwar/fightsystem/record/Recorder.java index e9ced16..225a82e 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/record/Recorder.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/record/Recorder.java @@ -41,7 +41,7 @@ public interface Recorder { DataOutputStream getStream(); default void enable() { - GlobalRecorder.getInstance().add(getStream()); + GlobalRecorder.getInstance().add(this); if(ArenaMode.Event.contains(Config.mode)){ teamIds(Config.EventTeamBlueID, Config.EventTeamRedID); @@ -69,7 +69,7 @@ public interface Recorder { } default void disable() { - GlobalRecorder.getInstance().remove(getStream()); + GlobalRecorder.getInstance().remove(this); try { getStream().close(); } catch (IOException e) {