From 340233b558c91675161ec237390db31260c36b5e Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 24 May 2021 11:20:08 +0200 Subject: [PATCH 1/2] 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); + } + +} From 7abdaa4b3e816097914320d26b70ace097ee0bfa Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 24 May 2021 11:28:28 +0200 Subject: [PATCH 2/2] Update AutostartListener Update Countingwand Signed-off-by: yoyosource --- .../features/autostart/AutostartListener.java | 7 +++++-- .../features/countingwand/Countingwand.java | 17 ++++++++++------- .../countingwand/CountingwandListener.java | 7 ++++--- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/autostart/AutostartListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/autostart/AutostartListener.java index a9f6cf53..98814659 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/autostart/AutostartListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/autostart/AutostartListener.java @@ -27,6 +27,7 @@ import de.steamwar.bausystem.region.Region; import de.steamwar.bausystem.region.RegionUtils; import de.steamwar.bausystem.region.utils.RegionExtensionType; import de.steamwar.bausystem.region.utils.RegionType; +import de.steamwar.bausystem.utils.ItemUtils; import de.steamwar.inventory.SWItem; import lombok.Getter; import org.bukkit.Material; @@ -47,7 +48,9 @@ import java.util.Map; public class AutostartListener implements Listener { public static ItemStack getWandItem(Player player) { - return new SWItem(Material.FIREWORK_STAR, BauSystem.MESSAGE.parse("AUTOSTART_ITEM_NAME", player), Arrays.asList(BauSystem.MESSAGE.parse("AUTOSTART_ITEM_LORE", player)), false, null).getItemStack(); + ItemStack itemStack = new SWItem(Material.FIREWORK_STAR, BauSystem.MESSAGE.parse("AUTOSTART_ITEM_NAME", player), Arrays.asList(BauSystem.MESSAGE.parse("AUTOSTART_ITEM_LORE", player)), false, null).getItemStack(); + ItemUtils.setItem(itemStack, "autostart"); + return itemStack; } @Getter @@ -55,7 +58,7 @@ public class AutostartListener implements Listener { @EventHandler public void onPlayerInteract(PlayerInteractEvent event) { - if (!AutostartListener.getWandItem(event.getPlayer()).isSimilar(event.getItem())) { + if (!ItemUtils.isItem(event.getItem(), "autostart")) { return; } if (event.getClickedBlock() == null) { diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/countingwand/Countingwand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/countingwand/Countingwand.java index 4c960577..4d09437e 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/countingwand/Countingwand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/countingwand/Countingwand.java @@ -20,30 +20,33 @@ package de.steamwar.bausystem.features.countingwand; import de.steamwar.bausystem.BauSystem; -import de.steamwar.bausystem.config.ColorConfig; import de.steamwar.bausystem.region.Point; import de.steamwar.bausystem.shared.Pair; +import de.steamwar.bausystem.utils.ItemUtils; import de.steamwar.inventory.SWItem; -import java.util.*; - -import de.steamwar.message.Message; import lombok.experimental.UtilityClass; import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + @UtilityClass public class Countingwand { public static ItemStack getWandItem(Player player) { - return new SWItem(Material.STICK, BauSystem.MESSAGE.parse("COUNTINGWAND_ITEM_NAME", player), Arrays.asList(BauSystem.MESSAGE.parse("COUNTINGWAND_ITEM_LORE1", player), BauSystem.MESSAGE.parse("COUNTINGWAND_ITEM_LORE2", player)), false, null).getItemStack(); + ItemStack itemStack = new SWItem(Material.STICK, BauSystem.MESSAGE.parse("COUNTINGWAND_ITEM_NAME", player), Arrays.asList(BauSystem.MESSAGE.parse("COUNTINGWAND_ITEM_LORE1", player), BauSystem.MESSAGE.parse("COUNTINGWAND_ITEM_LORE2", player)), false, null).getItemStack(); + ItemUtils.setItem(itemStack, "countingwand"); + return itemStack; } private final Map> selections = new HashMap<>(); - public boolean isCountingwand(Player player, ItemStack itemStack) { - return getWandItem(player).isSimilar(itemStack); + public boolean isCountingwand(ItemStack itemStack) { + return ItemUtils.isItem(itemStack, "countingwand"); } public void checkSelection(final Point point, final boolean pos1, final Player p) { diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/countingwand/CountingwandListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/countingwand/CountingwandListener.java index 26f67320..3fedffce 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/countingwand/CountingwandListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/countingwand/CountingwandListener.java @@ -22,7 +22,6 @@ package de.steamwar.bausystem.features.countingwand; import de.steamwar.bausystem.linkage.LinkageType; import de.steamwar.bausystem.linkage.Linked; import de.steamwar.bausystem.region.Point; -import java.util.Objects; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; @@ -30,13 +29,15 @@ import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerQuitEvent; +import java.util.Objects; + @Linked(LinkageType.LISTENER) public class CountingwandListener implements Listener { @EventHandler public void onBlockBreak(final BlockBreakEvent event) { - if (!Countingwand.isCountingwand(event.getPlayer(), event.getPlayer().getInventory().getItemInMainHand())) { + if (!Countingwand.isCountingwand(event.getPlayer().getInventory().getItemInMainHand())) { return; } @@ -46,7 +47,7 @@ public class CountingwandListener implements Listener { @EventHandler public void onPlayerInteract(final PlayerInteractEvent event) { - if (!Countingwand.isCountingwand(event.getPlayer(), event.getItem())) { + if (!Countingwand.isCountingwand(event.getItem())) { return; }