Skip to content

Commit

Permalink
[1.0.6.9] allow prefixing vanilla types with minecraft: and port of c…
Browse files Browse the repository at this point in the history
…ustom potion effects syntax from slib
  • Loading branch information
Misat11 committed Sep 28, 2023
1 parent 4f0f80a commit 897f7e0
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defaultTasks 'clean', 'screamCompile'

allprojects {
group = 'org.screamingsandals.simpleinventories'
version = '1.0.6.8'
version = '1.0.6.9'
}

subprojects {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.bukkit.enchantments.Enchantment;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class EnchantmentSearchEngine {
Expand Down Expand Up @@ -57,6 +58,13 @@ public static Enchantment searchEnchantment(Object enchantObj) {

/* NamespacedKeys are not available on old versions, use hardcoded map */

return Enchantment.getByName(LEGACY_MAPPING.get(enchant.toLowerCase()));
enchant = enchant.toLowerCase(Locale.ROOT);

// allow prefixing vanilla stuff with minecraft:
if (enchant.startsWith("minecraft:")) {
enchant = enchant.substring(10);
}

return Enchantment.getByName(LEGACY_MAPPING.get(enchant));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,11 @@ public static int getVersionNumber() {
}

public static Result find(String materialArgument) {
// allow prefixing vanilla stuff with minecraft:
if (materialArgument.toLowerCase().startsWith("minecraft:")) {
materialArgument = materialArgument.substring(10);
}

String[] splitByColon = materialArgument.split(":");
String materialString = splitByColon[0];
short damageShort = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.screamingsandals.simpleinventories.utils;

import org.bukkit.potion.PotionEffectType;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class PotionTypeEffectSearchEngine {
private static final Map<String, String> minecraft2bukkit = new HashMap<String, String>() {
{
put("slowness", "slow");
put("haste", "fast_digging");
put("mining_fatigue", "slow_digging");
put("strength", "increase_damage");
put("instant_health", "heal");
put("instant_damage", "harm");
put("jump_boost", "jump");
put("nausea", "confusion");
put("resistance", "damage_resistance");
}
};

public static PotionEffectType find(String potionType) {
String potion = potionType.toLowerCase(Locale.ROOT);

// allow prefixing vanilla stuff with minecraft:
if (potion.startsWith("minecraft:")) {
potion = potion.substring(10);
}

if (minecraft2bukkit.containsKey(potion)) {
potion = minecraft2bukkit.get(potion);
}

return PotionEffectType.getByName(potion);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public class PotionTypeSearchEngine {
Expand Down Expand Up @@ -70,7 +71,13 @@ public class PotionTypeSearchEngine {
};

public static PotionData find(String potionType) {
String potion = potionType.toLowerCase();
String potion = potionType.toLowerCase(Locale.ROOT);

// allow prefixing vanilla stuff with minecraft:
if (potion.startsWith("minecraft:")) {
potion = potion.substring(10);
}

return Attemptor.start()
.attempt(extendable.containsKey(potion), type -> new PotionData(PotionType.valueOf(extendable.get(type)), true, false))
.attempt(upgradeable.containsKey(potion), type -> new PotionData(PotionType.valueOf(upgradeable.get(type)), false, true))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public class PotionTypeSearchEngine1_8_8 {
Expand Down Expand Up @@ -60,7 +61,13 @@ public class PotionTypeSearchEngine1_8_8 {
};

public static Potion find(String potionType) {
String potion = potionType.toLowerCase();
String potion = potionType.toLowerCase(Locale.ROOT);

// allow prefixing vanilla stuff with minecraft:
if (potion.startsWith("minecraft:")) {
potion = potion.substring(10);
}

return Attemptor.start()
.attempt(extendable.containsKey(potion), type -> new Potion(PotionType.valueOf(extendable.get(type)), 1, false, true))
.attempt(upgradeable.containsKey(potion), type -> new Potion(PotionType.valueOf(upgradeable.get(type)), 2, false, false))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.logging.Level;
import java.util.stream.Collectors;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.configuration.serialization.ConfigurationSerialization;
import org.bukkit.enchantments.Enchantment;
Expand All @@ -17,6 +22,8 @@
import org.bukkit.inventory.meta.Repairable;
import org.bukkit.potion.Potion;
import org.bukkit.potion.PotionData;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.potion.PotionType;

public class StackParser {
Expand Down Expand Up @@ -321,9 +328,76 @@ public static ItemStack parseLongStack(Map<String, Object> obj) {
}
}
}

if (obj.containsKey("effects") && meta instanceof PotionMeta) {
Object ob = obj.get("effects");
if (ob instanceof List) {
for (Object o : (List<?>) ob) {
if (!(o instanceof Map)) {
continue;
}

PotionEffect effect1 = getPotionEffect((Map<String, Object>) o);

if (effect1 != null) {
((PotionMeta) meta).addCustomEffect(effect1, true);
}
}
} else if (ob instanceof Map) {
PotionEffect effect1 = getPotionEffect((Map<String, Object>) ob);

if (effect1 != null) {
((PotionMeta) meta).addCustomEffect(effect1, true);
}
}
}

stack.setItemMeta(meta);

return stack;
}

@SuppressWarnings("unchecked")
public static PotionEffect getPotionEffect(Object ob) {
if (ob instanceof PotionEffect) {
return (PotionEffect) ob;
}

if (ob instanceof Map) {
return getPotionEffect((Map<String, Object>) ob);
}

return null;
}

public static PotionEffect getPotionEffect(Map<String, Object> map) {
Object effect = map.get("effect");

if (effect == null) {
return null;
}

PotionEffectType potionEffect = PotionTypeEffectSearchEngine.find(effect.toString());

Map<String, Object> processedMap = new HashMap<>(map); // clone map to not change the input object

if (potionEffect != null) {
processedMap.put("effect", potionEffect.getId());
}

if (processedMap.containsKey("particles")) {
processedMap.put("has-particles", processedMap.get("particles"));
}

if (processedMap.containsKey("icon")) {
processedMap.put("has-icon", processedMap.get("icon"));
}

try {
return new PotionEffect(processedMap);
} catch (Exception ex) {
Bukkit.getLogger().log(Level.WARNING, "[SimpleInventories] An error occurred while resolving custom potion effect", ex);
return null;
}
}
}

0 comments on commit 897f7e0

Please sign in to comment.