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
353 lines (326 loc) · 15.8 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package com.denizenscript.denizen2sponge;
import com.denizenscript.denizen2core.Denizen2Core;
import com.denizenscript.denizen2core.tags.objects.MapTag;
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.*;
import com.denizenscript.denizen2sponge.commands.items.CreateInventoryCommand;
import com.denizenscript.denizen2sponge.commands.items.ForgetInventoryCommand;
import com.denizenscript.denizen2sponge.commands.items.RememberInventoryCommand;
import com.denizenscript.denizen2sponge.commands.player.*;
import com.denizenscript.denizen2sponge.commands.server.AnnounceCommand;
import com.denizenscript.denizen2sponge.commands.server.ExecuteCommand;
import com.denizenscript.denizen2sponge.commands.server.SaveDataCommand;
import com.denizenscript.denizen2sponge.commands.server.ShutdownCommand;
import com.denizenscript.denizen2sponge.commands.world.*;
import com.denizenscript.denizen2sponge.events.entity.*;
import com.denizenscript.denizen2sponge.events.player.*;
import com.denizenscript.denizen2sponge.events.server.ClientPingsServerScriptEvent;
import com.denizenscript.denizen2sponge.events.server.CommandSentScriptEvent;
import com.denizenscript.denizen2sponge.events.server.InternalScriptEvent;
import com.denizenscript.denizen2sponge.events.server.ServerStopsScriptEvent;
import com.denizenscript.denizen2sponge.events.world.*;
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.tags.objects.*;
import com.denizenscript.denizen2sponge.utilities.GameRules;
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.EventContext;
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.*;
import java.util.HashMap;
/**
* 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() {
// TODO: Decipher new cause system, make a nice generic cause
return Cause.builder().append(plugin).build(EventContext.empty());
}
public static Text parseColor(String inp) {
return TextSerializers.formattingCode(Denizen2Sponge.colorChar).deserialize(inp);
}
public static HashMap<String, InventoryTag> rememberedInventories = new HashMap<>();
@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, addons, and data folders exist
Denizen2Core.getImplementation().getScriptsFolder().mkdirs();
Denizen2Core.getImplementation().getAddonsFolder().mkdirs();
Denizen2Core.getImplementation().getScriptDataFolder().mkdirs();
// Commands: Entity
Denizen2Core.register(new AirCommand());
Denizen2Core.register(new CastCommand());
Denizen2Core.register(new DropCommand());
Denizen2Core.register(new EditEntityCommand());
Denizen2Core.register(new EquipCommand());
Denizen2Core.register(new FlagCommand());
Denizen2Core.register(new HealCommand());
Denizen2Core.register(new HurtCommand());
Denizen2Core.register(new LookAtCommand());
Denizen2Core.register(new MountCommand());
Denizen2Core.register(new RemoveCommand());
Denizen2Core.register(new SpawnCommand());
Denizen2Core.register(new TeleportCommand());
Denizen2Core.register(new UnflagCommand());
// Commands: Item
Denizen2Core.register(new CreateInventoryCommand());
Denizen2Core.register(new ForgetInventoryCommand());
Denizen2Core.register(new RememberInventoryCommand());
// Commands: Player
Denizen2Core.register(new ActionBarCommand());
Denizen2Core.register(new BanCommand());
Denizen2Core.register(new FeedCommand());
Denizen2Core.register(new GamemodeCommand());
Denizen2Core.register(new GiveCommand());
Denizen2Core.register(new HotbarCommand());
Denizen2Core.register(new KickCommand());
Denizen2Core.register(new NarrateCommand());
Denizen2Core.register(new PardonCommand());
Denizen2Core.register(new TellCommand());
Denizen2Core.register(new TitleCommand());
// Commands: Server
Denizen2Core.register(new AnnounceCommand());
Denizen2Core.register(new ExecuteCommand());
Denizen2Core.register(new SaveDataCommand());
Denizen2Core.register(new ShutdownCommand());
// Commands: World
Denizen2Core.register(new DeleteWorldCommand());
Denizen2Core.register(new DifficultyCommand());
Denizen2Core.register(new EditBlockCommand());
Denizen2Core.register(new ExplodeCommand());
Denizen2Core.register(new LoadWorldCommand());
Denizen2Core.register(new PlayEffectCommand());
Denizen2Core.register(new PlaySoundCommand());
Denizen2Core.register(new RemoveGameRuleCommand());
Denizen2Core.register(new SetBlockCommand());
Denizen2Core.register(new SetGameRuleCommand());
Denizen2Core.register(new UnloadWorldCommand());
Denizen2Core.register(new WeatherCommand());
// Events: Entity
Denizen2Core.register(new EntityDamagedScriptEvent());
Denizen2Core.register(new EntityDiesScriptEvent());
Denizen2Core.register(new EntityEntersAreaScriptEvent());
Denizen2Core.register(new EntityKilledScriptEvent());
Denizen2Core.register(new EntityLeavesAreaScriptEvent());
Denizen2Core.register(new EntityMovesScriptEvent());
Denizen2Core.register(new EntitySpawnScriptEvent());
Denizen2Core.register(new ProjectileHitsBlockScriptEvent());
// Events: Player
Denizen2Core.register(new ExperienceChangesScriptEvent());
Denizen2Core.register(new LevelChangesScriptEvent());
Denizen2Core.register(new PlayerBreaksBlockScriptEvent());
Denizen2Core.register(new PlayerChatsScriptEvent());
Denizen2Core.register(new PlayerDisconnectsScriptEvent());
Denizen2Core.register(new PlayerFinishesUsingItemScriptEvent());
Denizen2Core.register(new PlayerJoinsScriptEvent());
Denizen2Core.register(new PlayerKeepsUsingItemScriptEvent());
Denizen2Core.register(new PlayerLeftClicksBlockScriptEvent());
Denizen2Core.register(new PlayerLeftClicksEntityScriptEvent());
Denizen2Core.register(new PlayerLeftClicksScriptEvent());
Denizen2Core.register(new PlayerPlacesBlockScriptEvent());
Denizen2Core.register(new PlayerRightClicksBlockScriptEvent());
Denizen2Core.register(new PlayerRightClicksEntityScriptEvent());
Denizen2Core.register(new PlayerRightClicksScriptEvent());
Denizen2Core.register(new PlayerStartsUsingItemScriptEvent());
Denizen2Core.register(new PlayerStopsUsingItemScriptEvent());
Denizen2Core.register(new StatisticChangesScriptEvent());
// Events: Server
Denizen2Core.register(new ClientPingsServerScriptEvent());
Denizen2Core.register(new CommandSentScriptEvent());
Denizen2Core.register(new InternalScriptEvent());
Denizen2Core.register(new ServerStopsScriptEvent());
// Events: World
Denizen2Core.register(new BlockChangesScriptEvent());
Denizen2Core.register(new BlockFadesScriptEvent());
Denizen2Core.register(new ExplosionDetonatesScriptEvent());
Denizen2Core.register(new PortalFormedScriptEvent());
Denizen2Core.register(new WeatherChangesScriptEvent());
Denizen2Core.register(new WorldLoadsScriptEvent());
Denizen2Core.register(new WorldUnloadsScriptEvent());
// Tag Handlers: Sponge Basics
Denizen2Core.register(new AmpersandTagBase());
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 InventoryTagBase());
Denizen2Core.register(new ItemTagBase());
Denizen2Core.register(new ItemTypeTagBase());
Denizen2Core.register(new LocationTagBase());
Denizen2Core.register(new PlayerTagBase());
Denizen2Core.register(new ServerTagBase());
Denizen2Core.register(new TextsTagBase());
Denizen2Core.register(new WorldTagBase());
// Sponge Script Types
Denizen2Core.register("command", GameCommandScript::new);
// Tag Types
Denizen2Core.customSaveLoaders.put("BlockTypeTag", BlockTypeTag::getFor);
Denizen2Core.customSaveLoaders.put("CuboidTag", CuboidTag::getFor);
Denizen2Core.customSaveLoaders.put("EntityTag", EntityTag::getFor);
Denizen2Core.customSaveLoaders.put("EntityTypeTag", EntityTypeTag::getFor);
Denizen2Core.customSaveLoaders.put("FormattedTextTag", FormattedTextTag::getFor);
Denizen2Core.customSaveLoaders.put("InventoryTag", InventoryTag::getFor);
Denizen2Core.customSaveLoaders.put("ItemTag", ItemTag::getFor);
Denizen2Core.customSaveLoaders.put("ItemTypeTag", ItemTypeTag::getFor);
Denizen2Core.customSaveLoaders.put("LocationTag", LocationTag::getFor);
Denizen2Core.customSaveLoaders.put("PlayerTag", PlayerTag::getFor);
Denizen2Core.customSaveLoaders.put("WorldTag", WorldTag::getFor);
Denizen2Core.customSaveLoaders.put("ServerBaseTag", (e, s) -> new ServerTagBase.ServerBaseTag());
Denizen2Core.customSaveLoaders.put("TextsBaseTag", (e, s) -> new TextsTagBase.TextsBaseTag());
// Sponge Commands
ExCommand.register();
// Sponge related Helpers
FlagHelper.register();
GameRules.init();
// Server Flags
loadServerFlags();
// 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;
}
public File getMainDirectory() {
return new File("./config/denizen/");
}
@Listener
public void onServerStop(GameStoppedEvent event) {
// Save server data
saveServerFlags();
// Disable Denizen2
Denizen2Core.unload();
}
public File getConfigFile() {
return new File(getMainDirectory(), "./config/config.yml");
}
public void loadServerFlags() {
try {
if (!getServerFlagsFile().exists()) {
serverFlagMap = new MapTag();
return;
}
InputStream is = new FileInputStream(getServerFlagsFile());
String str = CoreUtilities.streamToString(is);
is.close();
serverFlagMap = (MapTag) Denizen2Core.loadFromSaved(Debug::error, str);
}
catch (Exception e) {
Debug.exception(e);
}
}
public void saveServerFlags() {
try {
String flags = serverFlagMap.savable();
OutputStream os = new FileOutputStream(getServerFlagsFile(), false);
OutputStreamWriter osw = new OutputStreamWriter(os);
osw.write(flags);
osw.flush();
os.flush();
osw.close();
os.close();
}
catch (Exception e) {
Debug.exception(e);
}
}
public MapTag serverFlagMap = new MapTag();
public File getServerFlagsFile() {
return new File(getMainDirectory(), "./server_flags.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();
}
}
}