diff --git a/src/main/java/net/knarcraft/knargui/item/AbstractGUIItemFactory.java b/src/main/java/net/knarcraft/knargui/item/AbstractGUIItemFactory.java index da6cce5..8349736 100644 --- a/src/main/java/net/knarcraft/knargui/item/AbstractGUIItemFactory.java +++ b/src/main/java/net/knarcraft/knargui/item/AbstractGUIItemFactory.java @@ -15,7 +15,7 @@ import java.util.function.Consumer; * @param

A child class implementing this class

*/ @SuppressWarnings("unused") -public abstract class AbstractGUIItemFactory> { +public abstract class AbstractGUIItemFactory> implements GUIItemFactory { private final ItemStack itemStack; private B child; @@ -37,45 +37,30 @@ public abstract class AbstractGUIItemFactory * * @param child

The child class extending this

*/ - protected void setChild(B child) { + protected void setChild(@NotNull B child) { this.child = child; } - /** - * Builds the item as described in previous calls - * - * @return

The output of this factory

- */ + @Override + @NotNull public ItemStack build() { return this.itemStack; } - /** - * Sets the name of this factory's item - * - * @param name

The new item name

- * @return

The factory. Used for chaining commands

- */ - public B setName(String name) { + @Override + @NotNull + public B setName(@NotNull String name) { return changeItemMeta((meta) -> meta.setDisplayName(name)); } - /** - * Sets the lore for this factory's item - * - * @param lore

The new lore

- * @return

The factory. Used for chaining commands

- */ - public B setLore(List lore) { + @Override + @NotNull + public B setLore(@NotNull List lore) { return changeItemMeta((meta) -> meta.setLore(lore)); } - /** - * Sets whether this factory's item should look enchanted - * - * @param enchanted

True if the item should look enchanted

- * @return

The factory. Used for chaining commands

- */ + @Override + @NotNull public B setEnchanted(boolean enchanted) { if (enchanted) { changeItemMeta((meta) -> meta.addEnchant(Enchantment.MENDING, 1, true)); @@ -87,25 +72,15 @@ public abstract class AbstractGUIItemFactory } } - /** - * Changes some metadata for this factory's item - * - * @param action

The action to be run on the metadata

- * @return

The factory. Used for chaining commands

- */ - protected B changeItemMeta(Consumer action) { + @Override + @NotNull + public B changeItemMeta(@NotNull Consumer action) { return changeItemMeta(ItemMeta.class, action); } - /** - * Changes some metadata for this factory's item - * - * @param metaType

The type of metadata to change

- * @param action

The action to be run on the metadata

- * @param

The type of the metadata

- * @return

The factory. Used for chaining commands

- */ - protected B changeItemMeta(Class metaType, Consumer action) { + @Override + @NotNull + public B changeItemMeta(@NotNull Class metaType, @NotNull Consumer action) { M meta = checkForNullMetadata(metaType); action.accept(meta); this.itemStack.setItemMeta(meta); @@ -115,12 +90,9 @@ public abstract class AbstractGUIItemFactory return child; } - /** - * Throws an exception if this factory's item has null metadata - * - * @return

The metadata for this factory's item

- */ - protected M checkForNullMetadata(Class metaType) { + @Override + @NotNull + public M checkForNullMetadata(@NotNull Class metaType) { M meta = metaType.cast(this.itemStack.getItemMeta()); if (meta == null) { throw new IllegalArgumentException("Cannot change missing metadata"); diff --git a/src/main/java/net/knarcraft/knargui/item/GUIItemFactory.java b/src/main/java/net/knarcraft/knargui/item/GUIItemFactory.java index 4a8f701..adb3b92 100644 --- a/src/main/java/net/knarcraft/knargui/item/GUIItemFactory.java +++ b/src/main/java/net/knarcraft/knargui/item/GUIItemFactory.java @@ -1,43 +1,77 @@ package net.knarcraft.knargui.item; -import org.bukkit.Material; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.jetbrains.annotations.NotNull; + +import java.util.List; +import java.util.function.Consumer; /** - * A class for crating visual items for the GUI + * A factory class for creating GUI items */ @SuppressWarnings("unused") -public class GUIItemFactory extends AbstractGUIItemFactory { +public interface GUIItemFactory { /** - * Instantiates a new item factory + * Builds the item as described in previous calls * - * @param material

The material to use for the new item

+ * @return

The output of this factory

*/ - public GUIItemFactory(Material material) { - super(new ItemStack(material, 1)); - setChild(this); - } + @NotNull + ItemStack build(); /** - * Instantiates a new item factory + * Sets the name of this factory's item * - * @param material

The material to use for the new item

- * @param amount

The number of items to be displayed

+ * @param name

The new item name

+ * @return

The factory. Used for chaining commands

*/ - public GUIItemFactory(Material material, int amount) { - super(new ItemStack(material, amount)); - setChild(this); - } + @NotNull + GUIItemFactory setName(@NotNull String name); /** - * Instantiates a new item factory + * Sets the lore for this factory's item * - * @param itemStack

The item stack to modify

+ * @param lore

The new lore

+ * @return

The factory. Used for chaining commands

*/ - public GUIItemFactory(ItemStack itemStack) { - super(itemStack); - setChild(this); - } + @NotNull + GUIItemFactory setLore(@NotNull List lore); + + /** + * Sets whether this factory's item should look enchanted + * + * @param enchanted

True if the item should look enchanted

+ * @return

The factory. Used for chaining commands

+ */ + @NotNull + GUIItemFactory setEnchanted(boolean enchanted); + + /** + * Changes some metadata for this factory's item + * + * @param action

The action to be run on the metadata

+ * @return

The factory. Used for chaining commands

+ */ + @NotNull + GUIItemFactory changeItemMeta(@NotNull Consumer action); + + /** + * Changes some metadata for this factory's item + * + * @param metaType

The type of metadata to change

+ * @param action

The action to be run on the metadata

+ * @param

The type of the metadata

+ * @return

The factory. Used for chaining commands

+ */ + @NotNull GUIItemFactory changeItemMeta(@NotNull Class metaType, @NotNull Consumer action); + + /** + * Throws an exception if this factory's item has null metadata + * + * @return

The metadata for this factory's item

+ */ + @NotNull M checkForNullMetadata(@NotNull Class metaType); } diff --git a/src/main/java/net/knarcraft/knargui/item/PlayerHeadGUIItemFactory.java b/src/main/java/net/knarcraft/knargui/item/PlayerHeadGUIItemFactory.java index 116f23f..83c83d7 100644 --- a/src/main/java/net/knarcraft/knargui/item/PlayerHeadGUIItemFactory.java +++ b/src/main/java/net/knarcraft/knargui/item/PlayerHeadGUIItemFactory.java @@ -6,6 +6,7 @@ import org.bukkit.OfflinePlayer; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.SkullMeta; import org.bukkit.profile.PlayerProfile; +import org.jetbrains.annotations.NotNull; import java.net.MalformedURLException; import java.net.URL; @@ -37,7 +38,8 @@ public class PlayerHeadGUIItemFactory extends AbstractGUIItemFactoryThe player whose skin should be used

* @return

The factory. Used for chaining commands

*/ - public PlayerHeadGUIItemFactory useSkin(OfflinePlayer player) { + @NotNull + public PlayerHeadGUIItemFactory useSkin(@NotNull OfflinePlayer player) { return changeItemMeta(SkullMeta.class, (meta) -> meta.setOwningPlayer(player)); } @@ -47,7 +49,8 @@ public class PlayerHeadGUIItemFactory extends AbstractGUIItemFactoryThe player profile whose skin should be used

* @return

The factory. Used for chaining commands

*/ - public PlayerHeadGUIItemFactory useSkin(PlayerProfile playerProfile) { + @NotNull + public PlayerHeadGUIItemFactory useSkin(@NotNull PlayerProfile playerProfile) { return changeItemMeta(SkullMeta.class, (meta) -> meta.setOwnerProfile(playerProfile)); } @@ -57,7 +60,8 @@ public class PlayerHeadGUIItemFactory extends AbstractGUIItemFactoryThe UUID of the skin to use

* @return

The factory. Used for chaining commands

*/ - public PlayerHeadGUIItemFactory useSkin(UUID uuid) { + @NotNull + public PlayerHeadGUIItemFactory useSkin(@NotNull UUID uuid) { return useSkin(Bukkit.getOfflinePlayer(uuid)); } @@ -71,7 +75,8 @@ public class PlayerHeadGUIItemFactory extends AbstractGUIItemFactoryThe id of the texture to use

* @return

The factory. Used for chaining commands

*/ - public PlayerHeadGUIItemFactory useSkin(String textureId) { + @NotNull + public PlayerHeadGUIItemFactory useSkin(@NotNull String textureId) { //Get the texture id from a Base64 encoded JSON string if (!textureId.isBlank() && textureId.length() % 4 == 0) { String decoded = new String(Base64.getDecoder().decode(textureId)); @@ -94,7 +99,8 @@ public class PlayerHeadGUIItemFactory extends AbstractGUIItemFactoryThe URL to use

* @return

The factory. Used for chaining commands

*/ - public PlayerHeadGUIItemFactory useSkin(URL url) { + @NotNull + public PlayerHeadGUIItemFactory useSkin(@NotNull URL url) { PlayerProfile profile = Bukkit.createPlayerProfile(UUID.randomUUID()); profile.getTextures().setSkin(url); return changeItemMeta(SkullMeta.class, (meta) -> meta.setOwnerProfile(profile)); diff --git a/src/main/java/net/knarcraft/knargui/item/SimpleGUIItemFactory.java b/src/main/java/net/knarcraft/knargui/item/SimpleGUIItemFactory.java new file mode 100644 index 0000000..e18607a --- /dev/null +++ b/src/main/java/net/knarcraft/knargui/item/SimpleGUIItemFactory.java @@ -0,0 +1,44 @@ +package net.knarcraft.knargui.item; + +import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.NotNull; + +/** + * A class for crating visual items for the GUI + */ +@SuppressWarnings("unused") +public class SimpleGUIItemFactory extends AbstractGUIItemFactory { + + /** + * Instantiates a new item factory + * + * @param material

The material to use for the new item

+ */ + public SimpleGUIItemFactory(@NotNull Material material) { + super(new ItemStack(material, 1)); + setChild(this); + } + + /** + * Instantiates a new item factory + * + * @param material

The material to use for the new item

+ * @param amount

The number of items to be displayed

+ */ + public SimpleGUIItemFactory(@NotNull Material material, int amount) { + super(new ItemStack(material, amount)); + setChild(this); + } + + /** + * Instantiates a new item factory + * + * @param itemStack

The item stack to modify

+ */ + public SimpleGUIItemFactory(@NotNull ItemStack itemStack) { + super(itemStack); + setChild(this); + } + +}