Question How do I make a variable local to an individual player?

Discussion in 'Bukkit Help' started by Lenger, Aug 6, 2020.

  1. Offline

    Lenger

    My friend and I are new to bukkit coding and we've been experimenting with some projects in order to learn about the process. We're trying to make a plugin that allows players to stack blocks by simply clicking the bottom block. Here's our code:

    Code:
    public class BlockExperiments implements Listener {
      
    public int wallHeight = 1;
        @EventHandler
        public void onInteract(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            Action action = event.getAction();
            Block clickedBlock = event.getClickedBlock();
            ItemStack oakBlocks = new ItemStack(Material.OAK_PLANKS);
          
            if(action.equals(Action.RIGHT_CLICK_BLOCK)) {
                if(event.getHand() == EquipmentSlot.HAND) {
                    player.sendMessage("Right clicked block");
                    if(clickedBlock.getType().equals(Material.OAK_PLANKS)) {
                        player.sendMessage("Block is oak plank");
                        if(clickedBlock.getLocation().add(0,wallHeight,0).getBlock().getType().equals(Material.AIR)) {
                            player.sendMessage("There is space above block");
                            player.getWorld().getBlockAt(clickedBlock.getLocation().add(0, wallHeight, 0).getBlockX(), clickedBlock.getLocation().add(0, wallHeight, 0).getBlockY(), clickedBlock.getLocation().add(0, wallHeight, 0).getBlockZ()).setType(Material.OAK_PLANKS);
                            player.sendMessage(ChatColor.RED + "Wall height was previously: " + Integer.toString(wallHeight) + ". Adding +1.");
                            wallHeight = wallHeight + 1;
                            player.sendMessage(ChatColor.RED + "Wall height is now: " + Integer.toString(wallHeight) + ".");
                            player.getInventory().removeItem(oakBlocks);
                        }
                        else {
                            player.sendMessage("Looks like there's a block above the last plank.");
                            player.sendMessage(ChatColor.RED + "Wall height was previously: " + Integer.toString(wallHeight) + ". Adding +1.");
                            wallHeight = wallHeight + 1;
                            player.getWorld().getBlockAt(clickedBlock.getLocation().add(0, wallHeight, 0).getBlockX(), clickedBlock.getLocation().add(0, wallHeight, 0).getBlockY(), clickedBlock.getLocation().add(0, wallHeight, 0).getBlockZ()).setType(Material.OAK_PLANKS);
                            player.sendMessage(ChatColor.RED + "Wall height is now: " + Integer.toString(wallHeight) + ".");
                            player.getInventory().removeItem(oakBlocks);
                        }
              
                    }
                    else {
                        player.sendMessage("The wall height has been set to 1.");
                        wallHeight = 1;
                    }
                }
            }
        }
    }
    This works fine when one person is building a stack at a time. Our issue is that the variable "wallheight" seems to be server-wide so when multiple players try to build a block stack it throws everything off. Is there a way to make this variable only impact each individual player?
     

Share This Page