Merge pull request #46 from D0gmaDev/patch-1

Add more item methods
This commit is contained in:
NichtStudioCode 2023-09-04 18:58:58 +02:00 committed by GitHub
commit de30faabd7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 5 deletions

@ -29,7 +29,7 @@ public class AdventureComponentWrapper implements ComponentWrapper {
} }
@Override @Override
public @NotNull ComponentWrapper withoutPreFormatting() { public @NotNull AdventureComponentWrapper withoutPreFormatting() {
return new AdventureComponentWrapper(AdventureComponentUtils.withoutPreFormatting(component)); return new AdventureComponentWrapper(AdventureComponentUtils.withoutPreFormatting(component));
} }

@ -32,6 +32,7 @@ public abstract class AbstractItemBuilder<S> implements ItemProvider {
protected int amount = 1; protected int amount = 1;
protected int damage; protected int damage;
protected int customModelData; protected int customModelData;
protected Boolean unbreakable;
protected ComponentWrapper displayName; protected ComponentWrapper displayName;
protected List<ComponentWrapper> lore; protected List<ComponentWrapper> lore;
protected List<ItemFlag> itemFlags; protected List<ItemFlag> itemFlags;
@ -116,6 +117,10 @@ public abstract class AbstractItemBuilder<S> implements ItemProvider {
if (customModelData != 0) if (customModelData != 0)
itemMeta.setCustomModelData(customModelData); itemMeta.setCustomModelData(customModelData);
// unbreakable
if (unbreakable != null)
itemMeta.setUnbreakable(unbreakable);
// enchantments // enchantments
if (enchantments != null) { if (enchantments != null) {
if (base != null) if (base != null)
@ -201,6 +206,16 @@ public abstract class AbstractItemBuilder<S> implements ItemProvider {
return (S) this; return (S) this;
} }
public @Nullable Boolean isUnbreakable() {
return unbreakable;
}
@Contract("_ -> this")
public @NotNull S setUnbreakable(boolean unbreakable) {
this.unbreakable = unbreakable;
return (S) this;
}
public @Nullable ComponentWrapper getDisplayName() { public @Nullable ComponentWrapper getDisplayName() {
return displayName; return displayName;
} }
@ -229,7 +244,7 @@ public abstract class AbstractItemBuilder<S> implements ItemProvider {
} }
@Contract("_ -> this") @Contract("_ -> this")
public @NotNull S setLore(List<ComponentWrapper> lore) { public @NotNull S setLore(@NotNull List<@NotNull ComponentWrapper> lore) {
this.lore = lore.stream() this.lore = lore.stream()
.map(ComponentWrapper::withoutPreFormatting) .map(ComponentWrapper::withoutPreFormatting)
.collect(Collectors.toList()); .collect(Collectors.toList());
@ -237,7 +252,7 @@ public abstract class AbstractItemBuilder<S> implements ItemProvider {
} }
@Contract("_ -> this") @Contract("_ -> this")
public @NotNull S setLegacyLore(@NotNull List<String> lore) { public @NotNull S setLegacyLore(@NotNull List<@NotNull String> lore) {
this.lore = lore.stream() this.lore = lore.stream()
.map(line -> new BungeeComponentWrapper(TextComponent.fromLegacyText(line)).withoutPreFormatting()) .map(line -> new BungeeComponentWrapper(TextComponent.fromLegacyText(line)).withoutPreFormatting())
.collect(Collectors.toList()); .collect(Collectors.toList());
@ -273,6 +288,26 @@ public abstract class AbstractItemBuilder<S> implements ItemProvider {
return (S) this; return (S) this;
} }
@Contract("_ -> this")
public @NotNull S addLoreLines(@NotNull List<@NotNull ComponentWrapper> lines) {
if (lore == null) lore = new ArrayList<>();
for (ComponentWrapper line : lines)
lore.add(line.withoutPreFormatting());
return (S) this;
}
@Contract("_ -> this")
public @NotNull S addLegacyLoreLines(@NotNull List<@NotNull String> lines) {
if (lore == null) lore = new ArrayList<>();
for (String line : lines)
lore.add(new BungeeComponentWrapper(TextComponent.fromLegacyText(line)).withoutPreFormatting());
return (S) this;
}
//</editor-fold> //</editor-fold>
//<editor-fold desc="item flags"> //<editor-fold desc="item flags">
@ -293,6 +328,12 @@ public abstract class AbstractItemBuilder<S> implements ItemProvider {
return (S) this; return (S) this;
} }
@Contract("-> this")
public @NotNull S addAllItemFlags() {
this.itemFlags = new ArrayList<>(Arrays.asList(ItemFlag.values()));
return (S) this;
}
@Contract("_ -> this") @Contract("_ -> this")
public @NotNull S removeItemFlags(@NotNull ItemFlag... itemFlags) { public @NotNull S removeItemFlags(@NotNull ItemFlag... itemFlags) {
if (this.itemFlags != null) if (this.itemFlags != null)
@ -327,8 +368,7 @@ public abstract class AbstractItemBuilder<S> implements ItemProvider {
@Contract("_ -> this") @Contract("_ -> this")
public @NotNull S removeEnchantment(Enchantment enchantment) { public @NotNull S removeEnchantment(Enchantment enchantment) {
if (enchantments == null) enchantments = new HashMap<>(); if (enchantments != null) enchantments.remove(enchantment);
enchantments.remove(enchantment);
return (S) this; return (S) this;
} }

@ -7,12 +7,14 @@ import org.bukkit.inventory.meta.FireworkMeta;
import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Range;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public final class FireworkBuilder extends AbstractItemBuilder<FireworkBuilder> { public final class FireworkBuilder extends AbstractItemBuilder<FireworkBuilder> {
private int power = -1;
private List<FireworkEffect> effects = new ArrayList<>(); private List<FireworkEffect> effects = new ArrayList<>();
public FireworkBuilder() { public FireworkBuilder() {
@ -27,6 +29,12 @@ public final class FireworkBuilder extends AbstractItemBuilder<FireworkBuilder>
super(base); super(base);
} }
@Contract("_ -> this")
public @NotNull FireworkBuilder setPower(@Range(from = 0, to = 127) int power) {
this.power = power;
return this;
}
@Contract("_ -> this") @Contract("_ -> this")
public @NotNull FireworkBuilder addFireworkEffect(@NotNull FireworkEffect effect) { public @NotNull FireworkBuilder addFireworkEffect(@NotNull FireworkEffect effect) {
effects.add(effect); effects.add(effect);
@ -57,6 +65,7 @@ public final class FireworkBuilder extends AbstractItemBuilder<FireworkBuilder>
ItemStack item = super.get(lang); ItemStack item = super.get(lang);
FireworkMeta meta = (FireworkMeta) item.getItemMeta(); FireworkMeta meta = (FireworkMeta) item.getItemMeta();
if (power != -1) meta.setPower(power);
meta.clearEffects(); meta.clearEffects();
meta.addEffects(effects); meta.addEffects(effects);