From 72296cf08eaa6adb385ded618930fe610d3d66cd Mon Sep 17 00:00:00 2001 From: Grafe Date: Fri, 18 Jan 2013 04:20:16 +0100 Subject: [PATCH] Added new place shields --- .../dungeonsxl/game/GamePlaceableBlock.java | 133 ++++++++++++++++++ src/com/dre/dungeonsxl/game/GameWorld.java | 13 +- .../dungeonsxl/listener/BlockListener.java | 5 +- 3 files changed, 139 insertions(+), 12 deletions(-) create mode 100644 src/com/dre/dungeonsxl/game/GamePlaceableBlock.java diff --git a/src/com/dre/dungeonsxl/game/GamePlaceableBlock.java b/src/com/dre/dungeonsxl/game/GamePlaceableBlock.java new file mode 100644 index 00000000..4e1439a5 --- /dev/null +++ b/src/com/dre/dungeonsxl/game/GamePlaceableBlock.java @@ -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 mats = new HashSet(); + + 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; + } +} diff --git a/src/com/dre/dungeonsxl/game/GameWorld.java b/src/com/dre/dungeonsxl/game/GameWorld.java index c748dea1..4c37740a 100644 --- a/src/com/dre/dungeonsxl/game/GameWorld.java +++ b/src/com/dre/dungeonsxl/game/GameWorld.java @@ -36,7 +36,7 @@ public class GameWorld { //Variables placeable public boolean isTutorial; - public CopyOnWriteArrayList placeableBlocks=new CopyOnWriteArrayList(); + public CopyOnWriteArrayList placeableBlocks=new CopyOnWriteArrayList(); 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); diff --git a/src/com/dre/dungeonsxl/listener/BlockListener.java b/src/com/dre/dungeonsxl/listener/BlockListener.java index d0f13d5e..f358bd9e 100644 --- a/src/com/dre/dungeonsxl/listener/BlockListener.java +++ b/src/com/dre/dungeonsxl/listener/BlockListener.java @@ -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); } }