Skip to content

Commit eeef780

Browse files
committed
Allow loading as a service/library only
1 parent a5f565b commit eeef780

20 files changed

+221
-65
lines changed

src/main/java/com/simon816/chatui/ActivePlayerChatView.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public class ActivePlayerChatView implements PlayerChatView {
4141
initNewTab(player);
4242
}
4343

44-
@Override
4544
public PlayerList getPlayerList() {
4645
return this.playerList;
4746
}
@@ -97,7 +96,6 @@ public Window getWindow() {
9796
return this.window;
9897
}
9998

100-
@Override
10199
public NewTab getNewTab() {
102100
return this.newTab;
103101
}
@@ -171,6 +169,11 @@ private void disable() {
171169
this.isUpdating = false;
172170
}
173171

172+
@Override
173+
public void onRemove() {
174+
this.window.closeAll();
175+
}
176+
174177
@Override
175178
public String toString() {
176179
return Objects.toStringHelper(this)

src/main/java/com/simon816/chatui/ChatUI.java

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.spongepowered.api.event.game.state.GameInitializationEvent;
2727
import org.spongepowered.api.event.game.state.GamePostInitializationEvent;
2828
import org.spongepowered.api.event.game.state.GamePreInitializationEvent;
29+
import org.spongepowered.api.event.game.state.GameStoppingServerEvent;
2930
import org.spongepowered.api.event.message.MessageChannelEvent;
3031
import org.spongepowered.api.event.network.ClientConnectionEvent;
3132
import org.spongepowered.api.plugin.Plugin;
@@ -56,6 +57,8 @@ public class ChatUI {
5657

5758
private static ChatUI instance;
5859

60+
private boolean enabledAsService = false;
61+
5962
@Inject
6063
@DefaultConfig(sharedRoot = true)
6164
private ConfigurationLoader<CommentedConfigurationNode> confLoader;
@@ -80,6 +83,14 @@ public static PlayerChatView getView(UUID uuid) {
8083
return instance.playerViewMap.get(uuid);
8184
}
8285

86+
public static ActivePlayerChatView getActiveView(CommandSource source) {
87+
return (ActivePlayerChatView) getView(source);
88+
}
89+
90+
public static ActivePlayerChatView getActiveView(UUID uuid) {
91+
return (ActivePlayerChatView) getView(uuid);
92+
}
93+
8394
public static ClickAction<?> command(String subcommand) {
8495
return TextActions.runCommand("/chatui " + subcommand);
8596
}
@@ -102,12 +113,22 @@ public void onInit(GameInitializationEvent event) {
102113
Sponge.getGame().getCommandManager().register(this, new ChatUICommand(), "chatui");
103114
Config.init(this.confLoader, this.logger);
104115

116+
this.enabledAsService = !Config.getRootNode().getNode("interfaceEnabled").getBoolean(true);
117+
118+
if (this.enabledAsService) {
119+
return;
120+
}
121+
105122
this.featuresToLoad = Maps.newHashMap();
106123

107124
this.registerFeature(this, "privmsg", PrivateMessageFeature::new);
108125
this.registerFeature(this, "chatgroup", ChatGroupFeature::new);
109126
}
110127

128+
public boolean isServiceOnlyMode() {
129+
return this.enabledAsService;
130+
}
131+
111132
public void registerFeature(Object plugin, String id, Supplier<AbstractFeature> featureLoader) {
112133
checkState(this.featuresToLoad != null, "Not accepting new features to be registered");
113134
String featureId = Sponge.getPluginManager().fromInstance(plugin).get().getId() + ":" + id;
@@ -116,6 +137,9 @@ public void registerFeature(Object plugin, String id, Supplier<AbstractFeature>
116137

117138
@Listener(order = Order.POST)
118139
public void onPostInit(GamePostInitializationEvent event) {
140+
if (this.enabledAsService) {
141+
return;
142+
}
119143
Optional<ProviderRegistration<PaginationService>> optService = Sponge.getGame().getServiceManager().getRegistration(PaginationService.class);
120144
if (!optService.isPresent()) {
121145
return;
@@ -157,6 +181,10 @@ public void onGameReload(GameReloadEvent event) {
157181

158182
@Listener
159183
public void onPlayerJoin(ClientConnectionEvent.Join event) {
184+
if (this.enabledAsService) {
185+
this.playerViewMap.put(event.getTargetEntity().getUniqueId(), new ExternalServiceView(event.getTargetEntity()));
186+
return;
187+
}
160188
initialize(event.getTargetEntity());
161189
}
162190

@@ -169,6 +197,9 @@ void initialize(Player player) {
169197
view = new ActivePlayerChatView(player, playerSettings);
170198
}
171199
PlayerChatView oldView = this.playerViewMap.put(player.getUniqueId(), view);
200+
if (oldView != null) {
201+
oldView.onRemove();
202+
}
172203
for (AbstractFeature feature : this.features) {
173204
if (oldView != null) {
174205
feature.onViewClose(oldView);
@@ -182,10 +213,12 @@ void initialize(Player player) {
182213
public void onPlayerQuit(ClientConnectionEvent.Disconnect event) {
183214
PlayerChatView view = this.playerViewMap.remove(event.getTargetEntity().getUniqueId());
184215
Config.saveConfig();
185-
for (AbstractFeature feature : this.features) {
186-
feature.onViewClose(view);
216+
if (!this.enabledAsService) {
217+
for (AbstractFeature feature : this.features) {
218+
feature.onViewClose(view);
219+
}
187220
}
188-
view.getWindow().closeAll();
221+
view.onRemove();
189222
// TODO Offline message buffering?
190223
}
191224

@@ -201,6 +234,11 @@ public void onIncomingMessage(MessageChannelEvent.Chat event, @Root Player playe
201234
}
202235
}
203236

237+
@Listener
238+
public void onServerStop(GameStoppingServerEvent event) {
239+
Config.saveConfig();
240+
}
241+
204242
// Try to be last so we can wrap around any messages sent
205243
@Listener(order = Order.POST)
206244
public void onOutgoingMessage(MessageChannelEvent event) {

src/main/java/com/simon816/chatui/Config.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ public static void loadConfig() {
6565
}
6666
CommentedConfigurationNode playerSettings = config.getNode("playerSettings");
6767
if (playerSettings.isVirtual()) {
68-
playerSettings.setValue(Collections.EMPTY_MAP);
68+
playerSettings.setValue(Collections.emptyMap());
69+
}
70+
CommentedConfigurationNode enabled = config.getNode("interfaceEnabled");
71+
if (enabled.isVirtual()) {
72+
enabled.setValue(true);
6973
}
7074
}
7175

src/main/java/com/simon816/chatui/DemoContent.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public DemoTab() {
3838
}));
3939
addButton("Sine Wave Animation", new SineWaveAnimLoader());
4040
addButton("UI Test", new NewTab.LaunchTabAction(() -> new UITest()));
41-
addButton("Back", new NewTab.LaunchTabAction(PlayerChatView::getNewTab));
41+
addButton("Back", new NewTab.LaunchTabAction(ActivePlayerChatView::getNewTab));
4242
}
4343

4444
@Override
@@ -53,7 +53,7 @@ public SineWaveAnimLoader() {
5353
}
5454

5555
@Override
56-
protected void onClick(PlayerChatView view) {
56+
protected void onClick(ActivePlayerChatView view) {
5757
CanvasUI canvas = new CanvasUI();
5858
Tab sineWaveTab = new Tab(Text.of("Sine Wave Demo"), new AnchorPaneUI(canvas)) {
5959

src/main/java/com/simon816/chatui/DisabledChatView.java

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.simon816.chatui;
22

3-
import com.simon816.chatui.tabs.NewTab;
43
import org.spongepowered.api.Sponge;
54
import org.spongepowered.api.command.CommandSource;
65
import org.spongepowered.api.entity.living.player.Player;
@@ -24,36 +23,21 @@ public class DisabledChatView implements PlayerChatView {
2423
DISABLED_MESSAGE = builder.build();
2524
}
2625
private final UUID playerUuid;
27-
private final PlayerList stubPlayerList;
28-
private final Window stubWindow;
29-
private final NewTab stubNewTab;
3026

3127
DisabledChatView(Player player) {
3228
this.playerUuid = player.getUniqueId();
33-
this.stubPlayerList = new PlayerList(player);
34-
this.stubWindow = new Window();
35-
this.stubNewTab = new NewTab();
3629
player.sendMessage(DISABLED_MESSAGE);
3730
}
3831

39-
@Override
40-
public PlayerList getPlayerList() {
41-
return this.stubPlayerList;
42-
}
43-
4432
@Override
4533
public Player getPlayer() {
4634
return Sponge.getServer().getPlayer(this.playerUuid).get();
4735
}
4836

4937
@Override
50-
public Window getWindow() {
51-
return this.stubWindow;
52-
}
53-
54-
@Override
55-
public NewTab getNewTab() {
56-
return this.stubNewTab;
38+
public TopWindow getWindow() {
39+
// TODO Auto-generated method stub
40+
return null;
5741
}
5842

5943
@Override
@@ -81,4 +65,8 @@ public boolean handleCommand(String[] args) {
8165
return false;
8266
}
8367

68+
@Override
69+
public void onRemove() {
70+
}
71+
8472
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.simon816.chatui;
2+
3+
import com.google.common.collect.Lists;
4+
import org.spongepowered.api.command.CommandSource;
5+
import org.spongepowered.api.entity.living.player.Player;
6+
import org.spongepowered.api.text.Text;
7+
import org.spongepowered.api.text.chat.ChatType;
8+
import org.spongepowered.api.util.Tuple;
9+
10+
import java.util.List;
11+
import java.util.Optional;
12+
13+
public class ExternalServiceView implements PlayerChatView {
14+
15+
private final PlayerContext context;
16+
17+
private TopWindow window;
18+
private boolean isUpdating;
19+
20+
private final List<Tuple<Text, ChatType>> chatBuffer = Lists.newArrayList();
21+
22+
public ExternalServiceView(Player player) {
23+
this.context = new PlayerContext(player, Config.DEFAULT_BUFFER_WIDTH, Config.DEFAULT_BUFFER_HEIGHT, false);
24+
}
25+
26+
@Override
27+
public Player getPlayer() {
28+
return this.context.getPlayer();
29+
}
30+
31+
@Override
32+
public TopWindow getWindow() {
33+
return this.window;
34+
}
35+
36+
public void setWindow(TopWindow window) {
37+
if (this.window != null) {
38+
this.window.onClose();
39+
}
40+
this.window = window;
41+
if (window != null) {
42+
update();
43+
} else {
44+
flushBuffer();
45+
}
46+
}
47+
48+
private void flushBuffer() {
49+
Player player = getPlayer();
50+
for (Tuple<Text, ChatType> message : this.chatBuffer) {
51+
player.sendMessage(message.getSecond(), message.getFirst());
52+
}
53+
this.chatBuffer.clear();
54+
}
55+
56+
@Override
57+
public void update() {
58+
if (this.window != null) {
59+
this.isUpdating = true;
60+
getPlayer().sendMessage(this.window.draw(this.context));
61+
this.isUpdating = false;
62+
}
63+
}
64+
65+
@Override
66+
public boolean handleIncoming(Text message) {
67+
if (this.window == null) {
68+
return false;
69+
}
70+
this.window.onTextInput(this, message);
71+
return true;
72+
}
73+
74+
@Override
75+
public Optional<Text> transformOutgoing(CommandSource sender, Text originalOutgoing, ChatType type) {
76+
if (this.window == null || this.isUpdating) {
77+
return Optional.of(originalOutgoing);
78+
}
79+
this.chatBuffer.add(new Tuple<>(originalOutgoing, type));
80+
return Optional.empty();
81+
}
82+
83+
@Override
84+
public boolean handleCommand(String[] args) {
85+
if (this.window == null) {
86+
return false;
87+
}
88+
boolean handled = this.window.onCommand(this, args);
89+
if (handled) {
90+
update();
91+
}
92+
return handled;
93+
}
94+
95+
@Override
96+
public void onRemove() {
97+
this.window = null;
98+
this.chatBuffer.clear();
99+
}
100+
101+
}
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.simon816.chatui;
22

3-
import com.simon816.chatui.tabs.NewTab;
43
import org.spongepowered.api.command.CommandSource;
54
import org.spongepowered.api.entity.living.player.Player;
65
import org.spongepowered.api.text.Text;
@@ -10,13 +9,9 @@
109

1110
public interface PlayerChatView {
1211

13-
public PlayerList getPlayerList();
14-
1512
public Player getPlayer();
1613

17-
public Window getWindow();
18-
19-
public NewTab getNewTab();
14+
public TopWindow getWindow();
2015

2116
public void update();
2217

@@ -26,4 +21,6 @@ public interface PlayerChatView {
2621

2722
public boolean handleCommand(String[] args);
2823

24+
public void onRemove();
25+
2926
}

src/main/java/com/simon816/chatui/Window.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,7 @@ public void onTextInput(PlayerChatView view, Text input) {
111111

112112
@Override
113113
public Text draw(PlayerContext ctx) {
114-
LineFactory factory = new LineFactory();
115-
this.pane.draw(ctx, factory);
116-
factory.fillBlank(ctx);
117-
return Text.builder().append(Text.joinWith(Text.NEW_LINE, factory.getLines())).build();
114+
return this.pane.draw(ctx);
118115
}
119116

120117
List<Tab> getTabs() {

src/main/java/com/simon816/chatui/group/ChatGroup.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void setNode(ConfigurationNode node) {
6363
public void onRemoved() {
6464
this.ignoreClose = true;
6565
for (Entry<UUID, ChatBufferTab> entry : this.tabs.entrySet()) {
66-
ChatUI.getView(entry.getKey()).getWindow().removeTab(entry.getValue());
66+
ChatUI.getActiveView(entry.getKey()).getWindow().removeTab(entry.getValue());
6767
ChatUI.getView(entry.getKey()).update();
6868
}
6969
this.players.clear();

0 commit comments

Comments
 (0)