Skip to content

Commit

Permalink
Code Clean Up
Browse files Browse the repository at this point in the history
  • Loading branch information
Achoowy committed Dec 4, 2020
1 parent 9367519 commit 1512278
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 26 deletions.
12 changes: 3 additions & 9 deletions Cameras/src/water/of/cup/Picture.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,17 @@ public static boolean takePicture(Player p) {

ItemStack itemStack = new ItemStack(Material.FILLED_MAP); // requires api-version: 1.13 in plugin.yml
MapMeta mapMeta = (MapMeta) itemStack.getItemMeta();
MapView mapView = Bukkit.createMap(p.getWorld()); // or Bukkit.getMap(int id); or another existing MapView
//mapView.setCenterX(p.getLocation().getBlockX());
//mapView.setCenterZ(p.getLocation().getBlockZ());
//mapView.setScale(Scale.NORMAL);
MapView mapView = Bukkit.createMap(p.getWorld());

mapView.setTrackingPosition(false);
//MapCanvas mapCanvas = new MapCanvas();

for(MapRenderer renderer : mapView.getRenderers())
mapView.removeRenderer(renderer);

Renderer customRenderer = new Renderer();

mapView.addRenderer(customRenderer);

mapMeta.setMapView(mapView);

// itemStack.setDurability((short) mapView.getId());
// mapMeta.set...
itemStack.setItemMeta(mapMeta);
p.getInventory().addItem(itemStack);

Expand Down
19 changes: 11 additions & 8 deletions Cameras/src/water/of/cup/Renderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,29 @@ public void render(MapView map, MapCanvas canvas, Player player) {
if (map.isLocked()) {
return;
}
Bukkit.getLogger().info("Rendering picture...");

Location eyes = player.getEyeLocation();

//get pitch and yaw of players head to calculate ray trace directions
Location eyes = player.getEyeLocation();
double pitch = -Math.toRadians(player.getEyeLocation().getPitch());
double yaw = Math.toRadians(player.getEyeLocation().getYaw() + 90);
double yaw = Math.toRadians(player.getEyeLocation().getYaw() + 90);

byte[][] canvasBytes = new byte[128][128];

//loop through every pixel on map
for (int x = 0; x < 128; x++) {
for (int y = 0; y < 128; y++) {
for (int y = 0; y < 128; y++) {

double yrotate = -((y) * .9 / 128 - .45);
//calculate ray rotations
double yrotate = -((y) * .9 / 128 - .45);
double xrotate = ((x) * .9 / 128 - .45);

RayTraceResult result = player.getWorld().rayTraceBlocks(eyes, new Vector(Math.cos(yaw + xrotate)*Math.cos(pitch + yrotate), Math.sin(pitch + yrotate), Math.sin(yaw + xrotate)*Math.cos(pitch + yrotate)), 256);
//Bukkit.getLogger().info("Pitch: " + pitch + ", Yaw: " + yaw);
if (result != null) {
if (result != null) {
//set map pixel to color of block found
canvas.setPixel(x, y, Utils.colorFromType(result.getHitBlock(), result.getHitBlock().getLightFromBlocks() + result.getHitBlock().getLightFromSky(), result.getHitBlockFace()));
canvasBytes[x][y] = Utils.colorFromType(result.getHitBlock(), result.getHitBlock().getLightFromBlocks() + result.getHitBlock().getLightFromSky(), result.getHitBlockFace());
} else {
//no block was hit, so we will assume we are looking at the sky
canvas.setPixel(x, y, MapPalette.PALE_BLUE);
canvasBytes[x][y] = MapPalette.PALE_BLUE;
}
Expand Down
10 changes: 6 additions & 4 deletions Cameras/src/water/of/cup/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class Utils {
static Map<Material, Color> blocksMap = new HashMap<Material, Color>();

public static void loadColors() {

//Materials we don't want to use minecraft images for (could be because the image provides a poor color)
blocksMap.put(Material.GRASS, new Color(49, 101, 25));
blocksMap.put(Material.TALL_GRASS, new Color(49, 101, 25));
blocksMap.put(Material.LARGE_FERN, new Color(49, 101, 25));
Expand Down Expand Up @@ -191,16 +191,18 @@ public static void loadColors() {
@SuppressWarnings("deprecation")
public static byte colorFromType(Block block, int light, BlockFace blockFace) {
HashMap<Material, BufferedImage> imageMap = Camera.getInstance().getResourcePackManager().getImageHashMap();
if (blocksMap.containsKey(block.getType())) {
if (blocksMap.containsKey(block.getType())) {
//if blockMap has a color for the material, use that color
Color color = blocksMap.get(block.getType());
return MapPalette.matchColor(color);
}
if (imageMap.containsKey(block.getType())) {
//if imageMap has a color for the material, use that color
BufferedImage image = imageMap.get(block.getType());
Color color = new Color(image.getRGB((int) (image.getWidth() / 1.5), (int) (image.getHeight() / 1.5)));
Color color = new Color(image.getRGB((int) (image.getWidth() / 1.5), (int) (image.getHeight() / 1.5))); //gets certain pixel in image to use as color TODO: Create a hashmap of colors so we don't need to access the image multiple times.
return MapPalette.matchColor(new Color(color.getRed(), color.getGreen(), color.getBlue()));
}
return MapPalette.GRAY_2;
return MapPalette.GRAY_2; //no color was found, use gray

}
}
2 changes: 0 additions & 2 deletions Cameras/src/water/of/cup/ZipUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ public static void unzip(File fileNew, String destPath) throws IOException {
long size = zipEntry.getSize();
long compressedSize = zipEntry.getCompressedSize();

// Bukkit.getLogger().info("ZipUtils: name: " + name + " | size: " + size + " | compressed size: + " + compressedSize);

File file = new File(destPath + name);
if (name.endsWith("/")) {
file.mkdirs();
Expand Down
9 changes: 6 additions & 3 deletions Cameras/src/water/of/cup/listeners/CameraClick.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@ public void cameraClicked(PlayerInteractEvent e) {
Player p = e.getPlayer();
if ((e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK))
&& e.getItem().getItemMeta().getDisplayName().equals(ChatColor.DARK_BLUE + "Camera")) {
if (p.getInventory().firstEmpty() == -1) {
if (p.getInventory().firstEmpty() == -1) { //check to make sure there is room in the inventory for the map
p.sendMessage("You can not take a picture with a full inventory");
return;
}
if (p.getInventory().contains(Material.PAPER)) {
Map<Integer, ? extends ItemStack> paperHash = p.getInventory().all(Material.PAPER);
if (p.getInventory().contains(Material.PAPER)) { //check to make sure the player has paper

//remove 1 paper from the player's inventory
Map<Integer, ? extends ItemStack> paperHash = p.getInventory().all(Material.PAPER);
for (ItemStack item : paperHash.values()) {
item.setAmount(item.getAmount() - 1);
break;
}

Picture.takePicture(p);
} else {
p.sendMessage("You must have paper in order to take a picture");
Expand Down

0 comments on commit 1512278

Please sign in to comment.