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.*;
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);

Datei anzeigen

@ -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<OutputStream> streams = new ArrayList<>();
private final List<Recorder> 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<OutputStream> it = new Iterator<OutputStream>() {
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());
}
}

Datei anzeigen

@ -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) {