Files
src
build.gradle
Code
8 483
src/main/java/com/kodari/backpacks/TorsoBackpacksPlugin.java
package com.kodari.backpacks;
import com.cryptomorin.xseries.XMaterial;
import org.bukkit.ChatColor;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.Arrays;
import java.util.UUID;
public final class TorsoBackpacksPlugin extends JavaPlugin {
private NamespacedKey typeKey;
private NamespacedKey idKey;
private BackpackStorage storage;
@Override
public void onEnable() {
typeKey = new NamespacedKey(this, "backpack_type");
idKey = new NamespacedKey(this, "backpack_id");
storage = new BackpackStorage(this);
getServer().getPluginManager().registerEvents(new BackpackListener(this), this);
BackpackCommand command = new BackpackCommand(this);
getCommand("plecak").setExecutor(command);
getCommand("plecak").setTabCompleter(command);
}
@Override
public void onDisable() {
if (storage != null) {
storage.saveActiveInventories();
}
}
public BackpackStorage getStorage() {
return storage;
}
public ItemStack createBackpack(BackpackType type) {
ItemStack item = XMaterial.matchXMaterial(type.getMaterialName())
.map(XMaterial::parseItem)
.orElse(null);
if (item == null) {
return null;
}
ItemMeta meta = item.getItemMeta();
if (meta == null) {
return null;
}
meta.setDisplayName(ChatColor.GOLD + type.getDisplayName());
meta.setLore(Arrays.asList(
ChatColor.GRAY + "Pojemność: " + type.getSize() + " miejsc",
ChatColor.YELLOW + "Kliknij prawym przyciskiem, aby założyć.",
ChatColor.YELLOW + "Kliknij prawym przyciskiem, mając go na torsie, aby otworzyć."
));
meta.getPersistentDataContainer().set(typeKey, PersistentDataType.STRING, type.name());
meta.getPersistentDataContainer().set(idKey, PersistentDataType.STRING, UUID.randomUUID().toString());
item.setItemMeta(meta);
return item;
}
public BackpackType getBackpackType(ItemStack item) {
if (item == null || !item.hasItemMeta()) {
return null;
}
ItemMeta meta = item.getItemMeta();
if (meta == null || getBackpackId(item) == null) {
return null;
}
String name = meta.getPersistentDataContainer().get(typeKey, PersistentDataType.STRING);
if (name == null) {
return null;
}
try {
return BackpackType.valueOf(name);
} catch (IllegalArgumentException exception) {
return null;
}
}
public UUID getBackpackId(ItemStack item) {
if (item == null || !item.hasItemMeta()) {
return null;
}
ItemMeta meta = item.getItemMeta();
if (meta == null) {
return null;
}
String id = meta.getPersistentDataContainer().get(idKey, PersistentDataType.STRING);
if (id == null) {
return null;
}
try {
return UUID.fromString(id);
} catch (IllegalArgumentException exception) {
return null;
}
}
}