|
| 1 | +package com.simon816.chatui; |
| 2 | + |
| 3 | +import com.google.common.base.Objects; |
| 4 | +import com.simon816.chatui.impl.ImplementationConfig; |
| 5 | +import com.simon816.chatui.tabs.GlobalTab; |
| 6 | +import com.simon816.chatui.tabs.NewTab; |
| 7 | +import com.simon816.chatui.tabs.PermissionsTab; |
| 8 | +import com.simon816.chatui.tabs.SceneTab; |
| 9 | +import com.simon816.chatui.tabs.Tab; |
| 10 | +import com.simon816.chatui.tabs.TextBufferTab; |
| 11 | +import com.simon816.chatui.tabs.TextFileTab; |
| 12 | +import com.simon816.chatui.tabs.config.ConfigEditTab; |
| 13 | +import ninja.leaping.configurate.ConfigurationNode; |
| 14 | +import org.spongepowered.api.command.CommandSource; |
| 15 | +import org.spongepowered.api.entity.living.player.Player; |
| 16 | +import org.spongepowered.api.text.Text; |
| 17 | +import org.spongepowered.api.text.chat.ChatType; |
| 18 | + |
| 19 | +import java.util.Optional; |
| 20 | +import java.util.UUID; |
| 21 | + |
| 22 | +public class ActivePlayerChatView implements PlayerChatView { |
| 23 | + |
| 24 | + private PlayerContext playerContext; |
| 25 | + private final Window window; |
| 26 | + private final TextBufferTab globalTab; |
| 27 | + private final NewTab newTab; |
| 28 | + boolean isUpdating; |
| 29 | + |
| 30 | + private final PlayerList playerList; |
| 31 | + |
| 32 | + ActivePlayerChatView(Player player, ConfigurationNode settings) { |
| 33 | + this.playerContext = new PlayerContext(player, |
| 34 | + settings.getNode("displayWidth").getInt(), |
| 35 | + settings.getNode("displayHeight").getInt(), |
| 36 | + settings.getNode("forceUnicode").getBoolean()); |
| 37 | + this.window = new Window(); |
| 38 | + this.window.addTab(this.globalTab = new GlobalTab(), true); |
| 39 | + this.newTab = new NewTab(); |
| 40 | + this.playerList = new PlayerList(player); |
| 41 | + initNewTab(player); |
| 42 | + ChatUI.instance().loadFeatures(this); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public PlayerList getPlayerList() { |
| 47 | + return this.playerList; |
| 48 | + } |
| 49 | + |
| 50 | + private void initNewTab(Player player) { |
| 51 | + this.newTab.addButton("Player List", new NewTab.LaunchTabAction(() -> new SceneTab(Text.of("Player List"), this.playerList.getRoot()))); |
| 52 | + UUID uuid = player.getUniqueId(); |
| 53 | + this.newTab.addButton("Settings", new NewTab.LaunchTabAction(() -> createSettingsTab(uuid))); |
| 54 | + if (player.hasPermission(ChatUI.ADMIN_PERMISSON)) { |
| 55 | + showNewTabAdminButtons(); |
| 56 | + } |
| 57 | + if (DemoContent.ENABLE_DEMO) { |
| 58 | + this.newTab.addButton("Demo Content", new NewTab.LaunchTabAction(() -> DemoContent.TAB)); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private Tab createSettingsTab(UUID uuid) { |
| 63 | + ConfigurationNode config = Config.playerConfig(uuid); |
| 64 | + ConfigEditTab.Options opts = new ConfigEditTab.Options(false, true, false, "Settings"); |
| 65 | + ConfigEditTab.ActionHandler handler = new ConfigEditTab.ActionHandler() { |
| 66 | + |
| 67 | + @Override |
| 68 | + public void onNodeChanged(ConfigurationNode node) { |
| 69 | + Config.saveConfig(); |
| 70 | + onConfigChange(node); |
| 71 | + } |
| 72 | + }; |
| 73 | + return new ConfigEditTab(config, Text.of("Settings"), opts, handler); |
| 74 | + } |
| 75 | + |
| 76 | + private void showNewTabAdminButtons() { |
| 77 | + if (ImplementationConfig.isSupported()) { |
| 78 | + this.newTab.addButton("Edit Config", new NewTab.LaunchTabAction(() -> new ConfigEditTab(ImplementationConfig.getRootNode(), |
| 79 | + Text.of("Sponge Config"), ConfigEditTab.Options.DEFAULTS, ImplementationConfig.getHandler()))); |
| 80 | + } |
| 81 | + this.newTab.addButton("Permissions", new NewTab.LaunchTabAction(() -> new PermissionsTab())); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public Player getPlayer() { |
| 86 | + return this.playerContext.player; |
| 87 | + } |
| 88 | + |
| 89 | + public PlayerContext getContext() { |
| 90 | + return this.playerContext; |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public Window getWindow() { |
| 95 | + return this.window; |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public NewTab getNewTab() { |
| 100 | + return this.newTab; |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void update() { |
| 105 | + this.isUpdating = true; |
| 106 | + this.playerContext.player.sendMessage(this.window.draw(this.playerContext)); |
| 107 | + this.isUpdating = false; |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public boolean handleIncoming(Text message) { |
| 112 | + try { |
| 113 | + this.window.getActiveTab().onTextEntered(this, message); |
| 114 | + // Only allow normal chat to get processed if on the global tab |
| 115 | + return this.window.getActiveTab() != this.globalTab; |
| 116 | + } catch (Exception e) { |
| 117 | + e.printStackTrace(); |
| 118 | + return true; // Just ignore the input |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public Optional<Text> transformOutgoing(CommandSource sender, Text originalOutgoing, ChatType type) { |
| 124 | + if (this.isUpdating) { |
| 125 | + // Send it straight out |
| 126 | + return Optional.of(originalOutgoing); |
| 127 | + } |
| 128 | + this.globalTab.appendMessage(originalOutgoing); |
| 129 | + return Optional.of(this.window.draw(this.playerContext)); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public boolean handleCommand(String[] args) { |
| 134 | + String cmd = args[0]; |
| 135 | + if (cmd.equals("settab")) { |
| 136 | + this.window.setTab(Integer.parseInt(args[1])); |
| 137 | + } else if (cmd.equals("closetab")) { |
| 138 | + this.window.removeTab(Integer.parseInt(args[1])); |
| 139 | + } else if (cmd.equals("newtab")) { |
| 140 | + this.window.addTab(this.newTab, true); |
| 141 | + } else if (cmd.equals("tf") && this.window.getActiveTab() instanceof TextFileTab) { |
| 142 | + // TextFileTab specific behaviour, see TextFileTab#clickAction |
| 143 | + ((TextFileTab) this.window.getActiveTab()).onCommand(this, Integer.parseInt(args[1]), Integer.parseInt(args[2])); |
| 144 | + } else { |
| 145 | + return false; |
| 146 | + } |
| 147 | + update(); |
| 148 | + return true; |
| 149 | + } |
| 150 | + |
| 151 | + public void onConfigChange(ConfigurationNode node) { |
| 152 | + if (node.getKey().equals("displayWidth")) { |
| 153 | + this.playerContext = this.playerContext.withWidth(node.getInt()); |
| 154 | + } else if (node.getKey().equals("displayHeight")) { |
| 155 | + this.playerContext = this.playerContext.withHeight(node.getInt()); |
| 156 | + } else if (node.getKey().equals("forceUnicode")) { |
| 157 | + this.playerContext = this.playerContext.withUnicode(node.getBoolean()); |
| 158 | + } else if (node.getKey().equals("enabled")) { |
| 159 | + if (!node.getBoolean()) { |
| 160 | + disable(); |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + private void disable() { |
| 166 | + // Force clear the screen |
| 167 | + this.isUpdating = true; |
| 168 | + for (int i = 0; i < this.playerContext.height; i++) { |
| 169 | + this.playerContext.player.sendMessage(Text.NEW_LINE); |
| 170 | + } |
| 171 | + this.isUpdating = false; |
| 172 | + ChatUI.instance().initialize(getPlayer()); |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public String toString() { |
| 177 | + return Objects.toStringHelper(this) |
| 178 | + .add("player", this.playerContext) |
| 179 | + .add("window", this.window) |
| 180 | + .toString(); |
| 181 | + } |
| 182 | + |
| 183 | +} |
0 commit comments