From 169285a111499dc20361501285c780373b7bc58d Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Fri, 4 Aug 2017 17:12:23 +0200 Subject: [PATCH] Fixes, actually rendering on each update, etc. Updated version Removed Movecraft as a soft dependency AllPixels is set on each change The direct renderer only renders on change and updates (altough it'd actually only render once before) --- VirtualComputer/dependency-reduced-pom.xml | 6 ------ VirtualComputer/plugin.yml | 5 ++--- VirtualComputer/pom.xml | 2 +- .../src/sznp/virtualcomputer/BukkitRenderer.java | 9 +++++++-- .../src/sznp/virtualcomputer/DirectRenderer.java | 16 ++++------------ .../src/sznp/virtualcomputer/MCFrameBuffer.java | 10 +++++++++- .../src/sznp/virtualcomputer/PluginMain.java | 12 ++++++------ 7 files changed, 29 insertions(+), 31 deletions(-) diff --git a/VirtualComputer/dependency-reduced-pom.xml b/VirtualComputer/dependency-reduced-pom.xml index 21feae7..24515fd 100644 --- a/VirtualComputer/dependency-reduced-pom.xml +++ b/VirtualComputer/dependency-reduced-pom.xml @@ -99,12 +99,6 @@ - - net.countercraft - Movecraft - 3.0.0 - provided - org.bukkit craftbukkit diff --git a/VirtualComputer/plugin.yml b/VirtualComputer/plugin.yml index 9500aab..cc1c5b0 100644 --- a/VirtualComputer/plugin.yml +++ b/VirtualComputer/plugin.yml @@ -1,8 +1,7 @@ name: VirtualComputer main: sznp.virtualcomputer.PluginMain -version: 1.1 +version: 2.0 commands: computer: - usage: Use /computer start|stop + usage: Use /computer start|stop|reset|key|mouse|input|fix alias: c -softdepend: [Movecraft] diff --git a/VirtualComputer/pom.xml b/VirtualComputer/pom.xml index 06988d9..cc92fd7 100644 --- a/VirtualComputer/pom.xml +++ b/VirtualComputer/pom.xml @@ -3,7 +3,7 @@ 4.0.0 io.github.NorbiPeti VirtualComputer - 1.2-SNAPSHOT + 2.0-SNAPSHOT src diff --git a/VirtualComputer/src/sznp/virtualcomputer/BukkitRenderer.java b/VirtualComputer/src/sznp/virtualcomputer/BukkitRenderer.java index c832e52..5ef0a31 100644 --- a/VirtualComputer/src/sznp/virtualcomputer/BukkitRenderer.java +++ b/VirtualComputer/src/sznp/virtualcomputer/BukkitRenderer.java @@ -15,6 +15,10 @@ public class BukkitRenderer extends MapRenderer implements IRenderer { private BufferedImage image; private int startindex; + public void setAllPixels(byte[] allpixels) { + this.allpixels = allpixels; + } + /** * Generic implementation, should work on most versions * @@ -27,10 +31,9 @@ public class BukkitRenderer extends MapRenderer implements IRenderer { * @param startindex * 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); map.addRenderer(this); - this.allpixels = allpixels; this.startindex = startindex; image = new BufferedImage(640, 480, BufferedImage.TYPE_INT_RGB); } @@ -40,6 +43,8 @@ public class BukkitRenderer extends MapRenderer implements IRenderer { @Override public void render(MapView view, MapCanvas canvas, Player player) { + if (allpixels == null) + return; long time = System.nanoTime(); final int[] a = ((DataBufferInt) image.getRaster().getDataBuffer()).getData(); // Directly update the bytes of the image diff --git a/VirtualComputer/src/sznp/virtualcomputer/DirectRenderer.java b/VirtualComputer/src/sznp/virtualcomputer/DirectRenderer.java index 73d9095..d150a3d 100644 --- a/VirtualComputer/src/sznp/virtualcomputer/DirectRenderer.java +++ b/VirtualComputer/src/sznp/virtualcomputer/DirectRenderer.java @@ -4,7 +4,6 @@ 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; @@ -14,7 +13,6 @@ 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; @@ -33,8 +31,7 @@ public class DirectRenderer implements IRenderer { * @throws Exception * Usually happens on incompatibility */ - public DirectRenderer(short id, World world, byte[] allpixels, int startindex) - throws Exception, Exception, Exception, Exception { + public DirectRenderer(short id, World world, int startindex) throws Exception, Exception, Exception, Exception { map = IRenderer.prepare(id, world); final Field field = map.getClass().getDeclaredField("renderCache"); field.setAccessible(true); @@ -43,22 +40,17 @@ public class DirectRenderer implements IRenderer { RenderData render = renderCache.get(null); - if (render == null) { - render = new RenderData(); - renderCache.put(null, render); - } + if (render == null) + renderCache.put(null, render = new RenderData()); - 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() { + public void render(byte[] allpixels, long x, long y, long width, long height) { // TODO try { for (int i = startindex, j = 0; i < startindex + 128 * 128 && i < allpixels.length && j < buffer.length; i += 4, j++) diff --git a/VirtualComputer/src/sznp/virtualcomputer/MCFrameBuffer.java b/VirtualComputer/src/sznp/virtualcomputer/MCFrameBuffer.java index 2550060..e4adc2c 100644 --- a/VirtualComputer/src/sznp/virtualcomputer/MCFrameBuffer.java +++ b/VirtualComputer/src/sznp/virtualcomputer/MCFrameBuffer.java @@ -80,12 +80,20 @@ public class MCFrameBuffer implements IFramebuffer { holder.value.getTypedWrapped().queryBitmapInfo(PluginMain.allpixels, new long[] { width }, new long[] { height }, new long[] { getBitsPerPixel() }, new long[] { getBytesPerLine() }, 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!"); }, 5); // Wait 1/4th of a second } @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!"); } diff --git a/VirtualComputer/src/sznp/virtualcomputer/PluginMain.java b/VirtualComputer/src/sznp/virtualcomputer/PluginMain.java index 788133e..5dc03eb 100644 --- a/VirtualComputer/src/sznp/virtualcomputer/PluginMain.java +++ b/VirtualComputer/src/sznp/virtualcomputer/PluginMain.java @@ -16,12 +16,12 @@ import com.google.common.collect.Lists; public class PluginMain extends JavaPlugin { private IVirtualBox vbox; private ISession session; - private ArrayList renderers = new ArrayList<>(); private IMachine machine; private BukkitTask screenupdatetask; 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 renderers = new ArrayList<>(); // Fired when plugin is first enabled @Override @@ -45,11 +45,11 @@ public class PluginMain extends JavaPlugin { ccs.sendMessage("§bLoading Screen..."); try { 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 - ccs.sendMessage("§bUsing Direct Renderer"); + 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"); } catch (NoClassDefFoundError e) { 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("§bLoaded!"); @@ -97,7 +97,7 @@ public class PluginMain extends JavaPlugin { screenupdatetask.cancel(); screenupdatetask = null; } - }, 100, 100); // Do a full update every 2 seconds + }, 100, 100); // Do a full update every 5 seconds sender.sendMessage("§eComputer started."); }); }