Implemented conversion between ItemStack and LinkedBeaconTeleporterItem

master
Ruakij 4 years ago
parent 48f961fc9c
commit 59e8768ddb

@ -1,8 +1,16 @@
package eu.ruekov.ruakij.LinkedBeaconTeleporters.linkedBeaconTeleporter; 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.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 { public class LinkedBeaconTeleporterItem extends LinkedBeaconTeleporter {
// INFO: Save to store as its always cloned by bukkit
ItemStack item; ItemStack item;
public LinkedBeaconTeleporterItem(String teleporterId, ItemStack item) { public LinkedBeaconTeleporterItem(String teleporterId, ItemStack item) {
@ -14,4 +22,13 @@ public class LinkedBeaconTeleporterItem extends LinkedBeaconTeleporter {
public ItemStack item(){ public ItemStack item(){
return item; return item;
} }
// Conversion methods
public ItemStack toItemStack(){
return Main.lbtManager.getItemStackFromLbtItem(this);
}
public static LinkedBeaconTeleporter getFromItemStack(ItemStack itemStack){
return Main.lbtManager.getLbtItemFromItemStack(itemStack);
}
} }

@ -19,9 +19,18 @@ public class LinkedBeaconTeleporterManager {
HashMap<String, List<LinkedBeaconTeleporterBlock>> linkedBeaconTeleporterById = new HashMap(); HashMap<String, List<LinkedBeaconTeleporterBlock>> linkedBeaconTeleporterById = new HashMap();
HashMap<String, LinkedBeaconTeleporterBlock> linkedBeaconTeleporterByLoc = new HashMap(); HashMap<String, LinkedBeaconTeleporterBlock> linkedBeaconTeleporterByLoc = new HashMap();
// Regex to match item-name&lore and extract id (group 1)
String regex;
public LinkedBeaconTeleporterManager(){ public LinkedBeaconTeleporterManager(){
Main.log.info("Loading LinkedBeaconTeleporter from config.."); 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 // Load teleporters
for(String teleporterId : Main.data.getKeys(false)){ for(String teleporterId : Main.data.getKeys(false)){
ConfigurationSection teleportersData = Main.data.getConfigurationSection(teleporterId); ConfigurationSection teleportersData = Main.data.getConfigurationSection(teleporterId);
@ -66,4 +75,71 @@ public class LinkedBeaconTeleporterManager {
Main.log.info("All done!"); 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<String> lore = Main.config.getStringList("item.lore");
// Replace parameters
name = name.replace("%id%", lbtItem.teleporterId);
for(int i=0; i<lore.size(); i++){
lore.set(i,
lore.get(i).replace("%id%", lbtItem.teleporterId)
);
}
// Set meta
itemMeta.setDisplayName(name);
itemMeta.setLore(lore);
return item;
}
public LinkedBeaconTeleporterItem getLbtItemFromItemStack(ItemStack itemStack){
// Serialize
String nameAndLore = serialiseBeaconItem(
itemStack.getI18NDisplayName(),
itemStack.getLore()
);
// Regex match
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(nameAndLore);
if(!matcher.find())
// No data
return null;
LinkedBeaconTeleporterItem lbtItem = new LinkedBeaconTeleporterItem(
matcher.group(1),
itemStack
);
return lbtItem;
}
String constructRegex(String name, List<String> 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<String> 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;
}
} }

Loading…
Cancel
Save