SteamWar/FightSystem
Archiviert
13
1

First player movement implementation

Signed-off-by: Lixfel <agga-games@gmx.de>
Dieser Commit ist enthalten in:
Lixfel 2020-08-22 10:01:57 +02:00
Ursprung b24a8636da
Commit 0f3943cd73
3 geänderte Dateien mit 107 neuen und 128 gelöschten Zeilen

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 {
socket.close(); spectateConnection = new SpectateConnection(ip, port);
inputStream.close();
outputStream.close();
} catch (IOException e) { } catch (IOException e) {
errorCode = 2; Bukkit.getLogger().log(Level.SEVERE, "Could not init spectateconnection", e);
} }
} }
/** private final Socket socket;
* 0 - OK private final DataInputStream inputStream;
* 1 - No Connect private final DataOutputStream outputStream;
* 2 - Error while cleanup
* 3 - Error while reading
* 4 - Error while writing
* 5 - Error while flushing
*/
private int errorCode = 0;
private Socket socket; private SpectateConnection(String ip, int port) throws IOException{
private InputStream inputStream; this.socket = new Socket(ip, port);
private OutputStream outputStream; this.inputStream = new DataInputStream(socket.getInputStream());
this.outputStream = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
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() { private SpectateConnection(){
return errorCode; socket = null;
this.inputStream = new DataInputStream(new NullInputStream());
this.outputStream = new DataOutputStream(new NullOutputStream());
} }
public InputStream getInputStream() { public InputStream getInput() {
return inputStream; return inputStream;
} }
public OutputStream getOutputStream() { public DataOutputStream getOutput() {
return outputStream; return outputStream;
} }
public int read() { public void close() {
try { try {
return inputStream.read(); if(socket != null)
socket.close();
inputStream.close();
outputStream.close();
} catch (IOException e) { } catch (IOException e) {
errorCode = 3; Bukkit.getLogger().log(Level.SEVERE, "IOException on close", e);
return -2; }
spectateConnection = new SpectateConnection();
}
private static class NullOutputStream extends OutputStream {
@Override
public void write(int b) throws IOException {
//ignored
} }
} }
public byte[] lazyRead(int length) { private static class NullInputStream extends InputStream {
try {
byte[] bytes = new byte[length]; @Override
inputStream.read(bytes); public int read() throws IOException {
return bytes; throw new IOException("NullInputStreamReadAttempt");
} 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;
}
}
} }