diff --git a/src/main/java/com/linyuanlin/minecraft/Manager/DiscordBotManager.java b/src/main/java/com/linyuanlin/minecraft/Manager/DiscordBotManager.java index 2cc91a3..c85dfef 100644 --- a/src/main/java/com/linyuanlin/minecraft/Manager/DiscordBotManager.java +++ b/src/main/java/com/linyuanlin/minecraft/Manager/DiscordBotManager.java @@ -1,6 +1,7 @@ package com.linyuanlin.minecraft.manager; import com.linyuanlin.minecraft.App; +import org.bukkit.plugin.java.JavaPlugin; import com.tjplaysnow.discord.object.Bot; import com.tjplaysnow.discord.object.ThreadSpigot; import net.dv8tion.jda.api.entities.TextChannel; @@ -13,9 +14,13 @@ public class DiscordBotManager { // Key: Bot Tag, Value: Bot private final HashMap bots = new HashMap<>(); + public static App getPlugin() { + return JavaPlugin.getPlugin(App.class); + } + public void registerNewBot(String tag, String token) { Bot b = new Bot(token, tag); - b.setBotThread(new ThreadSpigot(App.getPlugin())); + b.setBotThread(new ThreadSpigot(getPlugin())); bots.put(tag, b); } @@ -32,20 +37,20 @@ public void shutDownAllBot() { public void sendMessage(String botTag, String channelTag, String message) { Bot bot = bots.get(botTag); if (bot == null) { - App.getPlugin().getLogger().warning("Cannot let bot " + botTag + " send message to channel " + channelTag + getPlugin().getLogger().warning("Cannot let bot " + botTag + " send message to channel " + channelTag + " because bot is not exist."); return; } String cId = textChannels.get(channelTag); if (cId == null) { - App.getPlugin().getLogger().warning("Cannot let bot " + botTag + " send message to channel " + channelTag + getPlugin().getLogger().warning("Cannot let bot " + botTag + " send message to channel " + channelTag + " because channel is not exist."); return; } TextChannel c = bot.getBot().getTextChannelById(cId); if (c == null) { - App.getPlugin().getLogger().warning("Cannot let bot " + botTag + " send message to channel " + channelTag + getPlugin().getLogger().warning("Cannot let bot " + botTag + " send message to channel " + channelTag + " because channel is not exist."); return; } diff --git a/src/main/java/com/linyuanlin/minecraft/Manager/GuildManager.java b/src/main/java/com/linyuanlin/minecraft/Manager/GuildManager.java index 12e28ef..4cd9ccd 100644 --- a/src/main/java/com/linyuanlin/minecraft/Manager/GuildManager.java +++ b/src/main/java/com/linyuanlin/minecraft/Manager/GuildManager.java @@ -6,6 +6,7 @@ import org.bukkit.*; import org.bukkit.command.*; import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; @@ -13,15 +14,19 @@ import java.util.*; public class GuildManager implements CommandExecutor { + public static App getPlugin() { + return JavaPlugin.getPlugin(App.class); + } + public GuildManager() { - Objects.requireNonNull(App.getPlugin().getCommand("guild")).setExecutor(this); + Objects.requireNonNull(getPlugin().getCommand("guild")).setExecutor(this); } @Override public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { try { - PlayerData p = App.getPlugin().allPlayers.get(((Player) sender).getUniqueId()); + PlayerData p = getPlugin().allPlayers.get(((Player) sender).getUniqueId()); if (p == null) { return false; } @@ -40,7 +45,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); - App.getPlugin().discordBotManager.sendMessage("TEST", "Project-Minecraft", sw.toString()); + getPlugin().discordBotManager.sendMessage("TEST", "Project-Minecraft", sw.toString()); return false; } } diff --git a/src/main/java/com/linyuanlin/minecraft/Manager/LocationManager.java b/src/main/java/com/linyuanlin/minecraft/Manager/LocationManager.java index 266cfa9..f0337ce 100644 --- a/src/main/java/com/linyuanlin/minecraft/Manager/LocationManager.java +++ b/src/main/java/com/linyuanlin/minecraft/Manager/LocationManager.java @@ -4,6 +4,7 @@ import com.mongodb.client.model.Filters; import java.util.*; import org.bukkit.*; +import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.NotNull; import org.bson.Document; @@ -14,13 +15,17 @@ public class LocationManager { public final static String lobby_spawn = "lobby_spawn"; public final static String house_spawn = "house_spawn"; + public static App getPlugin() { + return JavaPlugin.getPlugin(App.class); + } + private HashMap tagLocationMap = new HashMap<>();; public void loadLocations() { - List docList = App.getPlugin().dbClient.findMany("Location", Filters.empty()); + List docList = getPlugin().dbClient.findMany("Location", Filters.empty()); for (Document doc : docList) { tagLocationMap.put(doc.getString("tag"), this.getLocation( - App.getPlugin().worldManager.getWorldData(WorldManager.world_lobby).getWorld(), + getPlugin().worldManager.getWorldData(WorldManager.world_lobby).getWorld(), doc)); } } diff --git a/src/main/java/com/linyuanlin/minecraft/Manager/TeamManager.java b/src/main/java/com/linyuanlin/minecraft/Manager/TeamManager.java index 435c9f0..ce684f3 100644 --- a/src/main/java/com/linyuanlin/minecraft/Manager/TeamManager.java +++ b/src/main/java/com/linyuanlin/minecraft/Manager/TeamManager.java @@ -7,6 +7,7 @@ import org.bukkit.*; import org.bukkit.command.*; import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.NotNull; import java.io.*; @@ -18,15 +19,19 @@ public class TeamManager implements CommandExecutor { */ public final static int INVITE_COOLING_MINS = 1; + public static App getPlugin() { + return JavaPlugin.getPlugin(App.class); + } + public TeamManager() { - Objects.requireNonNull(App.getPlugin().getCommand("team")).setExecutor(this); + Objects.requireNonNull(getPlugin().getCommand("team")).setExecutor(this); } @Override public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { try { - PlayerData p = App.getPlugin().allPlayers.get(((Player) sender).getUniqueId()); + PlayerData p = getPlugin().allPlayers.get(((Player) sender).getUniqueId()); if (p == null) { return false; } @@ -47,7 +52,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); - App.getPlugin().discordBotManager.sendMessage("TEST", "Project-Minecraft", sw.toString()); + getPlugin().discordBotManager.sendMessage("TEST", "Project-Minecraft", sw.toString()); return false; } } @@ -88,14 +93,14 @@ private boolean invite(PlayerData senderPlayer, String[] args) { return false; } - PlayerData receiverPlayer = App.getPlugin().allPlayers.get(p.getUniqueId()); + PlayerData receiverPlayer = getPlugin().allPlayers.get(p.getUniqueId()); if (receiverPlayer == null) { senderPlayer.player().sendMessage("你邀請的玩家 " + ChatColor.GOLD + args[1] + ChatColor.WHITE + " 不存在於allPlayers中,請聯繫開發人員"); return false; } - Optional team = App.getPlugin().allPlayers.get(receiverPlayer.player().getUniqueId()).team(); + Optional team = getPlugin().allPlayers.get(receiverPlayer.player().getUniqueId()).team(); if (receiverPlayer == senderPlayer) { senderPlayer.player().sendMessage(ChatColor.RED + "不能邀請自己"); @@ -163,7 +168,7 @@ private boolean join(PlayerData senderPlayer, String[] args) throws Exception { "你要加入的隊伍的邀請人 " + ChatColor.GOLD + args[1] + ChatColor.WHITE + " 不存在或是不在線上!"); return false; } - PlayerData receiverPlayer = App.getPlugin().allPlayers.get(p.getUniqueId()); + PlayerData receiverPlayer = getPlugin().allPlayers.get(p.getUniqueId()); if (receiverPlayer == null) { senderPlayer.player().sendMessage("你要加入的隊伍的邀請人" + ChatColor.GOLD + args[1] + ChatColor.WHITE + "不存在於allPlayers中, 請聯繫開發人員"); diff --git a/src/main/java/com/linyuanlin/minecraft/Manager/TradeManager.java b/src/main/java/com/linyuanlin/minecraft/Manager/TradeManager.java index 81e789a..54e7dda 100644 --- a/src/main/java/com/linyuanlin/minecraft/Manager/TradeManager.java +++ b/src/main/java/com/linyuanlin/minecraft/Manager/TradeManager.java @@ -5,6 +5,7 @@ import org.bukkit.*; import org.bukkit.command.*; import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.NotNull; import java.util.Objects; @@ -15,8 +16,12 @@ public class TradeManager implements CommandExecutor { * constants */ + public static App getPlugin() { + return JavaPlugin.getPlugin(App.class); + } + public TradeManager() { - Objects.requireNonNull(App.getPlugin().getCommand("trade")).setExecutor(this); + Objects.requireNonNull(getPlugin().getCommand("trade")).setExecutor(this); } @Override @@ -24,7 +29,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command @NotNull String[] args) { try { - PlayerData p = App.getPlugin().allPlayers.get(((Player) sender).getUniqueId()); + PlayerData p = getPlugin().allPlayers.get(((Player) sender).getUniqueId()); if (p == null) { return false; @@ -42,7 +47,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e.printStackTrace(pw); - App.getPlugin().discordBotManager.sendMessage("TEST", "Project-Minecraft", sw.toString()); + getPlugin().discordBotManager.sendMessage("TEST", "Project-Minecraft", sw.toString()); return false; } } @@ -80,7 +85,7 @@ private boolean pay(PlayerData senderPlayer, String[] args) { return false; } - PlayerData receiverPlayer = App.getPlugin().allPlayers.get(p.getUniqueId()); + PlayerData receiverPlayer = getPlugin().allPlayers.get(p.getUniqueId()); if (receiverPlayer == null) { senderPlayer.player().sendMessage( "玩家 " + ChatColor.GOLD + receiverName + ChatColor.WHITE + " 不存在或是不在線上"); diff --git a/src/main/java/com/linyuanlin/minecraft/PluginMessageHandler.java b/src/main/java/com/linyuanlin/minecraft/PluginMessageHandler.java index caa078d..3dc161d 100644 --- a/src/main/java/com/linyuanlin/minecraft/PluginMessageHandler.java +++ b/src/main/java/com/linyuanlin/minecraft/PluginMessageHandler.java @@ -7,12 +7,17 @@ import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.messaging.PluginMessageListener; import org.jetbrains.annotations.NotNull; import java.util.Objects; public class PluginMessageHandler implements PluginMessageListener { + public static App getPlugin() { + return JavaPlugin.getPlugin(App.class); + } + @Override public void onPluginMessageReceived(String channel, @NotNull Player player, byte[] message) { Bukkit.getConsoleSender().sendMessage("receive plugin message"); @@ -49,7 +54,7 @@ public void sendPluginMessage(String subChannel, String PluginMessage) { out.writeUTF(subChannel); out.writeUTF(PluginMessage); Bukkit.getConsoleSender().sendMessage("send plugin message: " + subChannel + ' ' + PluginMessage); - Objects.requireNonNull(Iterables.getFirst(Bukkit.getOnlinePlayers(), null)).sendPluginMessage(App.getPlugin(), + Objects.requireNonNull(Iterables.getFirst(Bukkit.getOnlinePlayers(), null)).sendPluginMessage(getPlugin(), "BungeeCord", out.toByteArray()); } diff --git a/src/main/java/com/linyuanlin/minecraft/mongodb/MongodbClient.java b/src/main/java/com/linyuanlin/minecraft/mongodb/MongodbClient.java index 1817551..b9d8175 100644 --- a/src/main/java/com/linyuanlin/minecraft/mongodb/MongodbClient.java +++ b/src/main/java/com/linyuanlin/minecraft/mongodb/MongodbClient.java @@ -5,17 +5,22 @@ import com.mongodb.client.*; import org.bson.Document; import org.bson.conversions.Bson; +import org.bukkit.plugin.java.JavaPlugin; import java.util.*; public class MongodbClient { + public static App getPlugin() { + return JavaPlugin.getPlugin(App.class); + } + public FindIterable cursor; private MongoClient client; private MongoDatabase database; public MongodbClient(String databaseName) { try { - this.client = MongoClients.create(App.getPlugin().mongodbConnectString); + this.client = MongoClients.create(getPlugin().mongodbConnectString); this.database = this.client.getDatabase(databaseName); } catch (Exception e) { e.printStackTrace();