feat(crafting): craft powder snow from snow and a bucket
CI / build (push) Successful in 1m4s
CI / build (pull_request) Successful in 2m3s

This commit is contained in:
dmg committed 2026-10-12 09:18:37 -04:00
1 parent 1cd894f980
commit b4613d2abf
9 files changed
+1305 -5

No files matched your search

@@ -0,0 +1,72 @@
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.ShapelessRecipe;
import org.bukkit.inventory.recipe.CraftingBookCategory;
/** Independently owns the fixed Snow Block and empty Bucket conversion. */
public final class PowderSnowRecipeRegistrar implements RecipeRegistrar {
private final NamespacedKey recipeKey;
private Server registeredServer;
private boolean owned;
public PowderSnowRecipeRegistrar(NamespacedKey recipeKey) {
this.recipeKey = Objects.requireNonNull(recipeKey, "recipeKey");
}
@Override
public boolean register(Server server) {
Objects.requireNonNull(server, "server");
if (owned) {
return true;
}
registeredServer = server;
if (server.getRecipe(recipeKey) != null) {
return false;
}
ShapelessRecipe recipe = new ShapelessRecipe(recipeKey, new ItemStack(Material.POWDER_SNOW_BUCKET));
recipe.addIngredient(Material.SNOW_BLOCK);
recipe.addIngredient(Material.BUCKET);
recipe.setCategory(CraftingBookCategory.MISC);
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);
}
@Override
public void unregister() {
if (!owned || registeredServer == null) {
return;
}
// A prior removal may have succeeded before its port threw. Retain ownership on
// failure, but let a later cleanup confirm absence instead of stranding the lifecycle.
if (!registeredServer.removeRecipe(recipeKey) && registeredServer.getRecipe(recipeKey) != null) {
throw new IllegalStateException("Could not remove owned recipe " + recipeKey);
}
owned = false;
}
@Override
public String description() {
return "Powder Snow crafting recipe";
}
}
@@ -69,8 +69,14 @@ public final class SpigotInventoryHelperPlugin extends JavaPlugin {
new CobwebRecipeRegistrar(new NamespacedKey(this, "string_cobweb")),
getLogger()
);
RecipeLifecycle powderSnowRecipe = new RecipeLifecycle(
getServer(),
this,
new PowderSnowRecipeRegistrar(new NamespacedKey(this, "snow_powder_snow")),
getLogger()
);
recipeLifecycles = new RecipeLifecycleGroup(
List.of(sortingRecipes, dispenserRecipe, cobwebRecipe));
List.of(sortingRecipes, dispenserRecipe, cobwebRecipe, powderSnowRecipe));
recipeLifecycles.enableAll();
InventorySortingService sorting = new InventorySortingService(new InventorySorter());
getServer().getPluginManager().registerEvents(