StringList won't update

Discussion in 'Plugin Development' started by yologamer384, Feb 23, 2017.

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

    yologamer384

    When i change my blocked list
    Code:
    Blocked:
       - DIAMOND_BLOCK
       - GOLD_BLOCK
       - EMERALD_BLOCK
       - IRON_BLOCK
    And I add/remove a block and i do
    /fw reload
    Code:
    } else if (args[0].equalsIgnoreCase("reload")) {
                            if (sender.hasPermission("myplugin.reload"))
                                Main.plugin.getConfig();
                                Main.plugin.reloadConfig();
    My list isn't updated but if i do /reload yes
    Can someone help me? Please

    Code:
    public class BlockCraft implements Listener {
      private Main plugin;
      private List<String> blockedItemsString;
      private List<Material> blockedItems = new ArrayList();
     
      BlockCraft(Main plugin)
      {
        this.plugin = plugin;
        this.blockedItems.clear();
        this.blockedItemsString = plugin.getConfig().getStringList("Blocked");
        for (String current : this.blockedItemsString) {
          try
          {
            this.blockedItems.add(Material.getMaterial(current.trim()));
          }
          catch (Exception e)
          {
            e.printStackTrace();
          }
        }
      }
     
      @EventHandler
      public void craftItem(PrepareItemCraftEvent e)
      {
        Material itemType = e.getRecipe().getResult().getType();
       
        Byte itemData = Byte.valueOf(e.getRecipe().getResult().getData().getData());
        for (Material currentMaterial : this.blockedItems) {
          if (itemType == currentMaterial)
          {
            e.getInventory().setResult(new ItemStack(Material.AIR));
            for (HumanEntity he : e.getViewers()) {
              if ((he instanceof Player)) {
                ((Player)he).sendMessage(this.plugin.getConfig().getString("prefix").replace('&', '�') + this.plugin.getConfig().getString("Messages.you-cant-craft-this-item").replace('&', '�'));
              }
            }
          }
        }
      }
    }
     
  2. Offline

    Zombie_Striker

    This line does nothing. Remove it.

    The reason this is happening is because you are not updating the List in the listener. When you issue the reload command, re-create blockedItems
     
  3. Offline

    yologamer384

    Ok thanks
    How? I don't know how update my List :/
     
  4. Offline

    Zombie_Striker

    @yologamer384
    Use the exact same method as the one in the constructor.
     
  5. Offline

    yologamer384

    I don't understand :/ i really don't know how use this... And the command and the blocked items are in 2 class

    up
    Can someone help me?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Feb 24, 2017
  6. Offline

    mine-care

    Well lets recap what @Zombie_Striker said above:
    The reason it doesnt work although the changed config is loaded, is because in the class BlockCraft you have a List called 'blockedItems' which contains the blocked items, but does not re-load them when the config is reloaded because this list is updated only when an object of BlockCraft is created.
    In your constructor you have the code @Zombie_Striker pointed out, which loads/parses the materials from config and adds them to the list. You need to somehow execute this bit of code when you reload the config so that the correct matterials are added in the list.
    A way to do this is to take this bit of code on a seperate method in that class and once you reload the config call this method...
     
  7. Offline

    yologamer384

    Yes but i don't know how, I'm not really good on this
     
  8. Offline

    mine-care

    @yologamer384 Well then i recomend you start with Java basics and then continue your plugin once you have granted the required Java knowledge.

    Writing a plugin without knowing Java is like writing a book in a language you don't speak based only on what you have heared from others...

    Spoonfeeding code is collectively avoided in these forums because our experience shows that it does not help.
     
    Last edited: Feb 24, 2017
  9. Offline

    yologamer384

    I only need one example not the exact code, I only need to see how to set it
     
  10. Offline

    mine-care

    @yologamer384
    Technically speaking now you must be able to spot what the problem in your code is and also think of a way to resolve it, but ill post an outline of what i mean ;)

    Code:
    yourConstructor{
    
       Code that loads materials.
    
    }
    
    should become:
    Code:
    yourConstructor{
    
       call method.
    
    }
    
    method{
       Code that loads materials.
    }
    
    When reloading the config{
       call method.
    }
    
     
  11. Offline

    yologamer384

    I'm stupid... I'm not able
     
  12. Offline

    mine-care

    @yologamer384 You aren't stipid! Just need some guidance and the basic Java knowlege! ;)
    Once you learn the basics of Java you will unferstand everything clearly and you will be able to do it too! :D
     
  13. Offline

    yologamer384

    Yes but now I need this for one plugin and this plugin is used in a server
     
Thread Status:
Not open for further replies.

Share This Page