I'm currently programming a Minecraft Plugin for Spigot 1.19 with custom spawners. I need to make the spawner spawn an item it's right clicked with. I'm detecting this using the PlayerInteractEvent. That's what I have so far:
@EventHandler
private void onRightClick(PlayerInteractEvent event) {
final Block block = event.getClickedBlock();
final CreatureSpawner spawner = (CreatureSpawner) block;
final ItemStack item = player.getInventory().getItemInMainHand();
final NamespacedKey key = item.getType().getKey();
// That's the thing that doesn't work
final String command = "execute as " + player.getName() + " at @s run setblock " + block.getX() + " " + block.getY() + " " + block.getZ() + " minecraft:spawner{SpawnData:{entity:{id:\"minecraft:item\",Item:{id:\"" + key.getNamespace() + ":" + key.getKey() + "\",Count:1b}}}} replace";
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command);
}
While it doesn't throw an error, it simply doesn't work. Do you have any solutions on how to fix this?
Please notice that I've left out a few parts to make it easier to understand.
You can just use a normal spawner and a Hashmap. Cancel the SpawnerSpawnEvent and drop the item stored in the Hashmap at the blocks position.
@EventHandler
private void onSpawn(SpawnerSpawnEvent event) {
event.setCancelled(true);
event.getLocation().getWorld().spawnItem(
event.getLocation(),
blockMap.get(event.getLocation())
);
}