dev/rework
This commit is contained in:
@@ -1,16 +1,19 @@
|
||||
package com.alttd.playershops.utils;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.World;
|
||||
import com.alttd.playershops.shop.PlayerShop;
|
||||
import com.destroystokyo.paper.profile.PlayerProfile;
|
||||
import org.bukkit.*;
|
||||
import org.bukkit.configuration.InvalidConfigurationException;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
|
||||
import org.bukkit.inventory.meta.SkullMeta;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
public class ShopUtil {
|
||||
|
||||
@@ -106,18 +109,89 @@ public class ShopUtil {
|
||||
return space;
|
||||
}
|
||||
|
||||
public static String serialize(ItemStack itemStack) {
|
||||
YamlConfiguration cfg = new YamlConfiguration();
|
||||
cfg.set("item", itemStack);
|
||||
return cfg.saveToString();
|
||||
public static String locationToString(Location location) {
|
||||
return location.getWorld().getName() + ":" +
|
||||
AMath.round(location.getX(), 1) + ":" +
|
||||
AMath.round(location.getY(), 1) + ":" +
|
||||
AMath.round(location.getZ(), 1);
|
||||
}
|
||||
|
||||
public static ItemStack deserialize(String config) throws InvalidConfigurationException {
|
||||
YamlConfiguration cfg = new YamlConfiguration();
|
||||
cfg.loadFromString(config);
|
||||
ItemStack stack = cfg.getItemStack("item");
|
||||
return stack;
|
||||
public static Location stringToLocation(String string) {
|
||||
String[] split = string.split(":");
|
||||
if (split.length != 4) {
|
||||
Logger.warn("Unable to load location [" + string + "] due to invalid format");
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
return new Location(Bukkit.getWorld(split[0]),
|
||||
Double.parseDouble(split[1]), Double.parseDouble(split[2]), Double.parseDouble(split[3]));
|
||||
} catch (NumberFormatException e) {
|
||||
Logger.warn("Unable to load location [" + string + "] due to invalid format");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static ItemStack getPlayerHead(UUID uuid) {
|
||||
ItemStack skull = new ItemStack(Material.PLAYER_HEAD);
|
||||
|
||||
Player player = Bukkit.getPlayer(uuid);
|
||||
if (player == null) return skull;
|
||||
|
||||
// TODO add skins to skulls and name them.
|
||||
SkullMeta meta = (SkullMeta) skull.getItemMeta();
|
||||
meta.setPlayerProfile(player.getPlayerProfile());
|
||||
skull.setItemMeta(meta);
|
||||
|
||||
return skull;
|
||||
}
|
||||
|
||||
// TODO upgrade this to an util method check if owner/trusted and open management interface
|
||||
public static boolean canManageShop(Player player, PlayerShop playerShop) {
|
||||
if (playerShop.getOwnerUUID().equals(player.getUniqueId())) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static String getItemName(ItemStack item) {
|
||||
if (item == null || !item.getType().equals(Material.AIR))
|
||||
return "Nothing";
|
||||
|
||||
String NAME = "{item}";
|
||||
boolean dname = item.hasItemMeta() && item.getItemMeta().hasDisplayName();
|
||||
String replacer = ChatColor.translateAlternateColorCodes('&',NAME+"&r");
|
||||
if (dname) {
|
||||
String trp = item.getItemMeta().getDisplayName();
|
||||
replacer = replacer.replace(NAME, trp);
|
||||
} else {
|
||||
replacer = replacer.replace(NAME, materialToName(item.getType()));
|
||||
}
|
||||
return replacer;
|
||||
}
|
||||
|
||||
private static String materialToName(Material m) {
|
||||
if (m.equals(Material.TNT)) {
|
||||
return "TNT";
|
||||
}
|
||||
String orig = m.toString().toLowerCase();
|
||||
String[] splits = orig.split("_");
|
||||
StringBuilder sb = new StringBuilder(orig.length());
|
||||
int pos = 0;
|
||||
for (String split : splits) {
|
||||
sb.append(split);
|
||||
int loc = sb.lastIndexOf(split);
|
||||
char charLoc = sb.charAt(loc);
|
||||
if (!(split.equalsIgnoreCase("of") || split.equalsIgnoreCase("and") ||
|
||||
split.equalsIgnoreCase("with") || split.equalsIgnoreCase("on")))
|
||||
sb.setCharAt(loc, Character.toUpperCase(charLoc));
|
||||
if (pos != splits.length - 1)
|
||||
sb.append(' ');
|
||||
++pos;
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user