Use new directions util (needs testing!)

This commit is contained in:
Daniel Saukel 2019-06-12 19:35:47 +02:00
parent c3c0539736
commit af9083bf35
3 changed files with 20 additions and 266 deletions

View File

@ -16,6 +16,7 @@
*/ */
package de.erethon.dungeonsxl.sign; package de.erethon.dungeonsxl.sign;
import de.erethon.commons.misc.BlockUtil;
import de.erethon.dungeonsxl.DungeonsXL; import de.erethon.dungeonsxl.DungeonsXL;
import de.erethon.dungeonsxl.world.DGameWorld; import de.erethon.dungeonsxl.world.DGameWorld;
import org.bukkit.Location; import org.bukkit.Location;
@ -37,7 +38,7 @@ public abstract class LocationSign extends DSign {
double x = getSign().getX() + 0.5; double x = getSign().getX() + 0.5;
double y = getSign().getY(); double y = getSign().getY();
double z = getSign().getZ() + 0.5; double z = getSign().getZ() + 0.5;
float yaw = letterToYaw(((org.bukkit.material.Sign) getSign().getData()).getFacing().getOppositeFace().name().charAt(0)); float yaw = BlockUtil.blockFaceToYaw(((org.bukkit.material.Sign) getSign().getData()).getFacing().getOppositeFace());
float pitch = 0; float pitch = 0;
location = new Location(getGameWorld().getWorld(), x, y, z, yaw, pitch); location = new Location(getGameWorld().getWorld(), x, y, z, yaw, pitch);
} }
@ -49,23 +50,4 @@ public abstract class LocationSign extends DSign {
return location; return location;
} }
public static int letterToYaw(char c) {
switch (c) {
case 'S':
case 's':
return 0;
case 'W':
case 'w':
return 90;
case 'N':
case 'n':
return 180;
case 'E':
case 'e':
return -90;
default:
return -1;
}
}
} }

View File

@ -17,6 +17,7 @@
package de.erethon.dungeonsxl.sign; package de.erethon.dungeonsxl.sign;
import de.erethon.caliburn.item.VanillaItem; import de.erethon.caliburn.item.VanillaItem;
import de.erethon.commons.misc.BlockUtil;
import de.erethon.commons.misc.NumberUtil; import de.erethon.commons.misc.NumberUtil;
import de.erethon.dungeonsxl.DungeonsXL; import de.erethon.dungeonsxl.DungeonsXL;
import de.erethon.dungeonsxl.world.DGameWorld; import de.erethon.dungeonsxl.world.DGameWorld;
@ -36,7 +37,7 @@ public class TeleportSign extends LocationSign {
public boolean check() { public boolean check() {
for (int i = 1; i <= 2; i++) { for (int i = 1; i <= 2; i++) {
if (!lines[i].isEmpty()) { if (!lines[i].isEmpty()) {
if (letterToYaw(lines[i].charAt(0)) == -1) { if (BlockUtil.lettersToYaw(lines[i]) == -1) {
String[] loc = lines[i].split(","); String[] loc = lines[i].split(",");
if (loc.length != 3) { if (loc.length != 3) {
return false; return false;
@ -52,7 +53,7 @@ public class TeleportSign extends LocationSign {
super.onInit(); super.onInit();
for (int i = 1; i <= 2; i++) { for (int i = 1; i <= 2; i++) {
if (!lines[i].isEmpty()) { if (!lines[i].isEmpty()) {
int yaw = letterToYaw(lines[i].charAt(0)); int yaw = BlockUtil.lettersToYaw(lines[i]);
if (yaw != -1) { if (yaw != -1) {
location.setYaw(yaw); location.setYaw(yaw);
} else { } else {

View File

@ -17,11 +17,11 @@
package de.erethon.dungeonsxl.world.block; package de.erethon.dungeonsxl.world.block;
import de.erethon.caliburn.item.ExItem; import de.erethon.caliburn.item.ExItem;
import de.erethon.commons.misc.BlockUtil;
import de.erethon.dungeonsxl.DungeonsXL; import de.erethon.dungeonsxl.DungeonsXL;
import de.erethon.dungeonsxl.world.DGameWorld; import de.erethon.dungeonsxl.world.DGameWorld;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockBreakEvent;
@ -33,224 +33,21 @@ public class PlaceableBlock extends GameBlock {
// Variables // Variables
private Set<ExItem> materials = new HashSet<>(); private Set<ExItem> materials = new HashSet<>();
private Set<BlockFace> faces = 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 PlaceableBlock(DungeonsXL plugin, Block block, String ids, String directions) { public PlaceableBlock(DungeonsXL plugin, Block block, String ids, String directions) {
super(plugin, block); super(plugin, block);
// Split ids for (String id : ids.split(",")) {
if (!ids.isEmpty()) { ExItem item = plugin.getCaliburn().getExItem(id);
String[] splittedIds = ids.split(","); if (item != null) {
for (String id : splittedIds) { materials.add(item);
ExItem item = plugin.getCaliburn().getExItem(id);
if (item != null) {
materials.add(item);
}
} }
} }
// Read directions faces.add(BlockFace.SELF);
if (directions.length() == 6) { for (String direction : directions.split(",")) {
for (byte direction = 0; direction < 6; direction++) { faces.add(BlockUtil.lettersToBlockFace(direction));
boolean positive = String.valueOf(directions.charAt(direction)).equals("x");
if (!positive) {
continue;
}
if (direction == 0) {
onTop = true;
}
if (direction == 1) {
onBottom = true;
}
if (block.getType() == Material.WALL_SIGN) {
byte 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 {
byte 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;
} }
} }
@ -260,42 +57,16 @@ public class PlaceableBlock extends GameBlock {
return false; return false;
} }
// Can build public boolean canPlace(Block toPlace, ExItem material) {
public static boolean canBuildHere(Block block, BlockFace blockFace, ExItem mat, DGameWorld gameWorld) { return faces.contains(toPlace.getFace(block)) && (materials.isEmpty() || materials.contains(material));
for (PlaceableBlock gamePlacableBlock : gameWorld.getPlaceableBlocks()) { }
if (gamePlacableBlock.block.getFace(block) != BlockFace.SELF) {
continue;
}
if (!(gamePlacableBlock.materials.contains(mat) || gamePlacableBlock.materials.isEmpty())) { public static boolean canBuildHere(Block block, BlockFace blockFace, ExItem material, DGameWorld gameWorld) {
continue; for (PlaceableBlock gamePlaceableBlock : gameWorld.getPlaceableBlocks()) {
} if (gamePlaceableBlock.canPlace(block, material)) {
if (blockFace == BlockFace.NORTH && gamePlacableBlock.onNorth) {
return true;
}
if (blockFace == BlockFace.SOUTH && gamePlacableBlock.onSouth) {
return true;
}
if (blockFace == BlockFace.EAST && gamePlacableBlock.onEast) {
return true;
}
if (blockFace == BlockFace.WEST && gamePlacableBlock.onWest) {
return true;
}
if (blockFace == BlockFace.UP && gamePlacableBlock.onTop) {
return true;
}
if (blockFace == BlockFace.DOWN && gamePlacableBlock.onBottom) {
return true; return true;
} }
} }
return false; return false;
} }