Skip to content

Commit 9ccb054

Browse files
author
Valandur
committed
Merge branch 'release/v4.6.0'
2 parents 2d6b0b6 + 061521b commit 9ccb054

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+806
-102
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ dependencies {
5353
compileOnly files("lib/Nucleus-1.1.3-LTS-api.jar")
5454
compileOnly files("lib/HuskyCrates-v1.7.2.jar")
5555
compileOnly files("lib/Nations-2.8-S6.1-MC1.11.2.jar")
56+
compileOnly files("lib/MMCTickets-1.4.0-API-5.2-7.X.jar")
5657
}
5758

5859
shadowJar {

docs/API.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ other plugins to provide their own endpoints for data without much effort.
55

66

77
## Table of Contents
8+
1. [Setup](#setup)
89
1. [Servlets](#servlets)
910
1. [Example](#servlet-example)
1011
1. [Endpoints](#endpoints)
@@ -13,6 +14,33 @@ other plugins to provide their own endpoints for data without much effort.
1314
1. [Responses](#responses)
1415

1516

17+
<a name="setup"></a>
18+
## Setup
19+
20+
> You have to register any servlets you create with the WebAPI
21+
22+
The easiest way to do this is to use `WebAPIAPI.getServletService()`, which will return an optional
23+
containing the ServletService. If the optional is empty than the WebAPI plugin is not present or
24+
loaded on the server. If the ServletService is present you can use
25+
`service.registerServlet(Class<? extends WebAPIBaseServlet> servlet)` to register your servlet class
26+
with the WebAPI.
27+
```java
28+
public void onInitialization(GameInitializationEvent event) {
29+
...
30+
31+
Optional<IServletService> optSrv = WebAPIAPI.getServletService();
32+
if (optSrv.isPresent()) {
33+
IServletService srv = optSrv.get();
34+
srv.registerServlet(MyServlet.class);
35+
}
36+
37+
...
38+
}
39+
```
40+
41+
> This should be done in the initialization phase of your plugin, before the server starts!
42+
43+
1644
<a name="servlets"></a>
1745
## Servlets
1846
Servlets are a collection of routes. This allows you to easily group all data that belongs together.
@@ -33,6 +61,17 @@ tells the Web-API that your class is indeed a valid servlet, and which base rout
3361
- The `basePath` specifies at which path your servlet will be available, and all the other
3462
routes in your servlet will be relative to this route.The `basePath` does **not** require any slashes `/` and may **not** contain any path parameters (see below).
3563

64+
Servlets may define a `public static void onRegister()` method which will get called by the WebAPI
65+
when the servlet is registered. This is only done once, even if the user reloads the plugins
66+
on the server. This is the best place to register any custom serializers that your servlets uses:
67+
```java
68+
public static void onRegister() {
69+
JsonService json = WebAPI.getJsonService();
70+
json.registerSerializer(MyData.class, MyDataSerializer.class);
71+
}
72+
```
73+
74+
3675
<a name="servlet-example"></a>
3776
### Example
3877

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
version=4.5.0
1+
version=4.6.0
22
minecraftVersion=1.11.2
33
spongeVersion=6.1
120 KB
Binary file not shown.

src/main/java/valandur/webapi/WebAPI.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
import valandur.webapi.hook.WebHookSerializer;
7676
import valandur.webapi.hook.WebHookService;
7777
import valandur.webapi.integration.huskycrates.HuskyCratesServlet;
78+
import valandur.webapi.integration.mmctickets.MMCTicketsServlet;
7879
import valandur.webapi.integration.nations.NationsServlet;
7980
import valandur.webapi.integration.nucleus.NucleusServlet;
8081
import valandur.webapi.integration.webbhooks.WebBookServlet;
@@ -335,9 +336,9 @@ public void onInitialization(GameInitializationEvent event) {
335336
} catch (ClassNotFoundException ignored) { }
336337

337338
try {
338-
Class.forName("io.github.nucleuspowered.nucleus.api.NucleusAPI");
339-
logger.info(" Integrating with Nucleus...");
340-
servletService.registerServlet(NucleusServlet.class);
339+
Class.forName("net.moddedminecraft.mmctickets.Main");
340+
logger.info(" Integrating with MMCTickets...");
341+
servletService.registerServlet(MMCTicketsServlet.class);
341342
} catch (ClassNotFoundException ignored) { }
342343

343344
try {
@@ -346,6 +347,12 @@ public void onInitialization(GameInitializationEvent event) {
346347
servletService.registerServlet(NationsServlet.class);
347348
} catch (ClassNotFoundException ignored) { }
348349

350+
try {
351+
Class.forName("io.github.nucleuspowered.nucleus.api.NucleusAPI");
352+
logger.info(" Integrating with Nucleus...");
353+
servletService.registerServlet(NucleusServlet.class);
354+
} catch (ClassNotFoundException ignored) { }
355+
349356
try {
350357
Class.forName("de.dosmike.sponge.WebBooks.WebBooks");
351358
logger.info(" Integrating with WebBooks...");

src/main/java/valandur/webapi/cache/CacheService.java

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import valandur.webapi.api.cache.plugin.ICachedPluginContainer;
3333
import valandur.webapi.api.cache.tileentity.ICachedTileEntity;
3434
import valandur.webapi.api.cache.world.ICachedWorld;
35+
import valandur.webapi.api.permission.IPermissionService;
3536
import valandur.webapi.cache.chat.CachedChatMessage;
3637
import valandur.webapi.cache.command.CachedCommand;
3738
import valandur.webapi.cache.command.CachedCommandCall;
@@ -45,7 +46,6 @@
4546
import valandur.webapi.cache.world.CachedChunk;
4647
import valandur.webapi.cache.world.CachedWorld;
4748
import valandur.webapi.json.JsonService;
48-
import valandur.webapi.permission.PermissionService;
4949
import valandur.webapi.util.Util;
5050

5151
import java.lang.reflect.Field;
@@ -54,6 +54,7 @@
5454
import java.util.*;
5555
import java.util.concurrent.ConcurrentHashMap;
5656
import java.util.concurrent.ConcurrentLinkedQueue;
57+
import java.util.function.Predicate;
5758

5859
public class CacheService implements ICacheService {
5960

@@ -259,6 +260,25 @@ public Collection<ICachedPlayer> getPlayers() {
259260
return new ArrayList<>(players.values());
260261
}
261262
@Override
263+
public Optional<ICachedPlayer> getPlayer(String nameOrUuid) {
264+
if (Util.isValidUUID(nameOrUuid)) {
265+
return getPlayer(UUID.fromString(nameOrUuid));
266+
}
267+
268+
Optional<CachedPlayer> player = players.values().stream().filter(p -> p.getName().equalsIgnoreCase(nameOrUuid)).findAny();
269+
if (player.isPresent())
270+
return player.flatMap(p -> getPlayer(p.getUUID()));
271+
272+
return WebAPI.runOnMain(() -> {
273+
Optional<UserStorageService> optSrv = Sponge.getServiceManager().provide(UserStorageService.class);
274+
if (!optSrv.isPresent())
275+
return null;
276+
277+
Optional<User> optUser = optSrv.get().get(nameOrUuid);
278+
return optUser.<ICachedPlayer>map(CachedPlayer::new).orElse(null);
279+
});
280+
}
281+
@Override
262282
public Optional<ICachedPlayer> getPlayer(UUID uuid) {
263283
if (!players.containsKey(uuid)) {
264284
return WebAPI.runOnMain(() -> {
@@ -267,10 +287,8 @@ public Optional<ICachedPlayer> getPlayer(UUID uuid) {
267287
return null;
268288

269289
Optional<User> optUser = optSrv.get().get(uuid);
270-
if (!optUser.isPresent())
271-
return null;
290+
return optUser.<ICachedPlayer>map(CachedPlayer::new).orElse(null);
272291

273-
return new CachedPlayer(optUser.get());
274292
});
275293
}
276294

@@ -411,32 +429,47 @@ public ICachedCommand updateCommand(CommandMapping command) {
411429
}
412430

413431
@Override
414-
public Optional<Collection<ICachedTileEntity>> getTileEntities() {
432+
public Optional<Collection<ICachedTileEntity>> getTileEntities(Predicate<TileEntity> predicate, int limit) {
415433
return WebAPI.runOnMain(() -> {
416434
Collection<ICachedTileEntity> entities = new LinkedList<>();
417435

418-
for (World world : Sponge.getServer().getWorlds()) {
419-
Collection<TileEntity> ents = world.getTileEntities();
436+
int i = 0;
437+
Collection<World> worlds = Sponge.getServer().getWorlds();
438+
for (World world : worlds) {
439+
Collection<TileEntity> ents = world.getTileEntities(predicate);
420440
for (TileEntity te : ents) {
441+
if (!te.isValid()) continue;
421442
entities.add(new CachedTileEntity(te));
443+
444+
i++;
445+
if (limit > 0 && i >= limit)
446+
break;
422447
}
448+
449+
if (limit > 0 && i >= limit)
450+
break;
423451
}
424452

425453
return entities;
426454
});
427455
}
428456
@Override
429-
public Optional<Collection<ICachedTileEntity>> getTileEntities(ICachedWorld world) {
457+
public Optional<Collection<ICachedTileEntity>> getTileEntities(ICachedWorld world, Predicate<TileEntity> predicate, int limit) {
430458
return WebAPI.runOnMain(() -> {
431459
Optional<?> w = world.getLive();
432460
if (!w.isPresent())
433461
return null;
434462

463+
int i = 0;
435464
Collection<ICachedTileEntity> entities = new LinkedList<>();
436-
Collection<TileEntity> ents = ((World)w.get()).getTileEntities();
465+
Collection<TileEntity> ents = ((World)w.get()).getTileEntities(predicate);
437466
for (TileEntity te : ents) {
438467
if (!te.isValid()) continue;
439468
entities.add(new CachedTileEntity(te));
469+
470+
i++;
471+
if (limit > 0 && i >= limit)
472+
break;
440473
}
441474

442475
return entities;
@@ -533,7 +566,7 @@ public Tuple<Map<String, JsonNode>, Map<String, JsonNode>> getExtraData(ICachedO
533566

534567
try {
535568
Object res = field.get().get(obj);
536-
fields.put(fieldName, json.toJson(res, true, PermissionService.permitAllNode()));
569+
fields.put(fieldName, json.toJson(res, true, IPermissionService.permitAllNode()));
537570
} catch (IllegalAccessException e) {
538571
fields.put(fieldName, TextNode.valueOf("ERROR: " + e.toString()));
539572
}
@@ -559,7 +592,7 @@ public Tuple<Map<String, JsonNode>, Map<String, JsonNode>> getExtraData(ICachedO
559592

560593
try {
561594
Object res = method.get().invoke(obj);
562-
methods.put(methodName, json.toJson(res, true, PermissionService.permitAllNode()));
595+
methods.put(methodName, json.toJson(res, true, IPermissionService.permitAllNode()));
563596
} catch (IllegalAccessException | InvocationTargetException e) {
564597
methods.put(methodName, TextNode.valueOf("ERROR: " + e.toString()));
565598
}

src/main/java/valandur/webapi/cache/misc/CachedLocation.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ public Vector3d getPosition() {
2525
}
2626

2727

28+
public CachedLocation(ICachedWorld world, double x, double y, double z) {
29+
super(null);
30+
31+
this.world = world;
32+
this.position = new Vector3d(x, y, z);
33+
}
2834
public CachedLocation(Location<World> location) {
2935
super(null);
3036

src/main/java/valandur/webapi/extension/ExtensionService.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package valandur.webapi.extension;
22

3-
import io.sentry.Sentry;
43
import org.slf4j.Logger;
54
import valandur.webapi.WebAPI;
65
import valandur.webapi.api.extension.IExtensionService;
@@ -30,11 +29,15 @@ public class ExtensionService implements IExtensionService {
3029
public void init() {
3130
// Relocated packages
3231
relocatedPackages.clear();
33-
relocatedPackages.put("import org.eclipse.jetty", "import valandur.webapi.shadow.org.eclipse.jetty");
34-
relocatedPackages.put("import com.fasterxml.jackson", "import valandur.webapi.shadow.fasterxml.jackson");
35-
relocatedPackages.put("import javax.servlet", "import valandur.webapi.shadow.javax.servlet");
36-
relocatedPackages.put("import org.reflections", "import valandur.webapi.shadow.org.reflections");
37-
relocatedPackages.put("import net.jodah", "import valandur.webapi.shadow.net.jodah");
32+
relocatedPackages.put("import io.sentry", "import valandur.webapi.shadow.io.sentry");
33+
relocatedPackages.put("import org.eclipse", "import valandur.webapi.shadow.org.eclipse");
34+
relocatedPackages.put("import com.fasterxml", "import valandur.webapi.shadow.fasterxml");
35+
relocatedPackages.put("import javax.servlet", "import valandur.webapi.shadow.javax.servlet");
36+
relocatedPackages.put("import net.jodah", "import valandur.webapi.shadow.net.jodah");
37+
relocatedPackages.put("import org.mindrot", "import valandur.webapi.shadow.org.mindrot");
38+
relocatedPackages.put("import edu.umdh", "import valandur.webapi.shadow.edu.umd");
39+
relocatedPackages.put("import javassist", "import valandur.webapi.shadow.javassist");
40+
relocatedPackages.put("import net.jcip", "import valandur.webapi.shadow.net.jcip");
3841
}
3942

4043
public <T> void loadPlugins(String pkg, Class<T> baseClass, Consumer<Class<T>> done) {

src/main/java/valandur/webapi/hook/WebHook.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import valandur.webapi.api.hook.IWebHook;
44
import valandur.webapi.api.hook.WebAPIBaseFilter;
55
import valandur.webapi.api.hook.WebHookHeader;
6+
import valandur.webapi.api.permission.IPermissionService;
67
import valandur.webapi.api.util.TreeNode;
7-
import valandur.webapi.permission.PermissionService;
88

99
import java.util.List;
1010

@@ -40,7 +40,7 @@ public boolean includeDetails() {
4040
return details;
4141
}
4242

43-
private TreeNode<String, Boolean> permissions = PermissionService.emptyNode();
43+
private TreeNode<String, Boolean> permissions = IPermissionService.emptyNode();
4444
public TreeNode<String, Boolean> getPermissions() {
4545
return permissions;
4646
}

src/main/java/valandur/webapi/hook/WebHookSerializer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
import valandur.webapi.WebAPI;
99
import valandur.webapi.api.hook.WebAPIBaseFilter;
1010
import valandur.webapi.api.hook.WebHookHeader;
11+
import valandur.webapi.api.permission.IPermissionService;
1112
import valandur.webapi.api.util.TreeNode;
12-
import valandur.webapi.permission.PermissionService;
1313

1414
import java.lang.reflect.Constructor;
1515
import java.lang.reflect.InvocationTargetException;
@@ -37,7 +37,7 @@ public WebHook deserialize(TypeToken<?> type, ConfigurationNode value) throws Ob
3737
String filterName = filterBase.getNode("name").getString();
3838
ConfigurationNode filterConfig = filterBase.getNode("config");
3939

40-
TreeNode<String, Boolean> permissions = PermissionService.permitAllNode();
40+
TreeNode<String, Boolean> permissions = IPermissionService.permitAllNode();
4141

4242
if (!enabled) {
4343
logger.info(" -> Disabled");

0 commit comments

Comments
 (0)