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
* 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
* 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
* @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
* @return LegacyMaterial or null if none found
@ -1220,6 +1220,65 @@ public enum CompatibleMaterial {
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;
public static Set<CompatibleMaterial> getAllValidItemMaterials() {

View File

@ -18,6 +18,7 @@ public enum LegacyMaterialBlockType {
BIRCH_DOOR("BIRCH_DOOR", true),
FURNACE("FURNACE", "BURNING_FURNACE"),
CAKE("CAKE_BLOCK"),
CARROTS("CARROT"), // totally makes sense, lol
CAULDRON("CAULDRON_BLOCK"),
COMPARATOR("REDSTONE_COMPARATOR_OFF", "REDSTONE_COMPARATOR_ON"),
DARK_OAK_DOOR("DARK_OAK_DOOR", true),
@ -29,29 +30,33 @@ public enum LegacyMaterialBlockType {
FLOWER_POT("FLOWER_POT"),
IRON_DOOR("IRON_DOOR_BLOCK", true),
JUNGLE_DOOR("JUNGLE_DOOR", true),
LAVA("STATIONARY_LAVA"),
NETHER_WART("NETHER_WARTS"),
/*
< PURPUR_DOUBLE_SLAB
*/
POTATOES("POTATO"),
REDSTONE_LAMP("REDSTONE_LAMP_OFF", "REDSTONE_LAMP_ON"),
REDSTONE_ORE("REDSTONE_ORE", "GLOWING_REDSTONE_ORE"),
REDSTONE_TORCH("REDSTONE_TORCH_ON", "REDSTONE_TORCH_OFF"),
SPRUCE_DOOR("SPRUCE_DOOR"),
/*
< STATIONARY_LAVA,
< STATIONARY_WATER,
*/
SUGAR_CANE("SUGAR_CANE_BLOCK"),
WATER("STATIONARY_WATER"),
WHEAT("CROPS");
final String blockMaterialName;
final String alternateBlockMaterialName;
final Material blockMaterial, alternateBlockMaterial;
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> reverseLookupTable = new HashMap();
static {
for (LegacyMaterialBlockType t : values()) {
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);
}
public static LegacyMaterialBlockType getFromLegacy(String lookup) {
return reverseLookupTable.get(lookup);
}
}

View File

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