feat(crafting): add stackable dispenser recipe
Release / release (push) Successful in 4m12s
CI / build (push) Successful in 3m23s

This commit is contained in:
dmg committed 2026-09-22 21:20:47 -04:00
1 parent 7854824f01
commit 534cab061c
15 files changed
+684 -21

No files matched your search

@@ -0,0 +1,86 @@
package games.dmg.spigotinventoryhelper;
import java.util.List;
import java.util.Objects;
import java.util.function.Supplier;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.Server;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.ShapelessRecipe;
import org.bukkit.inventory.recipe.CraftingBookCategory;
public final class DispenserRecipeRegistrar implements RecipeRegistrar {
private final NamespacedKey recipeKey;
private final Supplier<ItemStack> resultFactory;
private final RecipeFactory recipeFactory;
private Server registeredServer;
private boolean owned;
public DispenserRecipeRegistrar(NamespacedKey recipeKey) {
this(recipeKey, () -> new ItemStack(Material.DISPENSER), ShapelessRecipe::new);
}
DispenserRecipeRegistrar(
NamespacedKey recipeKey,
Supplier<ItemStack> resultFactory,
RecipeFactory recipeFactory
) {
this.recipeKey = Objects.requireNonNull(recipeKey, "recipeKey");
this.resultFactory = Objects.requireNonNull(resultFactory, "resultFactory");
this.recipeFactory = Objects.requireNonNull(recipeFactory, "recipeFactory");
}
public boolean register(Server server) {
Objects.requireNonNull(server, "server");
if (owned) {
return true;
}
registeredServer = server;
if (server.getRecipe(recipeKey) != null) {
return false;
}
ItemStack result = resultFactory.get();
ShapelessRecipe recipe = recipeFactory.create(recipeKey, result);
recipe.addIngredient(Material.DROPPER);
recipe.addIngredient(Material.TRIPWIRE_HOOK);
recipe.setCategory(CraftingBookCategory.REDSTONE);
try {
owned = server.addRecipe(recipe);
return owned;
} catch (RuntimeException exception) {
try {
owned = server.getRecipe(recipeKey) != null;
unregister();
} catch (RuntimeException cleanupFailure) {
exception.addSuppressed(cleanupFailure);
}
throw exception;
}
}
@Override
public List<NamespacedKey> recipeKeys() {
return List.of(recipeKey);
}
public void unregister() {
if (!owned || registeredServer == null) {
return;
}
if (!registeredServer.removeRecipe(recipeKey)) {
throw new IllegalStateException("Could not remove owned recipe " + recipeKey);
}
owned = false;
}
@Override
public String description() {
return "alternative Dispenser recipe";
}
@FunctionalInterface
interface RecipeFactory {
ShapelessRecipe create(NamespacedKey key, ItemStack result);
}
}
@@ -12,10 +12,16 @@ import org.bukkit.event.player.PlayerJoinEvent;
public final class RecipeDiscovery implements Listener {
private final List<NamespacedKey> recipeKeys;
private final String description;
private final Logger logger;
public RecipeDiscovery(List<NamespacedKey> recipeKeys, Logger logger) {
this(recipeKeys, "recipes", logger);
}
public RecipeDiscovery(List<NamespacedKey> recipeKeys, String description, Logger logger) {
this.recipeKeys = List.copyOf(recipeKeys);
this.description = Objects.requireNonNull(description, "description");
this.logger = Objects.requireNonNull(logger, "logger");
}
@@ -41,12 +47,12 @@ public final class RecipeDiscovery implements Listener {
.filter(key -> !player.hasDiscoveredRecipe(key))
.toList();
if (!stillUnknown.isEmpty()) {
logger.warning("Could not discover Sorting Stick recipes for "
logger.warning("Could not discover " + description + " for "
+ player.getName() + ": " + stillUnknown);
}
} catch (RuntimeException exception) {
logger.log(Level.WARNING,
"Could not discover Sorting Stick recipes for " + player.getName(), exception);
"Could not discover " + description + " for " + player.getName(), exception);
}
}
}
@@ -12,14 +12,14 @@ import org.bukkit.plugin.Plugin;
public final class RecipeLifecycle {
private final Server server;
private final Plugin plugin;
private final SortingStickRecipeRegistrar recipes;
private final RecipeRegistrar recipes;
private final Logger logger;
private RecipeDiscovery discovery;
public RecipeLifecycle(
Server server,
Plugin plugin,
SortingStickRecipeRegistrar recipes,
RecipeRegistrar recipes,
Logger logger
) {
this.server = Objects.requireNonNull(server, "server");
@@ -34,12 +34,13 @@ public final class RecipeLifecycle {
}
try {
if (!recipes.register(server)) {
logger.severe("Sorting Stick recipes were not registered as a complete group; "
+ "discovery is disabled.");
logger.severe(recipes.description()
+ " were not registered; discovery is disabled.");
return false;
}
List<? extends Player> onlinePlayers = List.copyOf(server.getOnlinePlayers());
discovery = new RecipeDiscovery(recipes.recipeKeys(), logger);
discovery = new RecipeDiscovery(
recipes.recipeKeys(), recipes.description(), logger);
server.getPluginManager().registerEvents(discovery, plugin);
discovery.discoverAll(onlinePlayers);
return true;
@@ -51,8 +52,7 @@ public final class RecipeLifecycle {
exception.addSuppressed(cleanupFailure);
}
logger.log(Level.SEVERE,
"Sorting Stick recipes were not registered as a complete group; "
+ "discovery is disabled.",
recipes.description() + " were not registered; discovery is disabled.",
exception);
return false;
}
@@ -0,0 +1,33 @@
package games.dmg.spigotinventoryhelper;
import java.util.List;
public final class RecipeLifecycleGroup {
private final List<RecipeLifecycle> lifecycles;
public RecipeLifecycleGroup(List<RecipeLifecycle> lifecycles) {
this.lifecycles = List.copyOf(lifecycles);
}
public void enableAll() {
lifecycles.forEach(RecipeLifecycle::enable);
}
public void disableAll() {
RuntimeException failure = null;
for (int index = lifecycles.size() - 1; index >= 0; index--) {
try {
lifecycles.get(index).disable();
} catch (RuntimeException exception) {
if (failure == null) {
failure = exception;
} else {
failure.addSuppressed(exception);
}
}
}
if (failure != null) {
throw failure;
}
}
}
@@ -0,0 +1,15 @@
package games.dmg.spigotinventoryhelper;
import java.util.List;
import org.bukkit.NamespacedKey;
import org.bukkit.Server;
public interface RecipeRegistrar {
boolean register(Server server);
List<NamespacedKey> recipeKeys();
void unregister();
String description();
}
@@ -11,7 +11,7 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.recipe.CraftingBookCategory;
public final class SortingStickRecipeRegistrar {
public final class SortingStickRecipeRegistrar implements RecipeRegistrar {
private final NamespacedKey recipeKey;
private final SortingStickItemFactory itemFactory;
private final SortingStickRecipeSpec recipeSpec;
@@ -81,6 +81,11 @@ public final class SortingStickRecipeRegistrar {
rollback();
}
@Override
public String description() {
return "Sorting Stick recipes";
}
private void rollback() {
if (registeredServer == null) {
return;
@@ -1,11 +1,12 @@
package games.dmg.spigotinventoryhelper;
import java.util.List;
import org.bukkit.NamespacedKey;
import org.bukkit.plugin.java.JavaPlugin;
public final class SpigotInventoryHelperPlugin extends JavaPlugin {
private SortingStickItemFactory sortingSticks;
private RecipeLifecycle recipeLifecycle;
private RecipeLifecycleGroup recipeLifecycles;
@Override
public void onEnable() {
@@ -15,8 +16,16 @@ public final class SpigotInventoryHelperPlugin extends JavaPlugin {
sortingSticks,
SortingStickRecipeSpec.defaultRecipe()
);
recipeLifecycle = new RecipeLifecycle(getServer(), this, recipes, getLogger());
recipeLifecycle.enable();
RecipeLifecycle sortingRecipes = new RecipeLifecycle(
getServer(), this, recipes, getLogger());
RecipeLifecycle dispenserRecipe = new RecipeLifecycle(
getServer(),
this,
new DispenserRecipeRegistrar(new NamespacedKey(this, "dropper_dispenser")),
getLogger()
);
recipeLifecycles = new RecipeLifecycleGroup(List.of(sortingRecipes, dispenserRecipe));
recipeLifecycles.enableAll();
InventorySortingService sorting = new InventorySortingService(new InventorySorter());
getServer().getPluginManager().registerEvents(
new SortingStickListener(sortingSticks, sorting),
@@ -31,9 +40,9 @@ public final class SpigotInventoryHelperPlugin extends JavaPlugin {
@Override
public void onDisable() {
if (recipeLifecycle != null) {
recipeLifecycle.disable();
recipeLifecycle = null;
if (recipeLifecycles != null) {
recipeLifecycles.disableAll();
recipeLifecycles = null;
}
}