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)
This commit is contained in:
Norbi Peti 2017-08-04 17:12:23 +02:00
parent 042c67a30b
commit 169285a111
7 changed files with 29 additions and 31 deletions

View file

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

View file

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

View file

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

View file

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

View file

@ -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++)

View file

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

View file

@ -16,12 +16,12 @@ import com.google.common.collect.Lists;
public class PluginMain extends JavaPlugin {
private IVirtualBox vbox;
private ISession session;
private ArrayList<IRenderer> 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<IRenderer> 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.");
});
}