0

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.

1 Answers1

0

It looks like you have an excess field in your SpawnData setup. Remove the entity: in your SpawnData. So, that portion of your String should look like this:

SpawnData:{id:\"minecraft:item\",Item:{id:\"" + key.getNameSpace() + ":" + key.getKey() + "\",Count:1b}}} replace"

Update: You can't use a CreatureSpawner to spawn things that aren't listed in the EntityType enum. However, you can "fake" this by calling the dropItem() function using the spawner's location as the first argument. So, something like this could be an option:

...
    ItemStack itemToSpawn = new ItemStack(item.getType() );
    itemToSpawn.setAmount(1);

    World world = player.getWorld();
    
    world.dropItem(spawner.getLocation(), itemToSpawn)

Update 2: If you want to "save" the type of item "spawned" by a CreatureSpawner, then, at a very high-level, my approach would be the following:

  1. In your onRightClick function, whenever a spawner is right-clicked, write the name of the World that the spawner is in, the x, y, z coordinates of the spawner, and the item to a config file. So, for example, if a certain spawner was in a world called "myAwesomeWorld" and located at x: 467, y: 867, z: -3987, and set to spawn diamonds, then this might be an entry in the config file:
myAwesomeWorld-467,867,-3987-diamond
  1. In your plugin's onEnable() method, read all the spawner info from the config and store it in some variables. I might suggest creating a Map<Location, ItemStack> to hold the locations of each spawner and the item you want each to spawn.

  2. In an event handler with SpawnerSpawnEvent, get the spawner's Location and check it against all of the spawners' information you read in from the configuration file in onEnable. If you get a matching Location (i.e. the world names match and the x/y/z coordinates match), then you do the following two things:

3a) Cancel the original SpawnerSpawnEvent by calling setCancelled(true) on the event so the mob doesn't spawn.

3b) Call the dropItem method as I mentioned above.

I'd be happy to provide more information on these points if you'd like. Just let me know.

fireshadow52
  • 6,298
  • 2
  • 30
  • 46
  • Sadly, this doesn't work. Actually, my command from the first post works, but only if you execute it ingame as player. Using the plugin it doesn't work. If possible, I'd preffer to use a solution using CreatureSpawner, since it's more version compatible. – Minecreator200 Apr 29 '23 at 17:38
  • @Minecreator200 Ah; sorry about that. I've updated my answer with a potential workaround. – fireshadow52 Apr 30 '23 at 02:31
  • That makes sense. But is it possible to combine this with a config and the SpawnerSpawnEvent too? Because I want to save the items and then spawn them as the spawner would normally do. Also I dont want the spawner to spawn other items than the specified one. How is this possible? – Minecreator200 Apr 30 '23 at 08:25
  • @Minecreator200 Sure; I've updated the post with my suggested high level approach. – fireshadow52 May 01 '23 at 19:29