Skip to content

Commit

Permalink
expand StringReplacer replace methods to be used in Item
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed Mar 7, 2024
1 parent c26ab29 commit 6de5d51
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class AmountModifier implements ItemComparator<ItemStack>, ItemModifier<I
@Override
public @NotNull ItemStack modify(@NotNull ItemStack original, UUID uuid, @NotNull StringReplacer stringReplacer) {
Validate
.getNumber(stringReplacer.tryReplace(amount, uuid))
.getNumber(stringReplacer.replaceOrOriginal(amount, uuid))
.ifPresent(bigDecimal -> original.setAmount(bigDecimal.intValue()));
return original;
}
Expand All @@ -42,7 +42,7 @@ public boolean loadFromItem(ItemStack item) {

@Override
public boolean compare(@NotNull ItemStack item, UUID uuid, @NotNull StringReplacer stringReplacer) {
return Validate.getNumber(stringReplacer.tryReplace(amount, uuid))
return Validate.getNumber(stringReplacer.replaceOrOriginal(amount, uuid))
.map(bigDecimal -> bigDecimal.intValue() >= item.getAmount())
.orElse(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class DurabilityModifier implements ItemComparator<ItemStack>, ItemModifi
@Override
public @NotNull ItemStack modify(@NotNull ItemStack original, UUID uuid, @NotNull StringReplacer stringReplacer) {
Validate
.getNumber(stringReplacer.tryReplace(durability, uuid))
.getNumber(stringReplacer.replaceOrOriginal(durability, uuid))
.ifPresent(bigDecimal -> original.setDurability(bigDecimal.shortValue()));
return original;
}
Expand All @@ -43,7 +43,7 @@ public boolean loadFromItem(ItemStack item) {

@Override
public boolean compare(@NotNull ItemStack item, UUID uuid, @NotNull StringReplacer stringReplacer) {
return Validate.getNumber(stringReplacer.tryReplace(durability, uuid))
return Validate.getNumber(stringReplacer.replaceOrOriginal(durability, uuid))
.map(bigDecimal -> bigDecimal.shortValue() == item.getDurability())
.orElse(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private static String normalizeEnchantmentName(String name) {
private Map<Enchantment, Integer> getParsed(UUID uuid, StringReplacer stringReplacer) {
Map<Enchantment, Integer> enchantments = new LinkedHashMap<>();
for (String string : enchantmentList) {
String replaced = stringReplacer.tryReplace(string, uuid);
String replaced = stringReplacer.replaceOrOriginal(string, uuid);
String[] split;
if (replaced.indexOf(',') != -1) {
split = replaced.split(",", 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class ItemFlagModifier implements ItemMetaModifier, ItemMetaComparator {
private Set<ItemFlag> getParsed(UUID uuid, StringReplacer stringReplacer) {
Set<ItemFlag> flags = new HashSet<>();
flagList.forEach(string -> {
string = stringReplacer.tryReplace(string, uuid).trim();
string = stringReplacer.replaceOrOriginal(string, uuid).trim();
if (string.equalsIgnoreCase("all")) {
Collections.addAll(flags, ItemFlag.values());
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class LoreModifier implements ItemMetaModifier, ItemMetaComparator {

private List<String> getReplacedLore(UUID uuid, StringReplacer stringReplacer) {
List<String> replacedLore = new ArrayList<>(lore);
replacedLore.replaceAll(s -> stringReplacer.tryReplace(s, uuid));
replacedLore.replaceAll(s -> stringReplacer.replaceOrOriginal(s, uuid));
return replacedLore;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private static boolean compareMaterial(ItemStack itemStack, String materialStrin
@Override
public @NotNull ItemStack modify(@NotNull ItemStack original, UUID uuid, @NotNull StringReplacer stringReplacer) {
for (String materialString : materialList) {
if (setMaterial(original, stringReplacer.tryReplace(materialString, uuid))) {
if (setMaterial(original, stringReplacer.replaceOrOriginal(materialString, uuid))) {
break;
}
}
Expand Down Expand Up @@ -115,7 +115,7 @@ public boolean compare(@NotNull ItemStack item, UUID uuid, @NotNull StringReplac
return true;
}
return materialList.parallelStream()
.map(s -> stringReplacer.tryReplace(s, uuid))
.map(s -> stringReplacer.replaceOrOriginal(s, uuid))
.anyMatch(s -> compareMaterial(item, s));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public NBTModifier setNbtData(String nbtData) {
return original;
}
try {
return Bukkit.getUnsafe().modifyItemStack(original, stringReplacer.tryReplace(nbtData, uuid));
return Bukkit.getUnsafe().modifyItemStack(original, stringReplacer.replaceOrOriginal(nbtData, uuid));
} catch (Throwable throwable) {
return original;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public NameModifier setName(String name) {
@Override
public @NotNull ItemMeta modifyMeta(@NotNull ItemMeta meta, @Nullable UUID uuid, @NotNull StringReplacer stringReplacer) {
if (this.name != null) {
meta.setDisplayName(stringReplacer.tryReplace(name, uuid));
meta.setDisplayName(stringReplacer.replaceOrOriginal(name, uuid));
}
return meta;
}
Expand All @@ -49,7 +49,7 @@ public boolean compare(@NotNull ItemMeta meta, @Nullable UUID uuid, @NotNull Str
if (!meta.hasDisplayName() && this.name == null) {
return true;
}
String replaced = this.name == null ? "" : stringReplacer.tryReplace(this.name, uuid);
String replaced = this.name == null ? "" : stringReplacer.replaceOrOriginal(this.name, uuid);
return replaced.equals(meta.getDisplayName());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private static String serializePotionEffect(PotionEffect potionEffect) {

private List<PotionEffect> getParsed(UUID uuid, StringReplacer stringReplacer) {
List<String> list = new ArrayList<>(potionEffectList);
list.replaceAll(s -> stringReplacer.tryReplace(s, uuid));
list.replaceAll(s -> stringReplacer.replaceOrOriginal(s, uuid));
return list.stream()
.map(PotionEffectModifier::pastePotionEffect)
.flatMap(optional -> optional.map(Stream::of).orElseGet(Stream::empty))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private static SkullMeta getSkullMeta(String skull) {
if (!(meta instanceof SkullMeta)) {
return meta;
}
setSkull((SkullMeta) meta, stringReplacer.tryReplace(skullString, uuid));
setSkull((SkullMeta) meta, stringReplacer.replaceOrOriginal(skullString, uuid));
return meta;
}

Expand All @@ -94,7 +94,7 @@ public boolean compare(@NotNull ItemMeta meta, @Nullable UUID uuid, @NotNull Str
return false;
}
return skullHandler.compareSkull(
getSkullMeta(stringReplacer.tryReplace(skullString, uuid)),
getSkullMeta(stringReplacer.replaceOrOriginal(skullString, uuid)),
(SkullMeta) meta
);
}
Expand Down
37 changes: 27 additions & 10 deletions common/src/main/java/me/hsgamer/hscore/common/StringReplacer.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,35 @@ default String replace(@NotNull String original, @NotNull UUID uuid) {
* @param original the original string
* @param uuid the unique id
*
* @return the replaced string, or an empty string if the replaced string is null
* @return the replaced string
*/
@NotNull
@Nullable
default String tryReplace(@NotNull String original, @Nullable UUID uuid) {
if (uuid != null) {
String replaced = replace(original, uuid);
if (replaced != null) {
return replaced;
}
}
return uuid == null ? replace(original) : replace(original, uuid);
}

String replaced = replace(original);
return replaced != null ? replaced : "";
/**
* Replace a string or return the default value
*
* @param original the original string
* @param uuid the unique id
* @param def the default value
*
* @return the replaced string or the default value
*/
default String replaceOrDefault(@NotNull String original, @Nullable UUID uuid, @NotNull String def) {
String replaced = tryReplace(original, uuid);
return replaced != null ? replaced : def;
}

/**
* Replace a string or return the original value
*
* @param original the original string
*
* @return the replaced string or the original value
*/
default String replaceOrOriginal(@NotNull String original, @Nullable UUID uuid) {
return replaceOrDefault(original, uuid, original);
}
}

0 comments on commit 6de5d51

Please sign in to comment.