Use static constructors for ItemStack

This commit is contained in:
DerFrZocker 2023-05-22 17:50:26 +02:00
parent b5f483b07d
commit 2200b334c7
No known key found for this signature in database
GPG key ID: 713F71FFFE1DDF91
7 changed files with 35 additions and 11 deletions

View file

@ -66,7 +66,7 @@ public class PlayerItemConsumeEvent extends PlayerEvent implements Cancellable {
*/
public void setItem(@Nullable ItemStack item) {
if (item == null) {
this.item = new ItemStack(ItemType.AIR);
this.item = ItemStack.of(ItemType.AIR);
} else {
this.item = item;
}

View file

@ -28,7 +28,7 @@ public class PlayerShearEntityEvent extends PlayerEvent implements Cancellable {
@Deprecated
public PlayerShearEntityEvent(@NotNull final Player who, @NotNull final Entity what) {
this(who, what, new ItemStack(ItemType.SHEARS), EquipmentSlot.HAND);
this(who, what, ItemStack.of(ItemType.SHEARS), EquipmentSlot.HAND);
}
@Override

View file

@ -24,6 +24,30 @@ import org.jetbrains.annotations.Nullable;
* returns false.</b>
*/
public class ItemStack implements Cloneable, ConfigurationSerializable, Translatable {
/**
* Creates a new ItemStack with an amount of 1.
*
* @param itemType item type
* @return A new ItemStack with an amount of 1
*/
@NotNull
public static ItemStack of(@NotNull ItemType itemType) {
return new ItemStack(itemType);
}
/**
* Creates a new ItemStack with the given amount.
*
* @param itemType item type
* @param amount stack size
* @return A new ItemStack with the given amount
*/
@NotNull
public static ItemStack of(@NotNull ItemType itemType, int amount) {
return new ItemStack(itemType, amount);
}
private ItemType type = ItemType.AIR;
private int amount = 0;
private MaterialData data = null;
@ -51,7 +75,7 @@ public class ItemStack implements Cloneable, ConfigurationSerializable, Translat
*
* @param type item type
*/
public ItemStack(@NotNull final ItemType type) {
private ItemStack(@NotNull final ItemType type) {
this(type, 1);
}
@ -76,7 +100,7 @@ public class ItemStack implements Cloneable, ConfigurationSerializable, Translat
* @param type item type
* @param amount stack size
*/
public ItemStack(@NotNull final ItemType type, final int amount) {
private ItemStack(@NotNull final ItemType type, final int amount) {
Preconditions.checkArgument(type != null, "Material cannot be null");
this.type = type;
this.amount = amount;

View file

@ -85,7 +85,7 @@ public interface RecipeChoice extends Predicate<ItemStack>, Cloneable {
@NotNull
@Override
public ItemStack getItemStack() {
ItemStack stack = new ItemStack(choices.get(0));
ItemStack stack = ItemStack.of(choices.get(0));
// For compat
if (choices.size() > 1) {

View file

@ -26,7 +26,7 @@ public class SmithingTrimRecipe extends SmithingRecipe implements ComplexRecipe
* @param addition The addition ingredient
*/
public SmithingTrimRecipe(@NotNull NamespacedKey key, @NotNull RecipeChoice template, @NotNull RecipeChoice base, @NotNull RecipeChoice addition) {
super(key, new ItemStack(ItemType.AIR), base, addition);
super(key, ItemStack.of(ItemType.AIR), base, addition);
this.template = template;
}

View file

@ -265,7 +265,7 @@ public class Potion {
} else {
itemType = ItemType.POTION;
}
ItemStack itemStack = new ItemStack(itemType, amount);
ItemStack itemStack = ItemStack.of(itemType, amount);
PotionMeta meta = (PotionMeta) itemStack.getItemMeta();
meta.setBasePotionData(new PotionData(type, level == 2, extended));
itemStack.setItemMeta(meta);

View file

@ -551,7 +551,7 @@ public abstract class ConfigurationSectionTest extends AbstractTestingBase {
public void testGetItemStack_String() {
ConfigurationSection section = getConfigurationSection();
String key = "exists";
ItemStack value = new ItemStack(ItemType.ACACIA_WOOD, 50);
ItemStack value = ItemStack.of(ItemType.ACACIA_WOOD, 50);
section.set(key, value);
@ -563,8 +563,8 @@ public abstract class ConfigurationSectionTest extends AbstractTestingBase {
public void testGetItemStack_String_ItemStack() {
ConfigurationSection section = getConfigurationSection();
String key = "exists";
ItemStack value = new ItemStack(ItemType.ACACIA_WOOD, 50);
ItemStack def = new ItemStack(ItemType.STONE, 1);
ItemStack value = ItemStack.of(ItemType.ACACIA_WOOD, 50);
ItemStack def = ItemStack.of(ItemType.STONE, 1);
section.set(key, value);
@ -576,7 +576,7 @@ public abstract class ConfigurationSectionTest extends AbstractTestingBase {
public void testIsItemStack() {
ConfigurationSection section = getConfigurationSection();
String key = "exists";
ItemStack value = new ItemStack(ItemType.ACACIA_WOOD, 50);
ItemStack value = ItemStack.of(ItemType.ACACIA_WOOD, 50);
section.set(key, value);