Rewrite Material switch statements to allow dynamic calls

This commit is contained in:
Daniel Saukel
2018-03-10 17:04:23 +01:00
parent 951fb18dc9
commit 00341a814d
4 changed files with 93 additions and 134 deletions
+1 -1
View File
@@ -4,7 +4,7 @@
<groupId>com.dre</groupId> <groupId>com.dre</groupId>
<artifactId>brewery</artifactId> <artifactId>brewery</artifactId>
<version>1.5</version> <version>1.6-SNAPSHOT</version>
<name>Brewery</name> <name>Brewery</name>
<properties> <properties>
+25 -84
View File
@@ -276,7 +276,7 @@ public class Barrel implements InventoryHolder {
if (hasWoodBlock(block)) { if (hasWoodBlock(block)) {
return true; return true;
} }
} else if (isStairs(block.getType())) { } else if (LegacyUtil.isWoodStairs(block.getType())) {
if (hasStairsBlock(block)) { if (hasStairsBlock(block)) {
return true; return true;
} }
@@ -337,37 +337,14 @@ public class Barrel implements InventoryHolder {
// Get the Barrel by Block, null if that block is not part of a barrel // Get the Barrel by Block, null if that block is not part of a barrel
public static Barrel get(Block block) { public static Barrel get(Block block) {
if (block != null) { if (block == null) {
switch (block.getType()) {
case FENCE:
case NETHER_FENCE:
case SIGN_POST:
case WALL_SIGN:
case ACACIA_FENCE:
case BIRCH_FENCE:
case DARK_OAK_FENCE:
case IRON_FENCE:
case JUNGLE_FENCE:
case SPRUCE_FENCE:
Barrel barrel = getBySpigot(block);
if (barrel != null) {
return barrel;
}
return null; return null;
case WOOD:
case WOOD_STAIRS:
case BIRCH_WOOD_STAIRS:
case JUNGLE_WOOD_STAIRS:
case SPRUCE_WOOD_STAIRS:
case ACACIA_STAIRS:
case DARK_OAK_STAIRS:
Barrel barrel2 = getByWood(block);
if (barrel2 != null) {
return barrel2;
}
default:
break;
} }
Material type = block.getType();
if (LegacyUtil.isFence(type) || LegacyUtil.isSign(type) ) {
return getBySpigot(block);
} else if (LegacyUtil.isWoodPlanks(type) || LegacyUtil.isWoodStairs(type)) {
return getByWood(block);
} }
return null; return null;
} }
@@ -404,7 +381,7 @@ public class Barrel implements InventoryHolder {
return barrel; return barrel;
} }
} }
} else if (isStairs(wood.getType())) { } else if (LegacyUtil.isWoodStairs(wood.getType())) {
for (Barrel barrel : Barrel.barrels) { for (Barrel barrel : Barrel.barrels) {
if (barrel.hasStairsBlock(wood)) { if (barrel.hasStairsBlock(wood)) {
return barrel; return barrel;
@@ -582,11 +559,11 @@ public class Barrel implements InventoryHolder {
public static int getDirection(Block spigot) { public static int getDirection(Block spigot) {
int direction = 0;// 1=x+ 2=x- 3=z+ 4=z- int direction = 0;// 1=x+ 2=x- 3=z+ 4=z-
Material type = spigot.getRelative(0, 0, 1).getType(); Material type = spigot.getRelative(0, 0, 1).getType();
if (LegacyUtil.isWoodPlanks(type) || isStairs(type)) { if (LegacyUtil.isWoodPlanks(type) || LegacyUtil.isWoodStairs(type)) {
direction = 3; direction = 3;
} }
type = spigot.getRelative(0, 0, -1).getType(); type = spigot.getRelative(0, 0, -1).getType();
if (LegacyUtil.isWoodPlanks(type) || isStairs(type)) { if (LegacyUtil.isWoodPlanks(type) || LegacyUtil.isWoodStairs(type)) {
if (direction == 0) { if (direction == 0) {
direction = 4; direction = 4;
} else { } else {
@@ -594,7 +571,7 @@ public class Barrel implements InventoryHolder {
} }
} }
type = spigot.getRelative(1, 0, 0).getType(); type = spigot.getRelative(1, 0, 0).getType();
if (LegacyUtil.isWoodPlanks(type) || isStairs(type)) { if (LegacyUtil.isWoodPlanks(type) || LegacyUtil.isWoodStairs(type)) {
if (direction == 0) { if (direction == 0) {
direction = 1; direction = 1;
} else { } else {
@@ -602,7 +579,7 @@ public class Barrel implements InventoryHolder {
} }
} }
type = spigot.getRelative(-1, 0, 0).getType(); type = spigot.getRelative(-1, 0, 0).getType();
if (LegacyUtil.isWoodPlanks(type) || isStairs(type)) { if (LegacyUtil.isWoodPlanks(type) || LegacyUtil.isWoodStairs(type)) {
if (direction == 0) { if (direction == 0) {
direction = 2; direction = 2;
} else { } else {
@@ -635,9 +612,9 @@ public class Barrel implements InventoryHolder {
default: default:
wood = spigot.getRelative(0, 0, -1); wood = spigot.getRelative(0, 0, -1);
} }
try {
switch (wood.getType()) { Material type = wood.getType();
case WOOD: if (LegacyUtil.isWoodPlanks(type)) {
MaterialData data = wood.getState().getData(); MaterialData data = wood.getState().getData();
TreeSpecies woodType; TreeSpecies woodType;
if (data instanceof Tree) { if (data instanceof Tree) {
@@ -664,25 +641,19 @@ public class Barrel implements InventoryHolder {
default: default:
return 0; return 0;
} }
} else if (type == LegacyUtil.OAK_STAIRS) {
case WOOD_STAIRS:
return 2; return 2;
case SPRUCE_WOOD_STAIRS: } else if (type == LegacyUtil.SPRUCE_STAIRS) {
return 4; return 4;
case BIRCH_WOOD_STAIRS: } else if (type == LegacyUtil.BIRCH_STAIRS) {
return 1; return 1;
case JUNGLE_WOOD_STAIRS: } else if (type == LegacyUtil.JUNGLE_STAIRS) {
return 3; return 3;
case ACACIA_STAIRS: } else if (type == LegacyUtil.ACACIA_STAIRS) {
return 5; return 5;
case DARK_OAK_STAIRS: } else if (type == LegacyUtil.DARK_OAK_STAIRS) {
return 6; return 6;
default: } else {
return 0;
}
} catch (NoSuchFieldError | NoClassDefFoundError e) {
// Using older minecraft versions some fields and classes do not exist
return 0; return 0;
} }
} }
@@ -710,7 +681,7 @@ public class Barrel implements InventoryHolder {
while (y <= 1) { while (y <= 1) {
// Fence and Netherfence // Fence and Netherfence
Block relative = block.getRelative(0, y, 0); Block relative = block.getRelative(0, y, 0);
if (isFence(relative.getType())) { if (LegacyUtil.isFence(relative.getType())) {
return (relative); return (relative);
} }
y++; y++;
@@ -718,36 +689,6 @@ public class Barrel implements InventoryHolder {
return block; return block;
} }
public static boolean isStairs(Material material) {
switch (material) {
case WOOD_STAIRS:
case SPRUCE_WOOD_STAIRS:
case BIRCH_WOOD_STAIRS:
case JUNGLE_WOOD_STAIRS:
case ACACIA_STAIRS:
case DARK_OAK_STAIRS:
return true;
default:
return false;
}
}
public static boolean isFence(Material material) {
switch (material) {
case FENCE:
case NETHER_FENCE:
case ACACIA_FENCE:
case BIRCH_FENCE:
case DARK_OAK_FENCE:
case IRON_FENCE:
case JUNGLE_FENCE:
case SPRUCE_FENCE:
return true;
default:
return false;
}
}
// returns null if Barrel is correctly placed; the block that is missing when not // returns null if Barrel is correctly placed; the block that is missing when not
// the barrel needs to be formed correctly // the barrel needs to be formed correctly
// flag force to also check if chunk is not loaded // flag force to also check if chunk is not loaded
@@ -807,7 +748,7 @@ public class Barrel implements InventoryHolder {
Block block = spigot.getRelative(x, y, z); Block block = spigot.getRelative(x, y, z);
type = block.getType(); type = block.getType();
if (isStairs(type)) { if (LegacyUtil.isWoodStairs(type)) {
if (y == 0) { if (y == 0) {
// stairs have to be upside down // stairs have to be upside down
MaterialData data = block.getState().getData(); MaterialData data = block.getState().getData();
@@ -901,7 +842,7 @@ public class Barrel implements InventoryHolder {
continue; continue;
} }
} }
if (LegacyUtil.isWoodPlanks(type) || isStairs(type)) { if (LegacyUtil.isWoodPlanks(type) || LegacyUtil.isWoodStairs(type)) {
if (LegacyUtil.isWoodPlanks(type)) { if (LegacyUtil.isWoodPlanks(type)) {
woods.add(block.getX()); woods.add(block.getX());
woods.add(block.getY()); woods.add(block.getY());
+35 -4
View File
@@ -4,16 +4,47 @@ import org.bukkit.Material;
public class LegacyUtil { public class LegacyUtil {
public static final Material FLOWING_LAVA = Material.valueOf(P.use1_13 ? "FLOWING_LAVA" : "LAVA"); public static final Material FLOWING_LAVA = get("FLOWING_LAVA", "LAVA");
public static final Material LAVA = Material.valueOf(P.use1_13 ? "LAVA" : "STATIONARY_LAVA"); public static final Material LAVA = get("LAVA", "STATIONARY_LAVA");
public static final Material CLOCK = Material.valueOf(P.use1_13 ? "CLOCK" : "WATCH"); public static final Material CLOCK = get("CLOCK", "WATCH");
public static final Material OAK_STAIRS = get("OAK_STAIRS", "WOOD_STAIRS");
public static final Material SPRUCE_STAIRS = get("SPRUCE_STAIRS", "SPRUCE_WOOD_STAIRS");
public static final Material BIRCH_STAIRS = get("BIRCH_STAIRS", "BIRCH_WOOD_STAIRS");
public static final Material JUNGLE_STAIRS = get("JUNGLE_STAIRS", "JUNGLE_WOOD_STAIRS");
public static final Material ACACIA_STAIRS = get("ACACIA_STAIRS");
public static final Material DARK_OAK_STAIRS = get("DARK_OAK_STAIRS");
private static Material get(String name) {
try {
return Material.valueOf(name);
} catch (IllegalArgumentException exception) {
return null;
}
}
private static Material get(String oldName, String newName) {
try {
return Material.valueOf(P.use1_13 ? newName : oldName);
} catch (IllegalArgumentException exception) {
return null;
}
}
public static boolean isWoodPlanks(Material type) { public static boolean isWoodPlanks(Material type) {
return type.name().contains("PLANKS") || type.name().equals("WOOD"); return type.name().contains("PLANKS") || type.name().equals("WOOD");
} }
public static boolean isWoodStairs(Material type) {
return type == OAK_STAIRS || type == SPRUCE_STAIRS || type == BIRCH_STAIRS || type == JUNGLE_STAIRS
|| (type == ACACIA_STAIRS && ACACIA_STAIRS != null) || (type == DARK_OAK_STAIRS && DARK_OAK_STAIRS != null);
}
public static boolean isFence(Material material) {
return material.name().endsWith("FENCE");
}
public static boolean isSign(Material type) { public static boolean isSign(Material type) {
return type.name().equals("LEGACY_SIGN_POST") || type == Material.SIGN || type == Material.WALL_SIGN; return type.name().equals("SIGN_POST") || type == Material.SIGN || type == Material.WALL_SIGN;
} }
} }
+8 -21
View File
@@ -697,19 +697,13 @@ public class P extends JavaPlugin {
// Returns true if the Block can be destroyed by the Player or something else (null) // Returns true if the Block can be destroyed by the Player or something else (null)
public boolean blockDestroy(Block block, Player player) { public boolean blockDestroy(Block block, Player player) {
switch (block.getType()) { Material type = block.getType();
case CAULDRON: if (type == Material.CAULDRON) {
// will only remove when existing // will only remove when existing
BCauldron.remove(block); BCauldron.remove(block);
return true; return true;
case FENCE:
case NETHER_FENCE: } else if (LegacyUtil.isFence(type)) {
case ACACIA_FENCE:
case BIRCH_FENCE:
case DARK_OAK_FENCE:
case IRON_FENCE:
case JUNGLE_FENCE:
case SPRUCE_FENCE:
// remove barrel and throw potions on the ground // remove barrel and throw potions on the ground
Barrel barrel = Barrel.getBySpigot(block); Barrel barrel = Barrel.getBySpigot(block);
if (barrel != null) { if (barrel != null) {
@@ -721,8 +715,8 @@ public class P extends JavaPlugin {
} }
} }
return true; return true;
case SIGN_POST:
case WALL_SIGN: } else if (LegacyUtil.isSign(type)) {
// remove small Barrels // remove small Barrels
Barrel barrel2 = Barrel.getBySpigot(block); Barrel barrel2 = Barrel.getBySpigot(block);
if (barrel2 != null) { if (barrel2 != null) {
@@ -738,13 +732,8 @@ public class P extends JavaPlugin {
} }
} }
return true; return true;
case WOOD:
case WOOD_STAIRS: } else if (LegacyUtil.isWoodPlanks(type) || LegacyUtil.isWoodStairs(type)){
case BIRCH_WOOD_STAIRS:
case JUNGLE_WOOD_STAIRS:
case SPRUCE_WOOD_STAIRS:
case ACACIA_STAIRS:
case DARK_OAK_STAIRS:
Barrel barrel3 = Barrel.getByWood(block); Barrel barrel3 = Barrel.getByWood(block);
if (barrel3 != null) { if (barrel3 != null) {
if (barrel3.hasPermsDestroy(player)) { if (barrel3.hasPermsDestroy(player)) {
@@ -753,8 +742,6 @@ public class P extends JavaPlugin {
return false; return false;
} }
} }
default:
break;
} }
return true; return true;
} }