How to access a painting on a right click.

Discussion in 'Plugin Development' started by EpicBlargh, Jul 5, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    EpicBlargh

    So, I can obviously get the block, but how can I get the painting so I can get the art that is displayed on the painting? I don't know how to access that.

    Thanks for any help.

    ~Me
     
  2. Paintings are not blocks but entities. The event you need is most likely "PlayerInteractEntityEvent", never tested whether that fires for paintings, though.
     
  3. Offline

    one4me

    It does.

    EpicBlargh - for instance, with this code:
    Code:
    public void onPlayerInteractEntityEvent(PlayerInteractEntityEvent event) {
      String s = (event.getRightClicked());
    }
    If you right clicked on an 'Aztec' picture, s would be set to "CraftPainting{art=AZTEC}"
     
  4. Offline

    EpicBlargh

    Awesome. It works. But, I've tried many different things. About 3. I'm trying to set the painting itself. So can I get the entity that I clicked in code? Like... if(event.ClickedEntity.equals(Material.Painting)){};
     
  5. if(entity instanceof Painting)

    and then you can cast entity to Painting and use its methods to get which picture it is.
     
  6. Offline

    EpicBlargh

    Code:
        public void onPlayerInteractEntityEvent(PlayerInteractEntityEvent event){
            if(event.getRightClicked() instanceof Painting){
               
            }
        }
    I have that. And I still can't grab any methods of retrieving type of art on the painting. It still acts as the event does.
     
  7. Offline

    EnvisionRed

    Cast it to painting and then there are painting.getArt() and painting.setArt() methods which you can call.
     
  8. Offline

    EpicBlargh

    Uh. *Cough* What do you mean cast? Sorry. I've been in C# courses and Javascript courses for a while. I imagine it's similar to Java, but I want to make sure. I just started getting back to making plugins.
     
  9. Offline

    EnvisionRed

    You can cast an object to another object like so:
    Code:
    Player p = (Player) exampleMethodThatReturnsEntity();
    Note that without the "(Player)" bit it would return as an entity, not a player. If the entity it returns is NOT a player, you're going to get an error. That's why you always check if the object is an instance of what you are going to cast it to. You do that like this:
    Code:
    Entity e = someMethodThatReturnsAnEntity();
    if (e instanceof Player) {
    Player player = (Player) e;
    } else {
    //do what you would if it's not a player, probably return.
    }
    So in your case, with the PlayerInteractEvent, you could do this:

    Code:
    Entity e = event.getEntity();
    if (e instanceof Painting) {
    Painting p = (Painting) e;
    Art art = p.getArt();
    if (art == someArtType) {
    //do stuff
    } else {
    return;
    }
    } else {
    return;
    }
    I hope this helps.
     
  10. Offline

    sayaad

    Well think of it like this, its not what im about to tell you but you should think of it as for better understanding. (When you become an expert in java you will understand why its not the following)

    Lets say you have a value tat is equal to 1. This can be an Integer, a float or a double (etc). You are using the variable that has an argument that requires a double. If you just simply do this :
    Code:java
    1. void(variable);

    There is no way of knowing what type of value the variable is. If it is a numeric value, you will need to convert the variable to a double and you do this by "casting" double. When done this your code will look like :
    Code:java
    1. void( (double) variable );

    Where (double) is the cast.

    Hope I helped you understand java better.
     
  11. Offline

    nisovin

    FYI, casting in Java works pretty much exactly the same as in C#.
     
  12. Offline

    one4me

    Here's an example of how you would go about setting a painting's art to AZTEC.
    Code:
    @EventHandler
    public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
      Entity entity = event.getRightClicked();
      if (entity instanceof Painting) {
        Painting painting = (Painting)entity;
        painting.setArt(Art.AZTEC);
      }
    }
     
    ferrybig likes this.
  13. Offline

    EpicBlargh

    Alright. Thanks! I promise not to be dumb anymore.
     
Thread Status:
Not open for further replies.

Share This Page