|
| 1 | +package com.ghostchu.quickshop.addon.displaycontrol; |
| 2 | + |
| 3 | +import com.ghostchu.quickshop.QuickShop; |
| 4 | +import com.ghostchu.quickshop.addon.displaycontrol.bean.ClientType; |
| 5 | +import com.ghostchu.quickshop.addon.displaycontrol.bean.DisplayOption; |
| 6 | +import com.ghostchu.quickshop.addon.displaycontrol.command.SubCommand_DisplayControl; |
| 7 | +import com.ghostchu.quickshop.addon.displaycontrol.database.DisplayControlDatabaseHelper; |
| 8 | +import com.ghostchu.quickshop.api.command.CommandContainer; |
| 9 | +import com.ghostchu.quickshop.api.event.DisplayApplicableCheckEvent; |
| 10 | +import com.ghostchu.quickshop.util.PackageUtil; |
| 11 | +import com.ghostchu.quickshop.util.Util; |
| 12 | +import com.ghostchu.quickshop.util.logger.Log; |
| 13 | +import com.google.common.io.ByteArrayDataInput; |
| 14 | +import com.google.common.io.ByteStreams; |
| 15 | +import org.bukkit.Bukkit; |
| 16 | +import org.bukkit.entity.Player; |
| 17 | +import org.bukkit.event.EventHandler; |
| 18 | +import org.bukkit.event.EventPriority; |
| 19 | +import org.bukkit.event.HandlerList; |
| 20 | +import org.bukkit.event.Listener; |
| 21 | +import org.bukkit.event.player.AsyncPlayerPreLoginEvent; |
| 22 | +import org.bukkit.event.player.PlayerQuitEvent; |
| 23 | +import org.bukkit.plugin.Plugin; |
| 24 | +import org.bukkit.plugin.java.JavaPlugin; |
| 25 | +import org.bukkit.plugin.messaging.PluginMessageListener; |
| 26 | +import org.jetbrains.annotations.NotNull; |
| 27 | + |
| 28 | +import java.sql.SQLException; |
| 29 | +import java.util.Map; |
| 30 | +import java.util.UUID; |
| 31 | +import java.util.concurrent.ConcurrentHashMap; |
| 32 | +import java.util.logging.Level; |
| 33 | + |
| 34 | +public final class Main extends JavaPlugin implements Listener, PluginMessageListener { |
| 35 | + private static final String BUNGEE_CHANNEL = "quickshopcompat:bcgeyser"; |
| 36 | + private static final String RESPONSE_PREFIX = "CLIENTTYPE"; |
| 37 | + static Main instance; |
| 38 | + private final Map<UUID, ClientType> playerClientMapping = new ConcurrentHashMap<>(); |
| 39 | + private final Map<UUID, DisplayOption> playerDisplayStatus = new ConcurrentHashMap<>(); |
| 40 | + private QuickShop plugin; |
| 41 | + private DisplayControlDatabaseHelper databaseHelper; |
| 42 | + |
| 43 | + @Override |
| 44 | + public void onLoad() { |
| 45 | + instance = this; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public void onDisable() { |
| 50 | + HandlerList.unregisterAll((Plugin) this); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void onEnable() { |
| 55 | + saveDefaultConfig(); |
| 56 | + plugin = QuickShop.getInstance(); |
| 57 | + try { |
| 58 | + databaseHelper = new DisplayControlDatabaseHelper(instance, plugin.getSqlManager(), plugin.getDbPrefix()); |
| 59 | + } catch (SQLException e) { |
| 60 | + getLogger().log(Level.WARNING, "Failed to init database helper", e); |
| 61 | + Bukkit.getPluginManager().disablePlugin(this); |
| 62 | + return; |
| 63 | + } |
| 64 | + Bukkit.getPluginManager().registerEvents(this, this); |
| 65 | + Bukkit.getScheduler().runTaskTimerAsynchronously(this, () -> this.playerClientMapping.entrySet().removeIf(e -> Bukkit.getPlayer(e.getKey()) == null), 60 * 20 * 60, 60 * 20 * 60); |
| 66 | + plugin.getCommandManager().registerCmd(CommandContainer.builder() |
| 67 | + .prefix("displaycontrol") |
| 68 | + .permission("quickshopaddon.displaycontrol.use") |
| 69 | + .description((locale) -> plugin.text().of("addon.displaycontrol.command.displaycontrol").forLocale(locale)) |
| 70 | + .executor(new SubCommand_DisplayControl(plugin, this)) |
| 71 | + .build()); |
| 72 | + getLogger().info("BungeeCord: " + Util.checkIfBungee()); |
| 73 | + if (Util.checkIfBungee()) { |
| 74 | + getLogger().info("Detected BungeeCord, register the BungeeCord client information forward module, you will need install Compat-BungeeCord-Geyser module to make this feature work."); |
| 75 | + Bukkit.getMessenger().registerIncomingPluginChannel |
| 76 | + (this, BUNGEE_CHANNEL, this); // we register the incoming channel |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) |
| 81 | + public void playerLogin(AsyncPlayerPreLoginEvent event) { |
| 82 | + UUID uuid = event.getUniqueId(); |
| 83 | + try { |
| 84 | + DisplayOption displayOption = databaseHelper.getDisplayOption(uuid); |
| 85 | + this.playerDisplayStatus.put(uuid, displayOption); |
| 86 | + } catch (SQLException e) { |
| 87 | + getLogger().log(Level.WARNING, "Failed to getting the player display status from database", e); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) |
| 92 | + public void playerQuit(PlayerQuitEvent event) { |
| 93 | + //noinspection ConstantConditions |
| 94 | + cleanup(event.getPlayer().getUniqueId()); |
| 95 | + } |
| 96 | + |
| 97 | + private void cleanup(UUID uuid) { |
| 98 | + this.playerDisplayStatus.remove(uuid); |
| 99 | + this.playerClientMapping.remove(uuid); |
| 100 | + } |
| 101 | + |
| 102 | + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) |
| 103 | + public void displaySending(DisplayApplicableCheckEvent event) { |
| 104 | + UUID uuid = event.getPlayer(); |
| 105 | + DisplayOption option = this.playerDisplayStatus.getOrDefault(uuid, DisplayOption.AUTO); |
| 106 | + boolean bedrockClient = this.playerClientMapping.getOrDefault(uuid, ClientType.UNDISCOVERED).isBedrockType(); |
| 107 | + if (!option.isDisplayAvailable(bedrockClient)) { |
| 108 | + event.setApplicable(false); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + public DisplayControlDatabaseHelper getDatabaseHelper() { |
| 113 | + return databaseHelper; |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, @NotNull byte[] message) { |
| 118 | + if (!channel.equalsIgnoreCase(BUNGEE_CHANNEL)) return; |
| 119 | + ByteArrayDataInput in = ByteStreams.newDataInput(message); |
| 120 | + String prefix = in.readUTF(); |
| 121 | + //noinspection SwitchStatementWithTooFewBranches |
| 122 | + switch (prefix) { |
| 123 | + case RESPONSE_PREFIX -> handleBungeeBedrockPlayerCallback(in); |
| 124 | + default -> getLogger().log(Level.WARNING, "Unrecognized type: " + prefix); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private void handleBungeeBedrockPlayerCallback(ByteArrayDataInput in) { |
| 129 | + if (!PackageUtil.parsePackageProperly("acceptBungeeCordBedrockPlayerDiscoveryCallback").asBoolean(true)) { |
| 130 | + return; |
| 131 | + } |
| 132 | + UUID uuid = UUID.fromString(in.readUTF()); |
| 133 | + int responseType = in.readShort(); |
| 134 | + ClientType clientType = ClientType.UNDISCOVERED; |
| 135 | + if (responseType == 0) { |
| 136 | + clientType = ClientType.JAVA_EDITION_PLAYER; |
| 137 | + } |
| 138 | + if (responseType == 1) { |
| 139 | + clientType = ClientType.BEDROCK_EDITION_PLAYER_GEYSER; |
| 140 | + } |
| 141 | + if (responseType == 2) { |
| 142 | + clientType = ClientType.BEDROCK_EDITION_PLAYER_FLOODGATE; |
| 143 | + } |
| 144 | + Log.debug("Player " + uuid + " client type check callback: " + clientType.name() + ", isBedrockClient: " + clientType.isBedrockType()); |
| 145 | + if (this.playerClientMapping.getOrDefault(uuid, ClientType.UNDISCOVERED).isWaitingDiscover()) { |
| 146 | + Log.debug("[BUNGEE-CALLBACK] Discovered player " + uuid + " client type is: " + clientType.name()); |
| 147 | + this.playerClientMapping.put(uuid, clientType); |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments