From 340233b558c91675161ec237390db31260c36b5e Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 24 May 2021 11:20:08 +0200 Subject: [PATCH] Add ItemUtils Signed-off-by: yoyosource --- .../src/de/steamwar/bausystem/utils/.gitkeep | 0 .../steamwar/bausystem/utils/ItemUtils.java | 62 +++++++++++++++++++ 2 files changed, 62 insertions(+) delete mode 100644 BauSystem_Main/src/de/steamwar/bausystem/utils/.gitkeep create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/utils/ItemUtils.java diff --git a/BauSystem_Main/src/de/steamwar/bausystem/utils/.gitkeep b/BauSystem_Main/src/de/steamwar/bausystem/utils/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/BauSystem_Main/src/de/steamwar/bausystem/utils/ItemUtils.java b/BauSystem_Main/src/de/steamwar/bausystem/utils/ItemUtils.java new file mode 100644 index 00000000..3dba79d1 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/utils/ItemUtils.java @@ -0,0 +1,62 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bausystem.utils; + +import de.steamwar.bausystem.SWUtils; +import lombok.experimental.UtilityClass; +import org.bukkit.NamespacedKey; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.persistence.PersistentDataContainer; +import org.bukkit.persistence.PersistentDataType; + +@UtilityClass +public class ItemUtils { + + private final NamespacedKey ITEM_KEY = SWUtils.getNamespaceKey("bau_item"); + + public boolean isItem(ItemStack itemStack, String tag) { + if (itemStack == null) { + return false; + } + ItemMeta meta = itemStack.getItemMeta(); + if (meta == null) { + return false; + } + PersistentDataContainer container = meta.getPersistentDataContainer(); + if (!container.has(ITEM_KEY, PersistentDataType.STRING)) { + return false; + } + return tag.equals(container.get(ITEM_KEY, PersistentDataType.STRING)); + } + + public void setItem(ItemStack itemStack, String tag) { + if (itemStack == null) { + return; + } + ItemMeta meta = itemStack.getItemMeta(); + if (meta == null) { + return; + } + meta.getPersistentDataContainer().set(ITEM_KEY, PersistentDataType.STRING, tag); + itemStack.setItemMeta(meta); + } + +}