forked from OASIS-learn-study/minecraft-storeys-maker
/
StoreysWebPlugin.java
131 lines (115 loc) · 5.79 KB
/
StoreysWebPlugin.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* ch.vorburger.minecraft.storeys
*
* Copyright (C) 2016 - 2018 Michael Vorburger.ch <mike@vorburger.ch>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package ch.vorburger.minecraft.storeys.web;
import ch.vorburger.minecraft.osgi.api.Listeners;
import ch.vorburger.minecraft.osgi.api.PluginInstance;
import ch.vorburger.minecraft.storeys.api.Minecraft;
import ch.vorburger.minecraft.storeys.api.impl.MinecraftImpl;
import ch.vorburger.minecraft.storeys.api.impl.TokenCommand;
import ch.vorburger.minecraft.storeys.plugin.AbstractStoreysPlugin;
import ch.vorburger.minecraft.storeys.simple.TokenProvider;
import ch.vorburger.minecraft.storeys.simple.impl.TokenProviderImpl;
import ch.vorburger.minecraft.storeys.util.Commands;
import com.google.common.reflect.TypeToken;
import com.google.inject.Injector;
import com.google.inject.TypeLiteral;
import com.google.inject.name.Names;
import io.vertx.core.Handler;
import io.vertx.core.eventbus.Message;
import io.vertx.core.json.JsonObject;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.ConfigurationOptions;
import ninja.leaping.configurate.commented.CommentedConfigurationNode;
import ninja.leaping.configurate.loader.ConfigurationLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.CommandMapping;
import org.spongepowered.api.config.DefaultConfig;
import org.spongepowered.api.plugin.Plugin;
import javax.inject.Inject;
import java.nio.file.Path;
import java.util.concurrent.ExecutionException;
@Plugin(id = "storeys-web", name = "Vorburger.ch's Storeys with Web API", version = "1.0",
description = "Makes entities narrate story lines so you can make your own movie in Minecraft",
url = "https://github.com/vorburger/minecraft-storeys-maker",
authors = "Michael Vorburger.ch")
public class StoreysWebPlugin extends AbstractStoreysPlugin implements Listeners {
// do not extend StoreysPlugin, because we exclude that class in shadowJar
private static final Logger LOG = LoggerFactory.getLogger(StoreysWebPlugin.class);
private VertxStarter vertxStarter;
private ActionsConsumer actionsConsumer;
private CommandMapping loginCommandMapping;
private CommandMapping tokenCommandMapping;
@Inject
@DefaultConfig(sharedRoot = true)
private ConfigurationLoader<CommentedConfigurationNode> configurationLoader;
@Override
public void start(PluginInstance plugin, Path configDir) throws Exception {
super.start(plugin, configDir);
Injector injector = pluginInjector.createChildInjector(binder -> {
binder.bind(TokenProvider.class).to(TokenProviderImpl.class);
binder.bind(Minecraft.class).to(MinecraftImpl.class);
binder.bind(EventBusSender.class).to(MinecraftVerticle.class);
binder.bind(new TypeLiteral<Handler<Message<JsonObject>>>(){}).to(ActionsConsumer.class);
// TODO read from some configuration
binder.bind(Integer.class).annotatedWith(Names.named("http-port")).toInstance(8080);
binder.bind(Integer.class).annotatedWith(Names.named("web-http-port")).toInstance(7070);
binder.bind(new TypeLiteral<ConfigurationLoader<CommentedConfigurationNode>>(){}).toInstance(configurationLoader);
binder.bind(LocationToolListener.class);
});
actionsConsumer = injector.getInstance(ActionsConsumer.class);
MinecraftVerticle minecraftVerticle = injector.getInstance(MinecraftVerticle.class);
StaticWebServerVerticle staticWebServerVerticle = injector.getInstance(StaticWebServerVerticle.class);
TokenProvider tokenProvider = injector.getInstance(TokenProvider.class);
loginCommandMapping = Commands.register(plugin, new LoginCommand(tokenProvider));
tokenCommandMapping = Commands.register(plugin, new TokenCommand(tokenProvider));
try {
try {
vertxStarter = new VertxStarter();
vertxStarter.deployVerticle(minecraftVerticle).toCompletableFuture().get();
vertxStarter.deployVerticle(staticWebServerVerticle).toCompletableFuture().get();
} catch (ExecutionException | InterruptedException e) {
throw new IllegalStateException("Vert.x start-up failed", e);
}
new NodeStarter(configDir).start();
} catch (RuntimeException e) {
// If something went wrong during the Vert.x set up, we must unregister the commands registered in super.start()
// so that, under OSGi, we'll manage to cleanly restart when whatever problem caused the start up to fail is fixed again.
super.stop();
throw e;
}
}
@Override
public void stop() throws Exception {
if (loginCommandMapping != null) {
Sponge.getCommandManager().removeMapping(loginCommandMapping);
}
if (tokenCommandMapping != null) {
Sponge.getCommandManager().removeMapping(tokenCommandMapping);
}
if (actionsConsumer != null) {
actionsConsumer.stop();
}
if (vertxStarter != null) {
vertxStarter.stop();
}
super.stop();
}
}