Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public abstract class GenesisConditionType {
WORLD,
WEATHER,
PLACEHOLDERNUMBER,
PLACEHOLDERMATCH;
PLACEHOLDERMATCH,
ENCHANTMENTLEVEL;


private static List<GenesisConditionType> types;
Expand Down Expand Up @@ -77,6 +78,7 @@ public static void loadTypes() {
WEATHER = registerType(new GenesisConditionTypeWeather());
PLACEHOLDERNUMBER = registerType(new GenesisConditionTypePlaceholderNumber());
PLACEHOLDERMATCH = registerType(new GenesisConditionTypePlaceholderMatch());
ENCHANTMENTLEVEL = registerType(new GenesisConditionTypeEnchantmentLevel());
}

public static GenesisConditionType registerType(GenesisConditionType type) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package studio.magemonkey.genesis.core.conditions;

import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import studio.magemonkey.genesis.core.GenesisBuy;
import studio.magemonkey.genesis.core.GenesisShopHolder;
import studio.magemonkey.genesis.managers.ClassManager;
import studio.magemonkey.genesis.managers.misc.InputReader;
import studio.magemonkey.genesis.misc.Misc;

public class GenesisConditionTypeEnchantmentLevel extends GenesisConditionType {

@Override
public boolean meetsCondition(GenesisShopHolder holder,
GenesisBuy shopItem,
Player p,
String conditiontype,
String condition) {
// conditiontype = enchantment name (e.g., "EFFICIENCY")
// condition = "over:5" / "under:3" / "equals:10" / "between:5-10"

String[] realparts = condition.split(":", 2);
if (realparts.length < 2) {
ClassManager.manager.getBugFinder()
.warn("Unable to read enchantmentlevel condition '" + conditiontype + ":" + condition
+ "'. It should look like: '<enchantment>:<operator>:<level>'. "
+ "Example: 'EFFICIENCY:equals:10'.");
return false;
}

String operator = realparts[0];
String value = realparts[1];
double level = getEnchantmentLevel(p, conditiontype);

if (operator.equalsIgnoreCase("over") || operator.equalsIgnoreCase(">")) {
return level > InputReader.getDouble(value, -1);
}
if (operator.equalsIgnoreCase("under") || operator.equalsIgnoreCase("<")
|| operator.equalsIgnoreCase("below")) {
return level < InputReader.getDouble(value, -1);
}
if (operator.equalsIgnoreCase("equals") || operator.equalsIgnoreCase("=")) {
for (String option : value.split(",")) {
if (level == InputReader.getDouble(option.trim(), -1)) {
return true;
}
}
return false;
}
if (operator.equalsIgnoreCase("between") || operator.equalsIgnoreCase("inbetween")) {
String separator = value.contains(":") ? ":" : "-";
String[] parts = value.split(separator);
if (parts.length == 2) {
double start = InputReader.getDouble(parts[0], -1);
double end = InputReader.getDouble(parts[1], -1);
return level >= start && level <= end;
} else {
ClassManager.manager.getBugFinder()
.warn("Unable to read enchantmentlevel condition '" + conditiontype + ":" + condition
+ "' of conditiontype 'between'. "
+ "It has to look like following: '<enchantment>:between:<level1>-<level2>'.");
return false;
}
}

return false;
}

/**
* Returns the level of the given enchantment on the item in the player's main hand,
* or 0 if the item does not have the enchantment.
*/
private double getEnchantmentLevel(Player p, String enchantmentName) {
ItemStack item = Misc.getItemInMainHand(p);
if (item == null || item.getType().isAir()) {
return 0;
}

Enchantment enchantment = InputReader.readEnchantment(enchantmentName);
if (enchantment == null) {
ClassManager.manager.getBugFinder()
.warn("Unknown enchantment '" + enchantmentName + "' in enchantmentlevel condition.");
return 0;
}

// Check regular item enchantments first
if (item.getEnchantments().containsKey(enchantment)) {
return item.getEnchantments().get(enchantment);
}

// Fallback: check enchantment books (EnchantmentStorageMeta)
if (item.getItemMeta() instanceof EnchantmentStorageMeta) {
EnchantmentStorageMeta meta = (EnchantmentStorageMeta) item.getItemMeta();
if (meta.getStoredEnchants().containsKey(enchantment)) {
return meta.getStoredEnchants().get(enchantment);
}
}

return 0;
}

@Override
public boolean dependsOnPlayer() {
return true;
}

@Override
public String[] createNames() {
return new String[]{"enchantmentlevel", "enchlevel", "enchantlevel"};
}

@Override
public void enableType() {
}

@Override
public String[] showStructure() {
return new String[]{
"[enchantment]:over:[int]",
"[enchantment]:under:[int]",
"[enchantment]:equals:[int]",
"[enchantment]:between:[int]-[int]"
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import lombok.NonNull;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import studio.magemonkey.genesis.core.GenesisBuy;
import studio.magemonkey.genesis.core.GenesisShop;
import studio.magemonkey.genesis.core.GenesisShopHolder;
Expand All @@ -20,15 +22,19 @@
import studio.magemonkey.genesis.misc.VersionManager;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class StringManager {

private static final Pattern hexPattern = Pattern.compile("(#[a-fA-F0-9]{6})");
private static final Pattern placeholderPattern = Pattern.compile("%(.*?)%");
private static final Pattern hexPattern = Pattern.compile("(#[a-fA-F0-9]{6})");
private static final Pattern placeholderPattern = Pattern.compile("%(.*?)%");
private static final Pattern enchantLevelPattern = Pattern.compile("%item_in_hand_enchant_([a-zA-Z0-9_]+)%",
Pattern.CASE_INSENSITIVE);

/**
* Transform specific strings from one thing to another
Expand Down Expand Up @@ -190,6 +196,31 @@ public String transform(String s, Player target, boolean colorize) {
s = s.replace("%item_in_hand%", Misc.getItemInMainHand(target).getType().name());
}

if (s.contains("%item_in_hand_enchant_")) {
ItemStack handItem = Misc.getItemInMainHand(target);
Matcher em = enchantLevelPattern.matcher(s);
Map<String, String> replacements = new LinkedHashMap<>();
while (em.find()) {
String enchantName = em.group(1);
Enchantment enchantment = InputReader.readEnchantment(enchantName);
int level = 0;
if (enchantment != null && handItem != null && !handItem.getType().isAir()) {
if (handItem.getEnchantments().containsKey(enchantment)) {
level = handItem.getEnchantments().get(enchantment);
} else if (handItem.getItemMeta() instanceof EnchantmentStorageMeta) {
EnchantmentStorageMeta meta = (EnchantmentStorageMeta) handItem.getItemMeta();
if (meta.getStoredEnchants().containsKey(enchantment)) {
level = meta.getStoredEnchants().get(enchantment);
}
}
}
replacements.put(em.group(0), String.valueOf(level));
}
for (Map.Entry<String, String> entry : replacements.entrySet()) {
s = s.replace(entry.getKey(), entry.getValue());
}
}

if (s.contains("%input%")) {
s = s.replace("%input%", ClassManager.manager.getPlayerDataHandler().getInput(target));
}
Expand Down Expand Up @@ -237,6 +268,10 @@ public boolean checkStringForFeatures(GenesisShop shop,
b = true;
}

if (s.contains("%item_in_hand%") || s.contains("%item_in_hand_enchant_")) {
b = true;
}

if (buy != null && shop != null && ClassManager.manager.getSettings().getServerPingingEnabled(true)) {
String serverNames = StringManipulationLib.figureOutVariable(s, 0, "players", "motd");
if (serverNames != null) {
Expand Down
Loading