Commits

Owen Nelson committed 9378dcf

formalizing tile operations

Comments (0)

Files changed (4)

src/com/theomn/cartography/Cartography.java

 public class Cartography extends JavaPlugin {
 
     Logger log = Logger.getLogger("Minecraft");
-//    private final CartographyPlayerListener playerListener = new CartographyPlayerListener(this);
-    private final CartographyWorldListener worldListener = new CartographyWorldListener(this);
+    private final CartographyPlayerListener playerListener = new CartographyPlayerListener(this);
+//    private final CartographyWorldListener worldListener = new CartographyWorldListener(this);
 
 
     public void onEnable(){
         log.info("Cartography has been enabled!");
         PluginManager pm = this.getServer().getPluginManager();
-//        pm.registerEvent(Event.Type.PLAYER_MOVE, playerListener, Event.Priority.Normal, this);
-        pm.registerEvent(Event.Type.CHUNK_LOAD, worldListener, Event.Priority.Normal, this);
+        pm.registerEvent(Event.Type.PLAYER_MOVE, playerListener, Event.Priority.Normal, this);
+//        pm.registerEvent(Event.Type.CHUNK_LOAD, worldListener, Event.Priority.Normal, this);
 
         for(World world : this.getServer().getWorlds()){
             log.info(world.getName()+" : "+world.getUID());

src/com/theomn/cartography/CartographyPlayerListener.java

 package com.theomn.cartography;
 
+import org.bukkit.ChunkSnapshot;
 import org.bukkit.Location;
 import org.bukkit.World;
 import org.bukkit.entity.Player;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.logging.Logger;
 
 public class CartographyPlayerListener extends PlayerListener {
 
     public static Cartography plugin;
     public static CartographyRelay relay;
+    static Logger log = Logger.getLogger("Minecraft");
 
     public final int radius = 128;   // how many blocks around the player to sample
     public double threshold = 15.0;  // distance travelled since last sample
     public void onPlayerMove(PlayerMoveEvent event) {
 
         Player player = event.getPlayer();
-        Location playerLoc = player.getLocation();
-        World world = player.getWorld();
+        MapTile tile = new MapTile(player);
+        this.log.info(tile.toString());
 
-        if (this.previousLoc == null) {
-            this.previousLoc = playerLoc;
-        }
-
-        int minX = playerLoc.getBlockX() - this.radius;
-        int maxX = playerLoc.getBlockX() + this.radius;
-
-        int minZ = playerLoc.getBlockZ() - this.radius;
-        int maxZ = playerLoc.getBlockZ() + this.radius;
-
-        List<Integer> bounds = new ArrayList<Integer>();
-
-        bounds.add(minX);
-        bounds.add(maxX);
-        bounds.add(minZ);
-        bounds.add(maxZ);
-
-        if (this.previousLoc.distance(playerLoc) > this.threshold) {
-            List<Integer> blocks = new ArrayList<Integer>();
-
-            for (int x = minX; x < maxX; x++) {
-                for (int z = minZ; z < maxZ; z++) {
-                    blocks.add(world.getBlockTypeIdAt(x, world.getHighestBlockYAt(x, z)-1, z));
-                }
-            }
-
-            Map<String,Object> payload = new HashMap<String,Object>();
-            payload.put("bounds", bounds);
-            payload.put("blocks", blocks);
-            this.relay.emit(payload);
-            this.previousLoc = playerLoc;
-        }
+//        Location playerLoc = player.getLocation();
+//        World world = player.getWorld();
+//
+//        if (this.previousLoc == null) {
+//            this.previousLoc = playerLoc;
+//        }
+//
+//        int minX = playerLoc.getBlockX() - this.radius;
+//        int maxX = playerLoc.getBlockX() + this.radius;
+//
+//        int minZ = playerLoc.getBlockZ() - this.radius;
+//        int maxZ = playerLoc.getBlockZ() + this.radius;
+//
+//        List<Integer> bounds = new ArrayList<Integer>();
+//
+//        bounds.add(minX);
+//        bounds.add(maxX);
+//        bounds.add(minZ);
+//        bounds.add(maxZ);
+//
+//        if (this.previousLoc.distance(playerLoc) > this.threshold) {
+//            List<Integer> blocks = new ArrayList<Integer>();
+//
+//            for (int x = minX; x < maxX; x++) {
+//                for (int z = minZ; z < maxZ; z++) {
+//                    blocks.add(world.getBlockTypeIdAt(x, world.getHighestBlockYAt(x, z)-1, z));
+//                }
+//            }
+//
+//            Map<String,Object> payload = new HashMap<String,Object>();
+//            payload.put("bounds", bounds);
+//            payload.put("blocks", blocks);
+//            this.relay.emit(payload);
+//
+//        }
 
     }
 

src/com/theomn/cartography/CartographyWorldListener.java

     }
 
 }
+

src/com/theomn/cartography/MapTile.java

+package com.theomn.cartography;
+
+import com.sun.org.apache.xpath.internal.axes.SelfIteratorNoPredicate;
+import org.bukkit.Chunk;
+import org.bukkit.ChunkSnapshot;
+import org.bukkit.Location;
+import org.bukkit.World;
+import org.bukkit.entity.Player;
+import sun.plugin.dom.css.CSSUnknownRule;
+
+import javax.lang.model.util.ElementScanner6;
+import java.util.ArrayList;
+import java.util.List;
+
+/*
+ * MapTile is a representation of a 10x10 chunk (160x160 block)
+ * area of the world.
+ *
+ */
+public class MapTile {
+
+    /* chunks are 16x16 blocks */
+    public final static int blocks_per_chunk = 16;
+
+    /* width/height of tile in chunks */
+    public final static int tile_size = 10;
+
+
+    private ChunkSnapshot source;
+    private World world;
+
+    private int top;
+    private int left;
+    private int right;
+    private int bottom;
+
+    private List<Integer> blocks = null;
+
+
+    public MapTile(Chunk chunk){
+        this.setSource(chunk);
+    }
+
+
+    public MapTile(Location location){
+        World world = location.getWorld();
+        this.setSource(world.getChunkAt(location));
+    }
+
+
+    public MapTile(Player player){
+        Location location = player.getLocation();
+        World world = location.getWorld();
+        this.setSource(world.getChunkAt(location));
+    }
+
+    /* populates source, top, left, right, and bottom */
+    private void setSource(Chunk chunk){
+        this.source = chunk.getChunkSnapshot(true, false, false);
+        this.world = chunk.getWorld();
+
+        int source_x = this.source.getX();
+        int source_z = this.source.getZ();
+
+        this.left = ((( source_x / tile_size ) + 1)  * tile_size ) - tile_size;
+        this.top = ((( (source_z) / tile_size ) + 1 ) * tile_size ) - tile_size;
+
+        if (source_x < 0) {
+            this.left = ((( (source_x+1) / tile_size ) + 1)  * tile_size ) - tile_size;
+            this.left -= tile_size;
+        }
+        this.right = (this.left + (tile_size-1));
+
+        if (source_z < 0) {
+            this.top = ((( (source_z+1) / tile_size ) + 1)  * tile_size ) - tile_size;
+            this.top -= tile_size;
+        }
+        this.bottom = (this.top + (tile_size-1));
+    }
+
+
+    public String toString(){
+        return "MapTile for ("+this.source.getX()+","+this.source.getZ()+"): ("+this.left+","+this.top+") -> ("+this.right+","+this.bottom+")";
+    }
+
+
+    /*
+     * Returns a list of block type ids for the selected tile.
+     */
+    public List<Integer> getBlocks(){
+        if (this.blocks == null){
+            this.blocks = new ArrayList<Integer>();
+
+        }
+        return this.blocks;
+    }
+}