Added new place shields

This commit is contained in:
Grafe 2013-01-18 04:20:16 +01:00
parent 1e3482fe3c
commit 72296cf08e
3 changed files with 139 additions and 12 deletions

View File

@ -0,0 +1,133 @@
package com.dre.dungeonsxl.game;
import java.util.HashSet;
import java.util.Set;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import com.dre.dungeonsxl.P;
public class GamePlaceableBlock {
//Variables
private Block block;
private Set<Material> mats = new HashSet<Material>();
private boolean onTop = false;
private boolean onBottom = false;
private boolean onNorth = false;
private boolean onSouth = false;
private boolean onEast = false;
private boolean onWest = false;
public GamePlaceableBlock(Block block, String ids, String directions){
this.block = block;
//Split ids
if(!ids.equals("")){
String[] splittedIds = ids.split(",");
for(String id:splittedIds){
Material mat = Material.getMaterial(P.p.parseInt(id));
if(mat!=null){
mats.add(mat);
}
}
}
//Read directions
if(directions.length()==6){
for(int direction = 0; direction < 6; direction++){
boolean positive = String.valueOf(directions.charAt(direction)).equals("x");
if(positive){
if(direction==0) onTop = true;
if(direction==1) onBottom = true;
if(block.getType()==Material.WALL_SIGN){
int data = block.getData();
switch (data){
case 3:
if(direction==2) onNorth = true;
if(direction==3) onEast = true;
if(direction==4) onSouth = true;
if(direction==5) onWest = true;
break;
case 4:
if(direction==5) onNorth = true;
if(direction==2) onEast = true;
if(direction==3) onSouth = true;
if(direction==4) onWest = true;
break;
case 2:
if(direction==4) onNorth = true;
if(direction==5) onEast = true;
if(direction==2) onSouth = true;
if(direction==3) onWest = true;
break;
case 5:
if(direction==3) onNorth = true;
if(direction==4) onEast = true;
if(direction==5) onSouth = true;
if(direction==2) onWest = true;
break;
}
} else {
int data = block.getData();
switch (data){
case 0: case 1: case 2: case 15:
if(direction==2) onNorth = true;
if(direction==3) onEast = true;
if(direction==4) onSouth = true;
if(direction==5) onWest = true;
break;
case 4: case 3: case 5: case 6:
if(direction==5) onNorth = true;
if(direction==2) onEast = true;
if(direction==3) onSouth = true;
if(direction==4) onWest = true;
break;
case 8: case 7: case 9: case 10:
if(direction==4) onNorth = true;
if(direction==5) onEast = true;
if(direction==2) onSouth = true;
if(direction==3) onWest = true;
break;
case 12: case 11: case 13: case 14:
if(direction==3) onNorth = true;
if(direction==4) onEast = true;
if(direction==5) onSouth = true;
if(direction==2) onWest = true;
break;
}
}
}
}
} else {
onTop = true;
onBottom = true;
onNorth = true;
onEast = true;
onSouth = true;
onWest = true;
}
}
//Canbuild
public static boolean canBuildHere(Block block,BlockFace blockFace, Material mat, GameWorld gworld){
for(GamePlaceableBlock gPBlock:gworld.placeableBlocks){
if(gPBlock.block.getFace(block) == BlockFace.SELF){
if(gPBlock.mats.contains(mat) || gPBlock.mats.isEmpty()){
if(blockFace == BlockFace.NORTH && gPBlock.onNorth) return true;
if(blockFace == BlockFace.SOUTH && gPBlock.onSouth) return true;
if(blockFace == BlockFace.EAST && gPBlock.onEast) return true;
if(blockFace == BlockFace.WEST && gPBlock.onWest) return true;
if(blockFace == BlockFace.UP && gPBlock.onTop) return true;
if(blockFace == BlockFace.DOWN && gPBlock.onBottom) return true;
}
}
}
return false;
}
}

View File

@ -36,7 +36,7 @@ public class GameWorld {
//Variables placeable
public boolean isTutorial;
public CopyOnWriteArrayList<Block> placeableBlocks=new CopyOnWriteArrayList<Block>();
public CopyOnWriteArrayList<GamePlaceableBlock> placeableBlocks=new CopyOnWriteArrayList<GamePlaceableBlock>();
public World world;
public String dungeonname;
public Location locLobby;
@ -183,7 +183,7 @@ public class GameWorld {
block.setTypeId(0);
}
if(lines[1].equalsIgnoreCase("place")){
placeableBlocks.add(block);
placeableBlocks.add(new GamePlaceableBlock(block, lines[2], lines[3]) );
block.setTypeId(0);
}
if(lines[1].equalsIgnoreCase("msg")){
@ -259,15 +259,6 @@ public class GameWorld {
}
}
public boolean canBuild(Block block){
for(Block placeableBlock:placeableBlocks){
if(placeableBlock.getLocation().distance(block.getLocation())<1){
return true;
}
}
return false;
}
public void msg(String msg) {
for(DPlayer dplayer:DPlayer.get(this.world)){
p.msg(dplayer.player, msg);

View File

@ -17,6 +17,7 @@ import com.dre.dungeonsxl.DPortal;
import com.dre.dungeonsxl.P;
import com.dre.dungeonsxl.EditWorld;
import com.dre.dungeonsxl.LeaveSign;
import com.dre.dungeonsxl.game.GamePlaceableBlock;
import com.dre.dungeonsxl.game.GameWorld;
public class BlockListener implements Listener {
@ -90,10 +91,12 @@ public class BlockListener implements Listener {
public void onBlockPlace(BlockPlaceEvent event){
Block block=event.getBlock();
//Deny GameWorld Blocks
GameWorld gworld=GameWorld.get(block.getWorld());
if(gworld!=null){
if(!gworld.canBuild(block)){
if(!GamePlaceableBlock.canBuildHere(block, block.getFace(event.getBlockAgainst()), event.getItemInHand().getType(), gworld)){
event.setCancelled(true);
}
}