wip: added persistent data tags on items

This commit is contained in:
2024-09-03 21:35:23 +02:00
parent e35145f8ed
commit 4f4a6aef10
6 changed files with 52 additions and 53 deletions

View File

@@ -11,30 +11,52 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Recipe;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
public class PixelBlockItem {
public static UUID unusedBlockID = UUID.fromString("98fdf0ae-c3ab-4ef7-ae25-efd518d600de");
public static final String itemTexture = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvYzE5NGU5ZTc3NTdkMDZkNmY1ZTViZTg0NTQ4YTdjYjUyMTczZDY4Y2NmODAyZDIxMTI3NWQzMWNkYmEwYTA2ZSJ9fX0=";
public static final NamespacedKey recipeKey = new NamespacedKey(PixelBlocksPlugin.plugin, "pixelblock");
public static NamespacedKey idProperty = new NamespacedKey(PixelBlocksPlugin.plugin, "id");
public static NamespacedKey ownerProperty = new NamespacedKey(PixelBlocksPlugin.plugin, "owner");
public static final String itemTexture = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvYzE5NGU5ZTc3NTdkMDZkNmY1ZTViZTg0NTQ4YTdjYjUyMTczZDY4Y2NmODAyZDIxMTI3NWQzMWNkYmEwYTA2ZSJ9fX0=";
public record BlockInfo(UUID id, @Nullable UUID owner) {
public boolean hasOwner() {
return owner != null;
}
}
public static @Nullable BlockInfo getBlockInfo(ItemStack item) {
PersistentDataContainer container = item.getItemMeta().getPersistentDataContainer();
if(!container.has(idProperty)) return null;
UUID blockId = UUID.fromString(Objects.requireNonNull(container.get(idProperty, PersistentDataType.STRING)));
UUID ownerId = container.has(ownerProperty)
? UUID.fromString(Objects.requireNonNull(container.get(ownerProperty, PersistentDataType.STRING)))
: null;
return new BlockInfo(blockId, ownerId);
}
public static @NotNull ItemStack getBlockAsItem(@NotNull PixelBlock block) {
String ownerName = Optional.ofNullable(Bukkit.getOfflinePlayer(block.ownerUUID).getName()).orElseGet(() -> block.ownerUUID.toString());
ItemStack itemStack = HeadUtil.getCustomTextureHead(itemTexture);
ItemMeta meta = itemStack.getItemMeta();
meta.itemName(Component.text(block.blockUUID.toString()));
meta.setMaxStackSize(1);
meta.getPersistentDataContainer().set(idProperty, PersistentDataType.STRING, block.blockUUID.toString());
meta.getPersistentDataContainer().set(ownerProperty, PersistentDataType.STRING, block.ownerUUID.toString());
meta.displayName(Component.text("Pixelblock von " + ownerName));
meta.lore(List.of(
Component.text(ownerName + " ist der Besitzer dieses Blocks."),
Component.text("Klicke auf den gesetzten Block, um diesen zu bearbeiten!"),
Component.text(block.blockUUID.toString()).color(NamedTextColor.DARK_GRAY)
));
meta.setEnchantmentGlintOverride(true);
itemStack.setItemMeta(meta);
return itemStack;
@@ -43,13 +65,13 @@ public class PixelBlockItem {
public static @NotNull ItemStack getEmptyPixelBlock() {
ItemStack item = HeadUtil.getCustomTextureHead(itemTexture);
ItemMeta meta = item.getItemMeta();
meta.setMaxStackSize(1);
meta.displayName(Component.text("Leerer Pixelblock"));
meta.lore(List.of(
Component.text("Der erste Spieler, der den Block platziert wird zum Besitzer des Blocks."),
Component.text("Klicke auf den gesetzten Block, um diesen zu bearbeiten!")
));
meta.setEnchantmentGlintOverride(true);
meta.itemName(Component.text(unusedBlockID.toString()));
meta.getPersistentDataContainer().set(idProperty, PersistentDataType.STRING, UUID.randomUUID().toString());
item.setItemMeta(meta);
return item;
}
@@ -61,7 +83,7 @@ public class PixelBlockItem {
recipe.setIngredient('B', Material.DIAMOND_BLOCK);
recipe.setIngredient('C', Material.HEART_OF_THE_SEA);
recipe.setIngredient('D', Material.GRASS_BLOCK);
recipe.setIngredient('E', Material.GLOW_BERRIES);
recipe.setIngredient('E', Material.END_CRYSTAL);
return recipe;
}
}