0

What's the best way to create a unique block that's can be identified with spigot?

Something like a id. I want to create a game where you can place items, but I dont know how to identify if the player clicked on the right block. When there's no better way I'll just use the block type/material

akut Game
  • 1
  • 1

2 Answers2

1

You could use a Hashmap to store the blocks Location as key and the players UUID as value, and then check if the UUID of the right-clicking player is equal to the one stored in the Hashmap using the clicked blocks Location.

For example: In any class it fits, probably your Event Listener:

public static HashMap<Location, UUID> blocks = new HashMap<>();

Store your data in the HashMap using BlockPlaceEvent in your Event Listener:

@EventHandler
public static void onBlockPlace(BlockPlaceEvent event) {
    blocks.put(event.getBlock().getLocation(), event.getPlayer().getUniqueId());
}

Check if the player clicked the correct block using PlayerInteractEvent in your EventListener:

@EventHandler
public static void onPlayerInteract(PlayerInteractEvent event) {
    if (blocks.get(event.getClickedBlock().getLocation()) != null && blocks.get(event.getClickedBlock().getLocation()) == event.getPlayer().getUniqueId()) {
        // Do whatever you want if player clicked the correct block right here, like:
        event.getPlayer().sendMessage("You clicked the correct block!");
    }
}

I hope I was able to help you out!

Simon
  • 21
  • 3
0

I'm not 100% what you need, so I have provided two answers I believe could solve your problem, along with some extra information that might be helpful.

Listener

I believe what you are looking for is a Listener. Here is a link to the Spigot article on how to set one up. I would take a look at it first so you know what a Listener is, how it works, and how to add it into your code (if you don't already know).

The particular Listener you are looking for is called PlayerInteractEvent. It is an event that is called when a player interacts with an object. Here is the documentation.

Specific Block Check

If you simply want to check whether or not a player clicked a specific block, you can use the getClickedBlock() and getType() methods to do that. I have provided an example implementation of that below. The code checks to see if a player clicks on stone and then performs an action if they have.

@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
    if (event.getClickedBlock() != null && event.getClickedBlock().getType() == Material.STONE) {
        // Actions to take here
    }
}

Custom Block Check

Depending on how many unique blocks you are wanting, there are a few different ways you could go about a custom block check. Based on the knowledge I currently have, I'm not 100% sure how to implement these (I just did a little research to find them), but they should suit your needs).

The first method is using a HashMap, which allows you to store a key and a value and then access them or reference them. Here is a link on how to implement them in Spigot.

The other thing you could use is the Material class in the Spigot API. Here are the docs for that class.

atlaska826
  • 64
  • 7