Integrate the DuneChest workbench recipe preview #35
4 changed files with 72 additions and 13 deletions
|
|
@ -1,6 +1,6 @@
|
|||
plugins { java }
|
||||
group = "com.example"
|
||||
version = "1.0.2"
|
||||
version = "1.0.3"
|
||||
repositories {
|
||||
mavenCentral()
|
||||
maven("https://repo.papermc.io/repository/maven-public/")
|
||||
|
|
|
|||
|
|
@ -37,8 +37,15 @@ public final class StorageModule implements Listener, CommandExecutor {
|
|||
|
||||
/** Menu holder: kind is tune, filter or recipe; blockKey names the tracked block. */
|
||||
private static final class Menu implements InventoryHolder {
|
||||
final String kind, blockKey; Inventory inventory;
|
||||
Menu(String kind, String blockKey) { this.kind = kind; this.blockKey = blockKey; }
|
||||
final String kind, blockKey;
|
||||
final StorageData.Placed recipeBlock;
|
||||
Inventory inventory;
|
||||
Menu(String kind, String blockKey) { this(kind, blockKey, null); }
|
||||
Menu(String kind, String blockKey, StorageData.Placed recipeBlock) {
|
||||
this.kind = kind;
|
||||
this.blockKey = blockKey;
|
||||
this.recipeBlock = recipeBlock;
|
||||
}
|
||||
@Override public Inventory getInventory() { return inventory; }
|
||||
}
|
||||
|
||||
|
|
@ -221,15 +228,27 @@ public final class StorageModule implements Listener, CommandExecutor {
|
|||
inv.setItem(8, icon(Material.PAPER, "Click a slot with an item to set it", "Click with an empty cursor to clear"));
|
||||
pl.openInventory(inv);
|
||||
}
|
||||
private static int gridSlot(int g) { return (g / 3) * 9 + (g % 3) + 1; }
|
||||
/** Vanilla crafting-table window: slot 0 is the result, slots 1-9 the 3x3 grid. */
|
||||
private static int gridSlot(int g) { return g + 1; }
|
||||
private void openRecipe(Player pl, String key, StorageData.Placed p) {
|
||||
Inventory inv = menu("recipe", key, 27, "Auto Crafter Recipe");
|
||||
for (int g = 0; g < 9; g++) inv.setItem(gridSlot(g), p.recipe[g] == null ? null : p.recipe[g].clone());
|
||||
previewRecipe(inv, pl.getWorld()); pl.openInventory(inv);
|
||||
pl.openInventory(recipePreview(key, p, pl.getWorld()));
|
||||
}
|
||||
|
||||
private Inventory recipePreview(String key, StorageData.Placed placed, World world) {
|
||||
var holder = new Menu("recipe", key, placed);
|
||||
Inventory preview = Bukkit.createInventory(holder, InventoryType.WORKBENCH, Component.text("Auto Crafter Recipe"));
|
||||
holder.inventory = preview;
|
||||
for (int g = 0; g < placed.recipe.length; g++) {
|
||||
ItemStack item = placed.recipe[g];
|
||||
preview.setItem(gridSlot(g), item == null ? null : item.clone());
|
||||
}
|
||||
previewRecipe(preview, world);
|
||||
return preview;
|
||||
}
|
||||
|
||||
private void previewRecipe(Inventory inv, World w) {
|
||||
ItemStack[] m = new ItemStack[9]; for (int g = 0; g < 9; g++) m[g] = inv.getItem(gridSlot(g));
|
||||
Recipe r = Bukkit.getCraftingRecipe(m, w); inv.setItem(14, r == null ? icon(Material.BARRIER, "No recipe") : r.getResult());
|
||||
Recipe r = Bukkit.getCraftingRecipe(m, w); inv.setItem(0, r == null ? null : r.getResult());
|
||||
}
|
||||
|
||||
@EventHandler public void click(InventoryClickEvent e) {
|
||||
|
|
@ -246,6 +265,7 @@ public final class StorageModule implements Listener, CommandExecutor {
|
|||
if (slot == 6) { p.whitelist = !p.whitelist; top.setItem(6, icon(p.whitelist ? Material.LIME_DYE : Material.RED_DYE, p.whitelist ? "Whitelist" : "Blacklist", "Click to switch", "Empty filter accepts everything.")); data.dirty = true; }
|
||||
}
|
||||
case "recipe" -> {
|
||||
if (p != m.recipeBlock) return;
|
||||
for (int g = 0; g < 9; g++) if (slot == gridSlot(g)) { top.setItem(slot, cursor == null || cursor.getType().isAir() ? null : cursor.asOne()); previewRecipe(top, pl.getWorld()); }
|
||||
}
|
||||
default -> { }
|
||||
|
|
@ -256,13 +276,29 @@ public final class StorageModule implements Listener, CommandExecutor {
|
|||
var inv = e.getInventory();
|
||||
if (inv.getHolder() instanceof StorageData.FreqHolder) { data.dirty = true; signatures.clear(); return; }
|
||||
if (!(inv.getHolder() instanceof Menu m)) return;
|
||||
var p = data.blocks.get(m.blockKey); if (p == null) return;
|
||||
var p = data.blocks.get(m.blockKey);
|
||||
if (m.recipeBlock != null) {
|
||||
saveRecipeAndClearPreview(inv, m.recipeBlock == p ? p : null);
|
||||
return;
|
||||
}
|
||||
if (p == null) return;
|
||||
if (m.kind.equals("filter")) {
|
||||
p.filter = new ArrayList<>(); for (int i = 0; i < 5; i++) { var s = inv.getItem(i); if (s != null && !s.getType().isAir() && !p.filter.contains(s.getType())) p.filter.add(s.getType()); }
|
||||
data.dirty = true;
|
||||
} else if (m.kind.equals("recipe")) {
|
||||
for (int g = 0; g < 9; g++) { var s = inv.getItem(gridSlot(g)); p.recipe[g] = s == null || s.getType().isAir() ? null : s.asOne(); }
|
||||
}
|
||||
}
|
||||
|
||||
private void saveRecipeAndClearPreview(Inventory preview, StorageData.Placed target) {
|
||||
try {
|
||||
if (target == null) return;
|
||||
for (int g = 0; g < target.recipe.length; g++) {
|
||||
ItemStack item = preview.getItem(gridSlot(g));
|
||||
target.recipe[g] = item == null || item.isEmpty() ? null : item.asOne();
|
||||
}
|
||||
data.dirty = true;
|
||||
} finally {
|
||||
// Nothing in the recipe window is a real player item.
|
||||
preview.clear();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
name: DuneChest
|
||||
version: 1.0.2
|
||||
version: 1.0.3
|
||||
main: com.example.dunechest.DuneChest
|
||||
api-version: '1.21.8'
|
||||
softdepend: [floodgate, DuneMenus, DuneEss]
|
||||
|
|
|
|||
|
|
@ -7,10 +7,12 @@ import java.lang.reflect.Method;
|
|||
import java.util.Arrays;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.BlockFace;
|
||||
import org.bukkit.block.Chest;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.inventory.InventoryType;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.server.ServerLoadEvent;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
|
|
@ -33,7 +35,7 @@ public final class DuneChestPaperValidation extends JavaPlugin implements Listen
|
|||
Bukkit.getScheduler().runTask(this, () -> {
|
||||
try {
|
||||
validateCrafter();
|
||||
getLogger().info("DUNECHEST_VALIDATION {\"ok\":true,\"cases\":4}");
|
||||
getLogger().info("DUNECHEST_VALIDATION {\"ok\":true,\"cases\":6}");
|
||||
} catch (Exception | AssertionError failure) {
|
||||
getLogger().log(java.util.logging.Level.SEVERE, "DUNECHEST_VALIDATION {\"ok\":false}", failure);
|
||||
} finally {
|
||||
|
|
@ -82,6 +84,27 @@ public final class DuneChestPaperValidation extends JavaPlugin implements Listen
|
|||
|| input.getItem(2) != null || !mk1.equals(input.getItem(3))) {
|
||||
throw new AssertionError("Valid upgrade did not credit and debit the exact stacks");
|
||||
}
|
||||
validateRecipePreview(module, table.getWorld(), placedType, placed, mk1, mk2);
|
||||
}
|
||||
|
||||
private void validateRecipePreview(StorageModule module, World world, Class<?> placedType,
|
||||
Object placed, ItemStack mk1, ItemStack mk2) throws ReflectiveOperationException {
|
||||
Method open = module.getClass().getDeclaredMethod("recipePreview", String.class, placedType, World.class);
|
||||
open.setAccessible(true);
|
||||
Method close = module.getClass().getDeclaredMethod("saveRecipeAndClearPreview", Inventory.class, placedType);
|
||||
close.setAccessible(true);
|
||||
|
||||
Inventory preview = (Inventory) open.invoke(module, "validation", placed, world);
|
||||
if (preview.getType() != InventoryType.WORKBENCH || preview.getSize() != 10 || preview.getHolder() == null
|
||||
|| !mk2.equals(preview.getItem(0)) || !mk1.equals(preview.getItem(5))) {
|
||||
throw new AssertionError("Recipe preview did not expose the expected workbench layout");
|
||||
}
|
||||
close.invoke(module, preview, placed);
|
||||
if (!preview.isEmpty()) throw new AssertionError("Recipe preview did not clear on normal close");
|
||||
|
||||
Inventory orphan = (Inventory) open.invoke(module, "validation", placed, world);
|
||||
close.invoke(module, orphan, null);
|
||||
if (!orphan.isEmpty()) throw new AssertionError("Orphaned recipe preview did not clear");
|
||||
}
|
||||
|
||||
private static Inventory chest(Block block) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue

Staff