feat(sort): add craftable sorting stick
This commit is contained in:
1 parent
ad29db0a79
commit
848f5c643f
9 files changed
+344
-9
No files matched your search
@@ -0,0 +1,58 @@
|
||||
package games.dmg.spigotinventoryhelper;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.function.Supplier;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
|
||||
public final class SortingStickItemFactory {
|
||||
private static final byte AUTHENTIC_MARKER = 1;
|
||||
|
||||
private final NamespacedKey identityKey;
|
||||
private final Supplier<ItemStack> itemSupplier;
|
||||
|
||||
public SortingStickItemFactory(NamespacedKey identityKey) {
|
||||
this(identityKey, () -> new ItemStack(Material.STICK));
|
||||
}
|
||||
|
||||
SortingStickItemFactory(NamespacedKey identityKey, Supplier<ItemStack> itemSupplier) {
|
||||
this.identityKey = Objects.requireNonNull(identityKey, "identityKey");
|
||||
this.itemSupplier = Objects.requireNonNull(itemSupplier, "itemSupplier");
|
||||
}
|
||||
|
||||
public ItemStack create() {
|
||||
ItemStack item = itemSupplier.get();
|
||||
item.setType(Material.STICK);
|
||||
item.setAmount(1);
|
||||
ItemMeta metadata = Objects.requireNonNull(item.getItemMeta(), "Stick metadata");
|
||||
metadata.displayName(Component.text("Sorting Stick", NamedTextColor.GOLD));
|
||||
metadata.setEnchantmentGlintOverride(true);
|
||||
metadata.getPersistentDataContainer().set(
|
||||
identityKey,
|
||||
PersistentDataType.BYTE,
|
||||
AUTHENTIC_MARKER
|
||||
);
|
||||
item.setItemMeta(metadata);
|
||||
return item;
|
||||
}
|
||||
|
||||
public boolean isSortingStick(ItemStack item) {
|
||||
if (item == null || item.getType() != Material.STICK || !item.hasItemMeta()) {
|
||||
return false;
|
||||
}
|
||||
ItemMeta metadata = item.getItemMeta();
|
||||
if (metadata == null) {
|
||||
return false;
|
||||
}
|
||||
Byte marker = metadata.getPersistentDataContainer().get(
|
||||
identityKey,
|
||||
PersistentDataType.BYTE
|
||||
);
|
||||
return marker != null && marker == AUTHENTIC_MARKER;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package games.dmg.spigotinventoryhelper;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.ShapedRecipe;
|
||||
|
||||
public final class SortingStickRecipeRegistrar {
|
||||
private final NamespacedKey recipeKey;
|
||||
private final SortingStickItemFactory itemFactory;
|
||||
private final SortingStickRecipeSpec recipeSpec;
|
||||
private final RecipeFactory recipeFactory;
|
||||
|
||||
public SortingStickRecipeRegistrar(
|
||||
NamespacedKey recipeKey,
|
||||
SortingStickItemFactory itemFactory,
|
||||
SortingStickRecipeSpec recipeSpec
|
||||
) {
|
||||
this(recipeKey, itemFactory, recipeSpec, ShapedRecipe::new);
|
||||
}
|
||||
|
||||
SortingStickRecipeRegistrar(
|
||||
NamespacedKey recipeKey,
|
||||
SortingStickItemFactory itemFactory,
|
||||
SortingStickRecipeSpec recipeSpec,
|
||||
RecipeFactory recipeFactory
|
||||
) {
|
||||
this.recipeKey = Objects.requireNonNull(recipeKey, "recipeKey");
|
||||
this.itemFactory = Objects.requireNonNull(itemFactory, "itemFactory");
|
||||
this.recipeSpec = Objects.requireNonNull(recipeSpec, "recipeSpec");
|
||||
this.recipeFactory = Objects.requireNonNull(recipeFactory, "recipeFactory");
|
||||
}
|
||||
|
||||
public boolean register(Server server) {
|
||||
Objects.requireNonNull(server, "server");
|
||||
boolean registered = true;
|
||||
for (Material storage : List.of(Material.CHEST, Material.BARREL)) {
|
||||
ShapedRecipe recipe = createRecipe(storage);
|
||||
registered = server.addRecipe(recipe) && registered;
|
||||
}
|
||||
return registered;
|
||||
}
|
||||
|
||||
private ShapedRecipe createRecipe(Material storage) {
|
||||
ItemStack result = itemFactory.create();
|
||||
result.setAmount(recipeSpec.outputAmount());
|
||||
NamespacedKey variantKey = new NamespacedKey(
|
||||
recipeKey.getNamespace(),
|
||||
recipeKey.getKey() + "_" + storage.name().toLowerCase(java.util.Locale.ROOT)
|
||||
);
|
||||
ShapedRecipe recipe = recipeFactory.create(variantKey, result);
|
||||
recipe.shape(recipeSpec.shape().toArray(String[]::new));
|
||||
recipe.setIngredient('C', storage);
|
||||
recipe.setIngredient('R', Material.REDSTONE);
|
||||
recipe.setIngredient('S', Material.STICK);
|
||||
return recipe;
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
interface RecipeFactory {
|
||||
ShapedRecipe create(NamespacedKey key, ItemStack result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package games.dmg.spigotinventoryhelper;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import org.bukkit.Material;
|
||||
|
||||
public record SortingStickRecipeSpec(
|
||||
List<String> shape,
|
||||
Map<Character, Set<Material>> ingredients,
|
||||
int outputAmount
|
||||
) {
|
||||
public SortingStickRecipeSpec {
|
||||
shape = List.copyOf(shape);
|
||||
ingredients = Map.copyOf(ingredients);
|
||||
}
|
||||
|
||||
public static SortingStickRecipeSpec defaultRecipe() {
|
||||
return new SortingStickRecipeSpec(
|
||||
List.of(" C ", " R ", " S "),
|
||||
Map.of(
|
||||
'C', Set.of(Material.CHEST, Material.BARREL),
|
||||
'R', Set.of(Material.REDSTONE),
|
||||
'S', Set.of(Material.STICK)
|
||||
),
|
||||
1
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,29 @@
|
||||
package games.dmg.spigotinventoryhelper;
|
||||
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public final class SpigotInventoryHelperPlugin extends JavaPlugin {
|
||||
private SortingStickItemFactory sortingSticks;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
sortingSticks = new SortingStickItemFactory(new NamespacedKey(this, "sorting_stick"));
|
||||
SortingStickRecipeRegistrar recipes = new SortingStickRecipeRegistrar(
|
||||
new NamespacedKey(this, "sorting_stick_recipe"),
|
||||
sortingSticks,
|
||||
SortingStickRecipeSpec.defaultRecipe()
|
||||
);
|
||||
if (!recipes.register(getServer())) {
|
||||
getLogger().warning("One or more Sorting Stick recipes could not be registered.");
|
||||
}
|
||||
getLogger().info("Spigot Inventory Helper enabled.");
|
||||
}
|
||||
|
||||
SortingStickItemFactory sortingSticks() {
|
||||
if (sortingSticks == null) {
|
||||
throw new IllegalStateException("Sorting Stick service is unavailable");
|
||||
}
|
||||
return sortingSticks;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user