Plugin Help Metadata not saving? And NullPointerException whilst trying to get from my config

Discussion in 'Plugin Help/Development/Requests' started by BizarrePlatinum, Apr 8, 2015.

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

    BizarrePlatinum

    Code:
    @EventHandler
        public void onInteract(PlayerInteractEvent e) {
            int given = randInt(1000, 2000);
            e.getAction();
            Player p = e.getPlayer();
            Block b = e.getClickedBlock();
         
            if(!b.hasMetadata("openedby") && b.getType().equals(Material.CHEST)) {
                b.setMetadata("openedby", new FixedMetadataValue(this, ""));
            }
         
            if(!b.getMetadata("openedby").contains(p.getUniqueId().toString())) {
                if(e.getAction().equals(Action.RIGHT_CLICK_BLOCK) && b.getType().equals(Material.CHEST)) {
                    p.sendMessage(ChatColor.BLUE + "You found R " + given + " in that chest!");
                    SettingsManager.getInstance().addBalance(p, given);
                    b.setMetadata("openedby", new FixedMetadataValue(this, p.getUniqueId().toString()));
                }
            }
        }
    
    My meta data appears not to be saving to my chest, I am attempting to make the chest give the player money, but only once. There something I'm doing wrong here?

    Also, I am trying to add broadcasts to my server, but I am getting an error when I try to get the information from my config. This is my first time using a scheduler, so there's a chance I'm doing something wrong there.
    Code:
    public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
            scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
                @Override
                public void run() {
                    int nextBroadcast = 0;
                        Bukkit.getServer().broadcastMessage(ChatColor.YELLOW + getConfig().getConfigurationSection("broadcasts." + nextBroadcast).toString());
                        saveDefaultConfig();
                        if(!(nextBroadcast == 4)) {
                            nextBroadcast += 1;
                        }else {
                            nextBroadcast = 0;
                        }
                    }
                }, 0L, 600L);
            }
    
     
  2. Offline

    cnc4

    @BizarrePlatinum

    -I think you should save the metadata to a config file.

    -Try getting the config buy: getConfig().get(YOURPATH);
     
  3. Offline

    BizarrePlatinum

    @cnc4 unless block metadata is different to player metadata, I shouldn't have to save it to a config, I'll try getting the config using that, but, as far as I know, I should be doing it right.

    EDIT -- It appears to have worked changing it to .get(), but now, my counter for the broadcast isn't working it appears, as it will only send the first broadcast.

    Code:
    public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
            scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
                @Override
                public void run() {
                    int nextBroadcast = 0;
                        Bukkit.getServer().broadcastMessage(ChatColor.YELLOW + getConfig().get("broadcasts." + nextBroadcast).toString());
                        if(nextBroadcast != 4) {
                            nextBroadcast++;
                        }else {
                            nextBroadcast = 0;
                        }
                    }
                }, 0L, 600L);
            }
    
    EDIT -- nvm(i'm dumb) just realized I was initializing it to 0 everytime the scheduler repeats.

    There any way I can have my counter not be overwritten everytime the task repeats? I've tried tried declaring it outside the scheduler, but I can only access it if it's a final, and if it's a final, it cannot be modified.

    <Edit by mrCookieSlime: Merged posts.>

    EDIT -- Fixed, had to declare outside my method I was scheduling the event.
     
    Last edited: Apr 9, 2015
Thread Status:
Not open for further replies.

Share This Page