BLEEDING EDGE 1.3_14

Recipe and Item Builder, and an IMPL to show how they're used.

TODO: Remove IMPL before LTS release, ideally before any RC is released.
This commit is contained in:
Paldiu 2021-04-06 13:50:23 -05:00
parent 5a04aeb32f
commit 6c800e077c
3 changed files with 140 additions and 3 deletions

View File

@ -0,0 +1,90 @@
package io.github.simplexdev.simplexcore.crafting;
import io.github.simplexdev.simplexcore.module.SimplexModule;
import io.github.simplexdev.simplexcore.utils.Utilities;
import org.bukkit.Material;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeModifier;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.Arrays;
public class ItemBuilder {
private final SimplexModule<?> plugin;
public ItemBuilder(SimplexModule<?> plugin) {
this.plugin = plugin;
}
public ItemStack createNoBounds(Material material) {
return new ItemStack(material, 1);
}
public ItemStack createWithMeta(Material material, ItemMeta metaData) {
ItemStack stack = new ItemStack(material, 1);
stack.setItemMeta(metaData);
return stack;
}
public Worker itemBuilder(Material material) {
return new Worker(new ItemStack(material, 1));
}
public AttributeModifier add(String name, double amount, boolean scalar) {
if (scalar) {
return new AttributeModifier(name, amount, AttributeModifier.Operation.ADD_SCALAR);
}
return new AttributeModifier(name, amount, AttributeModifier.Operation.ADD_NUMBER);
}
public AttributeModifier multiply(String name, double amount) {
return new AttributeModifier(name, amount, AttributeModifier.Operation.MULTIPLY_SCALAR_1);
}
protected final class Worker {
private final ItemStack stack;
private final ItemMeta meta;
public Worker(ItemStack stack) {
this.stack = stack;
this.meta = stack.getItemMeta();
}
public final Worker addLore(String... lore) {
meta.setLore(Arrays.asList(lore));
stack.setItemMeta(meta);
return this;
}
public final Worker setName(String customName) {
meta.setDisplayName(customName);
stack.setItemMeta(meta);
return this;
}
public final Worker addEnchant(Enchantment enchantment, int level) {
meta.addEnchant(enchantment, level, true);
stack.setItemMeta(meta);
return this;
}
public final Worker addAttribute(Attribute attribute, AttributeModifier modifier) {
meta.addAttributeModifier(attribute, modifier);
stack.setItemMeta(meta);
return this;
}
public final Worker addItemFlags(ItemFlag... flags) {
meta.addItemFlags(flags);
stack.setItemMeta(meta);
return this;
}
public final ItemStack get() {
return stack;
}
}
}

View File

@ -0,0 +1,47 @@
package io.github.simplexdev.simplexcore.crafting;
import io.github.simplexdev.simplexcore.module.SimplexModule;
import org.bukkit.Material;
import org.bukkit.attribute.Attribute;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
public class RBImpl {
private final RecipeBuilder builder;
private final SimplexModule<?> plugin;
private final ItemBuilder bldr;
public RBImpl(SimplexModule<?> plugin) {
this.plugin = plugin;
this.builder = new RecipeBuilder(plugin);
this.bldr = new ItemBuilder(plugin);
chainMailBoots();
customWand();
}
public void chainMailBoots() {
ItemStack is = bldr.createNoBounds(Material.CHAINMAIL_BOOTS);
builder.of(is, "chainmail_boots", true)
.addIngredient('c', Material.CHAIN)
.addIngredient('a', Material.AIR)
.setShape("aaa", "cac", "cac")
.create();
}
public void customWand() {
ItemStack is = bldr.itemBuilder(Material.BLAZE_ROD)
.setName("Magic Wand")
.addLore("This wand is magical.")
.addEnchant(Enchantment.KNOCKBACK, 10)
.addAttribute(Attribute.GENERIC_ATTACK_KNOCKBACK,
bldr.multiply("generic_attack_knockback", 24.0))
.get();
builder.of(is, "magic_wand", true)
.addIngredient('b', Material.BLAZE_ROD)
.addIngredient('n', Material.NETHER_STAR)
.addIngredient('a', Material.AIR)
.setShape("aan", "aba", "baa")
.create();
}
}

View File

@ -26,13 +26,13 @@ public final class RecipeBuilder {
return new Worker(result, recipeName, isShaped);
}
private final class Worker {
protected final class Worker {
private final Map<Character, Material> ingredients = new HashMap<>();
private final ItemStack stack;
private final NamespacedKey key;
private final boolean shaped;
private final List<Material> materials = new ArrayList<>();
private String[] shape = {"", "", ""};
private String[] shape = {"aaa", "aaa", "aaa"};
public Worker(ItemStack stack, String name, boolean isShaped) {
this.stack = stack;
@ -57,7 +57,7 @@ public final class RecipeBuilder {
return recipe;
}
public void make() {
public void create() {
plugin.getServer().addRecipe(shaped ? sha() : nosha());
}