Changes the GUI item factory code
All checks were successful
KnarCraft/KnarGUI/pipeline/head This commit looks good
All checks were successful
KnarCraft/KnarGUI/pipeline/head This commit looks good
Adds a new GUIItemFactory interface Renames the old GUIItemFactory to SimpleGUIItemFactory
This commit is contained in:
parent
e3ea5a5340
commit
9d5ee267a7
@ -15,7 +15,7 @@ import java.util.function.Consumer;
|
||||
* @param <B> <p>A child class implementing this class</p>
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public abstract class AbstractGUIItemFactory<B extends AbstractGUIItemFactory<B>> {
|
||||
public abstract class AbstractGUIItemFactory<B extends AbstractGUIItemFactory<B>> implements GUIItemFactory {
|
||||
|
||||
private final ItemStack itemStack;
|
||||
private B child;
|
||||
@ -37,45 +37,30 @@ public abstract class AbstractGUIItemFactory<B extends AbstractGUIItemFactory<B>
|
||||
*
|
||||
* @param child <p>The child class extending this</p>
|
||||
*/
|
||||
protected void setChild(B child) {
|
||||
protected void setChild(@NotNull B child) {
|
||||
this.child = child;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the item as described in previous calls
|
||||
*
|
||||
* @return <p>The output of this factory</p>
|
||||
*/
|
||||
@Override
|
||||
@NotNull
|
||||
public ItemStack build() {
|
||||
return this.itemStack;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the name of this factory's item
|
||||
*
|
||||
* @param name <p>The new item name</p>
|
||||
* @return <p>The factory. Used for chaining commands</p>
|
||||
*/
|
||||
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 <p>The new lore</p>
|
||||
* @return <p>The factory. Used for chaining commands</p>
|
||||
*/
|
||||
public B setLore(List<String> lore) {
|
||||
@Override
|
||||
@NotNull
|
||||
public B setLore(@NotNull List<String> lore) {
|
||||
return changeItemMeta((meta) -> meta.setLore(lore));
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets whether this factory's item should look enchanted
|
||||
*
|
||||
* @param enchanted <p>True if the item should look enchanted</p>
|
||||
* @return <p>The factory. Used for chaining commands</p>
|
||||
*/
|
||||
@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<B extends AbstractGUIItemFactory<B>
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes some metadata for this factory's item
|
||||
*
|
||||
* @param action <p>The action to be run on the metadata</p>
|
||||
* @return <p>The factory. Used for chaining commands</p>
|
||||
*/
|
||||
protected B changeItemMeta(Consumer<ItemMeta> action) {
|
||||
@Override
|
||||
@NotNull
|
||||
public B changeItemMeta(@NotNull Consumer<ItemMeta> action) {
|
||||
return changeItemMeta(ItemMeta.class, action);
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes some metadata for this factory's item
|
||||
*
|
||||
* @param metaType <p>The type of metadata to change</p>
|
||||
* @param action <p>The action to be run on the metadata</p>
|
||||
* @param <M> <p>The type of the metadata</p>
|
||||
* @return <p>The factory. Used for chaining commands</p>
|
||||
*/
|
||||
protected <M extends ItemMeta> B changeItemMeta(Class<M> metaType, Consumer<M> action) {
|
||||
@Override
|
||||
@NotNull
|
||||
public <M extends ItemMeta> B changeItemMeta(@NotNull Class<M> metaType, @NotNull Consumer<M> action) {
|
||||
M meta = checkForNullMetadata(metaType);
|
||||
action.accept(meta);
|
||||
this.itemStack.setItemMeta(meta);
|
||||
@ -115,12 +90,9 @@ public abstract class AbstractGUIItemFactory<B extends AbstractGUIItemFactory<B>
|
||||
return child;
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws an exception if this factory's item has null metadata
|
||||
*
|
||||
* @return <p>The metadata for this factory's item</p>
|
||||
*/
|
||||
protected <M extends ItemMeta> M checkForNullMetadata(Class<M> metaType) {
|
||||
@Override
|
||||
@NotNull
|
||||
public <M extends ItemMeta> M checkForNullMetadata(@NotNull Class<M> metaType) {
|
||||
M meta = metaType.cast(this.itemStack.getItemMeta());
|
||||
if (meta == null) {
|
||||
throw new IllegalArgumentException("Cannot change missing metadata");
|
||||
|
@ -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<GUIItemFactory> {
|
||||
public interface GUIItemFactory {
|
||||
|
||||
/**
|
||||
* Instantiates a new item factory
|
||||
* Builds the item as described in previous calls
|
||||
*
|
||||
* @param material <p>The material to use for the new item</p>
|
||||
* @return <p>The output of this factory</p>
|
||||
*/
|
||||
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 <p>The material to use for the new item</p>
|
||||
* @param amount <p>The number of items to be displayed</p>
|
||||
* @param name <p>The new item name</p>
|
||||
* @return <p>The factory. Used for chaining commands</p>
|
||||
*/
|
||||
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 <p>The item stack to modify</p>
|
||||
* @param lore <p>The new lore</p>
|
||||
* @return <p>The factory. Used for chaining commands</p>
|
||||
*/
|
||||
public GUIItemFactory(ItemStack itemStack) {
|
||||
super(itemStack);
|
||||
setChild(this);
|
||||
}
|
||||
@NotNull
|
||||
GUIItemFactory setLore(@NotNull List<String> lore);
|
||||
|
||||
/**
|
||||
* Sets whether this factory's item should look enchanted
|
||||
*
|
||||
* @param enchanted <p>True if the item should look enchanted</p>
|
||||
* @return <p>The factory. Used for chaining commands</p>
|
||||
*/
|
||||
@NotNull
|
||||
GUIItemFactory setEnchanted(boolean enchanted);
|
||||
|
||||
/**
|
||||
* Changes some metadata for this factory's item
|
||||
*
|
||||
* @param action <p>The action to be run on the metadata</p>
|
||||
* @return <p>The factory. Used for chaining commands</p>
|
||||
*/
|
||||
@NotNull
|
||||
GUIItemFactory changeItemMeta(@NotNull Consumer<ItemMeta> action);
|
||||
|
||||
/**
|
||||
* Changes some metadata for this factory's item
|
||||
*
|
||||
* @param metaType <p>The type of metadata to change</p>
|
||||
* @param action <p>The action to be run on the metadata</p>
|
||||
* @param <M> <p>The type of the metadata</p>
|
||||
* @return <p>The factory. Used for chaining commands</p>
|
||||
*/
|
||||
@NotNull <M extends ItemMeta> GUIItemFactory changeItemMeta(@NotNull Class<M> metaType, @NotNull Consumer<M> action);
|
||||
|
||||
/**
|
||||
* Throws an exception if this factory's item has null metadata
|
||||
*
|
||||
* @return <p>The metadata for this factory's item</p>
|
||||
*/
|
||||
@NotNull <M extends ItemMeta> M checkForNullMetadata(@NotNull Class<M> metaType);
|
||||
|
||||
}
|
||||
|
@ -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 AbstractGUIItemFactory<PlayerHeadG
|
||||
* @param player <p>The player whose skin should be used</p>
|
||||
* @return <p>The factory. Used for chaining commands</p>
|
||||
*/
|
||||
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 AbstractGUIItemFactory<PlayerHeadG
|
||||
* @param playerProfile <p>The player profile whose skin should be used</p>
|
||||
* @return <p>The factory. Used for chaining commands</p>
|
||||
*/
|
||||
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 AbstractGUIItemFactory<PlayerHeadG
|
||||
* @param uuid <p>The UUID of the skin to use</p>
|
||||
* @return <p>The factory. Used for chaining commands</p>
|
||||
*/
|
||||
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 AbstractGUIItemFactory<PlayerHeadG
|
||||
* @param textureId <p>The id of the texture to use</p>
|
||||
* @return <p>The factory. Used for chaining commands</p>
|
||||
*/
|
||||
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 AbstractGUIItemFactory<PlayerHeadG
|
||||
* @param url <p>The URL to use</p>
|
||||
* @return <p>The factory. Used for chaining commands</p>
|
||||
*/
|
||||
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));
|
||||
|
@ -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<SimpleGUIItemFactory> {
|
||||
|
||||
/**
|
||||
* Instantiates a new item factory
|
||||
*
|
||||
* @param material <p>The material to use for the new item</p>
|
||||
*/
|
||||
public SimpleGUIItemFactory(@NotNull Material material) {
|
||||
super(new ItemStack(material, 1));
|
||||
setChild(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiates a new item factory
|
||||
*
|
||||
* @param material <p>The material to use for the new item</p>
|
||||
* @param amount <p>The number of items to be displayed</p>
|
||||
*/
|
||||
public SimpleGUIItemFactory(@NotNull Material material, int amount) {
|
||||
super(new ItemStack(material, amount));
|
||||
setChild(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiates a new item factory
|
||||
*
|
||||
* @param itemStack <p>The item stack to modify</p>
|
||||
*/
|
||||
public SimpleGUIItemFactory(@NotNull ItemStack itemStack) {
|
||||
super(itemStack);
|
||||
setChild(this);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user