Directly using VirtualBox from Java #5

Merged
NorbiPeti merged 60 commits from directvb into master 2019-04-18 23:29:21 +00:00
8 changed files with 731 additions and 470 deletions
Showing only changes of commit 02a3686c30 - Show all commits

1
.gitignore vendored
View file

@ -466,4 +466,3 @@ TheButtonAutoFlair/out/artifacts/Autoflair/Autoflair.jar
*.iml
*.name
.idea/compiler.xml
*.xml

View file

@ -0,0 +1,114 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.NorbiPeti</groupId>
<artifactId>VirtualComputer</artifactId>
<version>1.2-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
<resource>
<directory>.</directory>
<includes>
<include>*.dll</include>
<include>*.yml</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<includes>
<include>org.virtualbox:VirtualBox</include>
</includes>
</artifactSet>
<pluginExecution>
<action>
<execute />
</action>
</pluginExecution>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
<repository>
<id>repo</id>
<url>file://${basedir}/repo</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.9.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<artifactId>commons-lang</artifactId>
<groupId>commons-lang</groupId>
</exclusion>
<exclusion>
<artifactId>json-simple</artifactId>
<groupId>com.googlecode.json-simple</groupId>
</exclusion>
<exclusion>
<artifactId>guava</artifactId>
<groupId>com.google.guava</groupId>
</exclusion>
<exclusion>
<artifactId>gson</artifactId>
<groupId>com.google.code.gson</groupId>
</exclusion>
<exclusion>
<artifactId>ebean</artifactId>
<groupId>org.avaje</groupId>
</exclusion>
<exclusion>
<artifactId>snakeyaml</artifactId>
<groupId>org.yaml</groupId>
</exclusion>
<exclusion>
<artifactId>bungeecord-chat</artifactId>
<groupId>net.md-5</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>net.countercraft</groupId>
<artifactId>Movecraft</artifactId>
<version>3.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View file

@ -71,7 +71,7 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.9.2-R0.1-SNAPSHOT</version>
<version>1.12-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
@ -85,5 +85,11 @@
<artifactId>VirtualBox</artifactId>
<version>5.1</version>
</dependency>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>craftbukkit</artifactId>
<version>1.12-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View file

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<metadata>
<groupId>org.virtualbox</groupId>
<artifactId>VirtualBox</artifactId>
<versioning>
<release>5.1</release>
<versions>
<version>5.1</version>
</versions>
<lastUpdated>20170111200516</lastUpdated>
</versioning>
</metadata>

View file

@ -0,0 +1,43 @@
package sznp.virtualcomputer;
import java.awt.image.BufferedImage;
import java.util.concurrent.TimeUnit;
import org.bukkit.entity.Player;
import org.bukkit.map.MapCanvas;
import org.bukkit.map.MapRenderer;
import org.bukkit.map.MapView;
public class BukkitRenderer extends MapRenderer implements IRenderer {
private BufferedImage image;
public BukkitRenderer(BufferedImage image) {
this.image = image;
}
private int progress = 0;
public static int updatepixels = 15;
@Override
public void render(MapView view, MapCanvas canvas, Player player) {
long time = System.nanoTime();
try {
canvas.drawImage(0, progress * updatepixels, image.getSubimage(0, progress * updatepixels, 128,
(progress * updatepixels + updatepixels >= 128 ? 128 - progress * updatepixels : updatepixels)));
if (progress < 128 / updatepixels)
progress++;
else
progress = 0;
long diff = System.nanoTime() - time;
if (TimeUnit.NANOSECONDS.toMillis(diff) > 40) {
System.out.println("Map rendering took " + TimeUnit.NANOSECONDS.toMillis(diff) + " ms");
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("Progess: " + progress);
System.out.println("UpdatePixels: " + updatepixels);
}
}
}

View file

@ -0,0 +1,81 @@
package sznp.virtualcomputer;
import java.awt.Color;
import java.lang.reflect.Field;
import java.util.Map;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_12_R1.entity.CraftPlayer;
import org.bukkit.craftbukkit.v1_12_R1.map.RenderData;
import org.bukkit.map.MapPalette;
import org.bukkit.map.MapView;
import net.minecraft.server.v1_12_R1.WorldMap;
public class DirectRenderer implements IRenderer {
private byte[] allpixels;
private int startindex;
private byte[] buffer;
private MapView map;
/**
* Attempt to use version-specific implementation
*
* @param id
* The ID of the current map
* @param world
* The world to create new maps in
* @param allpixels
* The raw pixel data from the machine in BGRA format
* @param startindex
* The index to start from in allpixels
* @throws Exception
* Usually happens on incompatibility
*/
@SuppressWarnings("deprecation")
public DirectRenderer(short id, World world, byte[] allpixels, int startindex)
throws Exception, Exception, Exception, Exception {
map = Bukkit.getMap(id);
if (map == null)
map = Bukkit.createMap(world);
map.getRenderers().clear();
final Field field = map.getClass().getDeclaredField("renderCache");
field.setAccessible(true);
@SuppressWarnings("unchecked")
final Map<CraftPlayer, RenderData> renderCache = (Map<CraftPlayer, RenderData>) field.get(map);
RenderData render = renderCache.get(null);
if (render == null) {
render = new RenderData();
renderCache.put(null, render);
}
this.allpixels = allpixels;
this.startindex = startindex;
this.buffer = render.buffer;
Bukkit.getScheduler().runTask(PluginMain.Instance, this::render);
}
private Exception ex;
@SuppressWarnings("deprecation")
public void render() {
try {
for (int i = startindex, j = 0; i < startindex + 128 * 128 && i < allpixels.length
&& j < buffer.length; i += 4, j++)
buffer[j] = MapPalette.matchColor(new Color(allpixels[i], allpixels[i + 1], allpixels[i + 2]));
final Field field = map.getClass().getField("worldmap");
field.setAccessible(true);
WorldMap wmap = (WorldMap) field.get(map);
wmap.flagDirty(0, 0);
wmap.flagDirty(128, 128); // Send the whole image - TODO: Only send changes
} catch (Exception e) {
if (ex != null && e.getMessage().equals(ex.getMessage()))
return;
(ex = e).printStackTrace();
}
}
}

View file

@ -0,0 +1,4 @@
package sznp.virtualcomputer;
public interface IRenderer {
}

View file

@ -36,6 +36,8 @@ public class PluginMain extends JavaPlugin {
ConsoleCommandSender ccs = getServer().getConsoleSender();
this.getCommand("computer").setExecutor(new Commands());
ccs.sendMessage("§bInitializing VirtualBox...");
if (System.getProperty("vbox.home") == null || System.getProperty("vbox.home").isEmpty())
System.setProperty("vbox.home", "/opt/virtualbox");
final VirtualBoxManager manager = VirtualBoxManager.createInstance(getDataFolder().getAbsolutePath());
vbox = manager.getVBox();
session = manager.getSessionObject();