Help With Block Data

Discussion in 'Spigot Plugin Development' started by IKFFxRaPzZ, Apr 18, 2019.

  1. Ive figured out a way to save block loxcations to the config but im not having much luck getting the server to look for the specific blocks location, every block of that type is triggering the event where as i want only specific ones to trigger it this is the code:

    Code (Text):
        @EventHandler
        public void onPlace(BlockPlaceEvent event) {

            Block block = event.getBlock();
            Player player = event.getPlayer();

            Location b_loc = block.getLocation();
            if (player.hasPermission("hs.hs")) {
                if (block.getType().equals(Material.getMaterial(plugin.getConfig().getString("Healingstone.BlockType")))) {
                    plugin.getConfig().set("HealingBlocks." + count + ".world", b_loc.getWorld().getName());
                    plugin.getConfig().set("HealingBlocks." + count + ".x", b_loc.getBlockX());
                    plugin.getConfig().set("HealingBlocks." + count + ".y", b_loc.getBlockY());
                    plugin.getConfig().set("HealingBlocks." + count + ".z", b_loc.getBlockZ());
                    plugin.saveConfig();
                    count++;
                    player.sendMessage(ChatColor.GOLD + "You Placed A Healing Block");
                }
            }
        }
    This is the event to trigger:

    Code (Text):
        @EventHandler
        public void onInteract(PlayerInteractEvent event) {

            Action action = event.getAction();
            Player player = event.getPlayer();
            Block block = event.getClickedBlock();

            if (action.equals(Action.LEFT_CLICK_BLOCK)) {
                if (block.getType().equals(Material.getMaterial(plugin.getConfig().getString("Healingstone.BlockType")))) {
                        if (player.hasPermission("hs.maxhealth")) {
                            if (player.getHealth() == player.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue()) {
                                player.sendMessage(ChatColor.translateAlternateColorCodes('&',
                                        plugin.getConfig().getString("Message.Healthfull")));
                                return;
                            }
                        }

                    final EconomyResponse response = plugin.getEconomy().withdrawPlayer(player,
                            plugin.getConfig().getDouble("Economy.Cost"));
                    if (!response.transactionSuccess()) {
                        player.sendMessage(ChatColor.translateAlternateColorCodes('&',
                                plugin.getConfig().getString("Message.Nofunds")));
                        player.sendMessage(ChatColor.GREEN + "You Need" + " " + "$"
                                + (plugin.getConfig().getString("Economy.Cost")) + " " + "To Heal");
                        // Not enough money
                        return;

                    } else {
                        player.setHealth(player.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue());
                        player.sendMessage(ChatColor.translateAlternateColorCodes('&',
                                plugin.getConfig().getString("Message.Healthrestored")));

                    }

                }
                return;
            }
     
  2. You could use block metadata, however upon the server restarting it will reset like a HashMap.
    I would put the location of the block in a HashSet, and then store it in a file.
    Then, when you right click a block, you could get the blocks location and check if the HashSet has it.
    Code (Java):
    public HashSet<Location> healingblocks = new HashSet<>();
    // Use this to store the blocks location (when they place it)
    healingblocks.add(block.getLocation());

    // Use this to check if the block is a healing block
    if (healingblocks.contains(block.getLocation()) {
          // Do stuff here
    }
    The last thing you would need to do is store the locations in a file when the server stops/starts. You would just do it the same way you already did.
     
  3. I recommend to save data in your own storing system. Like a location/line
     

Share This Page