From c5e33a93ba0e830989e791f72f03848d1d62c01f Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Wed, 20 Feb 2019 16:48:08 +0100 Subject: [PATCH] Moved chat-only stuff, F top, append text start #85 --- .../buttondevteam/chat/ChatProcessing.java | 6 +- .../chat/commands/ChatonlyCommand.java | 32 ---------- .../appendext/AppendTextComponent.java | 27 ++++++++ .../chatonly/ChatOnlyComponent.java | 62 +++++++++++++++++++ .../components/chatonly/ChatonlyCommand.java | 28 +++++++++ .../fun}/FTopCommand.java | 36 +++++------ .../chat/components/fun/FunComponent.java | 7 +++ .../listener/PlayerJoinLeaveListener.java | 7 +-- .../chat/listener/PlayerListener.java | 15 ----- 9 files changed, 143 insertions(+), 77 deletions(-) delete mode 100644 src/main/java/buttondevteam/chat/commands/ChatonlyCommand.java create mode 100644 src/main/java/buttondevteam/chat/components/appendext/AppendTextComponent.java create mode 100644 src/main/java/buttondevteam/chat/components/chatonly/ChatOnlyComponent.java create mode 100644 src/main/java/buttondevteam/chat/components/chatonly/ChatonlyCommand.java rename src/main/java/buttondevteam/chat/{commands => components/fun}/FTopCommand.java (73%) diff --git a/src/main/java/buttondevteam/chat/ChatProcessing.java b/src/main/java/buttondevteam/chat/ChatProcessing.java index eab698b..5486a2a 100644 --- a/src/main/java/buttondevteam/chat/ChatProcessing.java +++ b/src/main/java/buttondevteam/chat/ChatProcessing.java @@ -1,5 +1,6 @@ package buttondevteam.chat; +import buttondevteam.chat.components.chatonly.ChatOnlyComponent; import buttondevteam.chat.components.fun.FunComponent; import buttondevteam.chat.components.fun.UnlolCommand; import buttondevteam.chat.commands.ucmds.admin.DebugCommand; @@ -212,10 +213,7 @@ public class ChatProcessing { @Nullable ChatPlayer mp, @Nullable ChromaGamerBase cg, final String channelidentifier, String origin) { TellrawPart json = new TellrawPart(""); - if (mp != null && mp.ChatOnly) { - json.addExtra(new TellrawPart("[C]") - .setHoverEvent(TellrawEvent.create(TellrawEvent.HoverAction.SHOW_TEXT, "Chat only"))); - } + ChatOnlyComponent.tellrawCreate(mp, json); //TODO: Make nice API json.addExtra( new TellrawPart(channelidentifier) .setHoverEvent( diff --git a/src/main/java/buttondevteam/chat/commands/ChatonlyCommand.java b/src/main/java/buttondevteam/chat/commands/ChatonlyCommand.java deleted file mode 100644 index e85c7d3..0000000 --- a/src/main/java/buttondevteam/chat/commands/ChatonlyCommand.java +++ /dev/null @@ -1,32 +0,0 @@ -package buttondevteam.chat.commands; - -import org.bukkit.GameMode; -import org.bukkit.entity.Player; - -import buttondevteam.chat.ChatPlayer; -import buttondevteam.lib.chat.CommandClass; -import buttondevteam.lib.chat.PlayerCommandBase; -import buttondevteam.lib.player.TBMCPlayer; - -@CommandClass(modOnly = false) -public final class ChatonlyCommand extends PlayerCommandBase { - - @Override - public String[] GetHelpText(String alias) { - return new String[] { "§6---- Chat-only mode ----", // - "This mode makes you invincible but unable to move, teleport or interact with the world in any way", // - "It was designed for chat clients", // - "Once enabled, the only way of disabling it is by relogging to the server" // - }; - } - - @Override - public boolean OnCommand(Player player, String alias, String[] args) { - ChatPlayer p = TBMCPlayer.getPlayer(player.getUniqueId(), ChatPlayer.class); - p.ChatOnly = true; - player.setGameMode(GameMode.SPECTATOR); - player.sendMessage("§bChat-only mode enabled. You are now invincible."); - return true; - } - -} diff --git a/src/main/java/buttondevteam/chat/components/appendext/AppendTextComponent.java b/src/main/java/buttondevteam/chat/components/appendext/AppendTextComponent.java new file mode 100644 index 0000000..42637c1 --- /dev/null +++ b/src/main/java/buttondevteam/chat/components/appendext/AppendTextComponent.java @@ -0,0 +1,27 @@ +package buttondevteam.chat.components.appendext; + +import buttondevteam.lib.architecture.Component; +import buttondevteam.lib.architecture.ConfigData; +import buttondevteam.lib.architecture.IHaveConfig; +import lombok.val; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.function.Supplier; + +public class AppendTextComponent extends Component { + private HashMap appendTexts=new HashMap<>(); + private ConfigData helpText(IHaveConfig config) { + return config.getData("helpText", ()->new String[0]); + } + + @Override + protected void enable() { + val cs=getConfig().getConfig(); //TODO + } + + @Override + protected void disable() { + + } +} diff --git a/src/main/java/buttondevteam/chat/components/chatonly/ChatOnlyComponent.java b/src/main/java/buttondevteam/chat/components/chatonly/ChatOnlyComponent.java new file mode 100644 index 0000000..dce2089 --- /dev/null +++ b/src/main/java/buttondevteam/chat/components/chatonly/ChatOnlyComponent.java @@ -0,0 +1,62 @@ +package buttondevteam.chat.components.chatonly; + +import buttondevteam.chat.ChatPlayer; +import buttondevteam.chat.formatting.TellrawEvent; +import buttondevteam.chat.formatting.TellrawPart; +import buttondevteam.core.ComponentManager; +import buttondevteam.lib.architecture.Component; +import buttondevteam.lib.player.TBMCPlayer; +import buttondevteam.lib.player.TBMCPlayerJoinEvent; +import org.bukkit.GameMode; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerMoveEvent; +import org.bukkit.event.player.PlayerTeleportEvent; + +public class ChatOnlyComponent extends Component implements Listener { + @Override + protected void enable() { + registerListener(this); + registerCommand(new ChatonlyCommand()); + } + + @Override + protected void disable() { + + } + + public static void playerJoin(ChatPlayer cp, Player p) { + if(!ComponentManager.isEnabled(ChatOnlyComponent.class)) + return; + if (cp.ChatOnly || p.getGameMode().equals(GameMode.SPECTATOR)) { + cp.ChatOnly = false; + p.setGameMode(GameMode.SURVIVAL); + } + } + + public static void tellrawCreate(ChatPlayer mp, TellrawPart json) { + if(!ComponentManager.isEnabled(ChatOnlyComponent.class)) + return; + if (mp != null && mp.ChatOnly) { + json.addExtra(new TellrawPart("[C]") + .setHoverEvent(TellrawEvent.create(TellrawEvent.HoverAction.SHOW_TEXT, "Chat only"))); + } + } + + @EventHandler + public void onPlayerMove(PlayerMoveEvent e) { + ChatPlayer mp = TBMCPlayer.getPlayer(e.getPlayer().getUniqueId(), ChatPlayer.class); + if (mp.ChatOnly) + e.setCancelled(true); + } + + @EventHandler(priority = EventPriority.LOWEST) + public void onPlayerTeleport(PlayerTeleportEvent e) { + if (TBMCPlayer.getPlayer(e.getPlayer().getUniqueId(), ChatPlayer.class).ChatOnly) { + e.setCancelled(true); + e.getPlayer().sendMessage("§cYou are not allowed to teleport while in chat-only mode."); + } + } +} diff --git a/src/main/java/buttondevteam/chat/components/chatonly/ChatonlyCommand.java b/src/main/java/buttondevteam/chat/components/chatonly/ChatonlyCommand.java new file mode 100644 index 0000000..9af57be --- /dev/null +++ b/src/main/java/buttondevteam/chat/components/chatonly/ChatonlyCommand.java @@ -0,0 +1,28 @@ +package buttondevteam.chat.components.chatonly; + +import buttondevteam.lib.chat.ICommand2MC; +import org.bukkit.GameMode; +import org.bukkit.entity.Player; + +import buttondevteam.chat.ChatPlayer; +import buttondevteam.lib.chat.CommandClass; +import buttondevteam.lib.chat.PlayerCommandBase; +import buttondevteam.lib.player.TBMCPlayer; + +@CommandClass(modOnly = false, helpText = { + "§6---- Chat-only mode ----", // + "This mode makes you invincible but unable to move, teleport or interact with the world in any way", // + "It was designed for chat clients", // + "Once enabled, the only way of disabling it is by relogging to the server" // +}) +public final class ChatonlyCommand extends ICommand2MC { + + public boolean def(Player player) { + ChatPlayer p = TBMCPlayer.getPlayer(player.getUniqueId(), ChatPlayer.class); + p.ChatOnly = true; + player.setGameMode(GameMode.SPECTATOR); + player.sendMessage("§bChat-only mode enabled. You are now invincible."); + return true; + } + +} diff --git a/src/main/java/buttondevteam/chat/commands/FTopCommand.java b/src/main/java/buttondevteam/chat/components/fun/FTopCommand.java similarity index 73% rename from src/main/java/buttondevteam/chat/commands/FTopCommand.java rename to src/main/java/buttondevteam/chat/components/fun/FTopCommand.java index 8c10178..037811e 100644 --- a/src/main/java/buttondevteam/chat/commands/FTopCommand.java +++ b/src/main/java/buttondevteam/chat/components/fun/FTopCommand.java @@ -1,8 +1,10 @@ -package buttondevteam.chat.commands; +package buttondevteam.chat.components.fun; import buttondevteam.chat.ChatPlayer; import buttondevteam.chat.PluginMain; +import buttondevteam.lib.chat.Command2; import buttondevteam.lib.chat.CommandClass; +import buttondevteam.lib.chat.ICommand2MC; import buttondevteam.lib.chat.TBMCCommandBase; import buttondevteam.lib.player.TBMCPlayerBase; import lombok.val; @@ -16,23 +18,17 @@ import java.util.UUID; import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; -@CommandClass -public class FTopCommand extends TBMCCommandBase { - - @Override - public String[] GetHelpText(String arg0) { - return new String[]{ // - "§6---- F Top ----", // - "Shows the respect leaderboard" // - }; - } +@CommandClass(helpText = { + "§6---- F Top ----", // + "Shows the respect leaderboard" // +}) +public class FTopCommand extends ICommand2MC { private final File playerdir = new File(TBMCPlayerBase.TBMC_PLAYERS_DIR); private ChatPlayer[] cached; private long lastcache = 0; - @Override - public boolean OnCommand(CommandSender arg0, String arg1, String[] arg2) { + public boolean def(CommandSender sender, @Command2.OptionalArg int page) { Bukkit.getScheduler().runTaskAsynchronously(PluginMain.Instance, () -> { if (cached == null || lastcache < System.nanoTime() - 60000000000L) { // 1m - (no guarantees of nanoTime's relation to 0, so we need the null check too) cached = Arrays.stream(Objects.requireNonNull(playerdir.listFiles())).sequential() @@ -52,15 +48,13 @@ public class FTopCommand extends TBMCCommandBase { } int i; try { - i = arg2.length > 0 ? Integer.parseInt(arg2[0]) : 1; - if (i < 1) - i = 1; //i=1 - } catch (Exception e) { - i = 1; - } + i = page<1?1:page; + } catch (Exception e) { + i = 1; + } val ai = new AtomicInteger(); - arg0.sendMessage("§6---- Top Fs ----"); - arg0.sendMessage(Arrays.stream(cached).skip((i - 1) * 10).limit(i * 10) + sender.sendMessage("§6---- Top Fs ----"); + sender.sendMessage(Arrays.stream(cached).skip((i - 1) * 10).limit(i * 10) .map(cp -> String.format("%d. %s - %f.2", ai.incrementAndGet(), cp.PlayerName().get(), cp.getF())) .collect(Collectors.joining("\n"))); }); diff --git a/src/main/java/buttondevteam/chat/components/fun/FunComponent.java b/src/main/java/buttondevteam/chat/components/fun/FunComponent.java index 93ab4f6..b0f18e2 100644 --- a/src/main/java/buttondevteam/chat/components/fun/FunComponent.java +++ b/src/main/java/buttondevteam/chat/components/fun/FunComponent.java @@ -2,6 +2,7 @@ package buttondevteam.chat.components.fun; import buttondevteam.chat.ChatPlayer; import buttondevteam.chat.PluginMain; +import buttondevteam.chat.listener.PlayerListener; import buttondevteam.lib.TBMCChatEventBase; import buttondevteam.lib.architecture.Component; import buttondevteam.lib.player.TBMCPlayer; @@ -11,6 +12,7 @@ import org.bukkit.command.CommandSender; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.entity.PlayerDeathEvent; +import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.scheduler.BukkitTask; import java.util.ArrayList; @@ -30,6 +32,7 @@ public class FunComponent extends Component implements Listener { registerListener(pc); registerCommand(command=new UnlolCommand()); registerListener(this); + registerCommand(new FTopCommand()); } @Override @@ -84,4 +87,8 @@ public class FunComponent extends Component implements Listener { Bukkit.getScheduler().runTaskLaterAsynchronously(PluginMain.Instance, tt, 15 * 20); } } + @EventHandler + public void onPlayerLeave(PlayerQuitEvent event) { + command.Lastlol.values().removeIf(lld -> lld.getLolowner().equals(event.getPlayer())); + } } diff --git a/src/main/java/buttondevteam/chat/listener/PlayerJoinLeaveListener.java b/src/main/java/buttondevteam/chat/listener/PlayerJoinLeaveListener.java index 915596f..7f726bd 100644 --- a/src/main/java/buttondevteam/chat/listener/PlayerJoinLeaveListener.java +++ b/src/main/java/buttondevteam/chat/listener/PlayerJoinLeaveListener.java @@ -3,6 +3,7 @@ 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; @@ -62,10 +63,7 @@ public class PlayerJoinLeaveListener implements Listener { TownColorComponent.updatePlayerColors(p, cp); //TO!DO: Doesn't have effect - It can help to register the listener - if (cp.ChatOnly || p.getGameMode().equals(GameMode.SPECTATOR)) { - cp.ChatOnly = false; - p.setGameMode(GameMode.SURVIVAL); - } + ChatOnlyComponent.playerJoin(cp, p); HistoryCommand.showHistory(e.getPlayer(), "u history", new String[0], null); } @@ -77,7 +75,6 @@ public class PlayerJoinLeaveListener implements Listener { @EventHandler public void onPlayerLeave(PlayerQuitEvent event) { PlayerListener.nicknames.inverse().remove(event.getPlayer().getUniqueId()); - UnlolCommand.Lastlol.values().removeIf(lld -> lld.getLolowner().equals(event.getPlayer())); } } diff --git a/src/main/java/buttondevteam/chat/listener/PlayerListener.java b/src/main/java/buttondevteam/chat/listener/PlayerListener.java index d20ded5..1b50b8c 100644 --- a/src/main/java/buttondevteam/chat/listener/PlayerListener.java +++ b/src/main/java/buttondevteam/chat/listener/PlayerListener.java @@ -176,21 +176,6 @@ public class PlayerListener implements Listener { } } - @EventHandler - public void onPlayerMove(PlayerMoveEvent e) { - ChatPlayer mp = TBMCPlayer.getPlayer(e.getPlayer().getUniqueId(), ChatPlayer.class); - if (mp.ChatOnly) - e.setCancelled(true); - } - - @EventHandler(priority = EventPriority.LOWEST) - public void onPlayerTeleport(PlayerTeleportEvent e) { - if (TBMCPlayer.getPlayer(e.getPlayer().getUniqueId(), ChatPlayer.class).ChatOnly) { - e.setCancelled(true); - e.getPlayer().sendMessage("§cYou are not allowed to teleport while in chat-only mode."); - } - } - @EventHandler(priority = EventPriority.HIGHEST) public void onConsoleCommand(ServerCommandEvent event) { if (onCommandPreprocess(event.getSender(), event.getCommand()))