From ba3f8bfe982dfe94e1087b9be8ab7fea32693397 Mon Sep 17 00:00:00 2001 From: Mochaccino Date: Mon, 18 Mar 2013 22:15:23 +0100 Subject: [PATCH] Replicator class refactored and full implementation of getReplicators from given block --- .../java/de/craftinc/replicator/Pattern.java | 10 +-- .../de/craftinc/replicator/Replicator.java | 62 +++++++++++-------- 2 files changed, 42 insertions(+), 30 deletions(-) diff --git a/src/main/java/de/craftinc/replicator/Pattern.java b/src/main/java/de/craftinc/replicator/Pattern.java index 90eaee7..038ce62 100644 --- a/src/main/java/de/craftinc/replicator/Pattern.java +++ b/src/main/java/de/craftinc/replicator/Pattern.java @@ -32,15 +32,15 @@ public class Pattern { {{Material.OBSIDIAN,Material.OBSIDIAN,null},{Material.MOSSY_COBBLESTONE,Material.OBSIDIAN,null},{Material.GLOWSTONE,Material.OBSIDIAN,null}}}; //direction north (replicator direction) - public Material getCenter(){ + public static Material getCenter(){ return repPattern[1][1][1]; } - public Material[][][] getNorth(){ + public static Material[][][] getNorth(){ return repPattern; } - public Material[][][] getSouth(){ + public static Material[][][] getSouth(){ Material[][][] newPattern = new Material[3][3][3]; for(int x=0;x<=2;x++){ for(int y=0;y<=2;y++){ @@ -52,7 +52,7 @@ public class Pattern { return newPattern; } - public Material[][][] getWest(){ + public static Material[][][] getWest(){ Material[][][] newPattern = new Material[3][3][3]; for(int x=0;x<=2;x++){ for(int y=0;y<=2;y++){ @@ -64,7 +64,7 @@ public class Pattern { return newPattern; } - public Material[][][] getEast(){ + public static Material[][][] getEast(){ Material[][][] westPattern = getWest(); Material[][][] newPattern = new Material[3][3][3]; for(int y=0;y<=2;y++){ diff --git a/src/main/java/de/craftinc/replicator/Replicator.java b/src/main/java/de/craftinc/replicator/Replicator.java index e449a10..8e1cd95 100644 --- a/src/main/java/de/craftinc/replicator/Replicator.java +++ b/src/main/java/de/craftinc/replicator/Replicator.java @@ -20,7 +20,6 @@ package de.craftinc.replicator; import org.bukkit.Location; import org.bukkit.block.BlockFace; import org.bukkit.Material; -import org.bukkit.entity.Player; import java.util.ArrayList; @@ -36,15 +35,13 @@ public class Replicator { private ArrayList owners; private ArrayList users; - private Location spawn; - private Location pumpkin; + private Location center; - public Replicator(String firstOwner, Location spawn, Location pumpkin) { + public Replicator(String firstOwner, Location spawn, Location center) { this.owners = new ArrayList(); this.users = new ArrayList(); this.owners.add(firstOwner); - this.spawn = spawn; - this.pumpkin = pumpkin; + this.center = center; } public void addUser(String user) { @@ -65,36 +62,51 @@ public class Replicator { else return false; } - private BlockFace getDirection(Location pumpkin) { - - if(this.pumpkin.getBlock().get) - - if(this.spawn.getBlock().getRelative(BlockFace.EAST).equals(this.pumpkin.getBlock())) return BlockFace.EAST; - else if(this.spawn.getBlock().getRelative(BlockFace.SOUTH).equals(this.pumpkin.getBlock())) return BlockFace.SOUTH; - else if(this.spawn.getBlock().getRelative(BlockFace.WEST).equals(this.pumpkin.getBlock())) return BlockFace.WEST; - else if(this.spawn.getBlock().getRelative(BlockFace.NORTH).equals(this.pumpkin.getBlock())) return BlockFace.NORTH; - else return null; - } - - private boolean isValid(Location pumpkin){ - if(pumpkin.getBlock().getRelative(BlockFace.DOWN).equals(Material.DIAMOND_BLOCK)){ - + public static ArrayList getReplicators(Location currentBlock){ + ArrayList replicators = new ArrayList(); + ArrayList centers = getCenters(currentBlock); + for(Location center:centers){ + if(isValid(center)){ + replicators.add(center); + } } - return false; + return replicators; } - private ArrayList getPumpkin(Location currentBlock){ - ArrayList pumpkins = new ArrayList(); + private static Material[][][] getPattern(Location center){ + if(center.getBlock().getRelative(BlockFace.NORTH).getType().equals(Material.AIR)) return Pattern.getNorth(); + if(center.getBlock().getRelative(BlockFace.SOUTH).getType().equals(Material.AIR)) return Pattern.getSouth(); + if(center.getBlock().getRelative(BlockFace.WEST).getType().equals(Material.AIR)) return Pattern.getWest(); + if(center.getBlock().getRelative(BlockFace.EAST).getType().equals(Material.AIR)) return Pattern.getEast(); + return null; + } + + private static boolean isValid(Location center){ + Material[][][] pattern = getPattern(center); + for(int x=0;x<=2;x++){ + for(int y=0;y<=2;y++){ + for(int z=0;z<=2;z++){ + if((pattern[x][y][z]!=center.getBlock().getRelative(x-1,y-1,z-1).getType())&&((pattern[x][y][z]!=null))){ + return false; + } + } + } + } + return true; + } + + private static ArrayList getCenters(Location currentBlock){ + ArrayList centers = new ArrayList(); Location nextBlock; for(int x=-1;x<=1;x++){ for(int y=-1;y<=1;y++){ for(int z=-1;z<=1;z++){ nextBlock = currentBlock.getBlock().getRelative(x,y,z).getLocation(); - if(nextBlock.getBlock().getType().equals(Material.JACK_O_LANTERN)) pumpkins.add(nextBlock); + if(nextBlock.getBlock().getType().equals(Pattern.getCenter())) centers.add(nextBlock); } } } - return pumpkins; + return centers; } }