Replicator class refactored and full implementation of getReplicators from given block

This commit is contained in:
Mochaccino 2013-03-18 22:15:23 +01:00
parent c260287ed3
commit ba3f8bfe98
2 changed files with 42 additions and 30 deletions

View File

@ -32,15 +32,15 @@ public class Pattern {
{{Material.OBSIDIAN,Material.OBSIDIAN,null},{Material.MOSSY_COBBLESTONE,Material.OBSIDIAN,null},{Material.GLOWSTONE,Material.OBSIDIAN,null}}}; {{Material.OBSIDIAN,Material.OBSIDIAN,null},{Material.MOSSY_COBBLESTONE,Material.OBSIDIAN,null},{Material.GLOWSTONE,Material.OBSIDIAN,null}}};
//direction north (replicator direction) //direction north (replicator direction)
public Material getCenter(){ public static Material getCenter(){
return repPattern[1][1][1]; return repPattern[1][1][1];
} }
public Material[][][] getNorth(){ public static Material[][][] getNorth(){
return repPattern; return repPattern;
} }
public Material[][][] getSouth(){ public static Material[][][] getSouth(){
Material[][][] newPattern = new Material[3][3][3]; Material[][][] newPattern = new Material[3][3][3];
for(int x=0;x<=2;x++){ for(int x=0;x<=2;x++){
for(int y=0;y<=2;y++){ for(int y=0;y<=2;y++){
@ -52,7 +52,7 @@ public class Pattern {
return newPattern; return newPattern;
} }
public Material[][][] getWest(){ public static Material[][][] getWest(){
Material[][][] newPattern = new Material[3][3][3]; Material[][][] newPattern = new Material[3][3][3];
for(int x=0;x<=2;x++){ for(int x=0;x<=2;x++){
for(int y=0;y<=2;y++){ for(int y=0;y<=2;y++){
@ -64,7 +64,7 @@ public class Pattern {
return newPattern; return newPattern;
} }
public Material[][][] getEast(){ public static Material[][][] getEast(){
Material[][][] westPattern = getWest(); Material[][][] westPattern = getWest();
Material[][][] newPattern = new Material[3][3][3]; Material[][][] newPattern = new Material[3][3][3];
for(int y=0;y<=2;y++){ for(int y=0;y<=2;y++){

View File

@ -20,7 +20,6 @@ package de.craftinc.replicator;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.Player;
import java.util.ArrayList; import java.util.ArrayList;
@ -36,15 +35,13 @@ public class Replicator {
private ArrayList<String> owners; private ArrayList<String> owners;
private ArrayList<String> users; private ArrayList<String> users;
private Location spawn; private Location center;
private Location pumpkin;
public Replicator(String firstOwner, Location spawn, Location pumpkin) { public Replicator(String firstOwner, Location spawn, Location center) {
this.owners = new ArrayList<String>(); this.owners = new ArrayList<String>();
this.users = new ArrayList<String>(); this.users = new ArrayList<String>();
this.owners.add(firstOwner); this.owners.add(firstOwner);
this.spawn = spawn; this.center = center;
this.pumpkin = pumpkin;
} }
public void addUser(String user) { public void addUser(String user) {
@ -65,36 +62,51 @@ public class Replicator {
else return false; else return false;
} }
private BlockFace getDirection(Location pumpkin) { public static ArrayList<Location> getReplicators(Location currentBlock){
ArrayList<Location> replicators = new ArrayList<Location>();
if(this.pumpkin.getBlock().get) ArrayList<Location> centers = getCenters(currentBlock);
for(Location center:centers){
if(this.spawn.getBlock().getRelative(BlockFace.EAST).equals(this.pumpkin.getBlock())) return BlockFace.EAST; if(isValid(center)){
else if(this.spawn.getBlock().getRelative(BlockFace.SOUTH).equals(this.pumpkin.getBlock())) return BlockFace.SOUTH; replicators.add(center);
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; return replicators;
} }
private boolean isValid(Location pumpkin){ private static Material[][][] getPattern(Location center){
if(pumpkin.getBlock().getRelative(BlockFace.DOWN).equals(Material.DIAMOND_BLOCK)){ 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 false;
} }
}
}
}
return true;
}
private ArrayList<Location> getPumpkin(Location currentBlock){ private static ArrayList<Location> getCenters(Location currentBlock){
ArrayList<Location> pumpkins = new ArrayList<Location>(); ArrayList<Location> centers = new ArrayList<Location>();
Location nextBlock; Location nextBlock;
for(int x=-1;x<=1;x++){ for(int x=-1;x<=1;x++){
for(int y=-1;y<=1;y++){ for(int y=-1;y<=1;y++){
for(int z=-1;z<=1;z++){ for(int z=-1;z<=1;z++){
nextBlock = currentBlock.getBlock().getRelative(x,y,z).getLocation(); 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;
} }
} }