Smaller fixes for the pinger, slightly changed position behaviour. Added

getHeight() to Hologram.
This commit is contained in:
filoghost 2015-02-11 16:11:09 +01:00
parent db03559136
commit c4ef162caf
5 changed files with 58 additions and 9 deletions

View File

@ -89,6 +89,14 @@ public interface Hologram {
public int size();
/**
* The physical height of the hologram, counting all the lines. Since: v2.1.4
*
* @return the height of the hologram, counting all the lines and the gaps between them
*/
public double getHeight();
/**
* Teleports a hologram to the given location.
*

View File

@ -3,6 +3,7 @@ package com.gmail.filoghost.holographicdisplays.bridge.bungeecord.serverpinger;
import java.lang.String;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import com.gmail.filoghost.holographicdisplays.util.DebugHandler;
@ -20,7 +21,23 @@ public class PingResponse
this.maxPlayers = maxPlayers;
}
public PingResponse(JSONObject json) {
public PingResponse(String jsonString, ServerAddress address) {
if (jsonString == null || jsonString.isEmpty()) {
motd = "Invalid ping response";
DebugHandler.logToConsole("Received empty Json response from IP \"" + address.toString() + "\"!");
return;
}
Object jsonObject = JSONValue.parse(jsonString);
if (!(jsonObject instanceof JSONObject)) {
motd = "Invalid ping response";
DebugHandler.logToConsole("Received invalid Json response from IP \"" + address.toString() + "\": " + jsonString);
return;
}
JSONObject json = (JSONObject) jsonObject;
isOnline = true;
Object descriptionObject = json.get("description");
@ -29,7 +46,7 @@ public class PingResponse
motd = descriptionObject.toString();
} else {
motd = "Invalid ping response";
DebugHandler.logToConsole("Received invalid ping response: " + json.toString());
DebugHandler.logToConsole("Received invalid Json response from IP \"" + address.toString() + "\": " + jsonString);
}
Object playersObject = json.get("players");

View File

@ -8,9 +8,6 @@ import java.net.Socket;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
final class ServerPingerPostNetty extends ServerPinger {
@Override
@ -43,7 +40,7 @@ final class ServerPingerPostNetty extends ServerPinger {
final byte[] responseData = new byte[PacketUtils.readVarInt(dataIn)];
dataIn.readFully(responseData);
final String jsonString = new String(responseData, PacketUtils.UTF8);
return new PingResponse((JSONObject) JSONValue.parse(jsonString));
return new PingResponse(jsonString, serverAddress);
}
finally {
PacketUtils.closeQuietly(dataOut);

View File

@ -8,8 +8,11 @@ import java.lang.Integer;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
import java.util.Arrays;
import java.lang.String;
import com.gmail.filoghost.holographicdisplays.util.DebugHandler;
final class ServerPingerPreNetty extends ServerPinger {
@Override
@ -33,6 +36,12 @@ final class ServerPingerPreNetty extends ServerPinger {
dataIn.readFully(bytes);
socket.close();
final String[] info = new String(bytes, PacketUtils.UTF16BE).split(String.valueOf('\0'));
if (info.length < 6) {
DebugHandler.logToConsole("Received invalid ping response: " + Arrays.toString(info));
return new PingResponse(true, "Invalid ping response", 0, 0);
}
final PingResponse response = new PingResponse(true, info[3], Integer.parseInt(info[4]), Integer.parseInt(info[5]));
// String versionName = info[2];
// String protocol = info[1];

View File

@ -183,6 +183,21 @@ public class CraftHologram implements Hologram, com.gmail.filoghost.holograms.ap
return lines.size();
}
@Override
public double getHeight() {
if (lines.isEmpty()) {
return 0;
}
double height = 0.0;
for (CraftHologramLine line : lines) {
height += line.getHeight();
}
height += Configuration.spaceBetweenLines * (lines.size() - 1);
return height;
}
@Override
public CraftVisibilityManager getVisibilityManager() {
@ -241,10 +256,11 @@ public class CraftHologram implements Hologram, com.gmail.filoghost.holograms.ap
for (CraftHologramLine line : lines) {
currentY -= line.getHeight();
if (first) {
first = false;
} else {
currentY -= line.getHeight();
currentY -= Configuration.spaceBetweenLines;
}
@ -273,10 +289,11 @@ public class CraftHologram implements Hologram, com.gmail.filoghost.holograms.ap
for (CraftHologramLine line : lines) {
currentY -= line.getHeight();
if (first) {
first = false;
} else {
currentY -= line.getHeight();
currentY -= Configuration.spaceBetweenLines;
}
@ -325,10 +342,11 @@ public class CraftHologram implements Hologram, com.gmail.filoghost.holograms.ap
continue;
}
currentY -= line.getHeight();
if (first) {
first = false;
} else {
currentY -= line.getHeight();
currentY -= Configuration.spaceBetweenLines;
}