Complete bukkit plugin implementation

This commit is contained in:
Blue (Lukas Rieger) 2020-01-18 00:59:51 +01:00
parent 3daab62714
commit 0bb50f8044
5 changed files with 276 additions and 18 deletions

View File

@ -17,15 +17,17 @@ public BukkitCommandSource(CommandSender delegate) {
@Override
public void sendMessage(Text text) {
if (delegate instanceof Player) {
Player player = (Player) delegate;
Bukkit.getScheduler().runTask(BukkitPlugin.getInstance(), () -> {
if (delegate instanceof Player) {
Player player = (Player) delegate;
//kinda hacky but works
Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "tellraw " + player.getName() + " " + text.toJSONString());
return;
}
//kinda hacky but works
Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), "tellraw " + player.getName() + " " + text.toJSONString());
return;
}
delegate.sendMessage(text.toFormattingCodedString('§'));
delegate.sendMessage(text.toFormattingCodedString('§'));
});
}
}

View File

@ -1,24 +1,209 @@
package de.bluecolored.bluemap.bukkit;
import org.bukkit.command.Command;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import com.flowpowered.math.vector.Vector3i;
import de.bluecolored.bluemap.common.plugin.Commands;
import de.bluecolored.bluemap.common.plugin.serverinterface.CommandSource;
import de.bluecolored.bluemap.common.plugin.text.Text;
import de.bluecolored.bluemap.common.plugin.text.TextColor;
public class BukkitCommands implements CommandExecutor {
private Commands commands;
private Commands bluemapCommands;
private Collection<Command> commands;
public BukkitCommands(Commands commands) {
this.commands = commands;
this.bluemapCommands = commands;
this.commands = new ArrayList<>();
initCommands();
}
private void initCommands() {
commands.add(new Command("bluemap.status") {
@Override
public boolean execute(CommandSender sender, CommandSource source, String[] args) {
if (args.length != 0) return false;
bluemapCommands.executeRootCommand(source);
return true;
}
});
commands.add(new Command("bluemap.reload", "reload") {
@Override
public boolean execute(CommandSender sender, CommandSource source, String[] args) {
if (args.length != 0) return false;
bluemapCommands.executeReloadCommand(source);
return true;
}
});
commands.add(new Command("bluemap.pause", "pause") {
@Override
public boolean execute(CommandSender sender, CommandSource source, String[] args) {
if (args.length != 0) return false;
bluemapCommands.executePauseCommand(source);
return true;
}
});
commands.add(new Command("bluemap.resume", "resume") {
@Override
public boolean execute(CommandSender sender, CommandSource source, String[] args) {
if (args.length != 0) return false;
bluemapCommands.executeResumeCommand(source);
return true;
}
});
commands.add(new Command("bluemap.rendertask.create.world", "render") {
@Override
public boolean execute(CommandSender sender, CommandSource source, String[] args) {
if (args.length > 1) return false;
World world;
if (args.length == 1) {
world = Bukkit.getWorld(args[0]);
if (world == null) {
source.sendMessage(Text.of(TextColor.RED, "There is no world named '" + args[0] + "'!"));
return true;
}
} else {
if (sender instanceof Player) {
Player player = (Player) sender;
world = player.getWorld();
} else {
source.sendMessage(Text.of(TextColor.RED, "Since you are not a player, you have to specify a world!"));
return true;
}
}
bluemapCommands.executeRenderWorldCommand(source, world.getUID());
return true;
}
});
commands.add(new Command("bluemap.rendertask.prioritize", "render", "prioritize") {
@Override
public boolean execute(CommandSender sender, CommandSource source, String[] args) {
if (args.length != 1) return false;
try {
UUID uuid = UUID.fromString(args[0]);
bluemapCommands.executePrioritizeRenderTaskCommand(source, uuid);
return true;
} catch (IllegalArgumentException ex) {
source.sendMessage(Text.of(TextColor.RED, "'" + args[0] + "' is not a valid UUID!"));
return true;
}
}
});
commands.add(new Command("bluemap.rendertask.remove", "render", "remove") {
@Override
public boolean execute(CommandSender sender, CommandSource source, String[] args) {
if (args.length != 1) return false;
try {
UUID uuid = UUID.fromString(args[0]);
bluemapCommands.executeRemoveRenderTaskCommand(source, uuid);
return true;
} catch (IllegalArgumentException ex) {
source.sendMessage(Text.of(TextColor.RED, "'" + args[0] + "' is not a valid UUID!"));
return true;
}
}
});
commands.add(new Command("bluemap.debug", "debug") {
@Override
public boolean execute(CommandSender sender, CommandSource source, String[] args) {
if (!(sender instanceof Player)) {
source.sendMessage(Text.of(TextColor.RED, "You have to be a player to use this command!"));
return true;
}
Player player = (Player) sender;
UUID world = player.getWorld().getUID();
Vector3i pos = new Vector3i(
player.getLocation().getBlockX(),
player.getLocation().getBlockY(),
player.getLocation().getBlockZ()
);
bluemapCommands.executeDebugCommand(source, world, pos);
return true;
}
});
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
commands.executeRootCommand(new BukkitCommandSource(sender));
public boolean onCommand(CommandSender sender, org.bukkit.command.Command bukkitCommand, String label, String[] args) {
int max = -1;
Command maxCommand = null;
for (Command command : commands) {
int matchSize = command.matches(args);
if (matchSize > max) {
maxCommand = command;
max = matchSize;
}
}
return true;
if (maxCommand == null) return false;
BukkitCommandSource source = new BukkitCommandSource(sender);
if (!maxCommand.checkPermission(sender)) {
source.sendMessage(Text.of(TextColor.RED, "You don't have permission to use this command!"));
return true;
}
return maxCommand.execute(sender, source, Arrays.copyOfRange(args, max, args.length));
}
private abstract class Command {
private String[] command;
private String permission;
public Command(String permission, String... command) {
this.command = command;
}
public abstract boolean execute(CommandSender sender, CommandSource source, String[] args);
public int matches(String[] args) {
if (args.length < command.length) return -1;
for (int i = 0; i < command.length; i++) {
if (!args[i].equalsIgnoreCase(command[i])) return -1;
}
return command.length;
}
public boolean checkPermission(CommandSender sender) {
if (sender.isOp()) return true;
return sender.hasPermission(permission);
}
}
}

View File

@ -3,8 +3,12 @@
import java.io.File;
import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import org.bstats.bukkit.MetricsLite;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.plugin.java.JavaPlugin;
@ -15,6 +19,8 @@
public class BukkitPlugin extends JavaPlugin implements ServerInterface {
private static BukkitPlugin instance;
private Plugin bluemap;
private EventForwarder eventForwarder;
private BukkitCommands commands;
@ -25,6 +31,8 @@ public BukkitPlugin() {
this.eventForwarder = new EventForwarder();
this.bluemap = new Plugin("bukkit", this);
this.commands = new BukkitCommands(bluemap.getCommands());
BukkitPlugin.instance = this;
}
@Override
@ -64,9 +72,30 @@ public void unregisterAllListeners() {
@Override
public UUID getUUIDForWorld(File worldFolder) throws IOException {
worldFolder = worldFolder.getCanonicalFile();
final File normalizedWorldFolder = worldFolder.getCanonicalFile();
Future<UUID> futureUUID;
if (!Bukkit.isPrimaryThread()) {
futureUUID = Bukkit.getScheduler().callSyncMethod(BukkitPlugin.getInstance(), () -> getUUIDForWorldSync(normalizedWorldFolder));
} else {
futureUUID = CompletableFuture.completedFuture(getUUIDForWorldSync(normalizedWorldFolder));
}
try {
return futureUUID.get();
} catch (InterruptedException e) {
throw new IOException(e);
} catch (ExecutionException e) {
if (e.getCause() instanceof IOException) {
throw (IOException) e.getCause();
} else {
throw new IOException(e);
}
}
}
private UUID getUUIDForWorldSync (File worldFolder) throws IOException {
for (World world : getServer().getWorlds()) {
Logger.global.logInfo("Found world-folder: " + world.getWorldFolder().getCanonicalPath());
if (worldFolder.equals(world.getWorldFolder().getCanonicalFile())) return world.getUID();
}
@ -82,4 +111,8 @@ public Plugin getBlueMap() {
return bluemap;
}
public static BukkitPlugin getInstance() {
return instance;
}
}

View File

@ -1,8 +1,46 @@
name: BlueMap
description: "A 3d-map of your Minecraft worlds view-able in your browser using three.js (WebGL)"
main: de.bluecolored.bluemap.bukkit.BukkitPlugin
version: 0.2.1
author: Blue (TBlueF / Lukas Rieger)
author: "Blue (TBlueF / Lukas Rieger)"
authors: [Blue (TBlueF / Lukas Rieger)]
website: "https://github.com/BlueMap-Minecraft"
commands:
bluemap:
bluemap:
description: Root command for all bluemap commands
permission: bluemap
usage: |
/<command>
/<command> reload
/<command> pause
/<command> resume
/<command> render [world]
/<command> debug
permissions:
bluemap.*:
children:
bluemap.status: true
bluemap.reload: true
bluemap.pause: true
bluemap.resume: true
bluemap.rendertask.create.world: true
bluemap.rendertask.prioritize: true
bluemap.rendertask.remove: true
bluemap.debug: true
default: op
bluemap.status:
default: op
bluemap.reload:
default: op
bluemap.pause:
default: op
bluemap.resume:
default: op
bluemap.rendertask.create.world:
default: op
bluemap.rendertask.prioritize:
default: op
bluemap.rendertask.remove:
default: op
bluemap.debug:
default: op