fix compatibility with renamed legacy crops

This commit is contained in:
jascotty2 2019-10-20 16:10:07 -05:00
parent 02460fcc80
commit 29076ad759
3 changed files with 83 additions and 15 deletions

View File

@ -1171,7 +1171,7 @@ public enum CompatibleMaterial {
} }
/** /**
* Lookup a Legacy Material by its modern id name. <br /> * Lookup a Material by its modern id name. <br />
* This also can grab materials by their legacy, but only if there is no * This also can grab materials by their legacy, but only if there is no
* modern material by that name. * modern material by that name.
* *
@ -1183,7 +1183,7 @@ public enum CompatibleMaterial {
} }
/** /**
* Lookup a Legacy Material by its modern id name. <br /> * Lookup a Material by its modern id name. <br />
* This also can grab materials by their legacy, but only if there is no * This also can grab materials by their legacy, but only if there is no
* modern material by that name. * modern material by that name.
* *
@ -1196,7 +1196,7 @@ public enum CompatibleMaterial {
} }
/** /**
* Lookup a Legacy Material by bukkit material. * Lookup a Material by bukkit material.
* *
* @param mat item to lookup * @param mat item to lookup
* @return LegacyMaterial or null if none found * @return LegacyMaterial or null if none found
@ -1206,7 +1206,7 @@ public enum CompatibleMaterial {
} }
/** /**
* Lookup a Legacy Material by Itemstack. * Lookup a Material by Itemstack.
* *
* @param item item to lookup * @param item item to lookup
* @return LegacyMaterial or null if none found * @return LegacyMaterial or null if none found
@ -1218,7 +1218,66 @@ public enum CompatibleMaterial {
String key = item.getType() + ":"; String key = item.getType() + ":";
CompatibleMaterial m = lookupMap.get(key); CompatibleMaterial m = lookupMap.get(key);
return m != null ? m : lookupMap.get(key + item.getDurability()); return m != null ? m : lookupMap.get(key + item.getDurability());
} }
/**
* Lookup a Block Material by its modern id name. <br />
* This also can grab materials by their legacy, but only if there is no
* modern material by that name.
*
* @param name item to lookup
* @return LegacyMaterial or null if none found
*/
public static CompatibleMaterial getBlockMaterial(String name) {
if (name == null) {
return null;
} else if (useLegacy) {
LegacyMaterialBlockType legacyBlock = LegacyMaterialBlockType.getFromLegacy(name.toUpperCase());
if (legacyBlock != null) {
return lookupMap.get(legacyBlock.name());
}
}
return lookupMap.get(name.toUpperCase());
}
/**
* Lookup a Block Material by its modern id name. <br />
* This also can grab materials by their legacy, but only if there is no
* modern material by that name.
*
* @param name item to lookup
* @param def default item if this is not a valid material
* @return LegacyMaterial or null if none found
*/
public static CompatibleMaterial getBlockMaterial(String name, CompatibleMaterial def) {
if (name == null) {
return def;
} else if (useLegacy) {
LegacyMaterialBlockType legacyBlock = LegacyMaterialBlockType.getFromLegacy(name.toUpperCase());
if (legacyBlock != null) {
return lookupMap.get(legacyBlock.name());
}
}
return lookupMap.getOrDefault(name.toUpperCase(), def);
}
/**
* Lookup a Block Material by bukkit material.
*
* @param mat item to lookup
* @return LegacyMaterial or null if none found
*/
public static CompatibleMaterial getBlockMaterial(Material mat) {
if (mat == null) {
return null;
} else if (useLegacy) {
LegacyMaterialBlockType legacyBlock = LegacyMaterialBlockType.getFromLegacy(mat.name());
if (legacyBlock != null) {
return lookupMap.get(legacyBlock.name());
}
}
return lookupMap.get(mat.name());
}
static LinkedHashSet<CompatibleMaterial> all = null; static LinkedHashSet<CompatibleMaterial> all = null;

View File

@ -18,6 +18,7 @@ public enum LegacyMaterialBlockType {
BIRCH_DOOR("BIRCH_DOOR", true), BIRCH_DOOR("BIRCH_DOOR", true),
FURNACE("FURNACE", "BURNING_FURNACE"), FURNACE("FURNACE", "BURNING_FURNACE"),
CAKE("CAKE_BLOCK"), CAKE("CAKE_BLOCK"),
CARROTS("CARROT"), // totally makes sense, lol
CAULDRON("CAULDRON_BLOCK"), CAULDRON("CAULDRON_BLOCK"),
COMPARATOR("REDSTONE_COMPARATOR_OFF", "REDSTONE_COMPARATOR_ON"), COMPARATOR("REDSTONE_COMPARATOR_OFF", "REDSTONE_COMPARATOR_ON"),
DARK_OAK_DOOR("DARK_OAK_DOOR", true), DARK_OAK_DOOR("DARK_OAK_DOOR", true),
@ -29,29 +30,33 @@ public enum LegacyMaterialBlockType {
FLOWER_POT("FLOWER_POT"), FLOWER_POT("FLOWER_POT"),
IRON_DOOR("IRON_DOOR_BLOCK", true), IRON_DOOR("IRON_DOOR_BLOCK", true),
JUNGLE_DOOR("JUNGLE_DOOR", true), JUNGLE_DOOR("JUNGLE_DOOR", true),
LAVA("STATIONARY_LAVA"),
NETHER_WART("NETHER_WARTS"), NETHER_WART("NETHER_WARTS"),
/* /*
< PURPUR_DOUBLE_SLAB < PURPUR_DOUBLE_SLAB
*/ */
POTATOES("POTATO"),
REDSTONE_LAMP("REDSTONE_LAMP_OFF", "REDSTONE_LAMP_ON"), REDSTONE_LAMP("REDSTONE_LAMP_OFF", "REDSTONE_LAMP_ON"),
REDSTONE_ORE("REDSTONE_ORE", "GLOWING_REDSTONE_ORE"), REDSTONE_ORE("REDSTONE_ORE", "GLOWING_REDSTONE_ORE"),
REDSTONE_TORCH("REDSTONE_TORCH_ON", "REDSTONE_TORCH_OFF"), REDSTONE_TORCH("REDSTONE_TORCH_ON", "REDSTONE_TORCH_OFF"),
SPRUCE_DOOR("SPRUCE_DOOR"), SPRUCE_DOOR("SPRUCE_DOOR"),
/*
< STATIONARY_LAVA,
< STATIONARY_WATER,
*/
SUGAR_CANE("SUGAR_CANE_BLOCK"), SUGAR_CANE("SUGAR_CANE_BLOCK"),
WATER("STATIONARY_WATER"),
WHEAT("CROPS"); WHEAT("CROPS");
final String blockMaterialName; final String blockMaterialName;
final String alternateBlockMaterialName; final String alternateBlockMaterialName;
final Material blockMaterial, alternateBlockMaterial; final Material blockMaterial, alternateBlockMaterial;
final boolean requiresData; // some blocks require data to render properly (double blocks) final boolean requiresData; // some blocks require data to render properly (double blocks)
final static Map<String, LegacyMaterialBlockType> lookupTable = new HashMap(); final static Map<String, LegacyMaterialBlockType> lookupTable = new HashMap();
final static Map<String, LegacyMaterialBlockType> reverseLookupTable = new HashMap();
static { static {
for (LegacyMaterialBlockType t : values()) { for (LegacyMaterialBlockType t : values()) {
lookupTable.put(t.name(), t); lookupTable.put(t.name(), t);
reverseLookupTable.put(t.blockMaterialName, t);
if(t.alternateBlockMaterialName != null) {
reverseLookupTable.put(t.alternateBlockMaterialName, t);
}
} }
} }
@ -99,4 +104,8 @@ public enum LegacyMaterialBlockType {
return lookupTable.get(lookup); return lookupTable.get(lookup);
} }
public static LegacyMaterialBlockType getFromLegacy(String lookup) {
return reverseLookupTable.get(lookup);
}
} }

View File

@ -359,7 +359,7 @@ public class BlockUtils {
} else if (!useLegacy) { } else if (!useLegacy) {
return BlockUtilsModern._isCropFullyGrown(block); return BlockUtilsModern._isCropFullyGrown(block);
} }
CompatibleMaterial mat = CompatibleMaterial.getMaterial(block.getType()); CompatibleMaterial mat = CompatibleMaterial.getBlockMaterial(block.getType());
if (mat == null || !mat.isCrop()) { if (mat == null || !mat.isCrop()) {
return false; return false;
} else { } else {
@ -379,7 +379,7 @@ public class BlockUtils {
} else if (!useLegacy) { } else if (!useLegacy) {
return BlockUtilsModern._getMaxGrowthStage(block); return BlockUtilsModern._getMaxGrowthStage(block);
} }
CompatibleMaterial mat = CompatibleMaterial.getMaterial(block.getType()); CompatibleMaterial mat = CompatibleMaterial.getBlockMaterial(block.getType());
if (mat == null || !mat.isCrop()) { if (mat == null || !mat.isCrop()) {
return -1; return -1;
} else { } else {
@ -399,7 +399,7 @@ public class BlockUtils {
} else if (!useLegacy) { } else if (!useLegacy) {
return BlockUtilsModern._getMaxGrowthStage(material); return BlockUtilsModern._getMaxGrowthStage(material);
} }
CompatibleMaterial mat = CompatibleMaterial.getMaterial(material); CompatibleMaterial mat = CompatibleMaterial.getBlockMaterial(material);
if (mat == null || !mat.isCrop()) { if (mat == null || !mat.isCrop()) {
return -1; return -1;
} else { } else {
@ -418,7 +418,7 @@ public class BlockUtils {
} else if (!useLegacy) { } else if (!useLegacy) {
BlockUtilsModern._setGrowthStage(block, stage); BlockUtilsModern._setGrowthStage(block, stage);
} else { } else {
CompatibleMaterial mat = CompatibleMaterial.getMaterial(block.getType()); CompatibleMaterial mat = CompatibleMaterial.getBlockMaterial(block.getType());
if (mat != null && mat.isCrop()) { if (mat != null && mat.isCrop()) {
try { try {
legacySetBlockData.invoke(block, (byte) Math.max(0, Math.min(stage, mat == CompatibleMaterial.BEETROOTS ? 3 : 7))); legacySetBlockData.invoke(block, (byte) Math.max(0, Math.min(stage, mat == CompatibleMaterial.BEETROOTS ? 3 : 7)));
@ -439,7 +439,7 @@ public class BlockUtils {
} else if (!useLegacy) { } else if (!useLegacy) {
BlockUtilsModern._incrementGrowthStage(block); BlockUtilsModern._incrementGrowthStage(block);
} else { } else {
CompatibleMaterial mat = CompatibleMaterial.getMaterial(block.getType()); CompatibleMaterial mat = CompatibleMaterial.getBlockMaterial(block.getType());
if (mat != null && mat.isCrop() && block.getData() < (mat == CompatibleMaterial.BEETROOTS ? 3 : 7)) { if (mat != null && mat.isCrop() && block.getData() < (mat == CompatibleMaterial.BEETROOTS ? 3 : 7)) {
try { try {
legacySetBlockData.invoke(block, (byte) (block.getData() + 1)); legacySetBlockData.invoke(block, (byte) (block.getData() + 1));
@ -460,7 +460,7 @@ public class BlockUtils {
} else if (!useLegacy) { } else if (!useLegacy) {
BlockUtilsModern._resetGrowthStage(block); BlockUtilsModern._resetGrowthStage(block);
} else { } else {
CompatibleMaterial mat = CompatibleMaterial.getMaterial(block.getType()); CompatibleMaterial mat = CompatibleMaterial.getBlockMaterial(block.getType());
if (mat != null && mat.isCrop()) { if (mat != null && mat.isCrop()) {
try { try {
legacySetBlockData.invoke(block, (byte) 0); legacySetBlockData.invoke(block, (byte) 0);