95 lines
4.3 KiB
Java
95 lines
4.3 KiB
Java
package eu.mhsl.minecraft.pixelblocks;
|
|
|
|
import eu.mhsl.minecraft.pixelblocks.pixelblock.PixelBlock;
|
|
import eu.mhsl.minecraft.pixelblocks.utils.HeadUtil;
|
|
import net.kyori.adventure.text.Component;
|
|
import net.kyori.adventure.text.format.NamedTextColor;
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.Material;
|
|
import org.bukkit.NamespacedKey;
|
|
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 final NamespacedKey recipeKey = new NamespacedKey(Main.plugin(), "pixelblock");
|
|
public static NamespacedKey idProperty = new NamespacedKey(Main.plugin(), "id");
|
|
public static NamespacedKey ownerProperty = new NamespacedKey(Main.plugin(), "owner");
|
|
public static UUID emptyBlockUUID = UUID.fromString("98fdf0ae-c3ab-4ef7-ae25-efd518d600de");
|
|
|
|
public static final String itemTexture = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQ" +
|
|
"ubmV0L3RleHR1cmUvYzE5NGU5ZTc3NTdkMDZkNmY1ZTViZTg0NTQ4YTdjYjUyMTczZDY4Y2NmODAyZDIxMTI3NWQzMWNkYmEwYTA2ZSJ9fX0=";
|
|
|
|
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.getOwnerUUID()).getName()).orElseGet(() -> block.getOwnerUUID().toString());
|
|
|
|
ItemStack itemStack = HeadUtil.getCustomTextureHead(itemTexture);
|
|
ItemMeta meta = itemStack.getItemMeta();
|
|
meta.setMaxStackSize(1);
|
|
meta.getPersistentDataContainer().set(idProperty, PersistentDataType.STRING, block.getBlockUUID().toString());
|
|
meta.getPersistentDataContainer().set(ownerProperty, PersistentDataType.STRING, block.getOwnerUUID().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.getBlockUUID().toString()).color(NamedTextColor.DARK_GRAY)
|
|
));
|
|
itemStack.setItemMeta(meta);
|
|
|
|
return itemStack;
|
|
}
|
|
|
|
public static @NotNull ItemStack getEmptyPixelBlock() {
|
|
ItemStack item = HeadUtil.getCustomTextureHead(itemTexture);
|
|
ItemMeta meta = item.getItemMeta();
|
|
meta.setMaxStackSize(1);
|
|
meta.itemName(Component.text(emptyBlockUUID.toString()));
|
|
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.getPersistentDataContainer().set(idProperty, PersistentDataType.STRING, emptyBlockUUID.toString());
|
|
item.setItemMeta(meta);
|
|
return item;
|
|
}
|
|
|
|
public static @NotNull Recipe getRecipe() {
|
|
ShapedRecipe recipe = new ShapedRecipe(recipeKey, getEmptyPixelBlock());
|
|
recipe.shape("AEA", "BCB", "DDD");
|
|
recipe.setIngredient('A', Material.GLASS);
|
|
recipe.setIngredient('B', Material.DIAMOND_BLOCK);
|
|
recipe.setIngredient('C', Material.HEART_OF_THE_SEA);
|
|
recipe.setIngredient('D', Material.GRASS_BLOCK);
|
|
recipe.setIngredient('E', Material.END_CRYSTAL);
|
|
return recipe;
|
|
}
|
|
}
|
|
|