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

View File

@ -4,7 +4,7 @@
<groupId>com.dre</groupId>
<artifactId>brewery</artifactId>
<version>1.5</version>
<version>1.6-SNAPSHOT</version>
<name>Brewery</name>
<properties>

View File

@ -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());

View File

@ -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;
}
}

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)
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;
}