From 00341a814d12bdb7c29df33332e5eb38494916c5 Mon Sep 17 00:00:00 2001 From: Daniel Saukel Date: Sat, 10 Mar 2018 17:04:23 +0100 Subject: [PATCH] Rewrite Material switch statements to allow dynamic calls --- pom.xml | 2 +- src/com/dre/brewery/Barrel.java | 155 +++++++++------------------- src/com/dre/brewery/LegacyUtil.java | 39 ++++++- src/com/dre/brewery/P.java | 31 ++---- 4 files changed, 93 insertions(+), 134 deletions(-) diff --git a/pom.xml b/pom.xml index d93b253..d83fc69 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.dre brewery - 1.5 + 1.6-SNAPSHOT Brewery diff --git a/src/com/dre/brewery/Barrel.java b/src/com/dre/brewery/Barrel.java index 5407867..e32aa6e 100644 --- a/src/com/dre/brewery/Barrel.java +++ b/src/com/dre/brewery/Barrel.java @@ -276,7 +276,7 @@ public class Barrel implements InventoryHolder { if (hasWoodBlock(block)) { return true; } - } else if (isStairs(block.getType())) { + } else if (LegacyUtil.isWoodStairs(block.getType())) { if (hasStairsBlock(block)) { 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 public static Barrel get(Block block) { - 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; - 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; - } + if (block == null) { + return null; + } + 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; } @@ -404,7 +381,7 @@ public class Barrel implements InventoryHolder { return barrel; } } - } else if (isStairs(wood.getType())) { + } else if (LegacyUtil.isWoodStairs(wood.getType())) { for (Barrel barrel : Barrel.barrels) { if (barrel.hasStairsBlock(wood)) { return barrel; @@ -582,11 +559,11 @@ public class Barrel implements InventoryHolder { public static int getDirection(Block spigot) { int direction = 0;// 1=x+ 2=x- 3=z+ 4=z- Material type = spigot.getRelative(0, 0, 1).getType(); - if (LegacyUtil.isWoodPlanks(type) || isStairs(type)) { + if (LegacyUtil.isWoodPlanks(type) || LegacyUtil.isWoodStairs(type)) { direction = 3; } type = spigot.getRelative(0, 0, -1).getType(); - if (LegacyUtil.isWoodPlanks(type) || isStairs(type)) { + if (LegacyUtil.isWoodPlanks(type) || LegacyUtil.isWoodStairs(type)) { if (direction == 0) { direction = 4; } else { @@ -594,7 +571,7 @@ public class Barrel implements InventoryHolder { } } type = spigot.getRelative(1, 0, 0).getType(); - if (LegacyUtil.isWoodPlanks(type) || isStairs(type)) { + if (LegacyUtil.isWoodPlanks(type) || LegacyUtil.isWoodStairs(type)) { if (direction == 0) { direction = 1; } else { @@ -602,7 +579,7 @@ public class Barrel implements InventoryHolder { } } type = spigot.getRelative(-1, 0, 0).getType(); - if (LegacyUtil.isWoodPlanks(type) || isStairs(type)) { + if (LegacyUtil.isWoodPlanks(type) || LegacyUtil.isWoodStairs(type)) { if (direction == 0) { direction = 2; } else { @@ -635,54 +612,48 @@ public class Barrel implements InventoryHolder { default: wood = spigot.getRelative(0, 0, -1); } - try { - switch (wood.getType()) { - case WOOD: - MaterialData data = wood.getState().getData(); - TreeSpecies woodType; - if (data instanceof Tree) { - woodType = ((Tree) data).getSpecies(); - } else if (data instanceof Wood) { - woodType = ((Wood) data).getSpecies(); - } else { - return 0; - } - switch (woodType) { - case GENERIC: - return 2; - case REDWOOD: - return 4; - case BIRCH: - return 1; - case JUNGLE: - return 3; - case ACACIA: - return 5; - case DARK_OAK: - return 6; - default: - return 0; - } + Material type = wood.getType(); + if (LegacyUtil.isWoodPlanks(type)) { + MaterialData data = wood.getState().getData(); + TreeSpecies woodType; + if (data instanceof Tree) { + woodType = ((Tree) data).getSpecies(); + } else if (data instanceof Wood) { + woodType = ((Wood) data).getSpecies(); + } else { + return 0; + } - case WOOD_STAIRS: + switch (woodType) { + case GENERIC: return 2; - case SPRUCE_WOOD_STAIRS: + case REDWOOD: return 4; - case BIRCH_WOOD_STAIRS: + case BIRCH: return 1; - case JUNGLE_WOOD_STAIRS: + case JUNGLE: return 3; - case ACACIA_STAIRS: + case ACACIA: return 5; - case DARK_OAK_STAIRS: + case DARK_OAK: return 6; default: return 0; } - - } catch (NoSuchFieldError | NoClassDefFoundError e) { - // Using older minecraft versions some fields and classes do not exist + } else if (type == LegacyUtil.OAK_STAIRS) { + return 2; + } else if (type == LegacyUtil.SPRUCE_STAIRS) { + return 4; + } else if (type == LegacyUtil.BIRCH_STAIRS) { + return 1; + } else if (type == LegacyUtil.JUNGLE_STAIRS) { + return 3; + } else if (type == LegacyUtil.ACACIA_STAIRS) { + return 5; + } else if (type == LegacyUtil.DARK_OAK_STAIRS) { + return 6; + } else { return 0; } } @@ -710,7 +681,7 @@ public class Barrel implements InventoryHolder { while (y <= 1) { // Fence and Netherfence Block relative = block.getRelative(0, y, 0); - if (isFence(relative.getType())) { + if (LegacyUtil.isFence(relative.getType())) { return (relative); } y++; @@ -718,36 +689,6 @@ public class Barrel implements InventoryHolder { 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 // the barrel needs to be formed correctly // 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); type = block.getType(); - if (isStairs(type)) { + if (LegacyUtil.isWoodStairs(type)) { if (y == 0) { // stairs have to be upside down MaterialData data = block.getState().getData(); @@ -901,7 +842,7 @@ public class Barrel implements InventoryHolder { continue; } } - if (LegacyUtil.isWoodPlanks(type) || isStairs(type)) { + if (LegacyUtil.isWoodPlanks(type) || LegacyUtil.isWoodStairs(type)) { if (LegacyUtil.isWoodPlanks(type)) { woods.add(block.getX()); woods.add(block.getY()); diff --git a/src/com/dre/brewery/LegacyUtil.java b/src/com/dre/brewery/LegacyUtil.java index b82b5c4..f914b86 100644 --- a/src/com/dre/brewery/LegacyUtil.java +++ b/src/com/dre/brewery/LegacyUtil.java @@ -4,16 +4,47 @@ import org.bukkit.Material; public class LegacyUtil { - public static final Material FLOWING_LAVA = Material.valueOf(P.use1_13 ? "FLOWING_LAVA" : "LAVA"); - public static final Material LAVA = Material.valueOf(P.use1_13 ? "LAVA" : "STATIONARY_LAVA"); - public static final Material CLOCK = Material.valueOf(P.use1_13 ? "CLOCK" : "WATCH"); + public static final Material FLOWING_LAVA = get("FLOWING_LAVA", "LAVA"); + public static final Material LAVA = get("LAVA", "STATIONARY_LAVA"); + 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) { 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) { - 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; } } diff --git a/src/com/dre/brewery/P.java b/src/com/dre/brewery/P.java index d8cf2ab..ea3d8a0 100644 --- a/src/com/dre/brewery/P.java +++ b/src/com/dre/brewery/P.java @@ -697,19 +697,13 @@ public class P extends JavaPlugin { // Returns true if the Block can be destroyed by the Player or something else (null) public boolean blockDestroy(Block block, Player player) { - switch (block.getType()) { - case CAULDRON: + Material type = block.getType(); + if (type == Material.CAULDRON) { // will only remove when existing BCauldron.remove(block); return true; - case FENCE: - case NETHER_FENCE: - case ACACIA_FENCE: - case BIRCH_FENCE: - case DARK_OAK_FENCE: - case IRON_FENCE: - case JUNGLE_FENCE: - case SPRUCE_FENCE: + + } else if (LegacyUtil.isFence(type)) { // remove barrel and throw potions on the ground Barrel barrel = Barrel.getBySpigot(block); if (barrel != null) { @@ -721,8 +715,8 @@ public class P extends JavaPlugin { } } return true; - case SIGN_POST: - case WALL_SIGN: + + } else if (LegacyUtil.isSign(type)) { // remove small Barrels Barrel barrel2 = Barrel.getBySpigot(block); if (barrel2 != null) { @@ -738,13 +732,8 @@ public class P extends JavaPlugin { } } return true; - case WOOD: - case WOOD_STAIRS: - case BIRCH_WOOD_STAIRS: - case JUNGLE_WOOD_STAIRS: - case SPRUCE_WOOD_STAIRS: - case ACACIA_STAIRS: - case DARK_OAK_STAIRS: + + } else if (LegacyUtil.isWoodPlanks(type) || LegacyUtil.isWoodStairs(type)){ Barrel barrel3 = Barrel.getByWood(block); if (barrel3 != null) { if (barrel3.hasPermsDestroy(player)) { @@ -753,9 +742,7 @@ public class P extends JavaPlugin { return false; } } - default: - break; - } + } return true; }