Fixes, update Aparapi, set layout command

This commit is contained in:
Norbi Peti 2020-10-14 22:47:29 +02:00
parent 86b8e363e2
commit 6058842b61
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
7 changed files with 82 additions and 67 deletions

View file

@ -37,14 +37,6 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>9</source>
<target>9</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
@ -56,7 +48,7 @@
<dependency>
<groupId>com.aparapi</groupId>
<artifactId>aparapi</artifactId>
<version>1.10.0</version>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>io.github.NorbiPeti</groupId>

View file

@ -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<Long> w = new Holder<>(), h = new Holder<>(), bpp = new Holder<>();
Holder<Integer> xo = new Holder<>(), yo = new Holder<>();
var gms = new Holder<GuestMonitorStatus>();
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<Long> w = new Holder<>(), h = new Holder<>(), bpp = new Holder<>();
Holder<Integer> xo = new Holder<>(), yo = new Holder<>();
var gms = new Holder<GuestMonitorStatus>();
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.");
}
}

View file

@ -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);
}
}
}

View file

@ -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<Boolean> 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<String> kbLayout = getIConfig().getData("kbLayout", "en");
public File layoutFolder = new File(getDataFolder(), "layouts");
@Override
public void pluginEnable() {

View file

@ -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;
}
}

View file

@ -30,6 +30,7 @@ public class GPURenderer extends MapRenderer implements IRenderer {
private static ArrayList<GPURenderer> 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());

View file

@ -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];