From 59e8768ddbc0042069dfa59b3d45ce761163d235 Mon Sep 17 00:00:00 2001 From: Ruakij Date: Sun, 9 May 2021 22:40:18 +0200 Subject: [PATCH] Implemented conversion between ItemStack and LinkedBeaconTeleporterItem --- .../LinkedBeaconTeleporterItem.java | 17 +++++ .../LinkedBeaconTeleporterManager.java | 76 +++++++++++++++++++ 2 files changed, 93 insertions(+) diff --git a/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/linkedBeaconTeleporter/LinkedBeaconTeleporterItem.java b/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/linkedBeaconTeleporter/LinkedBeaconTeleporterItem.java index a39edd9..064bf27 100644 --- a/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/linkedBeaconTeleporter/LinkedBeaconTeleporterItem.java +++ b/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/linkedBeaconTeleporter/LinkedBeaconTeleporterItem.java @@ -1,8 +1,16 @@ package eu.ruekov.ruakij.LinkedBeaconTeleporters.linkedBeaconTeleporter; +import eu.ruekov.ruakij.LinkedBeaconTeleporters.Main; +import org.bukkit.Material; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; + +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class LinkedBeaconTeleporterItem extends LinkedBeaconTeleporter { + // INFO: Save to store as its always cloned by bukkit ItemStack item; public LinkedBeaconTeleporterItem(String teleporterId, ItemStack item) { @@ -14,4 +22,13 @@ public class LinkedBeaconTeleporterItem extends LinkedBeaconTeleporter { public ItemStack item(){ return item; } + + + // Conversion methods + public ItemStack toItemStack(){ + return Main.lbtManager.getItemStackFromLbtItem(this); + } + public static LinkedBeaconTeleporter getFromItemStack(ItemStack itemStack){ + return Main.lbtManager.getLbtItemFromItemStack(itemStack); + } } diff --git a/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/linkedBeaconTeleporter/LinkedBeaconTeleporterManager.java b/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/linkedBeaconTeleporter/LinkedBeaconTeleporterManager.java index 3f31a55..b57bded 100644 --- a/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/linkedBeaconTeleporter/LinkedBeaconTeleporterManager.java +++ b/src/main/java/eu/ruekov/ruakij/LinkedBeaconTeleporters/linkedBeaconTeleporter/LinkedBeaconTeleporterManager.java @@ -19,9 +19,18 @@ public class LinkedBeaconTeleporterManager { HashMap> linkedBeaconTeleporterById = new HashMap(); HashMap linkedBeaconTeleporterByLoc = new HashMap(); + // Regex to match item-name&lore and extract id (group 1) + String regex; + public LinkedBeaconTeleporterManager(){ Main.log.info("Loading LinkedBeaconTeleporter from config.."); + // Construct search item-regex + regex = constructRegex( + Main.config.getString("item.name"), + Main.config.getStringList("item.lore") + ); + // Load teleporters for(String teleporterId : Main.data.getKeys(false)){ ConfigurationSection teleportersData = Main.data.getConfigurationSection(teleporterId); @@ -66,4 +75,71 @@ public class LinkedBeaconTeleporterManager { Main.log.info("All done!"); } + + // Conversion-methods + public ItemStack getItemStackFromLbtItem(LinkedBeaconTeleporterItem lbtItem){ + ItemStack item = new ItemStack(Material.BEACON); + ItemMeta itemMeta = item.getItemMeta(); + + // Get config-values + String name = Main.config.getString("item.name"); + List lore = Main.config.getStringList("item.lore"); + + // Replace parameters + name = name.replace("%id%", lbtItem.teleporterId); + for(int i=0; i lore){ + // Serialize + String regex = serialiseBeaconItem(name, lore); + + // Wrap in regex-quotes + regex = "\\Q"+ regex +"\\E"; + // Exclude id from regex-quotes and create group + regex = regex.replace("%id%", "\\E(.*)\\Q"); + + return regex; + } + String serialiseBeaconItem(String name, List lore){ + // get name and remove control-chars + String str = name.replace("\\", "\\\\"); + for (String loreLine : lore) { + // get lore-lines and remove control-chars + str += "\n"+ loreLine.replace("\\", "\\\\"); + } + + return str; + } }