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

Datei anzeigen

@ -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();
}

Datei anzeigen

@ -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());
Recorder.rByte(0x00);
Recorder.rInt(p.getEntityId());
Recorder.rInt(user.getId());
entityMoves(p);
} catch (IOException ex) {
exceptionHandling(ex);
}
}
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(){

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;
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);
} 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.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 connection", e);
}
}
private SpectateConnection(){
socket = null;
this.inputStream = new DataInputStream(new NullInputStream());
this.outputStream = new DataOutputStream(new NullOutputStream());
}
public InputStream getInput() {
return inputStream;
}
public DataOutputStream getOutput() {
return outputStream;
}
public void close() {
@Override
protected void writeByte(int b) {
try{
outputStream.writeByte(b);
} catch (IOException e) {
close();
Bukkit.getLogger().log(Level.SEVERE, "Could not send", e);
}
}
@Override
protected void writeShort(short s) {
try{
outputStream.writeShort(s);
} 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 {
if(socket != null)
socket.close();
inputStream.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);
}
}
}