From ce39b0a27627b07dd12f8018a6699a16f00c767c Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sat, 22 Aug 2020 10:45:40 +0200 Subject: [PATCH] Rework recorder Signed-off-by: Lixfel --- .../src/de/steamwar/fightsystem/Config.java | 4 + .../listener/EventRecordListener.java | 2 - .../fightsystem/record/RecordSystem.java | 64 +++------ .../steamwar/fightsystem/record/Recorder.java | 65 +++++++++ .../record/SpectateConnection.java | 136 +++++++++++------- 5 files changed, 173 insertions(+), 98 deletions(-) create mode 100644 FightSystem_Main/src/de/steamwar/fightsystem/record/Recorder.java diff --git a/FightSystem_API/src/de/steamwar/fightsystem/Config.java b/FightSystem_API/src/de/steamwar/fightsystem/Config.java index f5da638..68d512d 100644 --- a/FightSystem_API/src/de/steamwar/fightsystem/Config.java +++ b/FightSystem_API/src/de/steamwar/fightsystem/Config.java @@ -125,6 +125,10 @@ public class Config { //check parameter public static final int CheckSchemID; + //live recorder parameter + public static final String spectateIP = "127.0.0.1"; + public static final int spectatePort = 2222; + static{ File worldConfigFile = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "config.yml"); if(!worldConfigFile.exists()) { diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/listener/EventRecordListener.java b/FightSystem_Main/src/de/steamwar/fightsystem/listener/EventRecordListener.java index 41b5d11..d3949f2 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/listener/EventRecordListener.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/listener/EventRecordListener.java @@ -4,7 +4,6 @@ import de.steamwar.fightsystem.Config; import de.steamwar.fightsystem.fight.Fight; import de.steamwar.fightsystem.fight.FightPlayer; import de.steamwar.fightsystem.record.RecordSystem; -import de.steamwar.fightsystem.record.SpectateConnection; import de.steamwar.fightsystem.states.FightState; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -23,7 +22,6 @@ public class EventRecordListener extends BasicListener { @Override public void enable() { RecordSystem.init(); - SpectateConnection.init("127.0.0.1", 2222); super.enable(); } diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/record/RecordSystem.java b/FightSystem_Main/src/de/steamwar/fightsystem/record/RecordSystem.java index dd194f9..a863899 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/record/RecordSystem.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/record/RecordSystem.java @@ -8,11 +8,6 @@ import org.bukkit.Location; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; -import java.io.DataOutputStream; -import java.io.IOException; -import java.net.SocketException; -import java.util.logging.Level; - public class RecordSystem { public static void init(){ @@ -20,6 +15,7 @@ public class RecordSystem { return; Bukkit.getScheduler().runTaskTimer(FightSystem.getPlugin(), RecordSystem::checkWorldState, 1, 1); + new SpectateConnection(); } /* @@ -47,57 +43,29 @@ public class RecordSystem { public static void playerJoins(Player p){ SteamwarUser user = SteamwarUser.get(p.getUniqueId()); - DataOutputStream out = getOutput(); - try { - out.writeByte(0x00); - out.writeInt(p.getEntityId()); - out.writeInt(user.getId()); - entityMoves(p); - } catch (IOException ex) { - exceptionHandling(ex); - } + Recorder.rByte(0x00); + Recorder.rInt(p.getEntityId()); + Recorder.rInt(user.getId()); + entityMoves(p); } public static void entityMoves(Entity e){ Location location = e.getLocation(); - DataOutputStream out = getOutput(); - try { - out.writeByte(0x01); - out.writeInt(e.getEntityId()); - out.writeDouble(location.getX()); - out.writeDouble(location.getY()); - out.writeDouble(location.getZ()); - out.writeFloat(location.getPitch()); - out.writeFloat(location.getYaw()); - out.flush(); - } catch (IOException ex) { - exceptionHandling(ex); - } + Recorder.rByte(0x01); + Recorder.rInt(e.getEntityId()); + Recorder.rDouble(location.getX()); + Recorder.rDouble(location.getY()); + Recorder.rDouble(location.getZ()); + Recorder.rFloat(location.getPitch()); + Recorder.rFloat(location.getYaw()); + Recorder.flush(); } public static void entityDespawns(Entity e){ - DataOutputStream out = getOutput(); - try { - out.writeByte(0x02); - out.writeInt(e.getEntityId()); - out.flush(); - } catch (IOException ex) { - exceptionHandling(ex); - } - } - - private static DataOutputStream getOutput(){ - return SpectateConnection.get().getOutput(); - } - - private static void exceptionHandling(IOException e){ - if(e instanceof SocketException){ - Bukkit.getLogger().log(Level.SEVERE, "SocketException", e); - SpectateConnection.get().close(); - }else{ - throw new SecurityException("Unknown Exception", e); - } + Recorder.rByte(0x02); + Recorder.rInt(e.getEntityId()); + Recorder.flush(); } private static void checkWorldState(){ diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/record/Recorder.java b/FightSystem_Main/src/de/steamwar/fightsystem/record/Recorder.java new file mode 100644 index 0000000..998e143 --- /dev/null +++ b/FightSystem_Main/src/de/steamwar/fightsystem/record/Recorder.java @@ -0,0 +1,65 @@ +package de.steamwar.fightsystem.record; + +import java.util.ArrayList; +import java.util.List; + +public abstract class Recorder { + + private static List recorders = new ArrayList<>(); + + public static void rByte(int b){ + recorders.forEach((recorder) -> recorder.writeByte(b)); + } + + public static void rShort(short s){ + recorders.forEach((recorder) -> recorder.writeShort(s)); + } + + public static void rInt(int i){ + recorders.forEach((recorder) -> recorder.writeInt(i)); + } + + public static void rLong(long l){ + recorders.forEach((recorder) -> recorder.writeLong(l)); + } + + public static void rFloat(float f){ + recorders.forEach((recorder) -> recorder.writeFloat(f)); + } + + public static void rDouble(double d){ + recorders.forEach((recorder) -> recorder.writeDouble(d)); + } + + public static void rString(String s){ + recorders.forEach((recorder) -> recorder.writeString(s)); + } + + public static void flush(){ + recorders.forEach(Recorder::doFlush); + } + + public static void closeAll(){ + recorders.forEach(Recorder::close); + } + + protected Recorder(){ + recorders.add(this); + } + + protected void close(){ + closeRecorder(); + recorders.remove(this); + } + + protected abstract void writeByte(int b); + protected abstract void writeShort(short s); + protected abstract void writeInt(int i); + protected abstract void writeLong(long l); + protected abstract void writeFloat(float f); + protected abstract void writeDouble(double d); + protected abstract void writeString(String s); + protected abstract void doFlush(); + + protected abstract void closeRecorder(); +} diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/record/SpectateConnection.java b/FightSystem_Main/src/de/steamwar/fightsystem/record/SpectateConnection.java index 2f85f89..7f50851 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/record/SpectateConnection.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/record/SpectateConnection.java @@ -1,76 +1,116 @@ package de.steamwar.fightsystem.record; +import de.steamwar.fightsystem.Config; import org.bukkit.Bukkit; -import java.io.*; +import java.io.BufferedOutputStream; +import java.io.DataOutputStream; +import java.io.IOException; import java.net.Socket; import java.util.logging.Level; -public class SpectateConnection { +public class SpectateConnection extends Recorder{ - private static SpectateConnection spectateConnection = new SpectateConnection(); + private Socket socket; + private DataOutputStream outputStream; - public static SpectateConnection get() { - return spectateConnection; - } - - public static void init(String ip, int port){ + SpectateConnection(){ + super(); try { - spectateConnection = new SpectateConnection(ip, port); + this.socket = new Socket(Config.spectateIP, Config.spectatePort); + this.outputStream = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream())); } catch (IOException e) { - Bukkit.getLogger().log(Level.SEVERE, "Could not init spectateconnection", e); + Bukkit.getLogger().log(Level.SEVERE, "Could not init connection", e); } } - private final Socket socket; - private final DataInputStream inputStream; - private final DataOutputStream outputStream; - - private SpectateConnection(String ip, int port) throws IOException{ - this.socket = new Socket(ip, port); - this.inputStream = new DataInputStream(socket.getInputStream()); - this.outputStream = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream())); + @Override + protected void writeByte(int b) { + try{ + outputStream.writeByte(b); + } catch (IOException e) { + close(); + Bukkit.getLogger().log(Level.SEVERE, "Could not send", e); + } } - private SpectateConnection(){ - socket = null; - this.inputStream = new DataInputStream(new NullInputStream()); - this.outputStream = new DataOutputStream(new NullOutputStream()); + @Override + protected void writeShort(short s) { + try{ + outputStream.writeShort(s); + } catch (IOException e) { + close(); + Bukkit.getLogger().log(Level.SEVERE, "Could not send", e); + } } - public InputStream getInput() { - return inputStream; + @Override + protected void writeInt(int i) { + try{ + outputStream.writeInt(i); + } catch (IOException e) { + close(); + Bukkit.getLogger().log(Level.SEVERE, "Could not send", e); + } } - public DataOutputStream getOutput() { - return outputStream; + @Override + protected void writeLong(long l) { + try{ + outputStream.writeLong(l); + } catch (IOException e) { + close(); + Bukkit.getLogger().log(Level.SEVERE, "Could not send", e); + } } - public void close() { + @Override + protected void writeFloat(float f) { + try{ + outputStream.writeFloat(f); + } catch (IOException e) { + close(); + Bukkit.getLogger().log(Level.SEVERE, "Could not send", e); + } + } + + @Override + protected void writeDouble(double d) { + try{ + outputStream.writeDouble(d); + } catch (IOException e) { + close(); + Bukkit.getLogger().log(Level.SEVERE, "Could not send", e); + } + } + + @Override + protected void writeString(String s) { + try{ + outputStream.writeChars(s); + } catch (IOException e) { + close(); + Bukkit.getLogger().log(Level.SEVERE, "Could not send", e); + } + } + + @Override + protected void doFlush() { + try{ + outputStream.flush(); + } catch (IOException e) { + close(); + Bukkit.getLogger().log(Level.SEVERE, "Could not flush", e); + } + } + + @Override + protected void closeRecorder() { try { - if(socket != null) - socket.close(); - inputStream.close(); + socket.close(); outputStream.close(); } catch (IOException e) { - Bukkit.getLogger().log(Level.SEVERE, "IOException on close", e); - } - spectateConnection = new SpectateConnection(); - } - - private static class NullOutputStream extends OutputStream { - - @Override - public void write(int b) throws IOException { - //ignored - } - } - - private static class NullInputStream extends InputStream { - - @Override - public int read() throws IOException { - throw new IOException("NullInputStreamReadAttempt"); + Bukkit.getLogger().log(Level.SEVERE, "IOException on socket close", e); } } }