Skip to content

Commit

Permalink
Fix a Java 11 compat issue, add an option for default duration units
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed May 14, 2023
1 parent f8004ec commit 33cdd6f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<release>8</release>
<source>1.8</source>
<target>1.8</target>
</configuration>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/citizensnpcs/api/ai/goals/WanderGoal.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ public boolean shouldExecute() {
npc.getNavigator().getLocalParameters().addSingleUseCallback((reason) -> forceFinish = true);
} else {
Random random = new Random();
dest.setX(dest.getX() + random.nextDouble(0.5));
dest.setZ(dest.getZ() + random.nextDouble(0.5));
dest.setX(dest.getX() + random.nextDouble() * 0.5);
dest.setZ(dest.getZ() + random.nextDouble() * 0.5);
movingTicks = 20 + random.nextInt(20);
}
this.target = dest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -61,6 +62,7 @@ public class CommandManager implements TabCompleter {
* (one for each alias) with the method.
*/
private final Map<String, CommandInfo> commands = Maps.newHashMap();
private TimeUnit defaultDurationUnits;
private Injector injector;

public CommandManager() {
Expand Down Expand Up @@ -181,7 +183,7 @@ private void executeCommand(String[] args, CommandSender sender, Object[] method
} else if (desiredType == UUID.class) {
val = UUID.fromString(val.toString());
} else if (desiredType == Duration.class) {
val = SpigotUtil.parseDuration(val.toString());
val = SpigotUtil.parseDuration(val.toString(), defaultDurationUnits);
}
methodArgs[entry.getKey()] = val;
}
Expand Down Expand Up @@ -597,6 +599,10 @@ private void sendSpecificHelp(CommandSender sender, String rootCommand, String m
Messaging.send(sender, "<aqua>" + help);
}

public void setDefaultDurationUnits(TimeUnit unit) {
this.defaultDurationUnits = unit;
}

public void setInjector(Injector injector) {
this.injector = injector;
}
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/net/citizensnpcs/api/util/SpigotUtil.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.citizensnpcs.api.util;

import java.time.Duration;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

import org.bukkit.Bukkit;
Expand Down Expand Up @@ -53,10 +54,15 @@ public static boolean isUsing1_13API() {
return using1_13API;
}

public static Duration parseDuration(String raw) {
Integer ticks = Ints.tryParse(raw.endsWith("t") ? raw.substring(0, raw.length() - 1) : raw);
if (ticks != null) {
return Duration.ofMillis(ticks * 50);
public static Duration parseDuration(String raw, TimeUnit defaultUnits) {
if (defaultUnits == null) {
Integer ticks = Ints.tryParse(raw);
if (ticks != null) {
return Duration.ofMillis(ticks * 50);
}
}
if (raw.endsWith("t")) {
return Duration.ofMillis(Integer.parseInt(raw.substring(0, raw.length() - 1)) * 50);
}
raw = NUMBER_MATCHER.matcher(raw).replaceFirst("P$1T").replace("min", "m").replace("hr", "h");
if (raw.charAt(0) != 'P') {
Expand Down

0 comments on commit 33cdd6f

Please sign in to comment.