Skip to content

Commit

Permalink
Merge pull request #8 from TheNextLvl-net/minimessage
Browse files Browse the repository at this point in the history
minimessage support
  • Loading branch information
NonSwag committed Jun 27, 2024
2 parents dd37d51 + 5999d16 commit 70c0993
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 20 deletions.
27 changes: 12 additions & 15 deletions src/main/java/net/thenextlvl/tweaks/command/item/LoreCommand.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package net.thenextlvl.tweaks.command.item;

import lombok.RequiredArgsConstructor;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.thenextlvl.tweaks.TweaksPlugin;
import net.thenextlvl.tweaks.command.api.CommandInfo;
import net.thenextlvl.tweaks.command.api.CommandSenderException;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
Expand All @@ -29,7 +30,6 @@ public class LoreCommand implements TabExecutor {
private final TweaksPlugin plugin;

@Override
@SuppressWarnings("deprecation")
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player player)) throw new CommandSenderException();
if (args.length < 1) return false;
Expand All @@ -46,11 +46,10 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
return false;
}

String lore = String.join(" ", Arrays.copyOfRange(args, 1, args.length)).replace("\\t", " ");
String[] loreLines = lore.split("\\\\n");
for (int i = 0; i < loreLines.length; i++) {
loreLines[i] = ChatColor.translateAlternateColorCodes('&', loreLines[i]);
}
var lore = String.join(" ", Arrays.copyOfRange(args, 1, args.length)).replace("\\t", " ");
var loreLines = Arrays.stream(lore.split("\\\\n"))
.map(MiniMessage.miniMessage()::deserialize)
.toList();

Consumer<? super ItemMeta> function = null;

Expand Down Expand Up @@ -78,17 +77,15 @@ private static Consumer<ItemMeta> unsetLore() {
return meta -> meta.lore(null);
}

@SuppressWarnings("deprecation")
private Consumer<? super ItemMeta> setLore(String[] lore) {
return itemMeta -> itemMeta.setLore(Arrays.asList(lore));
private Consumer<? super ItemMeta> setLore(List<Component> lore) {
return itemMeta -> itemMeta.lore(lore);
}

@SuppressWarnings("deprecation")
private Consumer<? super ItemMeta> appendLore(String[] lore) {
private Consumer<? super ItemMeta> appendLore(List<Component> lore) {
return itemMeta -> {
var currentLore = Objects.requireNonNullElse(itemMeta.getLore(), new ArrayList<String>());
currentLore.addAll(Arrays.asList(lore));
itemMeta.setLore(currentLore);
var currentLore = Objects.requireNonNullElse(itemMeta.lore(), new ArrayList<Component>());
currentLore.addAll(lore);
itemMeta.lore(currentLore);
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package net.thenextlvl.tweaks.command.item;

import lombok.RequiredArgsConstructor;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.thenextlvl.tweaks.TweaksPlugin;
import net.thenextlvl.tweaks.command.api.CommandInfo;
import net.thenextlvl.tweaks.command.api.CommandSenderException;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
Expand All @@ -24,7 +24,6 @@ public class RenameCommand implements TabExecutor {
private final TweaksPlugin plugin;

@Override
@SuppressWarnings("deprecation")
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player player))
throw new CommandSenderException();
Expand All @@ -33,10 +32,9 @@ public boolean onCommand(CommandSender sender, Command command, String label, St

var item = player.getInventory().getItemInMainHand();

var name = ChatColor.translateAlternateColorCodes('&', String.join(" ", args))
.replace("\\t", " ");
var name = MiniMessage.miniMessage().deserialize(String.join(" ", args).replace("\\t", " "));

if (!item.editMeta(itemMeta -> itemMeta.setDisplayName(name))) {
if (!item.editMeta(itemMeta -> itemMeta.displayName(name))) {
plugin.bundle().sendMessage(player, "item.rename.fail");
return true;
}
Expand Down

0 comments on commit 70c0993

Please sign in to comment.