From 3124d41f54a3009e808af611111c7afa7e5a240d Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Thu, 25 Oct 2018 14:13:56 +0200 Subject: [PATCH] AppendText, F and other user fixes #88 TBMCPlugins/DiscordPlugin#61 --- .../java/buttondevteam/chat/ChatPlayer.java | 2 - .../appendtext/AppendTextCommandBase.java | 31 +++----- .../ucmds/admin/NationColorCommand.java | 2 +- .../chat/listener/PlayerListener.java | 71 ++++++++----------- 4 files changed, 42 insertions(+), 64 deletions(-) diff --git a/src/main/java/buttondevteam/chat/ChatPlayer.java b/src/main/java/buttondevteam/chat/ChatPlayer.java index d4b3db5..2ffa48d 100644 --- a/src/main/java/buttondevteam/chat/ChatPlayer.java +++ b/src/main/java/buttondevteam/chat/ChatPlayer.java @@ -1,6 +1,5 @@ package buttondevteam.chat; -import buttondevteam.lib.chat.Channel; import buttondevteam.lib.chat.Color; import buttondevteam.lib.player.EnumPlayerData; import buttondevteam.lib.player.PlayerClass; @@ -50,7 +49,6 @@ public class ChatPlayer extends TBMCPlayerBase { public Location SavedLocation; public boolean Working; // public int Tables = 10; - public Channel CurrentChannel = Channel.GlobalChat; public boolean SendingLink = false; public boolean RainbowPresserColorMode = false; public Color OtherColorMode = null; diff --git a/src/main/java/buttondevteam/chat/commands/appendtext/AppendTextCommandBase.java b/src/main/java/buttondevteam/chat/commands/appendtext/AppendTextCommandBase.java index 129adef..74350d6 100644 --- a/src/main/java/buttondevteam/chat/commands/appendtext/AppendTextCommandBase.java +++ b/src/main/java/buttondevteam/chat/commands/appendtext/AppendTextCommandBase.java @@ -1,13 +1,12 @@ package buttondevteam.chat.commands.appendtext; -import buttondevteam.chat.ChatPlayer; -import buttondevteam.chat.listener.PlayerListener; -import buttondevteam.lib.chat.*; +import buttondevteam.lib.chat.ChatMessage; +import buttondevteam.lib.chat.CommandClass; +import buttondevteam.lib.chat.TBMCChatAPI; +import buttondevteam.lib.chat.TBMCCommandBase; +import buttondevteam.lib.player.ChromaGamerBase; import buttondevteam.lib.player.TBMCPlayer; import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -import java.util.UUID; @CommandClass(modOnly = false, excludeFromPath = true) public abstract class AppendTextCommandBase extends TBMCCommandBase { @@ -18,20 +17,12 @@ public abstract class AppendTextCommandBase extends TBMCCommandBase { @Override public boolean OnCommand(CommandSender sender, String alias, String[] args) { - String msg = GetAppendedText(); - for (int i = args.length - 1; i >= 0; i--) - msg = args[i] + " " + msg; - ChatPlayer cp; - if (sender instanceof Player) - TBMCChatAPI.SendChatMessage(ChatMessage.builder( - (cp = TBMCPlayer.getPlayer(((Player) sender).getUniqueId(), ChatPlayer.class)).CurrentChannel, sender, - cp, msg).fromCommand(true).build()); - else if (sender.isOp()) - TBMCChatAPI.SendChatMessage(ChatMessage.builder(PlayerListener.ConsoleChannel, sender, - (cp = TBMCPlayer.getPlayer(new UUID(0, 0), ChatPlayer.class)), msg).fromCommand(true).build()); - else - TBMCChatAPI.SendChatMessage(ChatMessage.builder(Channel.GlobalChat, sender, - (cp = TBMCPlayer.getPlayer(new UUID(0, 0), ChatPlayer.class)), msg).fromCommand(true).build()); //TODO + StringBuilder msg = new StringBuilder(); + for (String arg : args) msg.append(arg).append(" "); + msg.append(GetAppendedText()); + TBMCChatAPI.SendChatMessage(ChatMessage.builder(sender, + ChromaGamerBase.getFromSender(sender, TBMCPlayer.class), msg.toString()) + .fromCommand(true).build()); return true; } } diff --git a/src/main/java/buttondevteam/chat/commands/ucmds/admin/NationColorCommand.java b/src/main/java/buttondevteam/chat/commands/ucmds/admin/NationColorCommand.java index 19d0129..c2dd682 100644 --- a/src/main/java/buttondevteam/chat/commands/ucmds/admin/NationColorCommand.java +++ b/src/main/java/buttondevteam/chat/commands/ucmds/admin/NationColorCommand.java @@ -1,4 +1,4 @@ package buttondevteam.chat.commands.ucmds.admin; -public class NationColorCommand { +public class NationColorCommand { //TODO } diff --git a/src/main/java/buttondevteam/chat/listener/PlayerListener.java b/src/main/java/buttondevteam/chat/listener/PlayerListener.java index 1c379a8..aebd816 100644 --- a/src/main/java/buttondevteam/chat/listener/PlayerListener.java +++ b/src/main/java/buttondevteam/chat/listener/PlayerListener.java @@ -6,6 +6,7 @@ import buttondevteam.chat.PluginMain; import buttondevteam.lib.TBMCChatEvent; import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.chat.*; +import buttondevteam.lib.player.ChromaGamerBase; import buttondevteam.lib.player.ChromaGamerBase.InfoTarget; import buttondevteam.lib.player.TBMCPlayer; import buttondevteam.lib.player.TBMCPlayerGetInfoEvent; @@ -13,6 +14,7 @@ import com.google.common.collect.BiMap; import com.google.common.collect.HashBiMap; import com.vexsoftware.votifier.model.Vote; import com.vexsoftware.votifier.model.VotifierEvent; +import lombok.val; import net.ess3.api.events.NickChangeEvent; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -28,12 +30,14 @@ import org.bukkit.event.server.ServerCommandEvent; import org.bukkit.help.HelpTopic; import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; +import org.bukkit.scheduler.BukkitTask; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; import java.util.Map.Entry; +import java.util.Random; +import java.util.UUID; import java.util.function.BiPredicate; -import java.util.function.Consumer; -import java.util.function.Supplier; public class PlayerListener implements Listener { /** @@ -57,7 +61,7 @@ public class PlayerListener implements Listener { if (event.isCancelled()) return; ChatPlayer cp = TBMCPlayer.getPlayer(event.getPlayer().getUniqueId(), ChatPlayer.class); - TBMCChatAPI.SendChatMessage(ChatMessage.builder(cp.CurrentChannel, event.getPlayer(), cp, event.getMessage()).build()); + TBMCChatAPI.SendChatMessage(ChatMessage.builder(event.getPlayer(), cp, event.getMessage()).build()); event.setCancelled(true); // The custom event should only be cancelled when muted or similar } @@ -71,11 +75,7 @@ public class PlayerListener implements Listener { if (message.length() < 2) return false; int index = message.indexOf(" "); - ChatPlayer mp; - if (sender instanceof Player) - mp = TBMCPlayer.getPlayer(((Player) sender).getUniqueId(), ChatPlayer.class); - else - mp = TBMCPlayer.getPlayer(new UUID(0, 0), ChatPlayer.class); //Use 0, 0 for console and whatever - TODO: Refactor console stuff + val mp = ChromaGamerBase.getFromSender(sender, TBMCPlayer.class); String cmd; final BiPredicate checkchid = (chan, cmd1) -> cmd1.equalsIgnoreCase(chan.ID) || (chan.IDs != null && Arrays.stream(chan.IDs).anyMatch(cmd1::equalsIgnoreCase)); if (index == -1) { // Only the command is run @@ -85,24 +85,17 @@ public class PlayerListener implements Listener { cmd = sender instanceof ConsoleCommandSender ? message : message.substring(1); for (Channel channel : Channel.getChannels()) { if (checkchid.test(channel, cmd)) { - Supplier getch = () -> sender instanceof Player ? mp.CurrentChannel : ConsoleChannel; - Consumer setch = ch -> { - if (sender instanceof Player) - mp.CurrentChannel = ch; - else - ConsoleChannel = ch; - }; - Channel oldch = getch.get(); + Channel oldch = mp.channel().get(); if (oldch instanceof ChatRoom) ((ChatRoom) oldch).leaveRoom(sender); if (oldch.equals(channel)) - setch.accept(Channel.GlobalChat); + mp.channel().set(Channel.GlobalChat); else { - setch.accept(channel); + mp.channel().set(channel); if (channel instanceof ChatRoom) ((ChatRoom) channel).joinRoom(sender); } - sender.sendMessage("§6You are now talking in: §b" + getch.get().DisplayName); + sender.sendMessage("§6You are now talking in: §b" + mp.channel().get().DisplayName); return true; } } @@ -134,7 +127,7 @@ public class PlayerListener implements Listener { } else for (Channel channel : Channel.getChannels()) { if (checkchid.test(channel, cmd)) { //Apparently method references don't require final variables - TBMCChatAPI.SendChatMessage(ChatMessage.builder(channel, sender, mp, message.substring(index + 1)).build()); + TBMCChatAPI.SendChatMessage(ChatMessage.builder(sender, mp, message.substring(index + 1)).build(), channel); return true; } } @@ -174,7 +167,7 @@ public class PlayerListener implements Listener { public static boolean ActiveF = false; public static ChatPlayer FPlayer = null; - private Timer Ftimer; + public static BukkitTask Ftask = null; public static int AlphaDeaths; public static ArrayList Fs = new ArrayList<>(); @@ -184,28 +177,26 @@ public class PlayerListener implements Listener { AlphaDeaths++; // MinigamePlayer mgp = Minigames.plugin.pdata.getMinigamePlayer(e.getEntity()); if (/* (mgp != null && !mgp.isInMinigame()) && */ new Random().nextBoolean()) { // Don't store Fs for NPCs - if (Ftimer != null) - Ftimer.cancel(); + Runnable tt = () -> { + if (ActiveF) { + ActiveF = false; + if (FPlayer != null && FPlayer.FCount().get() < Integer.MAX_VALUE - 1) + FPlayer.FCount().set(FPlayer.FCount().get() + Fs.size()); + Bukkit.broadcastMessage("§b" + Fs.size() + " " + (Fs.size() == 1 ? "person" : "people") + + " paid their respects.§r"); + Fs.clear(); + } + }; + if (Ftask != null) { + Ftask.cancel(); + tt.run(); //Finish previous one + } ActiveF = true; Fs.clear(); FPlayer = TBMCPlayer.getPlayer(e.getEntity().getUniqueId(), ChatPlayer.class); FPlayer.FDeaths().set(FPlayer.FDeaths().get() + 1); Bukkit.broadcastMessage("§bPress F to pay respects.§r"); - Ftimer = new Timer(); - TimerTask tt = new TimerTask() { - @Override - public void run() { - if (ActiveF) { - ActiveF = false; - if (FPlayer != null && FPlayer.FCount().get() < Integer.MAX_VALUE - 1) - FPlayer.FCount().set(FPlayer.FCount().get() + Fs.size()); - Bukkit.broadcastMessage("§b" + Fs.size() + " " + (Fs.size() == 1 ? "person" : "people") - + " paid their respects.§r"); - Fs.clear(); - } - } - }; - Ftimer.schedule(tt, 15 * 1000); + Bukkit.getScheduler().runTaskLaterAsynchronously(PluginMain.Instance, tt, 15 * 20); } } @@ -239,8 +230,6 @@ public class PlayerListener implements Listener { } } - public static Channel ConsoleChannel = Channel.GlobalChat; - @EventHandler(priority = EventPriority.HIGHEST) public void onConsoleCommand(ServerCommandEvent event) { if (onCommandPreprocess(event.getSender(), event.getCommand()))