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 @@ -61,6 +61,8 @@ public void load()
ConfigurationSection section = config.getConfigurationSection("customizations." + key);
assert section != null;

customization.ignore = section.getBoolean("ignore", false);

if(!section.getBoolean("enabled", true))
continue;

Expand Down Expand Up @@ -129,9 +131,12 @@ public void load()
}
case "nbt" ->
{
Object value = rulesSection.get(matcherKey + ".value");
if (value == null)
throwMissingProperty("nbt.value");
RuleNbtMatcher nbtMatcher = new RuleNbtMatcher(
rulesSection.getString(matcherKey + ".path"),
rulesSection.getString(matcherKey + ".value"),
getStringOrThrow(rulesSection, matcherKey + ".path"),
value,
rulesSection.getString(matcherKey + ".type", "string")
);
customization.addRule(nbtMatcher);
Expand Down Expand Up @@ -192,13 +197,13 @@ public void load()
}

ConfigurationSection changesSection = section.getConfigurationSection("changes");
if (changesSection == null)
if (changesSection == null && !customization.ignore)
{
Msg.error("Error: Customization '" + key + "' missing 'changes'. File: " + config.getPartialFilePath());
continue;
}

for (String changeKey : changesSection.getKeys(false))
for (String changeKey : changesSection == null ? Collections.<String>emptySet() : changesSection.getKeys(false))
{
try
{
Expand All @@ -215,6 +220,10 @@ public void load()
String json = changesSection.getString(changeKey);
customization.addChange(new RenamerJson(json));
}
case "name_prefix", "rename_prefix" -> customization.addChange(new TextAffix(false, getStringOrThrow(changesSection, changeKey), ""));
case "name_suffix", "rename_suffix" -> customization.addChange(new TextAffix(false, "", getStringOrThrow(changesSection, changeKey)));
case "lore_prefix" -> customization.addChange(new TextAffix(true, getStringOrThrow(changesSection, changeKey), ""));
case "lore_suffix" -> customization.addChange(new TextAffix(true, "", getStringOrThrow(changesSection, changeKey)));
case "replace_word_display_name" ->
{
ConfigurationSection thisSection = changesSection.getConfigurationSection(changeKey);
Expand Down Expand Up @@ -488,11 +497,12 @@ public void handle(ItemStack itemStack, Player player)

boolean trackChanges = MainCommand.hasDebugTag(player);
ChangeSession session = new ChangeSession(itemStack, player, trackChanges);
for (Map.Entry<String, Customization> entry : customizations.entrySet())
{
Customization customization = entry.getValue();
customization.handle(session);
}
if (isIgnored(session))
return;

for (Customization customization : customizations.values())
if (!customization.ignore)
customization.handle(session);

// Warning! This doesn't edit the session data. Do not access use "customization" obj
// after this line since it would result not updated with changes done by the API handlers.
Expand All @@ -511,6 +521,14 @@ public void handle(ItemStack itemStack, Player player)
}
}

boolean isIgnored(ChangeSession session)
{
for (Customization customization : customizations.values())
if (customization.ignore && customization.matchesAll(session))
return true;
return false;
}

private static List<File> getAllYmlFiles()
{
File cosmeticsDir = new File(Main.inst().getDataFolder().getAbsolutePath() + "/contents/");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class Customization

public List<IRule> rules = new ArrayList<>();
public List<IChange> changes = new ArrayList<>();
public boolean ignore;

public Customization(boolean ignoreInventoriesEnabled, boolean ignoreAnyCustomInventory, List<String> ignoreInventoriesByTitle)
{
Expand All @@ -41,7 +42,7 @@ public void addChange(IChange change)
changes.add(change);
}

boolean matchesAll(ChangeSession session)
public boolean matchesAll(ChangeSession session)
{
InventoryView openInventory = session.getPlayer().getOpenInventory();
if(ignoreInventoriesEnabled)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package dev.lone.vanillacustomizer.customization.changes;

import beer.devs.fastnbt.nms.nbt.NItem;
import dev.lone.vanillacustomizer.ChangeSession;
import dev.lone.vanillacustomizer.utils.Comp;
import dev.lone.vanillacustomizer.utils.ConfigFile;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;

import java.util.List;

public class TextAffix implements IChange
{
private final boolean lore;
private final String prefix;
private final String suffix;

public TextAffix(boolean lore, String prefix, String suffix)
{
this.lore = lore;
this.prefix = ConfigFile.convertColor(prefix);
this.suffix = ConfigFile.convertColor(suffix);
}

@Override
public void apply(ChangeSession session)
{
NItem nbt = session.nbt();
if (lore)
{
List<Object> lines = nbt.getLoreCopy();
if (lines == null)
return;
for (int i = 0; i < lines.size(); i++)
lines.set(i, Comp.componentToNms(decorate(Comp.nmsToComponent(lines.get(i)), session)));
nbt.setLore(lines);
}
else
{
Object name = nbt.getCustomName();
if (name == null)
name = nbt.getItemName();
Component component = Comp.nmsToComponent(name);
if (component == null)
return;
nbt.setCustomName(Comp.componentToJson(decorate(component, session)));
}
nbt.save();
}

private Component decorate(Component original, ChangeSession session)
{
return decorate(original,
Comp.text(IChange.replacePlaceholders(session, prefix)),
Comp.text(IChange.replacePlaceholders(session, suffix)));
}

static Component decorate(Component original, Component prefix, Component suffix)
{
PlainTextComponentSerializer plain = PlainTextComponentSerializer.plainText();
String text = plain.serialize(original);
// ponytail: A naturally matching edge is treated as already added; use a persistent marker if provenance matters.
if (!text.startsWith(plain.serialize(prefix)))
original = prefix.append(original);
if (!text.endsWith(plain.serialize(suffix)))
original = original.append(suffix);
return original;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,26 @@
import org.bukkit.ChatColor;
import org.bukkit.inventory.ItemStack;

import java.util.ArrayList;
import java.util.List;
import java.util.function.DoublePredicate;

public class RuleNbtMatcher implements IRule
{
final String nbtPath;
Object nbtValue;
final Object nbtValue;
final List<Object> values = new ArrayList<>();
final List<DoublePredicate> bounds = new ArrayList<>();

String[] nbtPathSplit;

NBTType nbtValueType;

public RuleNbtMatcher(String nbtPath, String nbtValueStr, String nbtValueTypeStr)
public RuleNbtMatcher(String nbtPath, Object nbtValue, String nbtValueTypeStr)
{
if (nbtPath == null || nbtPath.isBlank() || nbtValue == null)
throw new IllegalArgumentException("nbt.path and nbt.value are required.");
this.nbtPath = nbtPath;
this.nbtValue = nbtValueStr;

String nbtTypeFixed = "NBTTag" + StringUtils.capitalize(nbtValueTypeStr.toLowerCase());

Expand All @@ -34,26 +41,39 @@ public RuleNbtMatcher(String nbtPath, String nbtValueStr, String nbtValueTypeStr
ChatColor.GRAY + " Allowed: string, int, float, double, byte, short");
}

switch (nbtValueType)
List<?> entries = nbtValue instanceof List<?> ? (List<?>) nbtValue : List.of(nbtValue);
if (entries.isEmpty())
throw new IllegalArgumentException("nbt.value must contain at least one value for '" + nbtPath + "'.");
for (Object entry : entries)
{
case NBTTagString:
break;
case NBTTagInt:
nbtValue = Integer.parseInt(nbtValueStr);
break;
case NBTTagFloat:
nbtValue = Float.parseFloat(nbtValueStr);
break;
case NBTTagDouble:
nbtValue = Double.parseDouble(nbtValueStr);
break;
case NBTTagByte:
nbtValue = Byte.parseByte(nbtValueStr);
break;
case NBTTagShort:
nbtValue = Short.parseShort(nbtValueStr);
break;
String value = String.valueOf(entry).trim();
if (nbtValueType != NBTType.NBTTagString && value.matches("^(>=|<=|>|<).+"))
{
int operatorLength = value.charAt(1) == '=' ? 2 : 1;
String operator = value.substring(0, operatorLength);
double threshold = Double.parseDouble(value.substring(operatorLength).trim());
bounds.add(actual -> switch (operator)
{
case ">=" -> actual >= threshold;
case "<=" -> actual <= threshold;
case ">" -> actual > threshold;
default -> actual < threshold;
});
}
else
{
values.add(switch (nbtValueType)
{
case NBTTagInt -> Integer.parseInt(value);
case NBTTagFloat -> Float.parseFloat(value);
case NBTTagDouble -> Double.parseDouble(value);
case NBTTagByte -> Byte.parseByte(value);
case NBTTagShort -> Short.parseShort(value);
default -> String.valueOf(entry);
});
}
}
this.nbtValue = nbtValue instanceof List<?> || values.isEmpty() ? nbtValue : values.get(0);

this.nbtPathSplit = nbtPath.split("\\.");
}
Expand All @@ -72,41 +92,49 @@ public Object getNbtValue()
public boolean matches(ItemStack item)
{
NBTItem nbt = new NBTItem(item);
if (!nbt.hasTag(nbtPathSplit[0]))
return false;

NBTCompound currentCompound = nbt.getCompound(nbtPathSplit[0]);
if(currentCompound == null)
return false;
for (int i = 1; i < nbtPathSplit.length - 1; i++)
NBTCompound currentCompound = nbt;
for (int i = 0; i < nbtPathSplit.length - 1; i++)
{
assert currentCompound != null;
if (!currentCompound.hasTag(nbtPathSplit[i]))
return false;
currentCompound = currentCompound.getCompound(nbtPathSplit[i]);
if (currentCompound == null)
return false;
}

assert currentCompound != null;
if (!currentCompound.hasTag(nbtPathSplit[nbtPathSplit.length - 1]))
return false;

NBTType nbtType = currentCompound.getType(nbtPathSplit[nbtPathSplit.length - 1]);
if (nbtType != nbtValueType)
return false;

if (nbtType == NBTType.NBTTagString)
return (currentCompound.getString(nbtPathSplit[nbtPathSplit.length - 1]).equals(nbtValue));
else if (nbtType == NBTType.NBTTagInt)
return (currentCompound.getInteger(nbtPathSplit[nbtPathSplit.length - 1]).equals(nbtValue));
else if (nbtType == NBTType.NBTTagDouble)
return (currentCompound.getDouble(nbtPathSplit[nbtPathSplit.length - 1]).equals(nbtValue));
else if (nbtType == NBTType.NBTTagFloat)
return (currentCompound.getFloat(nbtPathSplit[nbtPathSplit.length - 1]).equals(nbtValue));
else if (nbtType == NBTType.NBTTagByte)
return (currentCompound.getByte(nbtPathSplit[nbtPathSplit.length - 1]).equals(nbtValue));
else if (nbtType == NBTType.NBTTagShort)
return (currentCompound.getShort(nbtPathSplit[nbtPathSplit.length - 1]).equals(nbtValue));

return false;
Object actual = switch (nbtType)
{
case NBTTagString -> currentCompound.getString(nbtPathSplit[nbtPathSplit.length - 1]);
case NBTTagInt -> currentCompound.getInteger(nbtPathSplit[nbtPathSplit.length - 1]);
case NBTTagDouble -> currentCompound.getDouble(nbtPathSplit[nbtPathSplit.length - 1]);
case NBTTagFloat -> currentCompound.getFloat(nbtPathSplit[nbtPathSplit.length - 1]);
case NBTTagByte -> currentCompound.getByte(nbtPathSplit[nbtPathSplit.length - 1]);
case NBTTagShort -> currentCompound.getShort(nbtPathSplit[nbtPathSplit.length - 1]);
default -> null;
};
return matchesValue(actual);
}

boolean matchesValue(Object actual)
{
if (actual == null || (!values.isEmpty() && !values.contains(actual)))
return false;
if (!bounds.isEmpty())
{
if (!(actual instanceof Number))
return false;
double number = ((Number) actual).doubleValue();
for (DoublePredicate bound : bounds)
if (!bound.test(number))
return false;
}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package dev.lone.vanillacustomizer.nms;

import beer.devs.fastnbt.nms.Version;
import dev.lone.vanillacustomizer.Main;
import dev.lone.vanillacustomizer.utils.Msg;
import org.bukkit.Bukkit;
import org.jetbrains.annotations.Nullable;

import java.lang.reflect.InvocationTargetException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Utility to initialize NMS wrappers and avoid Maven circular dependency problems.
*/
public class NMS
{
private static final Pattern MINECRAFT_VERSION_PATTERN = Pattern.compile("\\(MC:\\s*([0-9]+(?:\\.[0-9]+)+)\\)");

/**
* Gets a suitable implementaion for the current Minecraft server version.
*
Expand All @@ -26,7 +29,10 @@ public class NMS
@SuppressWarnings("unchecked")
public static <T> T findImplementation(@SuppressWarnings("unused") Class<T> implClazz, Object nmsHolder, boolean ignoreError)
{
String nmsVersion = Version.get().name();
Matcher versionMatcher = MINECRAFT_VERSION_PATTERN.matcher(Bukkit.getServer().getVersion());
String nmsVersion = versionMatcher.find()
? "v" + versionMatcher.group(1).replace('.', '_')
: "UNKNOWN";

try
{
Expand Down
2 changes: 1 addition & 1 deletion VanillaCustomizer-core/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: VanillaCustomizer
version: 0.3
version: 0.5
main: dev.lone.vanillacustomizer.Main
api-version: 1.13
commands:
Expand Down
Loading