Solved Turn on redstone lamp on click

Discussion in 'Spigot Plugin Development' started by Faraaz, May 30, 2017.

  1. Hi Everyone,

    Can someone please tell me in pseudocode how to create a plugin that allows me to turn on a redstone lamp on right click?

    Thanks,
    Faraaz
     
  2. Code (Text):
    @EventHandler
    public void onClick(PlayerInteractEvent e) {
    if(!e.getClickBlock() == null) {
    return;
    }
    if(e.getClickedBlock() == Material.REDSTONE_LAMP) {
    BlockRedstoneLamp lamp = (BlockRedstoneLamp) e.getClickedBlock().getState();
    if(lamp.isOn()) {
    lamp.turnOff();
    return;
    }
    if(lamp.isOff()) {
    lamp.turnOn();
    }

    }
     
     
    • Useful Useful x 1
  3. Thanks for the help! Helped me a lot. Quick question, can I use the equals method instead of '==' or is one preferred over another?

    Thanks,
    Faraaz
     
  4. equals method is actually preferred for most things, the reason being as it's comparing two objects (of the same type) and it can return true when sometimes == can return false.
     
    • Like Like x 6
  5. Primitive types (int, double) don't have an equals method, so you have to use '=='.
    When comparing class instances, the '==' compares the memory location and will only return true, if the objects on the two sides are the same object. Equals, on the other hand, compares the fields inside the object and will return true, if the fields' values are the same.
    '==' is null-safe, meaning that if the *thing* on the left side is null, it won't throw a NullPointerException.
    I suck at explaining stuff, so if you don't yet understand, I advise you to google "java == vs equals", there are a lot of good stackoverflow answers!
     
    • Winner Winner x 1
    • Useful Useful x 1
  6. I think you explained it perfectly, but I was stating for two Objects like he is comparing, .equals surrounded by a try catch would be ideal especially if they want it to be null safe, they would see where the error was more precisely
     
    • Like Like x 5
  7. Thank you guys both for your help!

    Thanks,
    Faraaz
     
  8. Choco

    Moderator

    You should never be catching a NullPointerException. If you've ever caught an NPE, you know you're doing something wrong in your program. You should be doing null checks first, or alternatively, use == where it just makes sense to use it. Take for example World object comparisons. There will only every be one instance of a World object for every Minecraft world, so comparing with == will prevent any accidental NPE's and also compare things a bit more efficiently. If you look at CraftWorld#equals(), it compares their UUID's, but it has to make a null check and class comparison ahead of time as well to assure that there are no NPEs / CCEs
    Code (Java):
        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }

            final CraftWorld other = (CraftWorld) obj;

            return this.getUID() == other.getUID();
        }
    Also worth noting that when comparing enumeration constants (or constants in general), for the very same reason mentioned above by @Trigary, == compares their instances and there will only ever be one instance of a constant. Material constants should be compared with ==, but by default, #equals() compares objects with ==.

    See this Stackoverflow thread on Runtime Exception catches. (I love the term "Pokemon exception catching". Gotta catch 'em all!)

    TL;DR: If you can use ==, use it over .equals(), and never catch an NPE.
     
  9. This is not a resource, please remove the tag.
     
    • Agree Agree x 1
  10. Sorry, chose the wrong one.