From 6058842b61118fb3141d29e5b0c2dbeb8db534a1 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Wed, 14 Oct 2020 22:47:29 +0200 Subject: [PATCH] Fixes, update Aparapi, set layout command --- VirtualComputer-Core/pom.xml | 10 +---- .../java/sznp/virtualcomputer/Computer.java | 37 ++++++++-------- .../sznp/virtualcomputer/ComputerCommand.java | 43 +++++++++++++------ .../java/sznp/virtualcomputer/PluginMain.java | 5 +++ .../events/VBoxEventHandler.java | 41 ++++++++++-------- .../virtualcomputer/renderer/GPURenderer.java | 4 +- .../renderer/GPURendererInternal.java | 9 ---- 7 files changed, 82 insertions(+), 67 deletions(-) diff --git a/VirtualComputer-Core/pom.xml b/VirtualComputer-Core/pom.xml index a978847..97ceaa6 100644 --- a/VirtualComputer-Core/pom.xml +++ b/VirtualComputer-Core/pom.xml @@ -37,14 +37,6 @@ - - org.apache.maven.plugins - maven-compiler-plugin - - 9 - 9 - - @@ -56,7 +48,7 @@ com.aparapi aparapi - 1.10.0 + 2.0.0 io.github.NorbiPeti diff --git a/VirtualComputer-Core/src/main/java/sznp/virtualcomputer/Computer.java b/VirtualComputer-Core/src/main/java/sznp/virtualcomputer/Computer.java index 3a14e55..1b4fe9c 100644 --- a/VirtualComputer-Core/src/main/java/sznp/virtualcomputer/Computer.java +++ b/VirtualComputer-Core/src/main/java/sznp/virtualcomputer/Computer.java @@ -137,17 +137,14 @@ public final class Computer { public void PowerButton(CommandSender sender, int index) { sendMessage(sender, "§ePressing powerbutton..."); - Bukkit.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() { - @Override - public void run() { - if (session.getState() != SessionState.Locked || session.getMachine() == null) { - Start(sender, index); - } else { - synchronized (session) { - session.getConsole().powerButton(); - } - sendMessage(sender, "§ePowerbutton pressed."); + Bukkit.getServer().getScheduler().runTaskAsynchronously(plugin, () -> { + if (session.getState() != SessionState.Locked || session.getMachine() == null) { + Start(sender, index); + } else { + synchronized (session) { + session.getConsole().powerButton(); } + sendMessage(sender, "§ePowerbutton pressed."); } }); } @@ -310,14 +307,18 @@ public final class Computer { sender.sendMessage("§bThe computer is stuck. Use /c stop."); break; } - if (session.getState() == SessionState.Locked && machine.getState() == MachineState.Running) { - var con = session.getConsole(); - Holder w = new Holder<>(), h = new Holder<>(), bpp = new Holder<>(); - Holder xo = new Holder<>(), yo = new Holder<>(); - var gms = new Holder(); - con.getDisplay().getScreenResolution(0L, w, h, bpp, xo, yo, gms); - sender.sendMessage("§bScreen info: " + w.value + "x" + h.value + " (" + bpp.value + ") at " + xo.value + " " + yo.value + " - " + gms.value); - sender.sendMessage("§bKeyboard LEDs: " + con.getKeyboard().getKeyboardLEDs().stream().map(Enum::toString).collect(Collectors.joining(", "))); + if (session.getState() == SessionState.Locked) { + if (machine.getState() == MachineState.Running) { + var con = session.getConsole(); + Holder w = new Holder<>(), h = new Holder<>(), bpp = new Holder<>(); + Holder xo = new Holder<>(), yo = new Holder<>(); + var gms = new Holder(); + con.getDisplay().getScreenResolution(0L, w, h, bpp, xo, yo, gms); + sender.sendMessage("§bScreen info: " + w.value + "x" + h.value + " (" + bpp.value + ") at " + xo.value + " " + yo.value + " - " + gms.value); + sender.sendMessage("§bKeyboard LEDs: " + con.getKeyboard().getKeyboardLEDs().stream().map(Enum::toString).collect(Collectors.joining(", "))); + } else if (machine.getState().value() < MachineState.FirstOnline.value() + || machine.getState().value() > MachineState.LastOnline.value()) + sender.sendMessage("§bUse /c stop to fix the computer."); } } diff --git a/VirtualComputer-Core/src/main/java/sznp/virtualcomputer/ComputerCommand.java b/VirtualComputer-Core/src/main/java/sznp/virtualcomputer/ComputerCommand.java index 3259bc8..0964575 100644 --- a/VirtualComputer-Core/src/main/java/sznp/virtualcomputer/ComputerCommand.java +++ b/VirtualComputer-Core/src/main/java/sznp/virtualcomputer/ComputerCommand.java @@ -7,18 +7,17 @@ import lombok.var; import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.chat.ClickEvent; import net.md_5.bungee.api.chat.TextComponent; -import org.bukkit.DyeColor; import org.bukkit.Material; import org.bukkit.command.CommandSender; +import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.ItemFrame; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.MapMeta; -import org.bukkit.material.Wool; -import org.javatuples.Tuple; import org.virtualbox_6_1.VBoxException; import javax.annotation.Nullable; +import java.io.File; import java.util.Objects; @CommandClass(helpText = { @@ -158,20 +157,13 @@ public class ComputerCommand extends ICommand2MC { }) public void spawn(Player player) { var loc = player.getLocation(); - System.out.println("Player location: " + loc); var world = Objects.requireNonNull(loc.getWorld()); short id = PluginMain.Instance.startID.get(); - for (int i = 0; i < PluginMain.MCX; i++) { - for (int j = PluginMain.MCY - 1; j >= 0; j--) { + for (int j = PluginMain.MCY - 1; j >= 0; j--) { + for (int i = 0; i < PluginMain.MCX; i++) { var block = world.getBlockAt(loc.getBlockX() + i, loc.getBlockY() + j, loc.getBlockZ()); block.setType(Material.BLACK_WOOL); - /*var ws = block.getState(); - var wool = (Wool) ws.getData(); - wool.setColor(DyeColor.BLACK); - ws.setData(wool); - ws.update();*/ var frameLoc = block.getLocation().add(0, 0, 1); - System.out.println("Setting " + frameLoc + " to " + id); var map = new ItemStack(Material.FILLED_MAP, 1); var meta = ((MapMeta) map.getItemMeta()); if (meta == null) throw new NullPointerException("Map meta is null for " + frameLoc); @@ -181,4 +173,31 @@ public class ComputerCommand extends ICommand2MC { } } } + + @Command2.Subcommand(helpText = { + "Set layout", + "This command sets the keyboard layout used for /c show keyboard.", + "Valid options are files in the layouts folder in the plugin's directory." + }) + public void set_layout(CommandSender sender, String layout) { + var lf = PluginMain.Instance.layoutFolder; + if (!lf.mkdirs()) { + sender.sendMessage("§cFailed to create layouts folder!"); + return; + } + var l = new File(lf, layout + ".yml"); + if (!l.exists()) { + sender.sendMessage("§cThe file " + l + " does not exist."); + return; + } + var yc = YamlConfiguration.loadConfiguration(l); + var list = yc.getList("keyboard"); + if (list == null) { + sender.sendMessage("§cThe 'keyboard' key is missing."); + return; + } + for (var item : list) { + System.out.println("item: " + item); + } + } } diff --git a/VirtualComputer-Core/src/main/java/sznp/virtualcomputer/PluginMain.java b/VirtualComputer-Core/src/main/java/sznp/virtualcomputer/PluginMain.java index e69e2d2..f775f17 100644 --- a/VirtualComputer-Core/src/main/java/sznp/virtualcomputer/PluginMain.java +++ b/VirtualComputer-Core/src/main/java/sznp/virtualcomputer/PluginMain.java @@ -47,6 +47,11 @@ public class PluginMain extends ButtonPlugin { * If true, uses the GPU to accelerate screen rendering. Requires root on Linux. */ private final ConfigData useGPU = getIConfig().getData("useGPU", true); + /** + * Determines the keyboard layout to use for /c show keyboard. Layouts can be defined in VirtualComputer/layouts/. + */ + private final ConfigData kbLayout = getIConfig().getData("kbLayout", "en"); + public File layoutFolder = new File(getDataFolder(), "layouts"); @Override public void pluginEnable() { diff --git a/VirtualComputer-Core/src/main/java/sznp/virtualcomputer/events/VBoxEventHandler.java b/VirtualComputer-Core/src/main/java/sznp/virtualcomputer/events/VBoxEventHandler.java index cf66d24..ac1bae1 100644 --- a/VirtualComputer-Core/src/main/java/sznp/virtualcomputer/events/VBoxEventHandler.java +++ b/VirtualComputer-Core/src/main/java/sznp/virtualcomputer/events/VBoxEventHandler.java @@ -10,25 +10,30 @@ import org.virtualbox_6_1.VBoxEventType; import sznp.virtualcomputer.Computer; public class VBoxEventHandler extends EventHandlerBase { - public VBoxEventHandler() { - super(ImmutableMap.of(VBoxEventType.OnSessionStateChanged, ISessionStateChangedEvent.class)); - instance = this; - } + public VBoxEventHandler() { + super(ImmutableMap.of(VBoxEventType.OnSessionStateChanged, ISessionStateChangedEvent.class)); + instance = this; + } - @Getter - private static VBoxEventHandler instance; - private String machineID; - private CommandSender sender; + @Getter + private static VBoxEventHandler instance; + private String machineID; + private CommandSender sender; - @EventHandler - public void onSessionStateChange(ISessionStateChangedEvent event) { - if (!event.getMachineId().equals(machineID)) return; - if (event.getState() == SessionState.Locked) //Need to check here, because we can't access the console yet - Computer.getInstance().onLock(sender); - } + @EventHandler + public void onSessionStateChange(ISessionStateChangedEvent event) { + if (!event.getMachineId().equals(machineID)) return; + try { + if (event.getState() == SessionState.Locked) //Need to check here, because we can't access the console yet + Computer.getInstance().onLock(sender); + } catch (Exception e) { + sender.sendMessage("§cFailed to start computer! See the console for more details."); + throw e; + } + } - public void setup(String machineID, CommandSender sender) { - this.machineID = machineID; - this.sender = sender; - } + public void setup(String machineID, CommandSender sender) { + this.machineID = machineID; + this.sender = sender; + } } diff --git a/VirtualComputer-Core/src/main/java/sznp/virtualcomputer/renderer/GPURenderer.java b/VirtualComputer-Core/src/main/java/sznp/virtualcomputer/renderer/GPURenderer.java index 43a410a..01ec91b 100644 --- a/VirtualComputer-Core/src/main/java/sznp/virtualcomputer/renderer/GPURenderer.java +++ b/VirtualComputer-Core/src/main/java/sznp/virtualcomputer/renderer/GPURenderer.java @@ -30,6 +30,7 @@ public class GPURenderer extends MapRenderer implements IRenderer { private static ArrayList renderers = new ArrayList<>(); private static Method flagDirtyMethod; private static boolean enabled = true; + private static boolean warned = false; public GPURenderer(short id, World world, int mapx, int mapy) throws Exception { MapView map = IRenderer.prepare(id, world); @@ -81,7 +82,8 @@ public class GPURenderer extends MapRenderer implements IRenderer { field.setAccessible(true); buffer = (byte[]) field.get(canvas); } - if (mapx == 0 && mapy == 0) { //Only print once + if (!warned) { //Only print once + warned = true; if (kernel.getTargetDevice().getType() != Device.TYPE.GPU) { PluginMain.Instance.getLogger().warning("Cannot use GPU! Target device: " + kernel.getTargetDevice().getShortDescription() + " - Best device: " + KernelManager.instance().bestDevice().getShortDescription()); diff --git a/VirtualComputer-Core/src/main/java/sznp/virtualcomputer/renderer/GPURendererInternal.java b/VirtualComputer-Core/src/main/java/sznp/virtualcomputer/renderer/GPURendererInternal.java index 8920343..67896e3 100644 --- a/VirtualComputer-Core/src/main/java/sznp/virtualcomputer/renderer/GPURendererInternal.java +++ b/VirtualComputer-Core/src/main/java/sznp/virtualcomputer/renderer/GPURendererInternal.java @@ -2,11 +2,7 @@ package sznp.virtualcomputer.renderer; import com.aparapi.Kernel; import com.aparapi.Range; -import com.aparapi.device.Device; -import com.aparapi.internal.kernel.KernelManager; import lombok.Getter; -import lombok.var; -import sznp.virtualcomputer.PluginMain; //Accessing the GPURenderer results in ArrayIndexOutOfBoundsExceptions - IT'S THE LAMBDAS public class GPURendererInternal extends Kernel { @@ -28,12 +24,7 @@ public class GPURendererInternal extends Kernel { this.mapx = mapx; this.mapy = mapy; this.colors = colors; - //range = Range.create2D(128, 128); - //var dev = KernelManager.instance().bestDevice(); range = Range.create2D(128, 128); - /*var dev = getTargetDevice(); - if (mapx == mapy && mapx == 0) - PluginMain.Instance.getLogger().info("Using device: " + dev.getShortDescription());*/ //Do an intial draw of a black screen with Aparapi so it doesn't lag at start pixels = new byte[1];