Directly using VirtualBox from Java #5

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

View file

@ -1,217 +0,0 @@
package com.mcplugindev.slipswhitley.sketchmap;
/*
* This file was originally taken from https://github.com/slipswhitley/SketchMap
*/
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import com.mcplugindev.slipswhitley.sketchmap.file.SketchMapFileException;
import com.mcplugindev.slipswhitley.sketchmap.map.RelativeLocation;
import com.mcplugindev.slipswhitley.sketchmap.map.SketchMap;
//import com.mcplugindev.slipswhitley.sketchmap.map.SketchMap.BaseFormat;
public class SketchMapAPI
{
public static SketchMap getMapByID(String id)
{
for (SketchMap map : SketchMap.getLoadedMaps())
{
if (map.getID().equalsIgnoreCase(id))
{
return map;
}
}
return null;
}
public static List<ItemStack> getOrderedItemSet(SketchMap map)
{
List<ItemStack> items = new ArrayList<ItemStack>();
for (int y = 0; y < map.getLengthY(); y++)
{
for (int x = 0; x < map.getLengthX(); x++)
{
for (RelativeLocation loc : map.getMapCollection().keySet())
{
if (loc.getX() != x || loc.getY() != y)
{
continue;
}
ItemStack iStack = new ItemStack(Material.MAP, 1);
iStack.setDurability(SketchMapUtils.getMapID(map
.getMapCollection().get(loc)));
ItemMeta iMeta = iStack.getItemMeta();
iMeta.setDisplayName(ChatColor.GREEN + "SketchMap ID: "
+ ChatColor.GOLD + map.getID() + ChatColor.GREEN
+ " Pos-X: " + ChatColor.GOLD + (x + 1)
+ ChatColor.GREEN + " Pos-Y: " + ChatColor.GOLD
+ (y + 1));
iMeta.setLore(Arrays.asList(new String[] { ChatColor.GRAY
+ "SketchMap ID: " + map.getID()
}));
iStack.setItemMeta(iMeta);
items.add(iStack);
}
}
}
return items;
}
public static SketchMap loadSketchMapFromFile(File file)
throws SketchMapFileException
{
YamlConfiguration config = null;
try
{
config = YamlConfiguration.loadConfiguration(file);
} catch (Exception ex)
{
throw new SketchMapFileException("Invalid SketchMap File \""
+ file.getName() + "\"");
}
String[] fieldSet = { "x-panes", "y-panes", "public-protected",
"map-collection", "base-format", "map-image", };
for (String field : fieldSet)
{
if (!config.isSet(field))
{
throw new SketchMapFileException(
"Unable to load SketchMap file \"" + file.getName()
+ "\" missing field \"" + field + "\"");
}
}
Integer xPanes = config.getInt("x-panes");
if (xPanes == null || xPanes < 1)
{
throw new SketchMapFileException("Unable to load SketchMap file \""
+ file.getName() + "\" invalid field \"x-panes\"");
}
Integer yPanes = config.getInt("y-panes");
if (yPanes == null || yPanes < 1)
{
throw new SketchMapFileException("Unable to load SketchMap file \""
+ file.getName() + "\" invalid field \"y-panes\"");
}
Boolean publicProtected = config.getBoolean("public-protected");
if (publicProtected == null)
{
throw new SketchMapFileException("Unable to load SketchMap file \""
+ file.getName() + "\" invalid field \"public-protected\"");
}
List<String> mapList = config.getStringList("map-collection");
if (mapList == null)
{
throw new SketchMapFileException("Unable to load SketchMap file \""
+ file.getName() + "\" invalid field \"map-collection\"");
}
Map<Short, RelativeLocation> mapCollection = new HashMap<Short, RelativeLocation>();
for (String map : mapList)
{
String[] split = map.split(" ");
if (split.length != 2)
{
throw new SketchMapFileException(
"Unable to load SketchMap file \"" + file.getName()
+ "\" cannot parse field in \"map-colection\"");
}
RelativeLocation loc = RelativeLocation.fromString(split[0]);
if (loc == null)
{
throw new SketchMapFileException(
"Unable to load SketchMap file \"" + file.getName()
+ "\" cannot parse field in \"map-colection\"");
}
Short id = null;
try
{
id = Short.parseShort(split[1]);
} catch (Exception ex)
{
throw new SketchMapFileException(
"Unable to load SketchMap file \"" + file.getName()
+ "\" cannot parse field in \"map-colection\"");
}
mapCollection.put(id, loc);
}
//BaseFormat format = null;
/*
* try {
* format = BaseFormat.valueOf(config.getString("base-format"));
* }
* catch (Exception ex) {
* throw new SketchMapFileException("Unable to load SketchMap file \"" +
* file.getName()
* + "\" cannot parse BaseFormat from field \"base-format\"");
* }
*/
String b64Img = config.getString("map-image");
if (b64Img == null)
{
throw new SketchMapFileException("Unable to load SketchMap file \""
+ file.getName() + "\" invalid field \"map-image\"");
}
BufferedImage image = null;
try
{
image = SketchMapUtils.base64StringToImg(b64Img);
} catch (Exception ex)
{
throw new SketchMapFileException("Unable to load SketchMap file \""
+ file.getName()
+ "\" parse image from field \"map-image\"");
}
String imageID = file.getName().substring(0,
file.getName().lastIndexOf("."));
if (getMapByID(imageID) != null)
{
throw new SketchMapFileException("Unable to load SketchMap file \""
+ file.getName()
+ "\" A SketchMap by that ID already exists.");
}
return new SketchMap(image, imageID, yPanes, yPanes, publicProtected);
}
}

View file

@ -1,54 +0,0 @@
package com.mcplugindev.slipswhitley.sketchmap;
/*
* This file was originally taken from https://github.com/slipswhitley/SketchMap
*/
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.plugin.java.JavaPlugin;
import com.mcplugindev.slipswhitley.sketchmap.listener.PlayerListener;
public class SketchMapPlugin extends JavaPlugin
{
private static SketchMapPlugin plugin;
public void onEnable()
{
plugin = this;
setupListeners();
sendEnabledMessage();
}
private void sendEnabledMessage()
{
SketchMapUtils.sendColoredConsoleMessage(ChatColor.GREEN
+ "| |");
SketchMapUtils.sendColoredConsoleMessage(ChatColor.GREEN + "| "
+ ChatColor.AQUA + "SketchMap "
+ this.getDescription().getVersion() + " has been Enabled!"
+ ChatColor.GREEN + " |");
SketchMapUtils.sendColoredConsoleMessage(ChatColor.GREEN + "| "
+ ChatColor.AQUA + " Authors: SlipsWhitley & Fyrinlight"
+ ChatColor.GREEN + " |");
SketchMapUtils.sendColoredConsoleMessage(ChatColor.GREEN
+ "| |");
}
private void setupListeners()
{
Bukkit.getPluginManager().registerEvents(new PlayerListener(), this);
}
public static SketchMapPlugin getPlugin()
{
return plugin;
}
}

View file

@ -1,109 +0,0 @@
package com.mcplugindev.slipswhitley.sketchmap;
/*
* This file was originally taken from https://github.com/slipswhitley/SketchMap
*/
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Base64;
import java.util.HashSet;
import javax.imageio.ImageIO;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
import org.bukkit.map.MapView;
public class SketchMapUtils
{
/**
*
* Image Utils
*
*/
public static BufferedImage resize(Image img, Integer width, Integer height)
{
img = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
if (img instanceof BufferedImage)
{
return (BufferedImage) img;
}
BufferedImage bimage = new BufferedImage(img.getWidth(null),
img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D bGr = bimage.createGraphics();
bGr.drawImage(img, 0, 0, null);
bGr.dispose();
return bimage;
}
public static BufferedImage base64StringToImg(final String base64String)
{
try
{
return ImageIO.read(new ByteArrayInputStream(Base64.getDecoder()
.decode(base64String)));
} catch (final IOException ioe)
{
throw new UncheckedIOException(ioe);
}
}
public static void sendColoredConsoleMessage(String msg)
{
ConsoleCommandSender sender = Bukkit.getConsoleSender();
sender.sendMessage(msg);
}
/**
* Deprecated Methods Here :'c
*/
@SuppressWarnings("deprecation")
public static short getMapID(MapView map)
{
return map.getId();
}
@SuppressWarnings("deprecation")
public static MapView getMapView(short id)
{
MapView map = Bukkit.getMap(id);
if (map != null)
{
return map;
}
return Bukkit.createMap(getDefaultWorld());
}
/**
*
*/
public static Block getTargetBlock(Player player, int i)
{
return player.getTargetBlock((HashSet<Material>) null, i);
}
public static World getDefaultWorld()
{
return Bukkit.getWorlds().get(0);
}
}

View file

@ -1,14 +0,0 @@
package com.mcplugindev.slipswhitley.sketchmap.file;
public class SketchMapFileException extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;
public SketchMapFileException(String message) {
super(message);
}
}

View file

@ -1,69 +0,0 @@
package com.mcplugindev.slipswhitley.sketchmap.listener;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.entity.ItemFrame;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
public class PlayerListener implements Listener {
@EventHandler
public void onPlayerInteract(PlayerInteractEntityEvent event) {
if(!(event.getRightClicked() instanceof ItemFrame)) {
return;
}
ItemFrame iFrame = (ItemFrame) event.getRightClicked();
ItemStack iHand = event.getPlayer().getItemInHand();
if(iHand.getType() != Material.MAP) {
return;
}
String lore = iHand.getItemMeta().getLore().get(0);
if(!ChatColor.stripColor(lore).startsWith("SketchMap ID:")) {
return;
}
if(iFrame.getItem().getType() != Material.AIR) {
return;
}
if(event.isCancelled()) {
return;
}
event.setCancelled(true);
ItemStack frameItem = iHand.clone();
frameItem.setAmount(1);
ItemMeta frameIMeta = frameItem.getItemMeta();
frameIMeta.setDisplayName("");
frameItem.setItemMeta(frameIMeta);
iFrame.setItem(frameItem);
Player player = event.getPlayer();
if(player.getGameMode() == GameMode.CREATIVE) {
return;
}
if(iHand.getAmount() == 1) {
player.getInventory().setItemInHand(new ItemStack(Material.AIR));
return;
}
iHand.setAmount(iHand.getAmount() - 1);
}
}

View file

@ -1,45 +0,0 @@
package com.mcplugindev.slipswhitley.sketchmap.map;
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 ImageRenderer extends MapRenderer // Modified
{
private BufferedImage image;
public ImageRenderer(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

@ -1,53 +0,0 @@
package com.mcplugindev.slipswhitley.sketchmap.map;
public class RelativeLocation {
private int x;
private int y;
public RelativeLocation (int x, int y) {
this.x = x;
this.y = y;
}
public String toString() {
return x + ":" + y;
}
public static RelativeLocation fromString(String str) {
String[] args = str.split(":");
if(args.length != 2) {
return null;
}
int x = 0;
int y = 0;
try {
x = Integer.parseInt(args[0]);
y = Integer.parseInt(args[1]);
}
catch (Exception ex) {
return null;
}
return new RelativeLocation (x, y);
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}

View file

@ -1,220 +0,0 @@
package com.mcplugindev.slipswhitley.sketchmap.map;
/*
* This file was originally taken from https://github.com/slipswhitley/SketchMap
*/
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.map.MapView;
import com.mcplugindev.slipswhitley.sketchmap.SketchMapUtils;
public class SketchMap
{
public BufferedImage image;
private String mapID;
private Integer xPanes;
private Integer yPanes;
private Boolean publicProtected;
//private BaseFormat format;
private Map<RelativeLocation, MapView> mapCollection;
/**
*
* Create SketchMap using New Maps
*
*/
public SketchMap(BufferedImage image, String mapID, int xPanes, int yPanes,
boolean publicProtected)
{
this.image = SketchMapUtils.resize(image, xPanes * 128, yPanes * 128);
this.mapID = mapID;
this.xPanes = xPanes;
this.yPanes = yPanes;
this.publicProtected = publicProtected;
//this.format = format;
this.mapCollection = new HashMap<RelativeLocation, MapView>();
getLoadedMaps().add(this);
loadSketchMap();
}
private void loadSketchMap()
{
for (int x = 0; x < xPanes; x++)
{
for (int y = 0; y < yPanes; y++)
{
initMap(x, y,
Bukkit.createMap(SketchMapUtils.getDefaultWorld()));
}
}
}
/**
*
* Create SketchMap using Specified Maps
*
*/
public SketchMap(BufferedImage image, String mapID, int xPanes, int yPanes,
boolean publicProtected, Map<Short, RelativeLocation> mapCollection)
{
this.image = SketchMapUtils.resize(image, xPanes * 128, yPanes * 128);
this.mapID = mapID;
this.xPanes = xPanes;
this.yPanes = yPanes;
this.publicProtected = publicProtected;
//this.format = format;
this.mapCollection = new HashMap<RelativeLocation, MapView>();
getLoadedMaps().add(this);
loadSketchMap(mapCollection);
}
private void loadSketchMap(Map<Short, RelativeLocation> mapCollection)
{
for (Short mapID : mapCollection.keySet())
{
RelativeLocation loc = mapCollection.get(mapID);
initMap(loc.getX(), loc.getY(), SketchMapUtils.getMapView(mapID));
}
}
/**
*
*
*
*/
private void initMap(int x, int y, MapView mapView)
{
BufferedImage subImage = image.getSubimage(x * 128, y * 128, 128, 128);
mapView.getRenderers().clear();
mapView.addRenderer(new ImageRenderer(subImage));
mapCollection.put(new RelativeLocation(x, y), mapView);
}
/**
*
* Get Object information
*
*/
public String getID()
{
return mapID;
}
public BufferedImage getImage()
{
return image;
}
public int getLengthX()
{
return xPanes;
}
public int getLengthY()
{
return yPanes;
}
public boolean isPublicProtected()
{
return publicProtected;
}
public Map<RelativeLocation, MapView> getMapCollection()
{
return mapCollection;
}
/*
* public BaseFormat getBaseFormat() {
* return format;
* }
*/
/**
*
* Map Functions
*
*
*/
public void delete()
{
getLoadedMaps().remove(this);
try
{
this.finalize();
} catch (Throwable e)
{
}
}
/**
*
* Static Methods
*
*/
private static Set<SketchMap> sketchMaps;
public static Set<SketchMap> getLoadedMaps()
{
if (sketchMaps == null)
{
sketchMaps = new HashSet<SketchMap>();
}
return sketchMaps;
}
/*
* public enum BaseFormat {
* PNG,
* JPEG;
*
* public String getExtension() {
* if(this == BaseFormat.PNG) {
* return "png";
* }
* if(this == BaseFormat.JPEG) {
* return "jpg";
* }
* return null;
* }
*
* public static BaseFormat fromExtension(String ext) {
* if(ext.equalsIgnoreCase("png")) {
* return BaseFormat.PNG;
*
* }
* if(ext.equalsIgnoreCase("jpg")) {
* return BaseFormat.JPEG;
* }
* return null;
* }
* }
*/
}

View file

@ -3,91 +3,76 @@ package sznp.virtualcomputer;
import org.mozilla.interfaces.IFramebuffer;
import org.mozilla.interfaces.IFramebufferOverlay;
import org.mozilla.interfaces.nsISupports;
import org.virtualbox_5_1.BitmapFormat;
import org.virtualbox_5_1.FramebufferCapabilities;
public class MCFrameBuffer implements IFramebuffer {
@Override
public nsISupports queryInterface(String arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getBitsPerPixel() {
// TODO Auto-generated method stub
return 0;
return 32;
}
@Override
public long getBytesPerLine() {
// TODO Auto-generated method stub
return 0;
return 640L;
}
@Override
public long[] getCapabilities(long[] arg0) {
// TODO Auto-generated method stub
return null;
return new long[] { FramebufferCapabilities.UpdateImage.value() };
}
@Override
public long getHeight() {
// TODO Auto-generated method stub
return 0;
return 480;
}
@Override
public long getHeightReduction() {
// TODO Auto-generated method stub
return 0;
}
@Override
public IFramebufferOverlay getOverlay() {
// TODO Auto-generated method stub
return null;
}
@Override
public long getPixelFormat() {
// TODO Auto-generated method stub
return 0;
return BitmapFormat.BGRA.value();
}
@Override
public long getVisibleRegion(byte arg0, long arg1) {
// TODO Auto-generated method stub
return 0;
}
@Override
public long getWidth() {
// TODO Auto-generated method stub
return 0;
return 640;
}
@Override
public long getWinId() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void notify3DEvent(long arg0, byte[] arg1) {
// TODO Auto-generated method stub
}
@Override
public void notifyChange(long arg0, long arg1, long arg2, long arg3, long arg4) {
// TODO Auto-generated method stub
}
@Override
public void notifyUpdate(long arg0, long arg1, long arg2, long arg3) {
// TODO Auto-generated method stub
}
@Override
@ -97,19 +82,14 @@ public class MCFrameBuffer implements IFramebuffer {
@Override
public void processVHWACommand(byte arg0) {
// TODO Auto-generated method stub
}
@Override
public void setVisibleRegion(byte arg0, long arg1) {
// TODO Auto-generated method stub
}
@Override
public boolean videoModeSupported(long arg0, long arg1, long arg2) {
// TODO Auto-generated method stub
return false;
return true;
}
}