Last active
August 29, 2015 14:00
-
-
Save ase34/9b05bcdac9f183968838 to your computer and use it in GitHub Desktop.
drawable map code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* See the whitepaper at https://docs.google.com/file/d/0B9Sf4F-ig8lPUlhOdHZ2ZGtzdnM | |
*/ | |
package intersection; | |
import java.awt.Color; | |
import java.awt.Graphics2D; | |
import java.awt.image.BufferedImage; | |
import java.util.HashMap; | |
import org.apache.commons.lang.ArrayUtils; | |
import org.bukkit.Location; | |
import org.bukkit.entity.Player; | |
import org.bukkit.event.EventHandler; | |
import org.bukkit.event.Listener; | |
import org.bukkit.event.player.PlayerInteractEntityEvent; | |
import org.bukkit.event.player.PlayerInteractEvent; | |
import org.bukkit.map.MapCanvas; | |
import org.bukkit.map.MapRenderer; | |
import org.bukkit.map.MapView; | |
import org.bukkit.plugin.java.JavaPlugin; | |
import org.bukkit.util.Vector; | |
public class Demo extends JavaPlugin implements Listener { | |
// the methods in the whitepaper | |
public static double determinant(double[][] matrix) { | |
return matrix[0][0] * matrix[1][1] * matrix[2][2] + matrix[0][1] * matrix[1][2] * matrix[2][0] + matrix[0][2] * matrix[1][0] * matrix[2][1] - matrix[0][2] * matrix[1][1] * matrix[2][0] - matrix[0][1] * matrix[1][0] * matrix[2][2] - matrix[0][0] * matrix[1][2] * matrix[2][1]; | |
} | |
public static Vector getIntersection(Vector linePoint, Vector lineDirection, Vector planeOrigin, Vector planeXDirection, Vector planeYDirection) { | |
double[][] coefficients = { | |
{lineDirection.getX(), -planeXDirection.getX(), -planeYDirection.getX()}, | |
{lineDirection.getY(), -planeXDirection.getY(), -planeYDirection.getY()}, | |
{lineDirection.getZ(), -planeXDirection.getZ(), -planeYDirection.getZ()} | |
}; | |
double[] solutions = { | |
planeOrigin.getX() - linePoint.getX(), | |
planeOrigin.getY() - linePoint.getY(), | |
planeOrigin.getZ() - linePoint.getZ() | |
}; | |
double[][] dMatrix = { | |
{solutions[0], coefficients[0][1], coefficients[0][2]}, | |
{solutions[1], coefficients[1][1], coefficients[1][2]}, | |
{solutions[2], coefficients[2][1], coefficients[2][2]} | |
}; | |
double[][] uMatrix = { | |
{coefficients[0][0], solutions[0], coefficients[0][2]}, | |
{coefficients[1][0], solutions[1], coefficients[1][2]}, | |
{coefficients[2][0], solutions[2], coefficients[2][2]} | |
}; | |
double[][] vMatrix = { | |
{coefficients[0][0], coefficients[0][1], solutions[0]}, | |
{coefficients[1][0], coefficients[1][1], solutions[1]}, | |
{coefficients[2][0], coefficients[2][1], solutions[2]} | |
}; | |
System.out.println(planeOrigin); | |
System.out.println(linePoint); | |
System.out.println(ArrayUtils.toString(coefficients)); | |
System.out.println(ArrayUtils.toString(solutions)); | |
double det = determinant(coefficients); | |
double d = determinant(dMatrix) / det; | |
double u = determinant(uMatrix) / det; | |
double v = determinant(vMatrix) / det; | |
return new Vector(u, v, 0); | |
} | |
// utility class | |
public static class ClickAction { | |
private long date; | |
private Vector point; | |
public ClickAction(long date, Vector point) { | |
this.date = date; | |
this.point = point; | |
} | |
public long getDate() { | |
return date; | |
} | |
public Vector getPoint() { | |
return point; | |
} | |
} | |
private BufferedImage image; | |
private HashMap<String, ClickAction> lastClickAction; | |
private MapView view; | |
@Override | |
public void onEnable() { | |
// setup fields | |
this.lastClickAction = new HashMap<String, ClickAction>(); | |
this.image = new BufferedImage(128, 128, BufferedImage.TYPE_4BYTE_ABGR); | |
// fill the image with white | |
Graphics2D g = image.createGraphics(); | |
g.setColor(Color.white); | |
g.fillRect(0, 0, 127, 127); | |
g.dispose(); | |
this.view = getServer().getMap((short) 50); // map id | |
// remove renderers | |
for (MapRenderer renderer : view.getRenderers()) { | |
view.removeRenderer(renderer); | |
} | |
// adds the main renderer | |
view.addRenderer(new MapRenderer(true) { | |
@Override | |
public void render(MapView view, MapCanvas canvas, Player player) { | |
canvas.drawImage(0, 0, image); | |
} | |
}); | |
getServer().getPluginManager().registerEvents(this, this); | |
} | |
@EventHandler | |
public void onRightClick(PlayerInteractEntityEvent ev) { | |
ev.setCancelled(true); | |
draw(ev.getPlayer()); | |
} | |
@EventHandler | |
public void onRightClick(PlayerInteractEvent ev) { | |
ev.setCancelled(true); | |
draw(ev.getPlayer()); | |
} | |
public void draw(Player player) { | |
Location eyeLocation = player.getEyeLocation(); | |
Vector planeOrigin = new Vector(-183, 6, -1260.0625); // the exact coordinate of the top left corner of the frame (p_0 in the figure) | |
Vector planeXDirection = new Vector(-1, 0, 0); // vector pointing to the top right corner (u in the figure) | |
Vector planeYDirection = new Vector(0, -1, 0); // vector pointing to the bottom left corner (v in the figure) | |
Vector intersection = getIntersection(eyeLocation.toVector(), eyeLocation.getDirection(), planeOrigin, planeXDirection, planeYDirection); | |
ClickAction lastClick = lastClickAction.get(player.getName()); | |
// the coordinates on the map | |
int x = (int) (intersection.getX() * 128); | |
int y = (int) (intersection.getY() * 128); | |
// check if the last click was before 300ms | |
if (lastClick != null && System.currentTimeMillis() - lastClick.getDate() < 300) { | |
// draw the line between the current and the last point | |
Vector lastIntersection = lastClick.getPoint(); | |
int lastX = (int) (lastIntersection.getX() * 128); | |
int lastZ = (int) (lastIntersection.getY() * 128); | |
Graphics2D g = image.createGraphics(); | |
g.setColor(Color.red); | |
g.drawLine(lastX, lastZ, x, y); | |
g.dispose(); | |
} else { | |
// draw only the point | |
this.image.setRGB(x, y, Color.red.getRGB()); | |
} | |
// trigger re-render + send map to player | |
player.sendMap(this.view); | |
// write latest position | |
this.lastClickAction.put(player.getName(), new ClickAction(System.currentTimeMillis(), intersection)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment