From 123bce502710e0c0a07b5eb4bf63288e7c25bc49 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Sat, 16 Jun 2018 01:55:42 +0200 Subject: [PATCH] Still no display --- VirtualComputer/dependency-reduced-pom.xml | 8 +--- VirtualComputer/pom.xml | 5 -- .../sznp/virtualcomputer/BukkitRenderer.java | 12 ++--- .../sznp/virtualcomputer/DirectRenderer.java | 48 ++++++++++++++----- .../sznp/virtualcomputer/MCFrameBuffer.java | 20 +++++--- .../src/sznp/virtualcomputer/PluginMain.java | 22 ++++----- 6 files changed, 67 insertions(+), 48 deletions(-) diff --git a/VirtualComputer/dependency-reduced-pom.xml b/VirtualComputer/dependency-reduced-pom.xml index 3e79d26..9fbc306 100644 --- a/VirtualComputer/dependency-reduced-pom.xml +++ b/VirtualComputer/dependency-reduced-pom.xml @@ -39,13 +39,7 @@ shade - - - - - - - + diff --git a/VirtualComputer/pom.xml b/VirtualComputer/pom.xml index 31195d0..353b19d 100644 --- a/VirtualComputer/pom.xml +++ b/VirtualComputer/pom.xml @@ -41,11 +41,6 @@ shade - - - - - diff --git a/VirtualComputer/src/sznp/virtualcomputer/BukkitRenderer.java b/VirtualComputer/src/sznp/virtualcomputer/BukkitRenderer.java index ac881bc..9e80542 100644 --- a/VirtualComputer/src/sznp/virtualcomputer/BukkitRenderer.java +++ b/VirtualComputer/src/sznp/virtualcomputer/BukkitRenderer.java @@ -1,16 +1,16 @@ package sznp.virtualcomputer; -import java.awt.image.BufferedImage; -import java.awt.image.DataBufferInt; -import java.nio.ByteBuffer; -import java.util.concurrent.TimeUnit; - import org.bukkit.World; import org.bukkit.entity.Player; import org.bukkit.map.MapCanvas; import org.bukkit.map.MapRenderer; import org.bukkit.map.MapView; +import java.awt.image.BufferedImage; +import java.awt.image.DataBufferInt; +import java.nio.ByteBuffer; +import java.util.concurrent.TimeUnit; + public class BukkitRenderer extends MapRenderer implements IRenderer { private ByteBuffer allpixels; private BufferedImage image; @@ -54,7 +54,7 @@ public class BukkitRenderer extends MapRenderer implements IRenderer { for (int i = startindex, j = 0; i < startindex + 128 * 128; i = i + 4, j++) { int b, g, r; - b = allpixels.get(i) & 0xFF; + b = allpixels.get(i) & 0xFF; //TODO: <-- IndexOutOfBoundsException sometimes g = allpixels.get(i + 1) & 0xFF; r = allpixels.get(i + 2) & 0xFF; diff --git a/VirtualComputer/src/sznp/virtualcomputer/DirectRenderer.java b/VirtualComputer/src/sznp/virtualcomputer/DirectRenderer.java index b10178f..eecf2a8 100644 --- a/VirtualComputer/src/sznp/virtualcomputer/DirectRenderer.java +++ b/VirtualComputer/src/sznp/virtualcomputer/DirectRenderer.java @@ -1,18 +1,19 @@ package sznp.virtualcomputer; -import java.awt.Color; -import java.lang.reflect.Field; -import java.nio.ByteBuffer; -import java.util.Arrays; -import java.util.Map; - +import net.minecraft.server.v1_12_R1.WorldMap; 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.entity.Player; +import org.bukkit.map.MapCanvas; import org.bukkit.map.MapPalette; +import org.bukkit.map.MapRenderer; import org.bukkit.map.MapView; -import net.minecraft.server.v1_12_R1.WorldMap; +import java.awt.*; +import java.lang.reflect.Field; +import java.nio.ByteBuffer; +import java.util.Map; public class DirectRenderer implements IRenderer { private int startindex; @@ -26,8 +27,6 @@ public class DirectRenderer implements IRenderer { * 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 @@ -47,16 +46,41 @@ public class DirectRenderer implements IRenderer { this.startindex = startindex; this.buffer = render.buffer; + map.addRenderer(new DummyRenderer()); + } + + private final class DummyRenderer extends MapRenderer { + @Override + public void render(MapView map, MapCanvas canvas, Player player) { + if(allpixels != null) + DirectRenderer.this.render(allpixels, x, y, width, height); //Render after zeroing whole map + } } private Exception ex; + private ByteBuffer allpixels; + private long x, y, width, height; + private long lastrender; @SuppressWarnings("deprecation") public void render(ByteBuffer allpixels, long x, long y, long width, long height) { // TODO + this.allpixels=allpixels; + this.x=x; + this.y=y; + this.width=width; + this.height=height; + if(System.nanoTime()-lastrender<100*1000*1000) + return; + lastrender=System.nanoTime(); try { - for (int i = startindex, j = 0; i < allpixels.limit() - 4 && j < buffer.length; i += 4, j++) - buffer[j] = MapPalette - .matchColor(new Color(allpixels.get(i), allpixels.get(i + 1), allpixels.get(i + 2))); + boolean hascolor=false; + for (int i = startindex, j = 0; i < allpixels.limit() - 4 && j < buffer.length; i += 4, j++) { + buffer[j] = MapPalette.matchColor(new Color(allpixels.get(i), allpixels.get(i + 1), allpixels.get(i + 2))); + if(allpixels.get(i+2)>10) + hascolor=true; + } + if(hascolor) + System.out.println("Some color!"); final Field field = map.getClass().getDeclaredField("worldMap"); field.setAccessible(true); WorldMap wmap = (WorldMap) field.get(map); diff --git a/VirtualComputer/src/sznp/virtualcomputer/MCFrameBuffer.java b/VirtualComputer/src/sznp/virtualcomputer/MCFrameBuffer.java index 33d436f..7416d0e 100644 --- a/VirtualComputer/src/sznp/virtualcomputer/MCFrameBuffer.java +++ b/VirtualComputer/src/sznp/virtualcomputer/MCFrameBuffer.java @@ -6,7 +6,10 @@ import org.mozilla.interfaces.IFramebuffer; import org.mozilla.interfaces.IFramebufferOverlay; import org.mozilla.interfaces.nsISupports; import org.mozilla.xpcom.Mozilla; -import org.virtualbox_5_2.*; +import org.virtualbox_5_2.BitmapFormat; +import org.virtualbox_5_2.Holder; +import org.virtualbox_5_2.IDisplay; +import org.virtualbox_5_2.IDisplaySourceBitmap; public class MCFrameBuffer implements IFramebuffer { private IDisplay display; @@ -76,6 +79,7 @@ public class MCFrameBuffer implements IFramebuffer { } private BukkitTask tt; + private BukkitTask tttt; @Override public void notifyChange(long screenId, long xOrigin, long yOrigin, long width, long height) { @@ -86,12 +90,12 @@ public class MCFrameBuffer implements IFramebuffer { * if (width > 640 || height > 480) { tt = Bukkit.getScheduler().runTaskTimerAsynchronously(PluginMain.Instance, () -> display.setVideoModeHint(0L, true, false, 0, 0, 640L, 480L, 32L), 5, 5); * return; // Don't even try to render too large resolutions } */ - Bukkit.getScheduler().runTaskLaterAsynchronously(PluginMain.Instance, () -> { + tt = Bukkit.getScheduler().runTaskLaterAsynchronously(PluginMain.Instance, () -> { display.querySourceBitmap(0L, holder); byte[] arr = PluginMain.allpixels.array(); long[] w = new long[1], h = new long[1], bpp = new long[1], bpl = new long[1], pf = new long[1]; holder.value.getTypedWrapped().queryBitmapInfo(arr, w, h, bpp, bpl, pf); - System.out.println("Arr10:" + arr[10]); + System.out.println("Arr0:" + arr[0]); System.out.println("whbppbplpf: " + w[0] + " " + h[0] + " " + bpp[0] + " " + bpl[0] + " " + pf[0]); if (width * height > 640 * 480) PluginMain.allpixels.limit(640 * 480 * 4); @@ -108,9 +112,13 @@ public class MCFrameBuffer implements IFramebuffer { @Override 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); + if(tttt != null) + tttt.cancel(); //We are getting updates, but the pixel array isn't updated - VB reacts slowly + tttt = Bukkit.getScheduler().runTaskLaterAsynchronously(PluginMain.Instance, () -> { + for (IRenderer r : PluginMain.renderers) + if (r instanceof DirectRenderer) + ((DirectRenderer) r).render(PluginMain.allpixels, x, y, width, height); + }, 5); } @Override diff --git a/VirtualComputer/src/sznp/virtualcomputer/PluginMain.java b/VirtualComputer/src/sznp/virtualcomputer/PluginMain.java index 53c83d2..e637280 100644 --- a/VirtualComputer/src/sznp/virtualcomputer/PluginMain.java +++ b/VirtualComputer/src/sznp/virtualcomputer/PluginMain.java @@ -1,11 +1,7 @@ package sznp.virtualcomputer; -import java.io.File; -import java.lang.reflect.Field; -import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.Arrays; - +import com.google.common.collect.Lists; +import jnr.ffi.LibraryLoader; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.command.ConsoleCommandSender; @@ -13,9 +9,11 @@ import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.scheduler.BukkitTask; import org.virtualbox_5_2.*; -import com.google.common.collect.Lists; - -import jnr.ffi.LibraryLoader; +import java.io.File; +import java.lang.reflect.Field; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Arrays; public class PluginMain extends JavaPlugin { private IVirtualBox vbox; @@ -57,10 +55,10 @@ public class PluginMain extends JavaPlugin { session = manager.getSessionObject(); // TODO: Events ccs.sendMessage("§bLoading Screen..."); try { - throw new NoClassDefFoundError("Test error pls ignore"); - /*for (short i = 0; i < 20; i++) + //throw new NoClassDefFoundError("Test error pls ignore"); + for (short i = 0; i < 20; i++) 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, all good");*/ + ccs.sendMessage("§bUsing Direct Renderer, all good"); } catch (NoClassDefFoundError e) { for (short i = 0; i < 20; i++) renderers.add(new BukkitRenderer(i, Bukkit.getWorlds().get(0), i * 128 * 128 * 4));