From 6563b4f6cef3741f67b7df7156cca47241ebc8e8 Mon Sep 17 00:00:00 2001 From: Jikoo Date: Thu, 18 Mar 2021 20:31:45 -0400 Subject: [PATCH] Migrate API-only functions out of PlayerDataManager With the update to 1.16 there's no need to maintain multiple copies of the same code. Additionally, in 1.16 the action bar now supports JSON text. --- .../internal/v1_16_R3/PlayerDataManager.java | 23 ------------------- .../main/java/com/lishid/openinv/OpenInv.java | 20 +++++++++++++--- .../openinv/internal/IPlayerDataManager.java | 16 +++---------- 3 files changed, 20 insertions(+), 39 deletions(-) diff --git a/internal/v1_16_R3/src/main/java/com/lishid/openinv/internal/v1_16_R3/PlayerDataManager.java b/internal/v1_16_R3/src/main/java/com/lishid/openinv/internal/v1_16_R3/PlayerDataManager.java index 701fdba..5a29ceb 100644 --- a/internal/v1_16_R3/src/main/java/com/lishid/openinv/internal/v1_16_R3/PlayerDataManager.java +++ b/internal/v1_16_R3/src/main/java/com/lishid/openinv/internal/v1_16_R3/PlayerDataManager.java @@ -22,16 +22,13 @@ import com.lishid.openinv.internal.OpenInventoryView; import com.mojang.authlib.GameProfile; import java.lang.reflect.Field; import net.minecraft.server.v1_16_R3.ChatComponentText; -import net.minecraft.server.v1_16_R3.ChatMessageType; import net.minecraft.server.v1_16_R3.Container; import net.minecraft.server.v1_16_R3.Containers; import net.minecraft.server.v1_16_R3.Entity; import net.minecraft.server.v1_16_R3.EntityPlayer; import net.minecraft.server.v1_16_R3.MinecraftServer; -import net.minecraft.server.v1_16_R3.PacketPlayOutChat; import net.minecraft.server.v1_16_R3.PacketPlayOutOpenWindow; import net.minecraft.server.v1_16_R3.PlayerInteractManager; -import net.minecraft.server.v1_16_R3.SystemUtils; import net.minecraft.server.v1_16_R3.World; import net.minecraft.server.v1_16_R3.WorldServer; import org.bukkit.Bukkit; @@ -237,24 +234,4 @@ public class PlayerDataManager implements IPlayerDataManager { return -1; } - @Override - public void sendSystemMessage(@NotNull Player player, @NotNull String message) { - int newline = message.indexOf('\n'); - if (newline != -1) { - // No newlines in action bar chat. - message = message.substring(0, newline); - } - - if (message.isEmpty()) { - return; - } - - EntityPlayer nmsPlayer = getHandle(player); - - // For action bar chat, color codes are still supported but JSON text color is not allowed. Do not convert text. - if (nmsPlayer.playerConnection != null) { - nmsPlayer.playerConnection.sendPacket(new PacketPlayOutChat(new ChatComponentText(message), ChatMessageType.GAME_INFO, SystemUtils.b)); - } - } - } diff --git a/plugin/src/main/java/com/lishid/openinv/OpenInv.java b/plugin/src/main/java/com/lishid/openinv/OpenInv.java index 24cd9b0..8137584 100644 --- a/plugin/src/main/java/com/lishid/openinv/OpenInv.java +++ b/plugin/src/main/java/com/lishid/openinv/OpenInv.java @@ -42,6 +42,8 @@ import java.util.Map; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.function.Consumer; +import net.md_5.bungee.api.ChatMessageType; +import net.md_5.bungee.api.chat.TextComponent; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import org.bukkit.command.Command; @@ -299,9 +301,21 @@ public class OpenInv extends JavaPlugin implements IOpenInv { public void sendSystemMessage(@NotNull Player player, @NotNull String key) { String message = this.languageManager.getValue(key, getLocale(player)); - if (message != null) { - this.accessor.getPlayerDataManager().sendSystemMessage(player, message); + if (message == null) { + return; } + + int newline = message.indexOf('\n'); + if (newline != -1) { + // No newlines in action bar chat. + message = message.substring(0, newline); + } + + if (message.isEmpty()) { + return; + } + + player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(message)); } public @Nullable String getLocalizedMessage(@NotNull CommandSender sender, @NotNull String key) { @@ -314,7 +328,7 @@ public class OpenInv extends JavaPlugin implements IOpenInv { private @Nullable String getLocale(@NotNull CommandSender sender) { if (sender instanceof Player) { - return this.accessor.getPlayerDataManager().getLocale((Player) sender); + return ((Player) sender).getLocale(); } else { return this.getConfig().getString("settings.locale", "en_us"); } diff --git a/plugin/src/main/java/com/lishid/openinv/internal/IPlayerDataManager.java b/plugin/src/main/java/com/lishid/openinv/internal/IPlayerDataManager.java index b217479..1a81491 100644 --- a/plugin/src/main/java/com/lishid/openinv/internal/IPlayerDataManager.java +++ b/plugin/src/main/java/com/lishid/openinv/internal/IPlayerDataManager.java @@ -32,16 +32,14 @@ public interface IPlayerDataManager { * @param offline the OfflinePlayer * @return the Player loaded */ - @Nullable - Player loadPlayer(@NotNull OfflinePlayer offline); + @Nullable Player loadPlayer(@NotNull OfflinePlayer offline); /** * Creates a new Player from an existing one that will function slightly better offline. * * @return the Player */ - @NotNull - Player inject(@NotNull Player player); + @NotNull Player inject(@NotNull Player player); /** * Opens an ISpecialInventory for a Player. @@ -51,8 +49,7 @@ public interface IPlayerDataManager { *` * @return the InventoryView opened */ - @Nullable - InventoryView openInventory(@NotNull Player player, @NotNull ISpecialInventory inventory); + @Nullable InventoryView openInventory(@NotNull Player player, @NotNull ISpecialInventory inventory); /** * Convert a raw slot number into a player inventory slot number. @@ -66,11 +63,4 @@ public interface IPlayerDataManager { */ int convertToPlayerSlot(InventoryView view, int rawSlot); - void sendSystemMessage(@NotNull Player player, @NotNull String message); - - @NotNull - default String getLocale(Player player) { - return player.getLocale(); - } - }