Skip to content

Commit 7983fb6

Browse files
committed
zAPI 2.0.2
1 parent fa47ab8 commit 7983fb6

5 files changed

Lines changed: 67 additions & 58 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>me.yleoft</groupId>
88
<artifactId>zAPI</artifactId>
9-
<version>2.0.1</version>
9+
<version>2.0.2</version>
1010
<description>A library for creating Paper-based plugins.</description>
1111
<url>https://github.com/yL3oft/zAPI</url>
1212

src/main/java/me/yleoft/zAPI/command/CommandBasis.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,17 @@ final class ParameterParseResult {
189189
}
190190

191191
String[] paramArgs = Arrays.copyOfRange(args, start, start + taken);
192+
193+
// Whitelist validation
194+
for (String arg : paramArgs) {
195+
if (!param.isWhitelisted(arg)) {
196+
if (param.whitelistMessage() != null) {
197+
message(sender, param.whitelistMessage());
198+
}
199+
return new ParameterParseResult(new String[0], true);
200+
}
201+
}
202+
192203
alreadyUsed.add(param);
193204

194205
if (sender instanceof Player p) {

src/main/java/me/yleoft/zAPI/command/Parameter.java

Lines changed: 22 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,70 +2,45 @@
22

33
import org.bukkit.command.CommandSender;
44
import org.jetbrains.annotations.NotNull;
5+
import org.jetbrains.annotations.Nullable;
56

67
import java.util.List;
8+
import java.util.Set;
9+
import java.util.stream.Collectors;
710

8-
/**
9-
* Represents a command parameter with its properties.
10-
*/
1111
public interface Parameter {
1212

13-
/**
14-
* The name of the parameter.
15-
*
16-
* @return The name of the parameter.
17-
*/
1813
@NotNull String name();
1914

20-
/**
21-
* The aliases of the parameter.
22-
*
23-
* @return The aliases of the parameter.
24-
*/
2515
default @NotNull List<String> aliases() { return List.of(); }
2616

27-
/**
28-
* The permission required to use the parameter.
29-
*
30-
* @return The permission required to use the parameter.
31-
*/
3217
default String permission() { return null; }
3318

34-
/**
35-
* Whether to stop processing sub-commands after this parameter is executed.
36-
*
37-
* @return true to stop processing sub-commands, false otherwise.
38-
*/
3919
default boolean stopSubCommands() { return false; }
4020

41-
/**
42-
* The minimum number of arguments required for this parameter.
43-
*
44-
* @return The minimum number of arguments required for this parameter.
45-
*/
4621
default int minArgs() { return 0; }
4722

48-
/**
49-
* The maximum number of arguments allowed for this parameter.
50-
*
51-
* @return The maximum number of arguments allowed for this parameter.
52-
*/
5323
default int maxArgs() { return Integer.MAX_VALUE; }
5424

55-
/**
56-
* Called when the parameter is executed.
57-
* @param sender The command sender
58-
* @param fullArgs All args (same as Bukkit gives)
59-
* @param parameterArgs The args already typed for THIS parameter (not including the "-name" token)
60-
*/
61-
default void execute(@NotNull CommandSender sender, @NotNull String[] fullArgs, @NotNull String[] parameterArgs) {};
62-
63-
/**
64-
* Called for tab completion when the user is currently typing arguments for this parameter.
65-
* @param sender The command sender
66-
* @param fullArgs All args (same as Bukkit gives)
67-
* @param parameterArgs The args already typed for THIS parameter (not including the "-name" token)
68-
*/
25+
@Nullable
26+
default Set<String> whitelist() { return null; }
27+
28+
@Nullable
29+
default String whitelistMessage() { return null; }
30+
31+
default boolean isWhitelisted(@NotNull String value) {
32+
Set<String> whitelist = whitelist();
33+
if (whitelist == null || whitelist.isEmpty()) return true;
34+
35+
Set<String> lowercaseWhitelist = whitelist.stream()
36+
.map(String::toLowerCase)
37+
.collect(Collectors.toSet());
38+
39+
return lowercaseWhitelist.contains(value.toLowerCase());
40+
}
41+
42+
default void execute(@NotNull CommandSender sender, @NotNull String[] fullArgs, @NotNull String[] parameterArgs) {}
43+
6944
default @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String[] fullArgs, @NotNull String[] parameterArgs) {
7045
return List.of();
7146
}

src/main/java/me/yleoft/zAPI/location/LocationHandler.java

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package me.yleoft.zAPI.location;
22

3+
import me.yleoft.zAPI.zAPI;
34
import org.bukkit.Bukkit;
45
import org.bukkit.Location;
56
import org.bukkit.Material;
@@ -9,12 +10,10 @@
910
import org.jetbrains.annotations.Nullable;
1011

1112
import java.util.*;
13+
import java.util.concurrent.CompletableFuture;
1214

1315
import static java.util.Objects.requireNonNull;
1416

15-
/**
16-
* Utility class for location-related operations.
17-
*/
1817
public final class LocationHandler {
1918

2019
private static final EnumSet<Material> blacklistedGround = EnumSet.noneOf(Material.class);
@@ -154,6 +153,20 @@ public static Location findNearestSafeLocation(@NotNull final Location origin, i
154153
return bestLoc;
155154
}
156155

156+
@NotNull
157+
public static CompletableFuture<Location> findNearestSafeLocationAsync(@NotNull final Location origin, int radius, int heightCheckRange) {
158+
CompletableFuture<Location> future = new CompletableFuture<>();
159+
zAPI.getScheduler().runAtLocation(origin, task -> {
160+
try {
161+
Location result = findNearestSafeLocation(origin, radius, heightCheckRange);
162+
future.complete(result);
163+
} catch (Exception e) {
164+
future.completeExceptionally(e);
165+
}
166+
});
167+
return future;
168+
}
169+
157170
/**
158171
* Checks if the given location is safe for a player to stand on.
159172
* @param loc the location to check
@@ -170,6 +183,20 @@ public static boolean isSafeLocation(@NotNull final Location loc) {
170183
return isAirOrNonSolid(feet) && isAirOrNonSolid(head) && isSafeGround(ground);
171184
}
172185

186+
@NotNull
187+
public static CompletableFuture<Boolean> isSafeLocationAsync(@NotNull final Location loc) {
188+
CompletableFuture<Boolean> future = new CompletableFuture<>();
189+
zAPI.getScheduler().runAtLocation(loc, task -> {
190+
try {
191+
boolean result = isSafeLocation(loc);
192+
future.complete(result);
193+
} catch (Exception e) {
194+
future.completeExceptionally(e);
195+
}
196+
});
197+
return future;
198+
}
199+
173200
/**
174201
* Checks if the given block is air or a non-solid block.
175202
* @param block the block to check
@@ -189,7 +216,7 @@ private static boolean isSafeGround(@NotNull final Block block) {
189216
return block.getType().isSolid() && !block.isLiquid() && !blacklistedGround.contains(type);
190217
}
191218

192-
private static class MutableBlockLocation {
219+
public static class MutableBlockLocation {
193220
private final World world;
194221
private int x, y, z;
195222

@@ -235,6 +262,4 @@ public Location toLocation() {
235262
return new Location(world, x, y, z);
236263
}
237264
}
238-
239-
240265
}

src/main/java/me/yleoft/zAPI/utility/ExternalDependencyManager.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ public Map<String, Path> downloadAll() throws IOException, InterruptedException
119119
public URLClassLoader loadIsolated(String id, ClassLoader parent) throws IOException, InterruptedException {
120120
Path jar = download(id);
121121
URL url = jar.toUri().toURL();
122-
ClassLoader p = (parent != null) ? parent : Thread.currentThread().getContextClassLoader();
123-
return new URLClassLoader(new URL[]{url}, p);
122+
return new URLClassLoader(new URL[]{url}, parent);
124123
}
125124

126125
/**
@@ -132,8 +131,7 @@ public URLClassLoader loadAllIntoOneIsolatedLoader(ClassLoader parent) throws IO
132131
for (String id : sortedIds()) {
133132
urls.add(download(id).toUri().toURL());
134133
}
135-
ClassLoader p = (parent != null) ? parent : Thread.currentThread().getContextClassLoader();
136-
return new URLClassLoader(urls.toArray(URL[]::new), p);
134+
return new URLClassLoader(urls.toArray(URL[]::new), parent);
137135
}
138136

139137
/**

0 commit comments

Comments
 (0)