Bukkit command cleanup/fixes

This commit is contained in:
dfsek
2021-03-10 03:03:04 -07:00
parent 513c6a647f
commit a584ac2401
4 changed files with 21 additions and 2 deletions

View File

@@ -53,4 +53,15 @@ public interface TerraPlugin extends LoaderRegistrar {
default String getVersion() {
return "@VERSION@";
}
/**
* Runs a task that may or may not be thread safe, depending on platform.
* <p>
* Allows platforms to define what code is safe to be run asynchronously.
*
* @param task Task to be run.
*/
default void runPossiblyUnsafeTask(Runnable task) {
task.run();
}
}

View File

@@ -69,7 +69,7 @@ public class BiomeLocateCommand implements CommandTemplate {
if(location != null) {
sender.sendMessage(String.format("The nearest %s is at [%d, ~, %d] (%.1f blocks away)", biome.getID().toLowerCase(Locale.ROOT), location.getBlockX(), location.getBlockZ(), location.add(new Vector3(0, player.getLocation().getY(), 0)).distance(player.getLocation().toVector())));
if(teleport) {
player.setLocation(new Location(player.getWorld(), location.getX(), player.getLocation().getY(), location.getZ()));
main.runPossiblyUnsafeTask(() -> player.setLocation(new Location(player.getWorld(), location.getX(), player.getLocation().getY(), location.getZ())));
}
} else LangUtil.send("command.biome.unable-to-locate", sender);
}, main), "Biome Location Thread").start();

View File

@@ -134,6 +134,11 @@ public class TerraBukkitPlugin extends JavaPlugin implements TerraPlugin {
return eventManager;
}
@Override
public void runPossiblyUnsafeTask(Runnable task) {
Bukkit.getScheduler().runTask(this, task);
}
@Override
public void onDisable() {
BukkitChunkGeneratorWrapper.saveAll();

View File

@@ -14,6 +14,8 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
public class BukkitCommandAdapter implements CommandExecutor, TabCompleter {
private final CommandManager manager;
@@ -42,7 +44,8 @@ public class BukkitCommandAdapter implements CommandExecutor, TabCompleter {
List<String> argList = new ArrayList<>(Arrays.asList(args));
try {
return manager.tabComplete(argList.remove(0), BukkitAdapter.adapt(sender), argList);
return manager.tabComplete(argList.remove(0), BukkitAdapter.adapt(sender), argList).stream()
.filter(s -> s.toLowerCase(Locale.ROOT).startsWith(args[args.length - 1].toLowerCase(Locale.ROOT))).sorted(String::compareTo).collect(Collectors.toList());
} catch(CommandException e) {
e.printStackTrace();
return Collections.emptyList();