0

I'm trying to make a Minecraft spigot plugin using . The bit that I'm struggling with is that I would like an event to be triggered when I place a certain item with a certain name and then that item be replaced with another.

For example, when a soul torch renamed "hello" in an anvil is placed it should be replaced with a barrel with a kit in it. I know how to cancel the original item and replace it with another, I just can't figure out or find anywhere about having the placed item only trigger the event if it has a certain name, and I don't know how to have the barrel have items in it (I've copied the nbt tag thing ingame with the /data command on a barrel that has the items I would like to use in it).

I've tried using the BlockData() stuff, it could be right because I have not idea how to use it or it could be just really incorrect.

This is my code so far:

@EventHandler(priority = EventPriority.LOW)
public void onTorchPlace_Low(BlockPlaceEvent event) {
  if (event.getBlock().getType() == Material.TORCH) {
    event.getBlock().setType(Material.DIAMOND_BLOCK.);
  }
}

@EventHandler
public void onTorchPlace_Normal(BlockPlaceEvent event) {
  Block block = event.getBlock();
  if (block.getType() != Material.TORCH) {
    return;
  }
  Bukkit.getLogger().info("A torch was placed");
}
Lucan
  • 2,907
  • 2
  • 16
  • 30
  • Hello and welcome on StackOverflow. Can you show your actual code? Also, you can use logger to be sure event are called, you can be sure it's well register in your onEnable method – Elikill58 Jul 26 '23 at 17:42
  • @EventHandler(priority = EventPriority.LOW) public void onTorchPlace_Low(BlockPlaceEvent event) { if (event.getBlock().getType() == Material.TORCH) { event.getBlock().setType(Material.DIAMOND_BLOCK.); } } @EventHandler public void onTorchPlace_Normal(BlockPlaceEvent event) { Block block = event.getBlock(); if (block.getType() != Material.TORCH) { return; } Bukkit.getLogger().info("A torch was placed"); } – randomUser153 Jul 26 '23 at 18:38
  • I'm not sure if there's a better way to add it, this bit works at the moment but doesn't do what I would like it to. – randomUser153 Jul 26 '23 at 18:39
  • Please edit your question in the future. – Lucan Jul 26 '23 at 20:41

1 Answers1

1

You can achieve what you want like so:

@EventHandler
public void onBlockPlace(BlockPlaceEvent event){
    // Check the item is a SOUL_TORCH
    if(event.getItemInHand().getType() == Material.SOUL_TORCH
            // Check the item has metadata
            && event.getItemInHand().hasItemMeta()
            // Check that the item has a display name (has been renamed)
            && event.getItemInHand().getItemMeta().hasDisplayName()
            // Check if the name of item is what we're looking for (case-insensitive; e.g. "hello" or "HeLLo")
            && event.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase("hello")) {
        // Set the block to a barrel
        event.getBlock().setType(Material.BARREL);
        // Cast the block's state to `Barrel`, which we know is safe to do because we just set it to a barrel
        Barrel barrel = (Barrel) event.getBlock().getState();
        // Add our items to the inventory of the barrel. In this case, one diamond block
        barrel.getInventory().addItem(new ItemStack(Material.DIAMOND_BLOCK));
    }
}

This code is an example of how to achieve what you're after. It will cause a barrel to be placed with a diamond block inside, where the soul torch would've been so long as the soul torch's name was "hello" (regardless of case).

Lucan
  • 2,907
  • 2
  • 16
  • 30