From 8ff45ad676d1d0d1330603f19df25afde88115e5 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sat, 2 Mar 2019 19:02:01 +0100 Subject: [PATCH] Some fixes Crash on player join without Towny fixed --- .../java/buttondevteam/chat/ChatProcessing.java | 4 ++-- .../chat/components/chatonly/ChatOnlyComponent.java | 9 +++++---- .../components/towncolors/TownColorComponent.java | 13 +++++++++++-- .../chat/listener/PlayerJoinLeaveListener.java | 10 +--------- .../buttondevteam/chat/listener/PlayerListener.java | 12 +++++------- src/test/java/buttondevteam/chat/ChatFormatIT.java | 2 +- 6 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/main/java/buttondevteam/chat/ChatProcessing.java b/src/main/java/buttondevteam/chat/ChatProcessing.java index b9afa17..6028b8a 100644 --- a/src/main/java/buttondevteam/chat/ChatProcessing.java +++ b/src/main/java/buttondevteam/chat/ChatProcessing.java @@ -144,7 +144,7 @@ public class ChatProcessing { }).build()); } pingedconsole = false; // Will set it to true onmatch (static constructor) - final String channelidentifier = getChannelID(channel, sender, e.getOrigin()); + final String channelidentifier = getChannelID(channel, e.getOrigin()); TellrawPart json = createTellraw(sender, message, player, mp, e.getUser(), channelidentifier, e.getOrigin()); long combinetime = System.nanoTime(); @@ -238,7 +238,7 @@ public class ChatProcessing { return player.getDisplayName(); } - static String getChannelID(Channel channel, CommandSender sender, String origin) { + static String getChannelID(Channel channel, String origin) { return ("[" + (MCORIGIN.equals(origin) ? "" : "§8" + origin.substring(0, 1) + "§r|") + channel.DisplayName().get()) + "]"; } diff --git a/src/main/java/buttondevteam/chat/components/chatonly/ChatOnlyComponent.java b/src/main/java/buttondevteam/chat/components/chatonly/ChatOnlyComponent.java index dce2089..7f17848 100644 --- a/src/main/java/buttondevteam/chat/components/chatonly/ChatOnlyComponent.java +++ b/src/main/java/buttondevteam/chat/components/chatonly/ChatOnlyComponent.java @@ -7,8 +7,8 @@ import buttondevteam.core.ComponentManager; import buttondevteam.lib.architecture.Component; import buttondevteam.lib.player.TBMCPlayer; import buttondevteam.lib.player.TBMCPlayerJoinEvent; +import lombok.val; import org.bukkit.GameMode; -import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; @@ -27,9 +27,10 @@ public class ChatOnlyComponent extends Component implements Listener { } - public static void playerJoin(ChatPlayer cp, Player p) { - if(!ComponentManager.isEnabled(ChatOnlyComponent.class)) - return; + @EventHandler + public void playerJoin(TBMCPlayerJoinEvent event) { + val p = event.getPlayer(); + val cp = event.GetPlayer().asPluginPlayer(ChatPlayer.class); if (cp.ChatOnly || p.getGameMode().equals(GameMode.SPECTATOR)) { cp.ChatOnly = false; p.setGameMode(GameMode.SURVIVAL); diff --git a/src/main/java/buttondevteam/chat/components/towncolors/TownColorComponent.java b/src/main/java/buttondevteam/chat/components/towncolors/TownColorComponent.java index c7917fa..663768c 100644 --- a/src/main/java/buttondevteam/chat/components/towncolors/TownColorComponent.java +++ b/src/main/java/buttondevteam/chat/components/towncolors/TownColorComponent.java @@ -10,6 +10,7 @@ import buttondevteam.lib.architecture.Component; import buttondevteam.lib.architecture.ComponentMetadata; import buttondevteam.lib.architecture.ConfigData; import buttondevteam.lib.chat.Color; +import buttondevteam.lib.player.TBMCPlayerJoinEvent; import com.earth2me.essentials.User; import com.palmergames.bukkit.towny.exceptions.NotRegisteredException; import com.palmergames.bukkit.towny.object.Nation; @@ -20,6 +21,8 @@ import org.bukkit.ChatColor; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; import org.dynmap.towny.DTBridge; import org.dynmap.towny.DynmapTownyPlugin; @@ -32,7 +35,7 @@ import java.util.function.Function; import java.util.stream.Collectors; @ComponentMetadata(depends = TownyComponent.class) -public class TownColorComponent extends Component { +public class TownColorComponent extends Component implements Listener { /** * Names lowercased */ @@ -98,6 +101,7 @@ public class TownColorComponent extends Component { registerCommand(new buttondevteam.chat.components.towncolors.admin.NationColorCommand()); registerCommand(new TCCount()); registerListener(new TownyListener()); + registerListener(this); } @Override @@ -181,7 +185,7 @@ public class TownColorComponent extends Component { /** * Checks if the component is enabled */ - public static void updatePlayerColors(Player player, ChatPlayer cp) { //Probably at join - nop, nicknames + private static void updatePlayerColors(Player player, ChatPlayer cp) { //Probably at join - nop, nicknames if (!ComponentManager.isEnabled(TownColorComponent.class)) return; User user = PluginMain.essentials.getUser(player); @@ -190,6 +194,11 @@ public class TownColorComponent extends Component { cp.FlairUpdate(); //Update in list } + @EventHandler + public void onPlayerJoin(TBMCPlayerJoinEvent event) { + updatePlayerColors(event.getPlayer(), event.GetPlayer().asPluginPlayer(ChatPlayer.class)); + } + private static void load_old(Consumer loadTC, Consumer loadNC) { PluginMain.Instance.getLogger().info("Loading files..."); diff --git a/src/main/java/buttondevteam/chat/listener/PlayerJoinLeaveListener.java b/src/main/java/buttondevteam/chat/listener/PlayerJoinLeaveListener.java index 7f726bd..b8390b1 100644 --- a/src/main/java/buttondevteam/chat/listener/PlayerJoinLeaveListener.java +++ b/src/main/java/buttondevteam/chat/listener/PlayerJoinLeaveListener.java @@ -3,17 +3,13 @@ package buttondevteam.chat.listener; import buttondevteam.chat.ChatPlayer; import buttondevteam.chat.PlayerJoinTimerTask; import buttondevteam.chat.PluginMain; -import buttondevteam.chat.components.chatonly.ChatOnlyComponent; -import buttondevteam.chat.components.fun.UnlolCommand; import buttondevteam.chat.commands.ucmds.HistoryCommand; import buttondevteam.chat.components.flair.FlairComponent; import buttondevteam.chat.components.flair.FlairStates; -import buttondevteam.chat.components.towncolors.TownColorComponent; import buttondevteam.core.ComponentManager; import buttondevteam.lib.player.TBMCPlayerJoinEvent; import buttondevteam.lib.player.TBMCPlayerLoadEvent; import buttondevteam.lib.player.TBMCPlayerSaveEvent; -import org.bukkit.GameMode; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -59,11 +55,7 @@ public class PlayerJoinLeaveListener implements Listener { nwithoutformatting = nwithoutformatting.replace("§" + nwithoutformatting.charAt(index + 1), ""); } else nwithoutformatting = p.getName(); - PlayerListener.nicknames.forcePut(nwithoutformatting.toLowerCase(), p.getUniqueId()); - - TownColorComponent.updatePlayerColors(p, cp); //TO!DO: Doesn't have effect - It can help to register the listener - - ChatOnlyComponent.playerJoin(cp, p); + PlayerListener.nicknames.forcePut(nwithoutformatting.toLowerCase(), p.getUniqueId()); //TODO: FormatterComponent HistoryCommand.showHistory(e.getPlayer(), "u history", new String[0], null); } diff --git a/src/main/java/buttondevteam/chat/listener/PlayerListener.java b/src/main/java/buttondevteam/chat/listener/PlayerListener.java index 8dee232..84f7fc1 100644 --- a/src/main/java/buttondevteam/chat/listener/PlayerListener.java +++ b/src/main/java/buttondevteam/chat/listener/PlayerListener.java @@ -18,7 +18,6 @@ import buttondevteam.lib.chat.ChatMessage; import buttondevteam.lib.chat.TBMCChatAPI; import buttondevteam.lib.player.ChromaGamerBase; import buttondevteam.lib.player.ChromaGamerBase.InfoTarget; -import buttondevteam.lib.player.TBMCPlayer; import buttondevteam.lib.player.TBMCPlayerGetInfoEvent; import com.google.common.collect.BiMap; import com.google.common.collect.HashBiMap; @@ -52,8 +51,7 @@ public class PlayerListener implements Listener { public void onPlayerChat(AsyncPlayerChatEvent event) { if (event.isCancelled()) return; - ChatPlayer cp = TBMCPlayer.getPlayer(event.getPlayer().getUniqueId(), ChatPlayer.class); - TBMCChatAPI.SendChatMessage(ChatMessage.builder(event.getPlayer(), cp, event.getMessage()).build()); + //The custom event is called in the core, but doesn't cancel the MC event event.setCancelled(true); // The custom event should only be cancelled when muted or similar } @@ -170,12 +168,12 @@ public class PlayerListener implements Listener { HistoryCommand.addChatMessage(e.getCm(), e.getChannel()); e.setCancelled(ChatProcessing.ProcessChat(e)); } catch (NoClassDefFoundError | Exception ex) { // Weird things can happen + val str = "§c!§r[" + e.getChannel().DisplayName().get() + "] <" + + ThorpeUtils.getDisplayName(e.getSender()) + "> " + e.getMessage(); for (Player p : Bukkit.getOnlinePlayers()) if (e.shouldSendTo(p)) - p.sendMessage("§c!§r[" - + e.getChannel().DisplayName().get() + "] <" + (e.getSender() instanceof Player - ? ((Player) e.getSender()).getDisplayName() : e.getSender().getName()) - + "> " + e.getMessage()); + p.sendMessage(str); + Bukkit.getConsoleSender().sendMessage(str); TBMCCoreAPI.SendException("An error occured while processing a chat message!", ex); } } diff --git a/src/test/java/buttondevteam/chat/ChatFormatIT.java b/src/test/java/buttondevteam/chat/ChatFormatIT.java index 4531c16..839942f 100644 --- a/src/test/java/buttondevteam/chat/ChatFormatIT.java +++ b/src/test/java/buttondevteam/chat/ChatFormatIT.java @@ -92,7 +92,7 @@ public class ChatFormatIT { @Test public void testMessage() { ArrayList cfs = ChatProcessing.addFormatters(Color.White); - final String chid = ChatProcessing.getChannelID(Channel.GlobalChat, sender, ChatProcessing.MCORIGIN); + final String chid = ChatProcessing.getChannelID(Channel.GlobalChat, ChatProcessing.MCORIGIN); final TellrawPart tp = ChatProcessing.createTellraw(sender, message, null, null, null, chid, ChatProcessing.MCORIGIN); ChatFormatter.Combine(cfs, message, tp); System.out.println("Testing: " + message);