SteamWar/FightSystem
Archiviert
13
1

Improved error handling

Signed-off-by: Lixfel <agga-games@gmx.de>
Dieser Commit ist enthalten in:
Lixfel 2021-05-31 15:10:32 +02:00
Ursprung 6e784810a1
Commit 58e8bf9fac
3 geänderte Dateien mit 30 neuen und 36 gelöschten Zeilen

Datei anzeigen

@ -30,6 +30,7 @@ import de.steamwar.fightsystem.fight.IFight;
import de.steamwar.fightsystem.listener.Shutdown; import de.steamwar.fightsystem.listener.Shutdown;
import de.steamwar.fightsystem.listener.*; import de.steamwar.fightsystem.listener.*;
import de.steamwar.fightsystem.record.FileRecorder; import de.steamwar.fightsystem.record.FileRecorder;
import de.steamwar.fightsystem.record.GlobalRecorder;
import de.steamwar.fightsystem.record.LiveRecorder; import de.steamwar.fightsystem.record.LiveRecorder;
import de.steamwar.fightsystem.record.PacketProcessor; import de.steamwar.fightsystem.record.PacketProcessor;
import de.steamwar.fightsystem.states.FightState; 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() { public static void setPreLeaderState() {
FightState.setFightState(FightState.PRE_LEADER_SETUP); FightState.setFightState(FightState.PRE_LEADER_SETUP);

Datei anzeigen

@ -19,12 +19,14 @@
package de.steamwar.fightsystem.record; package de.steamwar.fightsystem.record;
import org.bukkit.Bukkit;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.logging.Level;
public class GlobalRecorder extends OutputStream implements Recorder { public class GlobalRecorder extends OutputStream implements Recorder {
@ -34,19 +36,19 @@ public class GlobalRecorder extends OutputStream implements Recorder {
return recorder; return recorder;
} }
private final List<OutputStream> streams = new ArrayList<>(); private final List<Recorder> recorders = new ArrayList<>();
private final DataOutputStream outputStream = new DataOutputStream(this); private final DataOutputStream outputStream = new DataOutputStream(this);
public void add(OutputStream stream){ public void add(Recorder recorder){
streams.add(stream); recorders.add(recorder);
} }
public void remove(OutputStream stream){ public void remove(Recorder recorder){
streams.remove(stream); recorders.remove(recorder);
} }
public boolean recording(){ public boolean recording(){
return !streams.isEmpty(); return !recorders.isEmpty();
} }
@Override @Override
@ -55,49 +57,35 @@ public class GlobalRecorder extends OutputStream implements Recorder {
} }
@Override @Override
public void write(int i) throws IOException { public void write(int i) {
foreach(stream -> stream.write(i)); foreach(stream -> stream.write(i));
} }
@Override @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)); foreach(stream -> stream.write(bytes, i, i1));
} }
@Override @Override
public void flush() throws IOException { public void flush() {
foreach(OutputStream::flush); foreach(OutputStream::flush);
} }
@Override @Override
public void close() throws IOException { public void close() {
foreach(OutputStream::close); foreach(OutputStream::close);
} }
private void foreach(IOThrower thrower) throws IOException { private void foreach(IOThrower thrower) {
//Custom iterator to allow removal of current element during iteration in underlying list for(int i = 0; i < recorders.size(); i++) {
Iterator<OutputStream> it = new Iterator<OutputStream>() { Recorder stream = recorders.get(i);
try{
private int pos = 0; thrower.run(stream.getStream());
private int size = streams.size(); }catch (IOException e){
Bukkit.getLogger().log(Level.SEVERE, "Could not operate on OutputStream", e);
@Override stream.disable();
public boolean hasNext() { i--; // Recorder was removed from recorders
return pos < streams.size();
} }
@Override
public OutputStream next() {
if(size > streams.size()){
pos--;
size--;
}
return streams.get(pos++);
}
};
while(it.hasNext()) {
thrower.run(it.next());
} }
} }

Datei anzeigen

@ -41,7 +41,7 @@ public interface Recorder {
DataOutputStream getStream(); DataOutputStream getStream();
default void enable() { default void enable() {
GlobalRecorder.getInstance().add(getStream()); GlobalRecorder.getInstance().add(this);
if(ArenaMode.Event.contains(Config.mode)){ if(ArenaMode.Event.contains(Config.mode)){
teamIds(Config.EventTeamBlueID, Config.EventTeamRedID); teamIds(Config.EventTeamBlueID, Config.EventTeamRedID);
@ -69,7 +69,7 @@ public interface Recorder {
} }
default void disable() { default void disable() {
GlobalRecorder.getInstance().remove(getStream()); GlobalRecorder.getInstance().remove(this);
try { try {
getStream().close(); getStream().close();
} catch (IOException e) { } catch (IOException e) {