This repository has been archived by the owner on Apr 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Denizen2Sponge.java
275 lines (250 loc) · 11.5 KB
/
Denizen2Sponge.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package com.denizenscript.denizen2sponge;
import com.denizenscript.denizen2core.Denizen2Core;
import com.denizenscript.denizen2core.utilities.CoreUtilities;
import com.denizenscript.denizen2core.utilities.debugging.ColorSet;
import com.denizenscript.denizen2core.utilities.debugging.Debug;
import com.denizenscript.denizen2core.utilities.yaml.YAMLConfiguration;
import com.denizenscript.denizen2sponge.commands.entity.EditEntityCommand;
import com.denizenscript.denizen2sponge.commands.entity.FlagEntityCommand;
import com.denizenscript.denizen2sponge.commands.entity.SpawnCommand;
import com.denizenscript.denizen2sponge.commands.entity.TeleportCommand;
import com.denizenscript.denizen2sponge.commands.player.ActionBarCommand;
import com.denizenscript.denizen2sponge.commands.player.GiveCommand;
import com.denizenscript.denizen2sponge.commands.player.TellCommand;
import com.denizenscript.denizen2sponge.commands.server.ExecuteCommand;
import com.denizenscript.denizen2sponge.commands.world.EditBlockCommand;
import com.denizenscript.denizen2sponge.commands.world.PlayEffectCommand;
import com.denizenscript.denizen2sponge.commands.world.SetBlockCommand;
import com.denizenscript.denizen2sponge.events.entity.EntityDamagedScriptEvent;
import com.denizenscript.denizen2sponge.events.player.PlayerBreaksBlockScriptEvent;
import com.denizenscript.denizen2sponge.events.player.PlayerChatsScriptEvent;
import com.denizenscript.denizen2sponge.events.player.PlayerJoinsScriptEvent;
import com.denizenscript.denizen2sponge.events.entity.EntityMovesScriptEvent;
import com.denizenscript.denizen2sponge.events.player.PlayerPlacesBlockScriptEvent;
import com.denizenscript.denizen2sponge.events.player.PlayerRightClicksBlockScriptEvent;
import com.denizenscript.denizen2sponge.events.player.PlayerRightClicksEntityScriptEvent;
import com.denizenscript.denizen2sponge.events.server.ClientPingsServerScriptEvent;
import com.denizenscript.denizen2sponge.events.world.BlockChangeScriptEvent;
import com.denizenscript.denizen2sponge.events.entity.EntitySpawnScriptEvent;
import com.denizenscript.denizen2sponge.spongecommands.ExCommand;
import com.denizenscript.denizen2sponge.spongeevents.Denizen2SpongeLoadedEvent;
import com.denizenscript.denizen2sponge.spongeevents.Denizen2SpongeLoadingEvent;
import com.denizenscript.denizen2sponge.spongescripts.GameCommandScript;
import com.denizenscript.denizen2sponge.tags.handlers.*;
import com.denizenscript.denizen2sponge.utilities.flags.FlagHelper;
import com.google.inject.Inject;
import org.slf4j.Logger;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.cause.Cause;
import org.spongepowered.api.event.cause.NamedCause;
import org.spongepowered.api.event.game.state.GamePreInitializationEvent;
import org.spongepowered.api.event.game.state.GameStoppedEvent;
import org.spongepowered.api.plugin.Plugin;
import org.spongepowered.api.plugin.PluginContainer;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.serializer.TextSerializers;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
/**
* Main plugin class for Denizen2Sponge.
*/
@Plugin(id = Denizen2Sponge.PLUGIN_ID, name = Denizen2Sponge.PLUGIN_NAME, version = Denizen2Sponge.PLUGIN_VERSION)
public class Denizen2Sponge {
public final static String PLUGIN_ID = "denizen2sponge";
public final static String PLUGIN_NAME = "Denizen2Sponge";
public final static String PLUGIN_VERSION = PomData.VERSION + " (build " + PomData.BUILD_NUMBER + ")";
public final static String version;
public static PluginContainer plugin;
public static Denizen2Sponge instance;
public YAMLConfiguration config;
public static char colorChar = '\u00A7';
public static Cause getGenericCause() {
return Cause.of(NamedCause.of("plugin", plugin));
}
public static Text parseColor(String inp) {
return TextSerializers.formattingCode(Denizen2Sponge.colorChar).deserialize(inp);
}
@Inject
public Logger logger;
static {
YAMLConfiguration tconfig = null;
try {
InputStream is = Denizen2Sponge.class.getResourceAsStream("/denizen2sponge.yml");
tconfig = YAMLConfiguration.load(CoreUtilities.streamToString(is));
is.close();
}
catch (Exception ex) {
Debug.exception(ex);
}
if (tconfig == null) {
version = "UNKNOWN (Error reading version file!)";
}
else {
version = tconfig.getString("VERSION", "UNKNOWN") + " (build " + tconfig.getString("BUILD_NUMBER", "UNKNOWN") + ")";
}
}
@Listener
public void onServerStart(GamePreInitializationEvent event) {
// Setup
instance = this;
plugin = Sponge.getPluginManager().getPlugin(PLUGIN_ID).orElse(null);
// Colors
ColorSet.base = colorChar + "7";
ColorSet.good = colorChar + "a";
ColorSet.warning = colorChar + "c";
ColorSet.emphasis = colorChar + "b";
// Load config (save default if it doesn't exist)
saveDefaultConfig();
loadConfig();
// Denizen2
Denizen2Core.init(new Denizen2SpongeImplementation());
// Ensure the scripts and addons folders exist
Denizen2Core.getImplementation().getScriptsFolder().mkdirs();
Denizen2Core.getImplementation().getAddonsFolder().mkdirs();
// Commands: Entity
Denizen2Core.register(new EditEntityCommand());
Denizen2Core.register(new FlagEntityCommand());
Denizen2Core.register(new SpawnCommand());
Denizen2Core.register(new TeleportCommand());
// Commands: Player
Denizen2Core.register(new ActionBarCommand());
Denizen2Core.register(new GiveCommand());
Denizen2Core.register(new TellCommand());
// Commands: Server
Denizen2Core.register(new ExecuteCommand());
// Commands: World
Denizen2Core.register(new EditBlockCommand());
Denizen2Core.register(new PlayEffectCommand());
Denizen2Core.register(new SetBlockCommand());
// Events: Entity
Denizen2Core.register(new EntityDamagedScriptEvent());
Denizen2Core.register(new EntityMovesScriptEvent());
Denizen2Core.register(new EntitySpawnScriptEvent());
// Events: Player
Denizen2Core.register(new PlayerBreaksBlockScriptEvent());
Denizen2Core.register(new PlayerChatsScriptEvent());
Denizen2Core.register(new PlayerJoinsScriptEvent());
Denizen2Core.register(new PlayerPlacesBlockScriptEvent());
Denizen2Core.register(new PlayerRightClicksBlockScriptEvent());
Denizen2Core.register(new PlayerRightClicksEntityScriptEvent());
// Events: Server
Denizen2Core.register(new ClientPingsServerScriptEvent());
// Events: World
Denizen2Core.register(new BlockChangeScriptEvent());
// Tag Handlers: Sponge Basics
Denizen2Core.register(new BlockTypeTagBase());
Denizen2Core.register(new ColorTagBase());
Denizen2Core.register(new ContextTagBase());
Denizen2Core.register(new CuboidTagBase());
Denizen2Core.register(new EntityTagBase());
Denizen2Core.register(new EntityTypeTagBase());
Denizen2Core.register(new FormattedTextTagBase());
Denizen2Core.register(new ItemTagBase());
Denizen2Core.register(new ItemTypeTagBase());
Denizen2Core.register(new LocationTagBase());
Denizen2Core.register(new OfflinePlayerTagBase());
Denizen2Core.register(new PlayerTagBase());
Denizen2Core.register(new ServerTagBase());
Denizen2Core.register(new TextsTagBase());
Denizen2Core.register(new WorldTagBase());
// Sponge Script Types
Denizen2Core.register("command", GameCommandScript::new);
// Sponge Commands
ExCommand.register();
// Sponge related Helpers
FlagHelper.register();
// Call loading event for sub-plugins registering things
Sponge.getEventManager().post(new Denizen2SpongeLoadingEvent(getGenericCause()));
// Load Denizen2
Denizen2Core.start();
// Central loop
Sponge.getScheduler().createTaskBuilder().intervalTicks(1).execute(() -> Denizen2Core.tick(0.05)).submit(this);
// Call loaded event for sub-plugins to listen for
Sponge.getEventManager().post(new Denizen2SpongeLoadedEvent(getGenericCause()));
// TODO: Config option -> readyToSpamEvents = true;
}
/*
private static boolean readyToSpamEvents = false;
private static HashMap<Class, Integer> limiter = new HashMap<>();
// TODO: Move this block to a separate class so that we can easily unregister it!
@Listener
public void onAnyEvent(Event evt) {
if (readyToSpamEvents) {
if (evt instanceof AITaskEvent
|| evt instanceof SpawnEntityEvent
|| evt instanceof LoadChunkEvent
|| evt instanceof MoveEntityEvent) {
return;
}
if (limiter.containsKey(evt.getClass())) {
int current = limiter.get(evt.getClass());
limiter.put(evt.getClass(), current + 1);
if (current > 100) {
return;
}
}
else {
limiter.put(evt.getClass(), 1);
}
StringBuilder sb = new StringBuilder();
sb.append(evt.getClass().getCanonicalName()).append(":");
Class clz = evt.getClass().getSuperclass();
while (clz != null) {
sb.append(clz.getCanonicalName()).append(",");
clz = clz.getSuperclass();
}
if (sb.length() > 0 && sb.toString().contains("spongepowered")) {
Debug.info("EVENT OCCURRED: " + sb.toString());
}
}
}
*/
public File getMainDirectory() {
return new File("./assets/Denizen/");
}
@Listener
public void onServerStop(GameStoppedEvent event) {
// Disable Denizen2
Denizen2Core.unload();
}
private File getConfigFile() {
return new File(getMainDirectory(), "./config/config.yml");
}
private void saveDefaultConfig() {
File cf = getConfigFile();
if (!cf.exists()) {
cf.getParentFile().mkdirs();
try {
InputStream is = getClass().getResourceAsStream("default_config.yml");
String res = CoreUtilities.streamToString(is);
is.close();
PrintWriter writer = new PrintWriter(cf);
writer.write(res);
writer.close();
}
catch (IOException ex) {
Debug.exception(ex);
}
}
}
private void loadConfig() {
config = null;
try {
File cf = getConfigFile();
if (!cf.exists()) {
saveDefaultConfig();
}
config = YAMLConfiguration.load(CoreUtilities.streamToString(new FileInputStream(cf)));
}
catch (IOException ex) {
Debug.exception(ex);
}
if (config == null) {
config = new YAMLConfiguration();
}
}
}