Directly using VirtualBox from Java #5

Merged
NorbiPeti merged 60 commits from directvb into master 2019-04-18 23:29:21 +00:00
7 changed files with 29 additions and 31 deletions
Showing only changes of commit 169285a111 - Show all commits

View file

@ -99,12 +99,6 @@
</exclusion> </exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
<dependency>
<groupId>net.countercraft</groupId>
<artifactId>Movecraft</artifactId>
<version>3.0.0</version>
<scope>provided</scope>
</dependency>
<dependency> <dependency>
<groupId>org.bukkit</groupId> <groupId>org.bukkit</groupId>
<artifactId>craftbukkit</artifactId> <artifactId>craftbukkit</artifactId>

View file

@ -1,8 +1,7 @@
name: VirtualComputer name: VirtualComputer
main: sznp.virtualcomputer.PluginMain main: sznp.virtualcomputer.PluginMain
version: 1.1 version: 2.0
commands: commands:
computer: computer:
usage: Use /computer start|stop usage: Use /computer start|stop|reset|key|mouse|input|fix
alias: c alias: c
softdepend: [Movecraft]

View file

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>io.github.NorbiPeti</groupId> <groupId>io.github.NorbiPeti</groupId>
<artifactId>VirtualComputer</artifactId> <artifactId>VirtualComputer</artifactId>
<version>1.2-SNAPSHOT</version> <version>2.0-SNAPSHOT</version>
<build> <build>
<sourceDirectory>src</sourceDirectory> <sourceDirectory>src</sourceDirectory>
<resources> <resources>

View file

@ -15,6 +15,10 @@ public class BukkitRenderer extends MapRenderer implements IRenderer {
private BufferedImage image; private BufferedImage image;
private int startindex; private int startindex;
public void setAllPixels(byte[] allpixels) {
this.allpixels = allpixels;
}
/** /**
* Generic implementation, should work on most versions * Generic implementation, should work on most versions
* *
@ -27,10 +31,9 @@ public class BukkitRenderer extends MapRenderer implements IRenderer {
* @param startindex * @param startindex
* The index to start from in allpixels * The index to start from in allpixels
*/ */
public BukkitRenderer(short id, World world, byte[] allpixels, int startindex) { public BukkitRenderer(short id, World world, int startindex) {
MapView map = IRenderer.prepare(id, world); MapView map = IRenderer.prepare(id, world);
map.addRenderer(this); map.addRenderer(this);
this.allpixels = allpixels;
this.startindex = startindex; this.startindex = startindex;
image = new BufferedImage(640, 480, BufferedImage.TYPE_INT_RGB); image = new BufferedImage(640, 480, BufferedImage.TYPE_INT_RGB);
} }
@ -40,6 +43,8 @@ public class BukkitRenderer extends MapRenderer implements IRenderer {
@Override @Override
public void render(MapView view, MapCanvas canvas, Player player) { public void render(MapView view, MapCanvas canvas, Player player) {
if (allpixels == null)
return;
long time = System.nanoTime(); long time = System.nanoTime();
final int[] a = ((DataBufferInt) image.getRaster().getDataBuffer()).getData(); // Directly update the bytes of the image final int[] a = ((DataBufferInt) image.getRaster().getDataBuffer()).getData(); // Directly update the bytes of the image

View file

@ -4,7 +4,6 @@ import java.awt.Color;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.Map; import java.util.Map;
import org.bukkit.Bukkit;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.craftbukkit.v1_12_R1.entity.CraftPlayer; import org.bukkit.craftbukkit.v1_12_R1.entity.CraftPlayer;
import org.bukkit.craftbukkit.v1_12_R1.map.RenderData; import org.bukkit.craftbukkit.v1_12_R1.map.RenderData;
@ -14,7 +13,6 @@ import org.bukkit.map.MapView;
import net.minecraft.server.v1_12_R1.WorldMap; import net.minecraft.server.v1_12_R1.WorldMap;
public class DirectRenderer implements IRenderer { public class DirectRenderer implements IRenderer {
private byte[] allpixels;
private int startindex; private int startindex;
private byte[] buffer; private byte[] buffer;
private MapView map; private MapView map;
@ -33,8 +31,7 @@ public class DirectRenderer implements IRenderer {
* @throws Exception * @throws Exception
* Usually happens on incompatibility * Usually happens on incompatibility
*/ */
public DirectRenderer(short id, World world, byte[] allpixels, int startindex) public DirectRenderer(short id, World world, int startindex) throws Exception, Exception, Exception, Exception {
throws Exception, Exception, Exception, Exception {
map = IRenderer.prepare(id, world); map = IRenderer.prepare(id, world);
final Field field = map.getClass().getDeclaredField("renderCache"); final Field field = map.getClass().getDeclaredField("renderCache");
field.setAccessible(true); field.setAccessible(true);
@ -43,22 +40,17 @@ public class DirectRenderer implements IRenderer {
RenderData render = renderCache.get(null); RenderData render = renderCache.get(null);
if (render == null) { if (render == null)
render = new RenderData(); renderCache.put(null, render = new RenderData());
renderCache.put(null, render);
}
this.allpixels = allpixels;
this.startindex = startindex; this.startindex = startindex;
this.buffer = render.buffer; this.buffer = render.buffer;
Bukkit.getScheduler().runTask(PluginMain.Instance, this::render);
} }
private Exception ex; private Exception ex;
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public void render() { public void render(byte[] allpixels, long x, long y, long width, long height) { // TODO
try { try {
for (int i = startindex, j = 0; i < startindex + 128 * 128 && i < allpixels.length for (int i = startindex, j = 0; i < startindex + 128 * 128 && i < allpixels.length
&& j < buffer.length; i += 4, j++) && j < buffer.length; i += 4, j++)

View file

@ -80,12 +80,20 @@ public class MCFrameBuffer implements IFramebuffer {
holder.value.getTypedWrapped().queryBitmapInfo(PluginMain.allpixels, new long[] { width }, holder.value.getTypedWrapped().queryBitmapInfo(PluginMain.allpixels, new long[] { width },
new long[] { height }, new long[] { getBitsPerPixel() }, new long[] { getBytesPerLine() }, new long[] { height }, new long[] { getBitsPerPixel() }, new long[] { getBytesPerLine() },
new long[] { getPixelFormat() }); // These are out params but whatever new long[] { getPixelFormat() }); // These are out params but whatever
for (IRenderer r : PluginMain.renderers)
if (r instanceof BukkitRenderer)
((BukkitRenderer) r).setAllPixels(PluginMain.allpixels);
else if (r instanceof DirectRenderer)
((DirectRenderer) r).render(PluginMain.allpixels, xOrigin, yOrigin, width, height);
System.out.println("Change!"); System.out.println("Change!");
}, 5); // Wait 1/4th of a second }, 5); // Wait 1/4th of a second
} }
@Override @Override
public void notifyUpdate(long arg0, long arg1, long arg2, long arg3) { public void notifyUpdate(long x, long y, long width, long height) {
for (IRenderer r : PluginMain.renderers)
if (r instanceof DirectRenderer)
((DirectRenderer) r).render(PluginMain.allpixels, x, y, width, height);
System.out.println("Update!"); System.out.println("Update!");
} }

View file

@ -16,12 +16,12 @@ import com.google.common.collect.Lists;
public class PluginMain extends JavaPlugin { public class PluginMain extends JavaPlugin {
private IVirtualBox vbox; private IVirtualBox vbox;
private ISession session; private ISession session;
private ArrayList<IRenderer> renderers = new ArrayList<>();
private IMachine machine; private IMachine machine;
private BukkitTask screenupdatetask; private BukkitTask screenupdatetask;
public static PluginMain Instance; public static PluginMain Instance;
public static byte[] allpixels = new byte[640 * 480]; public static byte[] allpixels = null; // It's set on each change
public static ArrayList<IRenderer> renderers = new ArrayList<>();
// Fired when plugin is first enabled // Fired when plugin is first enabled
@Override @Override
@ -45,11 +45,11 @@ public class PluginMain extends JavaPlugin {
ccs.sendMessage("§bLoading Screen..."); ccs.sendMessage("§bLoading Screen...");
try { try {
for (short i = 0; i < 20; i++) for (short i = 0; i < 20; i++)
renderers.add(new DirectRenderer(i, Bukkit.getWorlds().get(0), allpixels, i * 128 * 128 * 4)); // TODO: The pixels are selected in a horribly wrong way probably renderers.add(new DirectRenderer(i, Bukkit.getWorlds().get(0), i * 128 * 128 * 4)); // TODO: The pixels are selected in a horribly wrong way probably
ccs.sendMessage("§bUsing Direct Renderer"); ccs.sendMessage("§bUsing Direct Renderer, all good");
} catch (NoClassDefFoundError e) { } catch (NoClassDefFoundError e) {
for (short i = 0; i < 20; i++) for (short i = 0; i < 20; i++)
renderers.add(new BukkitRenderer(i, Bukkit.getWorlds().get(0), allpixels, i * 128 * 128 * 4)); renderers.add(new BukkitRenderer(i, Bukkit.getWorlds().get(0), i * 128 * 128 * 4));
ccs.sendMessage("§6Compability error, using slower renderer"); ccs.sendMessage("§6Compability error, using slower renderer");
} }
ccs.sendMessage("§bLoaded!"); ccs.sendMessage("§bLoaded!");
@ -97,7 +97,7 @@ public class PluginMain extends JavaPlugin {
screenupdatetask.cancel(); screenupdatetask.cancel();
screenupdatetask = null; screenupdatetask = null;
} }
}, 100, 100); // Do a full update every 2 seconds }, 100, 100); // Do a full update every 5 seconds
sender.sendMessage("§eComputer started."); sender.sendMessage("§eComputer started.");
}); });
} }