Skip to content

Commit

Permalink
Add ItemName Minimassage Support
Browse files Browse the repository at this point in the history
  • Loading branch information
Ste3et committed Dec 31, 2023
1 parent a72fc52 commit 6c22c51
Show file tree
Hide file tree
Showing 23 changed files with 252 additions and 34 deletions.
Binary file modified .gradle/7.2/executionHistory/executionHistory.bin
Binary file not shown.
Binary file modified .gradle/7.2/executionHistory/executionHistory.lock
Binary file not shown.
Binary file modified .gradle/7.2/fileHashes/fileHashes.bin
Binary file not shown.
Binary file modified .gradle/7.2/fileHashes/fileHashes.lock
Binary file not shown.
Binary file modified .gradle/7.2/fileHashes/resourceHashesCache.bin
Binary file not shown.
Binary file modified .gradle/buildOutputCleanup/buildOutputCleanup.lock
Binary file not shown.
Binary file modified .gradle/checksums/checksums.lock
Binary file not shown.
Binary file modified .gradle/checksums/md5-checksums.bin
Binary file not shown.
Binary file modified .gradle/checksums/sha1-checksums.bin
Binary file not shown.
@@ -1,13 +1,15 @@
package de.Ste3et_C0st.FurnitureLib.Command;

import java.util.Arrays;
import java.util.Optional;
import java.util.stream.Collectors;

import org.bukkit.command.CommandSender;

import de.Ste3et_C0st.FurnitureLib.Crafting.Project;
import de.Ste3et_C0st.FurnitureLib.Utilitis.StringTranslator;
import de.Ste3et_C0st.FurnitureLib.main.FurnitureLib;
import net.md_5.bungee.api.ChatColor;
import net.kyori.adventure.text.Component;

public class setName extends iCommand {

Expand All @@ -24,9 +26,10 @@ public void execute(CommandSender sender, String[] args) {
if(projectOpt.isPresent()) {
final Project project = projectOpt.get();
if(args.length > 2) {
final String name = ChatColor.translateAlternateColorCodes('&', args[2]);
project.getCraftingFile().setName(name);
getLHandler().sendMessage(sender, "command.setname.success", new StringTranslator("model", project.getName()), new StringTranslator("name", ChatColor.stripColor(name)));
final String name = Arrays.asList(args).stream().skip(2).collect(Collectors.joining(" "));
final Component component = getLHandler().stringConvert(name);
project.getCraftingFile().setName(component);
getLHandler().sendMessage(sender, "command.setname.success", new StringTranslator("model", project.getName()), new StringTranslator("name", component));
return;
}
}else {
Expand Down
Expand Up @@ -5,10 +5,15 @@
import com.google.gson.JsonParser;

import de.Ste3et_C0st.FurnitureLib.Utilitis.HiddenStringUtils;
import de.Ste3et_C0st.FurnitureLib.Utilitis.LanguageManager;
import de.Ste3et_C0st.FurnitureLib.Utilitis.MaterialConverter;
import de.Ste3et_C0st.FurnitureLib.Utilitis.SchedularHelper;
import de.Ste3et_C0st.FurnitureLib.main.FurnitureLib;
import de.Ste3et_C0st.FurnitureLib.main.Type.PlaceableSide;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.bungeecord.BungeeComponentSerializer;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
Expand Down Expand Up @@ -114,13 +119,10 @@ public String getHeader(final YamlConfiguration configuration) {
}
}

public void rename(String name) {
if (name == null || name.equalsIgnoreCase(""))
return;
public void rename(Component component) {
if (Objects.isNull(component)) return;
ItemStack stack = getRecipe().getResult();
ItemMeta meta = stack.getItemMeta();
meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', name));
stack.setItemMeta(meta);
FurnitureLib.getInstance().getServerFunction().displayName(stack, BungeeComponentSerializer.get().serialize(component));
}


Expand Down Expand Up @@ -186,19 +188,27 @@ public PlaceableSide getPlaceAbleSide(YamlConfiguration configuration) {
this.side = PlaceableSide.valueOf(configuration.getString(header + ".PlaceAbleSide", "TOP").toUpperCase());
return this.side;
}

public void saveName(Component component) {
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(this.getFilePath());
configuration.set(header + ".displayName", MiniMessage.miniMessage().serialize(component));
try {
configuration.save(this.getFilePath());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void setName(String s) {
ItemStack is = getRecipe().getResult();
ItemMeta im = is.getItemMeta();
im.setDisplayName(s);
is.setItemMeta(im);

public void setName(Component component) {
ItemStack stack = getRecipe().getResult();
FurnitureLib.getInstance().getServerFunction().displayName(stack, BungeeComponentSerializer.get().serialize(component));
org.bukkit.NamespacedKey key = new org.bukkit.NamespacedKey(FurnitureLib.getInstance(), this.name.toLowerCase());
ShapedRecipe recipe = new ShapedRecipe(key, is).shape(this.getRecipe().getShape());
this.recipe.getIngredientMap().forEach((key1, value) -> recipe.setIngredient(key1, value.getData()));
ShapedRecipe recipe = new ShapedRecipe(key, stack).shape(this.getRecipe().getShape());
this.recipe.getIngredientMap().entrySet().stream().filter(entry -> Objects.nonNull(entry.getValue())).forEach(entry -> recipe.setIngredient(entry.getKey(), entry.getValue().getData()));
this.recipe = recipe;
if (!isDisable)
Bukkit.getServer().addRecipe(this.recipe);
this.saveName(component);
if (!isDisable) Bukkit.getServer().addRecipe(this.recipe);
}

private ItemStack returnResult(final String name,final YamlConfiguration configuration) {
Expand Down Expand Up @@ -239,7 +249,9 @@ private ItemStack returnResult(final String name,final YamlConfiguration configu
mat = Material.getMaterial(str);
}
}
ItemStack is = new ItemStack(mat);

String displayName = configuration.getString(header + (FurnitureLib.isNewVersion() ? ".displayName" : ".name"), header);
ItemStack is = FurnitureLib.getInstance().getServerFunction().displayName(new ItemStack(mat), BungeeComponentSerializer.get().serialize(LanguageManager.getInstance().stringConvert(displayName)));
ItemMeta im = is.getItemMeta();
try {
if (configuration.contains(header + ".unbreakable")) {
Expand All @@ -249,10 +261,7 @@ private ItemStack returnResult(final String name,final YamlConfiguration configu
} catch (Exception e) {
e.printStackTrace();
}

String displayName = configuration.getString(header + (FurnitureLib.isNewVersion() ? ".displayName" : ".name"), header);
im.setDisplayName(ChatColor.translateAlternateColorCodes('&', displayName));


List<String> loreText = new ArrayList<String>();
if (im.hasLore()) loreText = im.getLore();

Expand Down
Expand Up @@ -12,6 +12,7 @@
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;
import org.checkerframework.checker.nullness.qual.NonNull;

import java.io.File;
import java.io.IOException;
Expand All @@ -20,6 +21,8 @@
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LanguageManager {

Expand Down Expand Up @@ -239,11 +242,45 @@ public void sendString(CommandSender sender, String key, StringTranslator ... st
sender.sendMessage(getString(key, stringTranslators));
}

public static final char COLOR_CHAR = '\u00A7';
public static final Pattern STRIP_COLOR_PATTERN = Pattern.compile( "(?i)" + String.valueOf( COLOR_CHAR ) + "[0-9A-FK-ORX]" );

public static void send(CommandSender sender, String key, StringTranslator ... stringTranslators) {
LanguageManager.getInstance().sendMessage(sender, key, stringTranslators);
}

public Component stringConvert(String value, StringTranslator ... stringTranslators) {
return stringConvert(value, Arrays.asList(stringTranslators));
}

public Component stringConvert(String value, List<StringTranslator> stringTranslaters) {
TagResolver[] tags = getTagsArray(stringTranslaters);
if(Objects.nonNull(tags)) {
return MiniMessage.miniMessage().deserialize(serializeLegacyColors(value), getTagsArray(stringTranslaters));
}else {
return MiniMessage.miniMessage().deserialize(serializeLegacyColors(value));
}
}

public static String serializeLegacyColors(String input) {
if(Objects.isNull(input)) return "";
if(input.isEmpty()) return "";
String output = ChatColor.translateAlternateColorCodes('&', input).replaceAll("§m", "<st>").replaceAll("§o", "<i>").replaceAll("§n", "<u>").replaceAll("§l", "<b>").replaceAll("§k", "<obf>");
Matcher matcher = STRIP_COLOR_PATTERN.matcher(output);

while (matcher.find()) {
String color = output.substring(matcher.start(), matcher.end());
ChatColor chatColor = ChatColor.getByChar(color.charAt(1));
if(Objects.isNull(chatColor)) continue;

String colorCode = "<" + chatColor.name().toLowerCase() + ">";
output = output.replaceAll(color, colorCode + "");
matcher = STRIP_COLOR_PATTERN.matcher(output);
}

return output;
}

public void sendMessage(CommandSender sender, String key, StringTranslator ... stringTranslators) {
final String rawString = this.placeholderAPI != null ? this.placeholderAPI.parsePlaceholders(LanguageConverter.serializeLegacyColors(this.getString(key, stringTranslators)), sender) : LanguageConverter.serializeLegacyColors(this.getString(key, stringTranslators));
final TagResolver[] tags = getTagsArray(Arrays.asList(stringTranslators));
Expand Down
@@ -0,0 +1,13 @@
package de.Ste3et_C0st.FurnitureLib.Utilitis;

import java.util.List;

import org.bukkit.inventory.ItemStack;

import net.md_5.bungee.api.chat.BaseComponent;

public interface ServerFunction {
public ItemStack displayName(ItemStack stack, BaseComponent[] baseComponent);
public ItemStack lore(ItemStack stack, List<BaseComponent[]> component);
public void onEnable();
}
@@ -0,0 +1,44 @@
package de.Ste3et_C0st.FurnitureLib.Utilitis;

import java.util.List;
import java.util.stream.Collectors;

import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import de.Ste3et_C0st.FurnitureLib.main.FurnitureLib;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.bungeecord.BungeeComponentSerializer;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.md_5.bungee.api.chat.BaseComponent;

public class SpigotFunctions implements ServerFunction{

@Override
public ItemStack displayName(ItemStack stack, BaseComponent[] component) {
ItemMeta meta = stack.getItemMeta();
meta.setDisplayName(convertToLegacy(BungeeComponentSerializer.get().deserialize(component)));
stack.setItemMeta(meta);
return stack;
}

@Override
public ItemStack lore(ItemStack stack, List<BaseComponent[]> component) {
List<String> legacyLore = component.stream().map(BungeeComponentSerializer.get()::deserialize).map(this::convertToLegacy).collect(Collectors.toList());
ItemMeta meta = stack.getItemMeta();
meta.setLore(legacyLore);
stack.setItemMeta(meta);
return stack;
}

private String convertToLegacy(Component component) {
return LegacyComponentSerializer.legacyAmpersand().serialize(component);
}

@Override
public void onEnable() {
FurnitureLib.debug("SpigotFunctions created");
}

}
@@ -1,21 +1,31 @@
package de.Ste3et_C0st.FurnitureLib.Utilitis;

import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;

public class StringTranslator {

private final String key, value;
private String key, value;
private Component component;

private static final Pattern VARIABLE_PATTERN_OLD = Pattern.compile("#[a-zA-Z0-9_]+#", Pattern.CASE_INSENSITIVE);
private static final Pattern VARIABLE_PATTERN_NEW = Pattern.compile("<[a-zA-Z0-9_]+>", Pattern.CASE_INSENSITIVE);

public StringTranslator(String key, String value) {
this.key = key.toLowerCase().replaceFirst("#", "").replace("#", "");
this.value = value;
}

public StringTranslator(String key, Component component) {
this.key = key.toLowerCase().replaceFirst("#", "").replace("#", "");
this.component = component;
}

public String getKey() {
return this.key;
Expand Down Expand Up @@ -53,6 +63,10 @@ public static String transfareVariable(String name) {
}

public TagResolver getPlaceHolder() {
return Placeholder.parsed(key, value);
return Placeholder.component(key, getComponent());
}

public Component getComponent() {
return Objects.nonNull(component) ? component : MiniMessage.miniMessage().deserialize(value);
}
}
Expand Up @@ -129,8 +129,6 @@ public void addTask(BukkitTask ... task) {
}
}



public void onClick(CallbackGUI click) {
this.callbackClick = click;
}
Expand Down
Expand Up @@ -35,7 +35,6 @@ public BlockManager() {

public void registerBlockEvents() {
if (listener.isEmpty()) {

if(FurnitureLib.isPaper()) {
isPaper = true;
try {
Expand Down
Expand Up @@ -87,6 +87,7 @@ public class FurnitureLib extends JavaPlugin {
private FurnitureConfig furnitureConfig;
private FurnitureProtocolListener furnitureProtocolListener;
private FloodgateManager floodgateManager = null;
private ServerFunction serverFunction = null;
private static boolean folia = false, paper = false;

static {
Expand Down Expand Up @@ -325,6 +326,7 @@ public void onEnable() {
this.enabledPlugin = true;
this.field = ProtocolFields.getField(getServer().getBukkitVersion());
this.lUtil = new LocationUtil();
this.loadServerFunctions();
this.manager = new FurnitureManager();
this.furnitureConfig.loadPluginConfig();
this.updater = new Updater();
Expand Down Expand Up @@ -364,7 +366,20 @@ public void onEnable() {
PluginCommand c = getCommand("furniture");
c.setExecutor(new command(this));
c.setTabCompleter(new TabCompleterHandler());

}

private void loadServerFunctions() {
try {
if(isPaper()) {
this.serverFunction = (ServerFunction) Class.forName("de.Ste3et_C0st.FurnitureLib.Paper.PaperFunctions").newInstance();
}else if(isFolia()) {
this.serverFunction = (ServerFunction) Class.forName("de.Ste3et_C0st.FurnitureLib.Folia.FoliaFunctions").newInstance();
}
}catch (Exception excpetion) {
excpetion.printStackTrace();
}
if(this.serverFunction == null) this.serverFunction = new SpigotFunctions();
this.serverFunction.onEnable();
}

private void registerEvents() {
Expand Down Expand Up @@ -548,4 +563,8 @@ public FurnitureConfig getFurnitureConfig() {
public static boolean getVersion(MinecraftVersion minecraftVersion) {
return minecraftVersion.atOrAbove();
}

public ServerFunction getServerFunction() {
return this.serverFunction;
}
}
@@ -0,0 +1,29 @@
package de.Ste3et_C0st.FurnitureLib.Folia;

import java.util.List;
import org.bukkit.inventory.ItemStack;

import de.Ste3et_C0st.FurnitureLib.Utilitis.ServerFunction;
import de.Ste3et_C0st.FurnitureLib.main.FurnitureLib;
import net.md_5.bungee.api.chat.BaseComponent;

public class FoliaFunctions implements ServerFunction{

@Override
public ItemStack displayName(ItemStack stack, BaseComponent[] baseComponents) {
stack.editMeta(meta -> meta.setDisplayNameComponent(baseComponents));
return stack;
}

@Override
public ItemStack lore(ItemStack stack, List<BaseComponent[]> baseComponents) {
stack.editMeta(meta -> meta.setLoreComponents(baseComponents));
return stack;
}

@Override
public void onEnable() {
FurnitureLib.debug("FoliaFunctions created");
}

}

0 comments on commit 6c22c51

Please sign in to comment.