Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 37 additions & 27 deletions src/main/java/de/minebench/syncinv/SyncInv.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
import de.minebench.syncinv.listeners.PlayerConnectionValidateLoginListener;
import de.minebench.syncinv.listeners.PlayerFreezeListener;
import de.minebench.syncinv.listeners.PlayerJoinListener;
import de.minebench.syncinv.listeners.PlayerLoginListener;
import de.minebench.syncinv.listeners.PlayerQuitListener;
import de.minebench.syncinv.listeners.PlayerLoginListener;
import de.minebench.syncinv.messenger.Message;
import de.minebench.syncinv.messenger.MessageType;
import de.minebench.syncinv.messenger.PlayerDataQuery;
Expand Down Expand Up @@ -50,7 +50,6 @@
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.AbstractMap;
import java.util.Date;
Expand Down Expand Up @@ -177,8 +176,8 @@ public void onEnable() {
loadConfig();

playerDataFolder = getServer().getMinecraftVersion().startsWith("1.")
? new File(getServer().getWorlds().get(0).getWorldFolder(), "playerdata")
: new File(new File(getServer().getWorlds().get(0).getWorldFolder(), "players"), "data");
? new File(getServer().getWorlds().getFirst().getWorldFolder(), "playerdata")
: new File(new File(getServer().getWorlds().getFirst().getWorldFolder(), "players"), "data");

MethodHandle tempUUIDGetterHandle = null;
try {
Expand Down Expand Up @@ -410,7 +409,7 @@ && getConfig().contains("sync-" + syncType.getKey(), true)) {
}

if (getServer().getMap((short) 0) == null) {
getServer().createMap(getServer().getWorlds().get(0));
getServer().createMap(getServer().getWorlds().getFirst());
}
try {
MapView map = null;
Expand Down Expand Up @@ -483,10 +482,10 @@ public boolean isLocked(UUID playerId) {

/**
* Get the date when a player last logged out
* @param playerId The UUID of the player
* @param online Whether or not it should return the current time if the player is online
* @return The timestamp of his last known data on the server in milliseconds;
* 0 if the file doesn't exist or an error occurs. (Take a look at {File#lastModified})
* @param playerId The UUID of the player
* @param online Whether or not it should return the current time if the player is online
* @return The timestamp of his last known data on the server in milliseconds;
* 0 if the file doesn't exist or an error occurs. (Take a look at {File#lastModified})
*/
public long getLastSeen(UUID playerId, boolean online) {
if (online) {
Expand Down Expand Up @@ -536,7 +535,7 @@ public boolean setLastSeen(UUID playerId, long timeStamp) {
}
// Workaround for systems that don't allow modifying the dat directly
try {
Files.write(lastSeen.toPath(), String.valueOf(timeStamp).getBytes(StandardCharsets.UTF_8));
Files.writeString(lastSeen.toPath(), String.valueOf(timeStamp));
return true;
} catch (IOException e) {
getLogger().log(Level.SEVERE, "Unable to store lastseen file for " + playerId, e);
Expand Down Expand Up @@ -585,7 +584,7 @@ public void connectToServer(UUID playerId, String server) {

/**
* Apply a PlayerData object to its player
* @param data The data to apply
* @param data The data to apply
*/
public void applyData(PlayerData data, Runnable finished) {
if (data == null)
Expand Down Expand Up @@ -929,27 +928,32 @@ private void forceRender(MapView map) {
map.addRenderer(new EmptyRenderer());
}

/**
* @return Reference to the OpenInv plugin to load data for the query option
*/
public OpenInv getOpenInv() {
return this.openInv;
return openInv;
}

/**
* @return The messenger for communications between the servers
*/
public ServerMessenger getMessenger() {
return this.messenger;
return messenger;
}

/**
* @return The amount of seconds we should wait for a query to stopTimeout
*/
public int getQueryTimeout() {
return this.queryTimeout;
return queryTimeout;
}

/**
* @return The id of the newest map that was seen on this server
*/
public int getNewestMap() {
return this.newestMap;
}

private static class EmptyRenderer extends MapRenderer {
@Override
public void render(@NotNull MapView map, @NotNull MapCanvas canvas, @NotNull Player player) {

}
return newestMap;
}

private void cacheData(PlayerData data, Runnable finished) {
Expand All @@ -958,7 +962,7 @@ private void cacheData(PlayerData data, Runnable finished) {

/**
* Get data that was cached which should be applied on a player's login
* @param player The player to get the data for
* @param player The player to get the data for
* @return A cache entry containing the PlayerData and the notification Runnable when applied successfully
*/
public Map.Entry<PlayerData, Runnable> getCachedData(Player player) {
Expand All @@ -967,7 +971,7 @@ public Map.Entry<PlayerData, Runnable> getCachedData(Player player) {

/**
* Remove the cached data of a player
* @param player The player to remove the data for
* @param player The player to remove the data for
*/
public void removeCachedData(Player player) {
playerDataCache.invalidate(player.getUniqueId());
Expand Down Expand Up @@ -1124,7 +1128,7 @@ public PlayerData getData(Player player) {

/**
* The sound to play when a player gets unlocked, should match the vanilla levelup
* @param playerId The uuid of the Player to play the sound to
* @param playerId The uuid of the Player to play the sound to
*/
public void playLoadSound(UUID playerId) {
Player player = getServer().getPlayer(playerId);
Expand All @@ -1135,7 +1139,7 @@ public void playLoadSound(UUID playerId) {

/**
* The sound to play when a player gets unlocked, should match the vanilla levelup
* @param player The Player to play the sound to
* @param player The Player to play the sound to
*/
public void playLoadSound(Player player) {
player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 0.7f, 1);
Expand All @@ -1150,7 +1154,7 @@ public void checkMap(int id) {
logDebug("Checking map " + id);
try {
while (getServer().getMap(id) == null) {
MapView map = getServer().createMap(getServer().getWorlds().get(0));
MapView map = getServer().createMap(getServer().getWorlds().getFirst());
logDebug("Created map " + map.getId());
}
} catch (Exception e) {
Expand Down Expand Up @@ -1235,4 +1239,10 @@ private enum FilterMode {
DENY,
ALLOW
}

private static class EmptyRenderer extends MapRenderer {
@Override
public void render(@NotNull MapView map, @NotNull MapCanvas canvas, @NotNull Player player) {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ public void onPlayerLogin(PlayerConnectionValidateLoginEvent e) {
// Event will have pre-cancelled result set for whitelist/banlist
if (e.isAllowed() && e.getConnection() instanceof PlayerLoginConnection connection && connection.getUnsafeProfile() != null) {
// Sync login listener for sanity checking as we don't want to allow the player to exist twice
Entity entity = plugin.getServer().getEntity(connection.getUnsafeProfile().getUniqueId());
Entity entity = plugin.getServer().getEntity(connection.getUnsafeProfile().getId());
if (entity instanceof Player) {
e.kickMessage(Component.text("A player with your UUID already exists!").color(NamedTextColor.RED));
plugin.logDebug("A player object with the same UUID " + connection.getUnsafeProfile().getUniqueId() + " already exists on the server.");
plugin.logDebug("A player object with the same UUID " + connection.getUnsafeProfile().getId() + " already exists on the server.");
// Kick player. This should do nothing if it's not a real one (e.g. one loaded by OpenInv)
// Removal of such players is up to OpenInv itself
((Player) entity).kickPlayer("Login from different location.");
} else if (entity != null) {
// Well... this is weird. An entity with the same UUID as the player's exists?!? Removing it just to be sure...
plugin.getLogger().info("A " + entity + " with the same UUID " + connection.getUnsafeProfile().getUniqueId()
plugin.getLogger().info("A " + entity + " with the same UUID " + connection.getUnsafeProfile().getId()
+ " as the player login in existed on the server at " + entity.getLocation() + "... removing it!");
entity.remove();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ public PlayerFreezeListener(SyncInv plugin) {

@EventHandler(ignoreCancelled = true)
public void onDropItem(PlayerDropItemEvent e) {
if(plugin.isLocked(e.getPlayer().getUniqueId())) {
if (plugin.isLocked(e.getPlayer().getUniqueId())) {
e.setCancelled(true);
e.getPlayer().sendMessage(plugin.getLang("cant-drop-items"));
}
}

@EventHandler(ignoreCancelled = true)
public void onPlayerMove(PlayerMoveEvent e) {
if(!sameBlock(e.getFrom(), e.getTo()) && plugin.isLocked(e.getPlayer().getUniqueId())) {
if (!sameBlock(e.getFrom(), e.getTo()) && plugin.isLocked(e.getPlayer().getUniqueId())) {
e.setCancelled(true);
if (e.getFrom().getBlockY() == e.getTo().getBlockY()) {
e.getPlayer().sendMessage(plugin.getLang("cant-move"));
Expand All @@ -79,15 +79,15 @@ public void onPlayerPickupItem(EntityPickupItemEvent e) {

@EventHandler(ignoreCancelled = true)
public void onPlayerPickupExp(PlayerExpChangeEvent e) {
if(plugin.isLocked(e.getPlayer().getUniqueId())) {
if (plugin.isLocked(e.getPlayer().getUniqueId())) {
e.setAmount(0);
e.getPlayer().sendMessage(plugin.getLang("cant-pickup-exp"));
}
}

@EventHandler(ignoreCancelled = true)
public void onPlayerDamage(EntityDamageEvent e) {
if(e.getEntity() instanceof Player && plugin.isLocked(e.getEntity().getUniqueId())) {
if (e.getEntity() instanceof Player && plugin.isLocked(e.getEntity().getUniqueId())) {
e.setCancelled(true);
}
}
Expand All @@ -101,63 +101,63 @@ public void onPlayerDeath(PlayerDeathEvent e) {

@EventHandler(ignoreCancelled = true)
public void onInventoryInteraction(InventoryClickEvent e) {
if(plugin.isLocked(e.getWhoClicked().getUniqueId())) {
if (plugin.isLocked(e.getWhoClicked().getUniqueId())) {
e.setCancelled(true);
e.getWhoClicked().sendMessage(plugin.getLang("wait-for-loading"));
}
}

@EventHandler(ignoreCancelled = true)
public void onInventoryInteraction(InventoryDragEvent e) {
if(plugin.isLocked(e.getWhoClicked().getUniqueId())) {
if (plugin.isLocked(e.getWhoClicked().getUniqueId())) {
e.setCancelled(true);
e.getWhoClicked().sendMessage(plugin.getLang("wait-for-loading"));
}
}

@EventHandler(ignoreCancelled = true)
public void onInventoryInteraction(InventoryOpenEvent e) {
if(plugin.isLocked(e.getPlayer().getUniqueId())) {
if (plugin.isLocked(e.getPlayer().getUniqueId())) {
e.setCancelled(true);
e.getPlayer().sendMessage(plugin.getLang("wait-for-loading"));
}
}

@EventHandler(ignoreCancelled = true)
public void onInteraction(PlayerInteractEvent e) {
if(plugin.isLocked(e.getPlayer().getUniqueId())) {
if (plugin.isLocked(e.getPlayer().getUniqueId())) {
e.setCancelled(true);
e.getPlayer().sendMessage(plugin.getLang("wait-for-loading"));
}
}

@EventHandler(ignoreCancelled = true)
public void onDamageEntity(EntityDamageByEntityEvent e) {
if(e.getDamager() instanceof Player && plugin.isLocked(e.getDamager().getUniqueId())) {
if (e.getDamager() instanceof Player && plugin.isLocked(e.getDamager().getUniqueId())) {
e.setCancelled(true);
e.getDamager().sendMessage(plugin.getLang("wait-for-loading"));
}
}

@EventHandler(ignoreCancelled = true)
public void onBlockDamage(BlockDamageEvent e) {
if(plugin.isLocked(e.getPlayer().getUniqueId())) {
if (plugin.isLocked(e.getPlayer().getUniqueId())) {
e.setCancelled(true);
e.getPlayer().sendMessage(plugin.getLang("wait-for-loading"));
}
}

@EventHandler(ignoreCancelled = true)
public void onChat(AsyncPlayerChatEvent e) {
if(plugin.isLocked(e.getPlayer().getUniqueId())) {
if (plugin.isLocked(e.getPlayer().getUniqueId())) {
e.setCancelled(true);
e.getPlayer().sendMessage(plugin.getLang("wait-for-loading"));
}
}

@EventHandler(ignoreCancelled = true)
public void onChat(PlayerCommandPreprocessEvent e) {
if(plugin.isLocked(e.getPlayer().getUniqueId())) {
if (plugin.isLocked(e.getPlayer().getUniqueId())) {
e.setCancelled(true);
e.getPlayer().sendMessage(plugin.getLang("wait-for-loading"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public void onPlayerPreLogin(AsyncPlayerPreLoginEvent e) {
if (plugin.getMessenger().queryData(e.getUniqueId()) == null && (!plugin.getMessenger().isAllowedToBeAlone() || !plugin.getMessenger().isAlone())) {
e.setLoginResult(AsyncPlayerPreLoginEvent.Result.KICK_OTHER);
e.setKickMessage(ChatColor.RED + "Unable to query player data!");
return;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void onPlayerQuit(PlayerQuitEvent event) {
}
PlayerDataQuery query = plugin.getMessenger().removeQuery(event.getPlayer().getUniqueId());
if (query != null) {
// The player is gone although he had a query...
// The player is gone, although he had a query...
// We have to make sure now that the time of the data file matches the old one
// and not send our data to all the other servers as it might be outdated
plugin.runLater(() -> {
Expand All @@ -67,5 +67,4 @@ public void onPlayerQuit(PlayerQuitEvent event) {
// Update last seen
plugin.runLater(() -> plugin.setLastSeen(event.getPlayer().getUniqueId(), System.currentTimeMillis()), 1);
}

}
28 changes: 16 additions & 12 deletions src/main/java/de/minebench/syncinv/messenger/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ public class Message {

/**
* A Message of a certain type. Optionally with some data
* @param sender The sender which sent the message
* @param id The transaction ID this Message is associated with
* @param type The type of the message
* @param objects The data, in the order that it should be sent
*
* @param sender The sender which sent the message
* @param id The transaction ID this Message is associated with
* @param type The type of the message
* @param objects The data, in the order that it should be sent
* @throws IllegalArgumentException when the amount of Objects given didn't match the MessageType requirements
*/
public Message(String sender, long id, MessageType type, Object... objects) {
Expand Down Expand Up @@ -100,10 +101,10 @@ public Object read() {

/**
* Generate a byte array out of the data of this message
* @return The generated byte array (starts with the sender
* then the type ordinal, then the amount
* of data being sent and each data object);
* an empty one if an error occurred
* @return The generated byte array (starts with the sender
* then the type ordinal, then the amount
* of data being sent and each data object);
* an empty one if an error occurred
*/
public byte[] toByteArray() {
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
Expand All @@ -127,12 +128,12 @@ public byte[] toByteArray() {
/**
* Get the message from a bytearray generated as described in the toByteArray method
* @param bytes The bytes
* @return The Message object
* @return The Message object
* @throws IOException
* @throws IllegalArgumentException When the message type is not supported
* @throws IllegalArgumentException When the message type is not supported
* @throws ClassNotFoundException
* @throws InvalidConfigurationException If the data is invalid
* @throws VersionMismatchException If the received message is of a different version than it can accept
* @throws InvalidConfigurationException If the data is invalid
* @throws VersionMismatchException If the received message is of a different version than it can accept
*/
public static Message fromByteArray(byte[] bytes) throws IOException, IllegalArgumentException, ClassNotFoundException, InvalidConfigurationException, VersionMismatchException {
try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
Expand All @@ -152,6 +153,9 @@ public static Message fromByteArray(byte[] bytes) throws IOException, IllegalArg
}
}

/**
* @return a Sting representation of this message
*/
public String toString() {
return "Message(sender=" + this.getSender() + ", id=" + this.getId() + ", type=" + this.getType() + ", data=" + this.getData() + ")";
}
Expand Down
Loading