RecordSystem (Beta) #199
@ -4,9 +4,11 @@ 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;
|
||||
import org.bukkit.event.entity.PlayerDeathEvent;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerMoveEvent;
|
||||
|
||||
@ -18,6 +20,13 @@ public class EventRecordListener extends BasicListener {
|
||||
super(Config.event() ? EnumSet.allOf(FightState.class) : EnumSet.noneOf(FightState.class));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enable() {
|
||||
RecordSystem.init();
|
||||
SpectateConnection.init("127.0.0.1", 2222);
|
||||
super.enable();
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
||||
public void onPlayerJoin(PlayerJoinEvent e){
|
||||
FightPlayer fp = Fight.getFightPlayer(e.getPlayer());
|
||||
@ -36,5 +45,14 @@ public class EventRecordListener extends BasicListener {
|
||||
RecordSystem.entityMoves(e.getPlayer());
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
|
||||
public void onPlayerDeath(PlayerDeathEvent e){
|
||||
FightPlayer fp = Fight.getFightPlayer(e.getEntity());
|
||||
if(fp == null || fp.isLiving())
|
||||
return;
|
||||
|
||||
RecordSystem.entityDespawns(e.getEntity());
|
||||
}
|
||||
|
||||
//TODO: Listener, if player gets out (leaves, dies or starts to spectate), alternatively: track sent players
|
||||
}
|
||||
|
@ -3,14 +3,16 @@ package de.steamwar.fightsystem.record;
|
||||
import de.steamwar.fightsystem.Config;
|
||||
import de.steamwar.fightsystem.FightSystem;
|
||||
import de.steamwar.sql.SteamwarUser;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.ByteBufAllocator;
|
||||
import io.netty.buffer.UnpooledUnsafeDirectByteBuf;
|
||||
import org.bukkit.Bukkit;
|
||||
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(){
|
||||
@ -23,7 +25,7 @@ public class RecordSystem {
|
||||
/*
|
||||
* PlayerJoinPacket (0x00) + int EntityId + int SWUserId
|
||||
* EntityMovePacket (0x01) + int EntityId + double x, y, z + float pitch, yaw
|
||||
* EntityDespawnsPacket (0x02) + int EntityId TODO Implementation
|
||||
* EntityDespawnsPacket (0x02) + int EntityId
|
||||
*
|
||||
* TODO (Player-Oriented):
|
||||
* ItemInHandPacket (0x03) + int EntityId
|
||||
@ -45,43 +47,57 @@ public class RecordSystem {
|
||||
public static void playerJoins(Player p){
|
||||
SteamwarUser user = SteamwarUser.get(p.getUniqueId());
|
||||
|
||||
ByteBuf buf = getByteBuf();
|
||||
buf.writeByte(0x00);
|
||||
buf.writeInt(p.getEntityId());
|
||||
buf.writeInt(user.getId());
|
||||
send(buf);
|
||||
|
||||
entityMoves(p);
|
||||
DataOutputStream out = getOutput();
|
||||
try {
|
||||
out.writeByte(0x00);
|
||||
out.writeInt(p.getEntityId());
|
||||
out.writeInt(user.getId());
|
||||
entityMoves(p);
|
||||
} catch (IOException ex) {
|
||||
exceptionHandling(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static void entityMoves(Entity e){
|
||||
Location location = e.getLocation();
|
||||
|
||||
ByteBuf buf = getByteBuf();
|
||||
buf.writeByte(0x01);
|
||||
buf.writeInt(e.getEntityId());
|
||||
buf.writeDouble(location.getX());
|
||||
buf.writeDouble(location.getY());
|
||||
buf.writeDouble(location.getZ());
|
||||
buf.writeFloat(location.getPitch());
|
||||
buf.writeFloat(location.getYaw());
|
||||
send(buf);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
public static void playerLeaves(Player p){
|
||||
SteamwarUser user = SteamwarUser.get(p.getUniqueId());
|
||||
|
||||
ByteBuf buf = getByteBuf();
|
||||
buf.writeByte(0x02);
|
||||
buf.writeInt(p.getEntityId());
|
||||
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 ByteBuf getByteBuf(){
|
||||
return new UnpooledUnsafeDirectByteBuf(ByteBufAllocator.DEFAULT, 8, 8); //TODO size
|
||||
private static DataOutputStream getOutput(){
|
||||
return SpectateConnection.get().getOutput();
|
||||
}
|
||||
|
||||
private static void send(ByteBuf buf){
|
||||
|
||||
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(){
|
||||
|
@ -1,131 +1,76 @@
|
||||
package de.steamwar.fightsystem.record;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.Socket;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class SpectateConnection {
|
||||
|
||||
public static SpectateConnection getInstance() {
|
||||
private static SpectateConnection spectateConnection = new SpectateConnection();
|
||||
|
||||
public static SpectateConnection get() {
|
||||
return spectateConnection;
|
||||
}
|
||||
|
||||
public static SpectateConnection build(String ip, int port) {
|
||||
spectateConnection = new SpectateConnection(ip, port);
|
||||
return spectateConnection;
|
||||
}
|
||||
|
||||
public static void cleanUp() {
|
||||
if (spectateConnection == null) {
|
||||
return;
|
||||
}
|
||||
spectateConnection.internalCleanUp();
|
||||
spectateConnection = null;
|
||||
}
|
||||
|
||||
private static SpectateConnection spectateConnection = null;
|
||||
|
||||
private void internalCleanUp() {
|
||||
public static void init(String ip, int port){
|
||||
try {
|
||||
socket.close();
|
||||
inputStream.close();
|
||||
outputStream.close();
|
||||
spectateConnection = new SpectateConnection(ip, port);
|
||||
} catch (IOException e) {
|
||||
errorCode = 2;
|
||||
Bukkit.getLogger().log(Level.SEVERE, "Could not init spectateconnection", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 0 - OK
|
||||
* 1 - No Connect
|
||||
* 2 - Error while cleanup
|
||||
* 3 - Error while reading
|
||||
* 4 - Error while writing
|
||||
* 5 - Error while flushing
|
||||
*/
|
||||
private int errorCode = 0;
|
||||
private final Socket socket;
|
||||
private final DataInputStream inputStream;
|
||||
private final DataOutputStream outputStream;
|
||||
|
||||
private Socket socket;
|
||||
private InputStream inputStream;
|
||||
private OutputStream outputStream;
|
||||
|
||||
private SpectateConnection(String ip, int port) {
|
||||
try {
|
||||
this.socket = new Socket(ip, port);
|
||||
this.inputStream = socket.getInputStream();
|
||||
this.outputStream = socket.getOutputStream();
|
||||
} catch (IOException e) {
|
||||
errorCode = 1;
|
||||
}
|
||||
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()));
|
||||
}
|
||||
|
||||
public int getErrorCode() {
|
||||
return errorCode;
|
||||
private SpectateConnection(){
|
||||
socket = null;
|
||||
this.inputStream = new DataInputStream(new NullInputStream());
|
||||
this.outputStream = new DataOutputStream(new NullOutputStream());
|
||||
}
|
||||
|
||||
public InputStream getInputStream() {
|
||||
public InputStream getInput() {
|
||||
return inputStream;
|
||||
}
|
||||
|
||||
public OutputStream getOutputStream() {
|
||||
public DataOutputStream getOutput() {
|
||||
return outputStream;
|
||||
}
|
||||
|
||||
public int read() {
|
||||
public void close() {
|
||||
try {
|
||||
return inputStream.read();
|
||||
if(socket != null)
|
||||
socket.close();
|
||||
inputStream.close();
|
||||
outputStream.close();
|
||||
} catch (IOException e) {
|
||||
errorCode = 3;
|
||||
return -2;
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] lazyRead(int length) {
|
||||
try {
|
||||
byte[] bytes = new byte[length];
|
||||
inputStream.read(bytes);
|
||||
return bytes;
|
||||
} catch (IOException e) {
|
||||
errorCode = 3;
|
||||
return new byte[length];
|
||||
private static class NullInputStream extends InputStream {
|
||||
|
||||
@Override
|
||||
public int read() throws IOException {
|
||||
throw new IOException("NullInputStreamReadAttempt");
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] read(int length) {
|
||||
byte[] bytes = new byte[length];
|
||||
for (int i = 0; i < length; i++) {
|
||||
bytes[i] = (byte) read();
|
||||
if (errorCode != 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
public void write(byte b) {
|
||||
try {
|
||||
outputStream.write(b);
|
||||
} catch (IOException e) {
|
||||
errorCode = 4;
|
||||
}
|
||||
}
|
||||
|
||||
public void write(byte... bytes) {
|
||||
try {
|
||||
outputStream.write(bytes);
|
||||
} catch (IOException e) {
|
||||
errorCode = 4;
|
||||
}
|
||||
flush();
|
||||
}
|
||||
|
||||
public void flush() {
|
||||
try {
|
||||
outputStream.flush();
|
||||
} catch (IOException e) {
|
||||
errorCode = 5;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
In neuem Issue referenzieren
Einen Benutzer sperren