SteamWar/FightSystem
Archiviert
13
1

RecordSystem (Beta) #199

Manuell gemergt
Lixfel hat 40 Commits von recordSystem nach master 2020-11-01 20:11:45 +01:00 zusammengeführt
3 geänderte Dateien mit 107 neuen und 128 gelöschten Zeilen
Nur Änderungen aus Commit 0f3943cd73 werden angezeigt - Alle Commits anzeigen

Datei anzeigen

@ -4,9 +4,11 @@ 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;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent; 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)); 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) @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onPlayerJoin(PlayerJoinEvent e){ public void onPlayerJoin(PlayerJoinEvent e){
FightPlayer fp = Fight.getFightPlayer(e.getPlayer()); FightPlayer fp = Fight.getFightPlayer(e.getPlayer());
@ -36,5 +45,14 @@ public class EventRecordListener extends BasicListener {
RecordSystem.entityMoves(e.getPlayer()); 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 //TODO: Listener, if player gets out (leaves, dies or starts to spectate), alternatively: track sent players
} }

Datei anzeigen

@ -3,14 +3,16 @@ package de.steamwar.fightsystem.record;
import de.steamwar.fightsystem.Config; import de.steamwar.fightsystem.Config;
import de.steamwar.fightsystem.FightSystem; import de.steamwar.fightsystem.FightSystem;
import de.steamwar.sql.SteamwarUser; 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.Bukkit;
import org.bukkit.Location; 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(){
@ -23,7 +25,7 @@ public class RecordSystem {
/* /*
* PlayerJoinPacket (0x00) + int EntityId + int SWUserId * PlayerJoinPacket (0x00) + int EntityId + int SWUserId
* EntityMovePacket (0x01) + int EntityId + double x, y, z + float pitch, yaw * EntityMovePacket (0x01) + int EntityId + double x, y, z + float pitch, yaw
* EntityDespawnsPacket (0x02) + int EntityId TODO Implementation * EntityDespawnsPacket (0x02) + int EntityId
* *
* TODO (Player-Oriented): * TODO (Player-Oriented):
* ItemInHandPacket (0x03) + int EntityId * ItemInHandPacket (0x03) + int EntityId
@ -45,43 +47,57 @@ 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());
ByteBuf buf = getByteBuf(); DataOutputStream out = getOutput();
buf.writeByte(0x00); try {
buf.writeInt(p.getEntityId()); out.writeByte(0x00);
buf.writeInt(user.getId()); out.writeInt(p.getEntityId());
send(buf); 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();
ByteBuf buf = getByteBuf(); DataOutputStream out = getOutput();
buf.writeByte(0x01); try {
buf.writeInt(e.getEntityId()); out.writeByte(0x01);
buf.writeDouble(location.getX()); out.writeInt(e.getEntityId());
buf.writeDouble(location.getY()); out.writeDouble(location.getX());
buf.writeDouble(location.getZ()); out.writeDouble(location.getY());
buf.writeFloat(location.getPitch()); out.writeDouble(location.getZ());
buf.writeFloat(location.getYaw()); out.writeFloat(location.getPitch());
send(buf); out.writeFloat(location.getYaw());
out.flush();
} catch (IOException ex) {
exceptionHandling(ex);
}
} }
public static void playerLeaves(Player p){ public static void entityDespawns(Entity e){
SteamwarUser user = SteamwarUser.get(p.getUniqueId()); DataOutputStream out = getOutput();
try {
ByteBuf buf = getByteBuf(); out.writeByte(0x02);
buf.writeByte(0x02); out.writeInt(e.getEntityId());
buf.writeInt(p.getEntityId()); out.flush();
} catch (IOException ex) {
exceptionHandling(ex);
}
} }
private static ByteBuf getByteBuf(){ private static DataOutputStream getOutput(){
return new UnpooledUnsafeDirectByteBuf(ByteBufAllocator.DEFAULT, 8, 8); //TODO size 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(){ private static void checkWorldState(){

Datei anzeigen

@ -1,131 +1,76 @@
package de.steamwar.fightsystem.record; package de.steamwar.fightsystem.record;
import java.io.IOException; import org.bukkit.Bukkit;
import java.io.InputStream;
import java.io.OutputStream; import java.io.*;
import java.net.Socket; import java.net.Socket;
import java.util.logging.Level;
public class SpectateConnection { public class SpectateConnection {
public static SpectateConnection getInstance() { private static SpectateConnection spectateConnection = new SpectateConnection();
public static SpectateConnection get() {
return spectateConnection; return spectateConnection;
} }
public static SpectateConnection build(String ip, int port) { public static void init(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() {
try { 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.outputStream = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
}
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() {
try {
if(socket != null)
socket.close(); socket.close();
inputStream.close(); inputStream.close();
outputStream.close(); outputStream.close();
} catch (IOException e) { } catch (IOException e) {
errorCode = 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
} }
} }
/** private static class NullInputStream extends InputStream {
* 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 Socket socket; @Override
private InputStream inputStream; public int read() throws IOException {
private OutputStream outputStream; throw new IOException("NullInputStreamReadAttempt");
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;
} }
} }
public int getErrorCode() {
return errorCode;
}
public InputStream getInputStream() {
return inputStream;
}
public OutputStream getOutputStream() {
return outputStream;
}
public int read() {
try {
return inputStream.read();
} catch (IOException e) {
errorCode = 3;
return -2;
}
}
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];
}
}
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;
}
}
} }