SteamWar/FightSystem
Archiviert
13
1

Rework recorder

Signed-off-by: Lixfel <agga-games@gmx.de>
Dieser Commit ist enthalten in:
Lixfel 2020-08-22 10:45:40 +02:00
Ursprung 0f3943cd73
Commit ce39b0a276
5 geänderte Dateien mit 173 neuen und 98 gelöschten Zeilen

Datei anzeigen

@ -125,6 +125,10 @@ public class Config {
//check parameter //check parameter
public static final int CheckSchemID; public static final int CheckSchemID;
//live recorder parameter
public static final String spectateIP = "127.0.0.1";
public static final int spectatePort = 2222;
static{ static{
File worldConfigFile = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "config.yml"); File worldConfigFile = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "config.yml");
if(!worldConfigFile.exists()) { if(!worldConfigFile.exists()) {

Datei anzeigen

@ -4,7 +4,6 @@ import de.steamwar.fightsystem.Config;
import de.steamwar.fightsystem.fight.Fight; import de.steamwar.fightsystem.fight.Fight;
import de.steamwar.fightsystem.fight.FightPlayer; import de.steamwar.fightsystem.fight.FightPlayer;
import de.steamwar.fightsystem.record.RecordSystem; import de.steamwar.fightsystem.record.RecordSystem;
import de.steamwar.fightsystem.record.SpectateConnection;
import de.steamwar.fightsystem.states.FightState; import de.steamwar.fightsystem.states.FightState;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
@ -23,7 +22,6 @@ public class EventRecordListener extends BasicListener {
@Override @Override
public void enable() { public void enable() {
RecordSystem.init(); RecordSystem.init();
SpectateConnection.init("127.0.0.1", 2222);
super.enable(); super.enable();
} }

Datei anzeigen

@ -8,11 +8,6 @@ import org.bukkit.Location;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; 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 class RecordSystem {
public static void init(){ public static void init(){
@ -20,6 +15,7 @@ public class RecordSystem {
return; return;
Bukkit.getScheduler().runTaskTimer(FightSystem.getPlugin(), RecordSystem::checkWorldState, 1, 1); Bukkit.getScheduler().runTaskTimer(FightSystem.getPlugin(), RecordSystem::checkWorldState, 1, 1);
new SpectateConnection();
} }
/* /*
@ -47,57 +43,29 @@ public class RecordSystem {
public static void playerJoins(Player p){ public static void playerJoins(Player p){
SteamwarUser user = SteamwarUser.get(p.getUniqueId()); SteamwarUser user = SteamwarUser.get(p.getUniqueId());
DataOutputStream out = getOutput(); Recorder.rByte(0x00);
try { Recorder.rInt(p.getEntityId());
out.writeByte(0x00); Recorder.rInt(user.getId());
out.writeInt(p.getEntityId());
out.writeInt(user.getId());
entityMoves(p); entityMoves(p);
} catch (IOException ex) {
exceptionHandling(ex);
}
} }
public static void entityMoves(Entity e){ public static void entityMoves(Entity e){
Location location = e.getLocation(); Location location = e.getLocation();
DataOutputStream out = getOutput(); Recorder.rByte(0x01);
try { Recorder.rInt(e.getEntityId());
out.writeByte(0x01); Recorder.rDouble(location.getX());
out.writeInt(e.getEntityId()); Recorder.rDouble(location.getY());
out.writeDouble(location.getX()); Recorder.rDouble(location.getZ());
out.writeDouble(location.getY()); Recorder.rFloat(location.getPitch());
out.writeDouble(location.getZ()); Recorder.rFloat(location.getYaw());
out.writeFloat(location.getPitch()); Recorder.flush();
out.writeFloat(location.getYaw());
out.flush();
} catch (IOException ex) {
exceptionHandling(ex);
}
} }
public static void entityDespawns(Entity e){ public static void entityDespawns(Entity e){
DataOutputStream out = getOutput(); Recorder.rByte(0x02);
try { Recorder.rInt(e.getEntityId());
out.writeByte(0x02); Recorder.flush();
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);
}
} }
private static void checkWorldState(){ private static void checkWorldState(){

Datei anzeigen

@ -0,0 +1,65 @@
package de.steamwar.fightsystem.record;
import java.util.ArrayList;
import java.util.List;
public abstract class Recorder {
private static List<Recorder> 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();
}

Datei anzeigen

@ -1,76 +1,116 @@
package de.steamwar.fightsystem.record; package de.steamwar.fightsystem.record;
import de.steamwar.fightsystem.Config;
import org.bukkit.Bukkit; 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.net.Socket;
import java.util.logging.Level; 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() { SpectateConnection(){
return spectateConnection; super();
}
public static void init(String ip, int port){
try { try {
spectateConnection = new SpectateConnection(ip, port); this.socket = new Socket(Config.spectateIP, Config.spectatePort);
} catch (IOException e) {
Bukkit.getLogger().log(Level.SEVERE, "Could not init spectateconnection", 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())); this.outputStream = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
} catch (IOException e) {
Bukkit.getLogger().log(Level.SEVERE, "Could not init connection", e);
}
} }
private SpectateConnection(){ @Override
socket = null; protected void writeByte(int b) {
this.inputStream = new DataInputStream(new NullInputStream()); try{
this.outputStream = new DataOutputStream(new NullOutputStream()); outputStream.writeByte(b);
} } catch (IOException e) {
close();
public InputStream getInput() { Bukkit.getLogger().log(Level.SEVERE, "Could not send", e);
return inputStream; }
} }
public DataOutputStream getOutput() { @Override
return outputStream; protected void writeShort(short s) {
} try{
outputStream.writeShort(s);
public void close() { } catch (IOException e) {
close();
Bukkit.getLogger().log(Level.SEVERE, "Could not send", e);
}
}
@Override
protected void writeInt(int i) {
try{
outputStream.writeInt(i);
} catch (IOException e) {
close();
Bukkit.getLogger().log(Level.SEVERE, "Could not send", e);
}
}
@Override
protected void writeLong(long l) {
try{
outputStream.writeLong(l);
} catch (IOException e) {
close();
Bukkit.getLogger().log(Level.SEVERE, "Could not send", e);
}
}
@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 { try {
if(socket != null)
socket.close(); socket.close();
inputStream.close();
outputStream.close(); outputStream.close();
} catch (IOException e) { } catch (IOException e) {
Bukkit.getLogger().log(Level.SEVERE, "IOException on close", e); Bukkit.getLogger().log(Level.SEVERE, "IOException on socket 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");
} }
} }
} }