Solved Final variable being changed?

Discussion in 'Plugin Development' started by HeyAwesomePeople, Feb 21, 2015.

Thread Status:
Not open for further replies.
  1. Hello! I am creating a plugin where I need to place snow block and then remove them and place back the old item again. Only I am having an issue.

    Here is my code:
    Code:
        public void createSnowTrail(Entity p) {
            if (p.getPassenger() == null) return;
            if (!(p.getPassenger() instanceof Player)) return;
            final Block bOld = p.getWorld().getBlockAt(p.getLocation());
            final Block b = p.getWorld().getBlockAt(p.getLocation());
            Bukkit.broadcastMessage("1Block: " + b.getType().toString());
            Bukkit.broadcastMessage("1Block Old: " + bOld.getType().toString());
            if (b.getType().equals(Material.SNOW)) return;
            if (plugin.config.unbreakableBlocks.contains(b.getType().toString())) return;
            if (p.getWorld().getBlockAt(p.getLocation().add(0D, -1D, 0D)).getType().equals(Material.AIR)) {
                if (!((Player) p.getPassenger()).hasPermission("mounts.admin.zolt")) {
                    return;
                }
            }
    
            b.setType(Material.SNOW);
    
            Bukkit.broadcastMessage("2Block: " + b.getType().toString());
            Bukkit.broadcastMessage("2Block Old: " + bOld.getType().toString());
    
            Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                @SuppressWarnings("deprecation")
                public void run() {
                    if (b.getType().equals(Material.SNOW)) {
                        Bukkit.broadcastMessage("3Block: " + b.getType().toString());
                        Bukkit.broadcastMessage("3Block Old: " + bOld.getType().toString());
                        b.setType(bOld.getType());
                        b.setData(bOld.getData());
                    }
                }
            }, 80L);
        }
    What this should do it set any ground to snow and then set it back to whatever it was after about 4 seconds. Only issue is, it doesn't do that. What is happening is I am getting the same block and storing it into 2 locations. One location, just b, I will use to change and edit the ground. The second, bOld, should be the old block that was at that location. Only this isn't the case. As soon as I change b(location noted in code), both bOld and b change. In this case, I am changing b to a snow slab. This causes the second and third set of debug messages to return snow for both variables, rendering the block replacing useless. How can I get around this issue? I have even tried storing the old block in a hashmap but same result!

    And shouldn't a final variable mean it doesn't change? The variable is acting like a method, running each time I get it. Same thing happens if the final variable is gone. How is this even possible?

    Thanks,
    HeyAwesomePeople
     
  2. Offline

    Qwertyness_

    If you just set both variables to the same block instance, its just that, the same instance. You have to clone the instance in order to get them to change independently. (Should be block.clone(), but Im not sure if that class has a clone method)
     
  3. @Qwertyness_ I guess, but no cloning in the class. But you sparked an idea that worked.

    I just made bOld a blockstate, meaning that b and bOld are genetically different, but can be used basically the same way. Works perfectly. Thanks.

    @Qwertyness_ Well now new issue.
    When I break a Long_Grass to make it snow, when it reverts back to its orginal block, the grass is now dead, even though I set a data.

    Code:
    b.setType(bOld.getType());
                        b.setData(bOld.getBlock().getData());
    Why isn't this working?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
  4. Offline

    Qwertyness_

    I don't know if this is why, but setData() is deprecated. You may want to go through BlockState to set the data value.
     
  5. I keep posting then finding the answer myself. Seems the hour I spent debugging before this was useless, but once I post I figure it all out quickly. I fixed it by just updating the old blockstate. Thanks
     
  6. Offline

    MCMatters

  7. I did like, an hour and a half before you posted that.
     
    Skionz likes this.
Thread Status:
Not open for further replies.

Share This Page