EssemCSH
EssemCSH

Reputation: 103

Minecraft Java Plugin - Block locations are the same but it still doesnt work

Anyone who can answer this problem. I am storing beacon location in a config. When a beacon is broken i am getting the location of the block that is breaking and checking if its equal to the one in my config. I dont know why its not working and I even did p.sendmessage and sent both values as a message and they're the excact same...

https://pastebin.com/Kd4eZyJc Line 1181 is the block break event.

Block break event:

public HashMap<Integer, Block> beaconStore = new HashMap<>();
// WHEN PLAYER BREAKS BLOCK
@EventHandler
public void onBreak(BlockBreakEvent event){
    Player p = event.getPlayer();
    Block block = event.getBlock();

    if (p.getItemInHand().getType() == Material.WOOD_HOE) {
        event.setCancelled(true);

        w = block.getLocation().getWorld().getName();
        x = block.getLocation().getX() + 0.500;
        y = block.getLocation().getY();
        z = block.getLocation().getZ() + 0.500;

        p.sendMessage("§6Saved coordinates for block. (World:" + w + " x:" + x + "," + " y:" + y + "," + " z:" + z + ")");

    }

    if (gameRunning == true) {
        boolean okBreak = false;
        for(Integer integer : BlockReset.keySet()){
              Block bl = BlockReset.get(integer);

              if (bl.equals(block)) {
                  okBreak = true;
              }
              else {

              }
        }

        Location blockLocation = block.getLocation();

        if (block.getType() == Material.BEACON) {
            okBreak = true;

            p.sendMessage(" " + blockLocation);

            World world = block.getWorld();



            double xyellowBeacon1 = getConfig().getDouble("yellowbeacon1.x");
            double yyellowBeacon1 = getConfig().getDouble("yellowbeacon1.y");
            double zyellowBeacon1 = getConfig().getDouble("yellowbeacon1.z");
            Location yellowBeacon1Location = new Location(world, xyellowBeacon1, yyellowBeacon1, zyellowBeacon1);


            double xyellowBeacon2 = getConfig().getDouble("yellowbeacon2.x");
            double yyellowBeacon2 = getConfig().getDouble("yellowbeacon2.y");
            double zyellowBeacon2 = getConfig().getDouble("yellowbeacon2.z");
            Location yellowBeacon2Location = new Location(world, xyellowBeacon2, yyellowBeacon2, zyellowBeacon2);

            double xyellowBeacon3 = getConfig().getDouble("yellowbeacon3.x");
            double yyellowBeacon3 = getConfig().getDouble("yellowbeacon3.y");
            double zyellowBeacon3 = getConfig().getDouble("yellowbeacon3.z");
            Location yellowBeacon3Location = new Location(world, xyellowBeacon3, yyellowBeacon3, zyellowBeacon3);

            double xyellowBeacon4 = getConfig().getDouble("yellowbeacon4.x");
            double yyellowBeacon4 = getConfig().getDouble("yellowbeacon4.y");
            double zyellowBeacon4 = getConfig().getDouble("yellowbeacon4.z");
            Location yellowBeacon4Location = new Location(world, xyellowBeacon4, yyellowBeacon4, zyellowBeacon4);

            if (blockLocation == yellowBeacon1Location) {
                yellowbcount--;
            }
            if (blockLocation == yellowBeacon2Location) {
                yellowbcount--;
            }
            if (blockLocation == yellowBeacon3Location) {
                yellowbcount--;
            }
            if (blockLocation == yellowBeacon4Location) {
                yellowbcount--;
            }



            double xblueBeacon1 = getConfig().getDouble("bluebeacon1.x");
            double yblueBeacon1 = getConfig().getDouble("bluebeacon1.y");
            double zblueBeacon1 = getConfig().getDouble("bluebeacon1.z");
            Location blueBeacon1Location = new Location(world, xblueBeacon1, yblueBeacon1, zblueBeacon1);

            double xblueBeacon2 = getConfig().getDouble("bluebeacon2.x");
            double yblueBeacon2 = getConfig().getDouble("bluebeacon2.y");
            double zblueBeacon2 = getConfig().getDouble("bluebeacon2.z");
            Location blueBeacon2Location = new Location(world, xblueBeacon2, yblueBeacon2, zblueBeacon2);

            double xblueBeacon3 = getConfig().getDouble("bluebeacon3.x");
            double yblueBeacon3 = getConfig().getDouble("bluebeacon3.y");
            double zblueBeacon3 = getConfig().getDouble("bluebeacon3.z");
            Location blueBeacon3Location = new Location(world, xblueBeacon3, yblueBeacon3, zblueBeacon3);

            double xblueBeacon4 = getConfig().getDouble("bluebeacon4.x");
            double yblueBeacon4 = getConfig().getDouble("bluebeacon4.y");
            double zblueBeacon4 = getConfig().getDouble("bluebeacon4.z");
            Location blueBeacon4Location = new Location(world, xblueBeacon4, yblueBeacon4, zblueBeacon4);

            if (blockLocation == blueBeacon1Location) {
                bluebcount--;
            }
            if (blockLocation == blueBeacon2Location) {
                bluebcount--;
            }
            if (blockLocation == blueBeacon3Location) {
                bluebcount--;
            }
            if (blockLocation == blueBeacon4Location) {
                bluebcount--;
            }



            double xgreenBeacon1 = getConfig().getDouble("greenbeacon1.x");
            double ygreenBeacon1 = getConfig().getDouble("greenbeacon1.y");
            double zgreenBeacon1 = getConfig().getDouble("greenbeacon1.z");
            Location greenBeacon1Location = new Location(world, xgreenBeacon1, ygreenBeacon1, zgreenBeacon1);

            double xgreenBeacon2 = getConfig().getDouble("greenbeacon2.x");
            double ygreenBeacon2 = getConfig().getDouble("greenbeacon2.y");
            double zgreenBeacon2 = getConfig().getDouble("greenbeacon2.z");
            Location greenBeacon2Location = new Location(world, xgreenBeacon2, ygreenBeacon2, zgreenBeacon2);

            double xgreenBeacon3 = getConfig().getDouble("greenbeacon3.x");
            double ygreenBeacon3 = getConfig().getDouble("greenbeacon3.y");
            double zgreenBeacon3 = getConfig().getDouble("greenbeacon3.z");
            Location greenBeacon3Location = new Location(world, xgreenBeacon3, ygreenBeacon3, zgreenBeacon3);

            double xgreenBeacon4 = getConfig().getDouble("greenbeacon4.x");
            double ygreenBeacon4 = getConfig().getDouble("greenbeacon4.y");
            double zgreenBeacon4 = getConfig().getDouble("greenbeacon4.z");
            Location greenBeacon4Location = new Location(world, xgreenBeacon4, ygreenBeacon4, zgreenBeacon4);

            if (blockLocation == greenBeacon1Location) {
                greenbcount--;
            }
            if (blockLocation == greenBeacon2Location) {
                greenbcount--;
            }
            if (blockLocation == greenBeacon3Location) {
                greenbcount--;
            }
            if (blockLocation == greenBeacon4Location) {
                greenbcount--;
            }



            double xredBeacon1 = getConfig().getDouble("redbeacon1.x");
            double yredBeacon1 = getConfig().getDouble("redbeacon1.y");
            double zredBeacon1 = getConfig().getDouble("redbeacon1.z");
            Location redBeacon1Location = new Location(world, xredBeacon1, yredBeacon1, zredBeacon1);
            p.sendMessage(" " + redBeacon1Location);

            double xredBeacon2 = getConfig().getDouble("redbeacon2.x");
            double yredBeacon2 = getConfig().getDouble("redbeacon2.y");
            double zredBeacon2 = getConfig().getDouble("redbeacon2.z");
            Location redBeacon2Location = new Location(world, xredBeacon2, yredBeacon2, zredBeacon2);

            double xredBeacon3 = getConfig().getDouble("redbeacon3.x");
            double yredBeacon3 = getConfig().getDouble("redbeacon3.y");
            double zredBeacon3 = getConfig().getDouble("redbeacon3.z");
            Location redBeacon3Location = new Location(world, xredBeacon3, yredBeacon3, zredBeacon3);

            double xredBeacon4 = getConfig().getDouble("redbeacon4.x");
            double yredBeacon4 = getConfig().getDouble("redbeacon4.y");
            double zredBeacon4 = getConfig().getDouble("redbeacon4.z");
            Location redBeacon4Location = new Location(world, xredBeacon4, yredBeacon4, zredBeacon4);

            if (blockLocation == redBeacon1Location) {
                redbcount--;
                p.sendMessage("Okay!");
            }
            if (blockLocation == redBeacon2Location) {
                redbcount--;
            }
            if (blockLocation == redBeacon3Location) {
                redbcount--;
            }
            if (blockLocation == redBeacon4Location) {
                redbcount--;
            }
        }

        if (okBreak == true) {
            event.setCancelled(false);
        }
        else {
            event.setCancelled(true);
            p.sendMessage("§9§lPillars " + dot + " §7You can only break blocks placed by a player!");
        }
    }
}

Setting the beacon location in command:

if (args[0].equalsIgnoreCase("yellowbeacon1")) {
    getConfig().set("yellowbeacon1.world", w);
    getConfig().set("yellowbeacon1.x", x - 0.5);
    getConfig().set("yellowbeacon1.y", y);
    getConfig().set("yellowbeacon1.z", z - 0.5);
    saveConfig();

    p.sendMessage("§9§lPillars " + dot + " §7Successfully set §eyellow §5beacon1!");
}

^^ I do that for every beacon.

I hope you understand my problem.Getting both locations

Upvotes: 0

Views: 79

Answers (1)

Henri Viik
Henri Viik

Reputation: 664

Might not be causing the issue you're getting, but when comparing Location class instances, use equals method instead of == as they're definitely not the same instance when you're creating one of the compared locations in the same method.

Not strongly related to your issue, but refactor your code a bit. If you have to copy-paste something, it marks as something that you could extract as method. So create method with signature Location getLocation(World world, String beaconprefix), which fetches block location from config and creates it, significantly reducing the code length.

private Location getLocation(World world, String beaconPrefix) {
  double x = getConfig().getDouble(beaconPrefix + ".x");
  double y = getConfig().getDouble(beaconPrefix + ".y");
  double z = getConfig().getDouble(beaconPrefix + ".z");
  return new Location(world, x, y, z);
}

Upvotes: 2

Related Questions