Player Looking At Block Not Working

Discussion in 'Plugin Development' started by Mudkiper202, Oct 12, 2016.

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

    Mudkiper202

    Code:
    for(int id=0; plugin.stakes.size()>id; id++) {
                        p.sendMessage("test1");
                        if(p.getTargetBlock((Set) null, 5) == plugin.stakes.get(id)) {
                            p.sendMessage("test2");
    Code:
    public HashMap<Integer, Location> stakes = new HashMap<Integer, Location>();
    Ok so i have tried many things to fix it and still no use. I try supresswarning but that only tells it to not underline it as a warning and doesnt fix the problem. After the if(p.gettarget.........) the code does not run, ive tried to do (Set<Material>) but no hope. Please help!
     
  2. Offline

    timtower Administrator Administrator Moderator

    @Mudkiper202 You are comparing a block with a location.
    Those will never be the same.
    Use getLocation() on the block at least.
     
  3. Offline

    Zombie_Striker

    This will never be true:
    1. p.getTargetBlock returns a block. Not a location.
    2. == compares memory space. That means the location not only has to have the right values, but also has to be the same instance as the blocks (which will almost never be true). Use .equals for comparing values

    Never just suppress warnings. Warnings are there for a reason. Don't just ignore them.

    Also, use a for-each loop for the key set instead of doing an int increment. That will stop null or empty ids from being tested (and as such, will mean you have to do less checks and will receive error less often).
     
  4. Offline

    Mudkiper202

    I have fixed the code and got rid of suppress warnings but still doesnt work
    Code:
    if(p.getTargetBlock((Set<Material>) null, 5).getLocation().equals(plugin.stakes.get(id)))
     
  5. Offline

    Zombie_Striker

    @Mudkiper202
    1. Does "targetblock" return the block you want?
    2. Does the Location at "id" return the same location as the target's location?
    3. Are you sure the location at "id" is not null? Are you sure the hashmap contains the location/id?
    Also, remember that if the targeted block is "air", then it will return a null value. Make sure you nullcheck before you get the target block.
     
  6. Offline

    Mudkiper202

    What i am doing is seeing if the block they click is equal to any of the hashmap locations

    Can You please help?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 13, 2016
Thread Status:
Not open for further replies.

Share This Page