Skip to content

Commit 6ad9d97

Browse files
committed
Fix mcMMO trying to load incompatible potions on older versions
1 parent 6df73e5 commit 6ad9d97

File tree

2 files changed

+84
-22
lines changed

2 files changed

+84
-22
lines changed

Changelog.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
Version 2.2.033
2+
Added Breeze_Rod entries to potions.yml for Awkward potion (see notes)
3+
Added missing Wind Charging potion entries to potions.yml (see notes)
4+
Fixed bug where mcMMO would attempt to load potions that required ingredients or effects from newer versions of Minecraft
5+
6+
NOTES:
7+
You will have to update your potions.yml manually to receive these changes, it is highly recommended that if you haven't customized this file that you simply just delete it, mcMMO will generate a new one on the next start up and it will contain all the missing entries.
8+
If you have customized this file, you can find the "default" version of this file here on the mcMMO github repo: https://github.com/mcMMO-Dev/mcMMO/blob/master/src/main/resources/potions.yml
9+
You can use this file to compare it to your own and make the manual changes needed to get the new entries.
10+
After making the changes to potions.yml (either via deleting it or not...) mcMMO should now recognize the Wind Charging potion and the Awkward potion created from Breeze Rods
11+
112
Version 2.2.032
213
Fixed bug where Roll would throw exceptions with certain CMI interactions
314
Blast Mining no longer drops infested block variants

src/main/java/com/gmail/nossr50/config/skills/alchemy/PotionConfig.java

Lines changed: 73 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.gmail.nossr50.datatypes.skills.alchemy.AlchemyPotion;
55
import com.gmail.nossr50.mcMMO;
66
import com.gmail.nossr50.util.ItemUtils;
7+
import com.gmail.nossr50.util.LogUtils;
78
import org.bukkit.ChatColor;
89
import org.bukkit.Color;
910
import org.bukkit.Material;
@@ -14,6 +15,7 @@
1415
import org.bukkit.potion.PotionEffect;
1516
import org.bukkit.potion.PotionEffectType;
1617
import org.bukkit.potion.PotionType;
18+
import org.jetbrains.annotations.NotNull;
1719
import org.jetbrains.annotations.VisibleForTesting;
1820

1921
import java.io.File;
@@ -26,6 +28,14 @@
2628
import static com.gmail.nossr50.util.PotionUtil.*;
2729

2830
public class PotionConfig extends LegacyConfigLoader {
31+
private static final String BREEZE_ROD_STR = "BREEZE_ROD";
32+
private static final String INFESTED_EFFECT_STR = "INFESTED";
33+
private static final String WEAVING_EFFECT_STR = "WEAVING";
34+
private static final String OOZING_EFFECT_STR = "OOZING";
35+
private static final String WIND_CHARGED_EFFECT_STR = "WIND_CHARGED";
36+
private static final String SLIME_BLOCK_STR = "SLIME_BLOCK";
37+
private static final String COBWEB_STR = "COBWEB";
38+
private static final String STONE_STR = "STONE";
2939

3040
private final List<ItemStack> concoctionsIngredientsTierOne = new ArrayList<>();
3141
private final List<ItemStack> concoctionsIngredientsTierTwo = new ArrayList<>();
@@ -35,6 +45,17 @@ public class PotionConfig extends LegacyConfigLoader {
3545
private final List<ItemStack> concoctionsIngredientsTierSix = new ArrayList<>();
3646
private final List<ItemStack> concoctionsIngredientsTierSeven = new ArrayList<>();
3747
private final List<ItemStack> concoctionsIngredientsTierEight = new ArrayList<>();
48+
private final AlchemyPotionConfigResult INCOMPATIBLE_POTION_RESULT = new AlchemyPotionConfigResult(null,
49+
AlchemyPotionConfigResultType.INCOMPATIBLE);
50+
private final AlchemyPotionConfigResult ERROR_POTION_RESULT = new AlchemyPotionConfigResult(null,
51+
AlchemyPotionConfigResultType.ERROR);
52+
53+
record AlchemyPotionConfigResult(AlchemyPotion alchemyPotion, AlchemyPotionConfigResultType resultType) {}
54+
enum AlchemyPotionConfigResultType {
55+
LOADED,
56+
INCOMPATIBLE,
57+
ERROR;
58+
}
3859

3960
/**
4061
* Map of potion names to AlchemyPotion objects.
@@ -93,39 +114,45 @@ private void loadConcoctionsTier(List<ItemStack> ingredientList, List<String> in
93114
}
94115
}
95116
}
96-
97117
/**
98118
* Find the Potions configuration section and load all defined potions.
99119
*/
100120
int loadPotionMap() {
101121
ConfigurationSection potionSection = config.getConfigurationSection("Potions");
102122
int potionsLoaded = 0;
123+
int incompatible = 0;
103124
int failures = 0;
104125

105126
for (String potionName : potionSection.getKeys(false)) {
106-
AlchemyPotion potion = loadPotion(potionSection.getConfigurationSection(potionName));
127+
AlchemyPotionConfigResult alchemyPotionConfigResult = loadPotion(potionSection.getConfigurationSection(potionName));
128+
AlchemyPotion potion = alchemyPotionConfigResult.alchemyPotion;
107129

108130
if (potion != null) {
109131
alchemyPotions.put(potionName, potion);
110132
potionsLoaded++;
111133
} else {
112-
failures++;
134+
if (alchemyPotionConfigResult.resultType == AlchemyPotionConfigResultType.INCOMPATIBLE) {
135+
incompatible++;
136+
} else {
137+
failures++;
138+
}
113139
}
114140
}
115141

116-
mcMMO.p.getLogger().info("Loaded " + potionsLoaded + " Alchemy potions, skipped " + failures + ".");
142+
int totalPotions = potionsLoaded + incompatible + failures;
143+
144+
mcMMO.p.getLogger().info("Loaded " + potionsLoaded + "/" + totalPotions + " Alchemy potions.");
145+
if (incompatible > 0) {
146+
mcMMO.p.getLogger().info("Skipped " + incompatible + " Alchemy potions that require a newer" +
147+
" Minecraft game version.");
148+
}
149+
if (failures > 0) {
150+
mcMMO.p.getLogger().info("Failed to load " + failures + " Alchemy potions that encountered errors while loading.");
151+
}
117152
return potionsLoaded;
118153
}
119154

120-
/**
121-
* Parse a ConfigurationSection representing a AlchemyPotion.
122-
* Returns null if input cannot be parsed.
123-
*
124-
* @param potion_section ConfigurationSection to be parsed.
125-
*
126-
* @return Parsed AlchemyPotion.
127-
*/
128-
private AlchemyPotion loadPotion(ConfigurationSection potion_section) {
155+
private @NotNull AlchemyPotionConfigResult loadPotion(ConfigurationSection potion_section) {
129156
try {
130157
final String key = potion_section.getName();
131158

@@ -159,7 +186,7 @@ private AlchemyPotion loadPotion(ConfigurationSection potion_section) {
159186
if (potionMeta == null) {
160187
mcMMO.p.getLogger().severe("PotionConfig: Failed to get PotionMeta for " + key + ", from configuration section:" +
161188
" " + potion_section);
162-
return null;
189+
return ERROR_POTION_RESULT;
163190
}
164191

165192
// extended and upgraded seem to be mutually exclusive
@@ -173,14 +200,14 @@ private AlchemyPotion loadPotion(ConfigurationSection potion_section) {
173200
if (potionTypeStr == null) {
174201
mcMMO.p.getLogger().severe("PotionConfig: Missing PotionType for " + key + ", from configuration section:" +
175202
" " + potion_section);
176-
return null;
203+
return ERROR_POTION_RESULT;
177204
}
178205

179206
// This works via side effects
180207
// TODO: Redesign later, side effects are stupid
181208
if(!setPotionType(potionMeta, potionTypeStr, upgraded, extended)) {
182209
mcMMO.p.getLogger().severe("PotionConfig: Failed to set parameters of potion for " + key + ": " + potionTypeStr);
183-
return null;
210+
return ERROR_POTION_RESULT;
184211
}
185212

186213
final List<String> lore = new ArrayList<>();
@@ -194,6 +221,12 @@ private AlchemyPotion loadPotion(ConfigurationSection potion_section) {
194221
if (potion_section.contains("Effects")) {
195222
for (String effect : potion_section.getStringList("Effects")) {
196223
String[] parts = effect.split(" ");
224+
if (isTrickyTrialsPotionEffect(parts[0])
225+
&& !mcMMO.getCompatibilityManager().getMinecraftGameVersion().isAtLeast(1, 21, 0)) {
226+
LogUtils.debug(mcMMO.p.getLogger(), "Skipping potion effect " + effect +" because it is not" +
227+
" compatible with the current Minecraft game version.");
228+
return INCOMPATIBLE_POTION_RESULT;
229+
}
197230

198231
PotionEffectType type = parts.length > 0 ? PotionEffectType.getByName(parts[0]) : null;
199232
int amplifier = parts.length > 1 ? Integer.parseInt(parts[1]) : 0;
@@ -217,12 +250,19 @@ private AlchemyPotion loadPotion(ConfigurationSection potion_section) {
217250

218251
final Map<ItemStack, String> children = new HashMap<>();
219252
if (potion_section.contains("Children")) {
220-
for (String child : potion_section.getConfigurationSection("Children").getKeys(false)) {
221-
ItemStack ingredient = loadIngredient(child);
253+
for (String childIngredient : potion_section.getConfigurationSection("Children").getKeys(false)) {
254+
// Breeze Rod was only for potions after 1.21.0
255+
if (isTrickyTrialsIngredient(childIngredient)
256+
&& !mcMMO.getCompatibilityManager().getMinecraftGameVersion().isAtLeast(1, 21, 0)) {
257+
continue;
258+
}
259+
ItemStack ingredient = loadIngredient(childIngredient);
222260
if (ingredient != null) {
223-
children.put(ingredient, potion_section.getConfigurationSection("Children").getString(child));
261+
children.put(ingredient,
262+
potion_section.getConfigurationSection("Children").getString(childIngredient));
224263
} else {
225-
mcMMO.p.getLogger().severe("PotionConfig: Failed to parse child for potion " + key + ": " + child);
264+
mcMMO.p.getLogger().severe(
265+
"PotionConfig: Failed to parse child for potion " + key + ": " + childIngredient);
226266
}
227267
}
228268
}
@@ -231,14 +271,25 @@ private AlchemyPotion loadPotion(ConfigurationSection potion_section) {
231271

232272
// TODO: Might not need to .setItemMeta
233273
itemStack.setItemMeta(potionMeta);
234-
return new AlchemyPotion(potion_section.getName(), itemStack, children);
274+
return new AlchemyPotionConfigResult(new AlchemyPotion(potion_section.getName(), itemStack, children),
275+
AlchemyPotionConfigResultType.LOADED);
235276
} catch (Exception e) {
236277
mcMMO.p.getLogger().warning("PotionConfig: Failed to load Alchemy potion: " + potion_section.getName());
237278
e.printStackTrace();
238-
return null;
279+
return ERROR_POTION_RESULT;
239280
}
240281
}
241282

283+
private static boolean isTrickyTrialsIngredient(String ingredientStr) {
284+
return ingredientStr.equalsIgnoreCase(BREEZE_ROD_STR) || ingredientStr.equalsIgnoreCase(SLIME_BLOCK_STR)
285+
|| ingredientStr.equalsIgnoreCase(COBWEB_STR) || ingredientStr.equalsIgnoreCase(STONE_STR);
286+
}
287+
288+
private static boolean isTrickyTrialsPotionEffect(String effectStr) {
289+
return effectStr.equalsIgnoreCase(INFESTED_EFFECT_STR) || effectStr.equalsIgnoreCase(WEAVING_EFFECT_STR)
290+
|| effectStr.equalsIgnoreCase(OOZING_EFFECT_STR) || effectStr.equalsIgnoreCase(WIND_CHARGED_EFFECT_STR);
291+
}
292+
242293
private boolean setPotionType(PotionMeta potionMeta, String potionTypeStr, boolean upgraded, boolean extended) {
243294
final PotionType potionType = matchPotionType(potionTypeStr, upgraded, extended);
244295

0 commit comments

Comments
 (0)