Material getstring at config would be possible ?

Discussion in 'Spigot Plugin Development' started by Raymart, Jun 22, 2015.

  1. Hello getting the string at the config using materials like tnt would be possible and how ?

    example :
    Code (Text):
        if (e.getRecipe().getResult().getType() == Material.HOPPER)
    how do I make it
    Code (Text):
    getConfig().getString("Material");
     
  2. There are 3 methods in the class Material:
    Material.getMaterial(String)
    Material.matchMaterial(String)
    Material.valueOf(String)
    I suggest having a look at the docs: Material
     
  3. woah ty :p
     
  4. @Querz
    how do I fix my code ? :p
    Code (Text):
        if (e.getRecipe().getResult().getType() == Material.getMaterial(plugin.getConfig().getIntegerList("Block Ids"))
     
  5. this is the whole code
    Code (Text):
    package me.raymart;

    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.CraftItemEvent;

    public class CraftListener implements Listener {
        public static NoHopper plugin;
        public CraftListener(NoHopper instance) {
            plugin = instance;
        }
    @EventHandler(priority=EventPriority.HIGHEST)
      public void onCraft(CraftItemEvent e) {
          Player user = (Player)e.getWhoClicked();
          String id = String.valueOf(plugin.getConfig().getStringList("Block_IDs"));
        if (e.getRecipe().getResult().getType() == Material.getMaterial(id)) {
          if(!user.hasPermission(plugin.getConfig().getString("Permission Node"))) {
          e.setCancelled(true);
          if(plugin.getConfig().getBoolean("Warn If No Permission")) {
          user.sendMessage(plugin.getConfig().getString("NoPerm Message").replace("&", "§").replace("%player%", user.getName()));
          }else {
              user.sendMessage("");
          }
          }
        }
      }
    }
    any wrong with that ?
     
  6. sothatsit

    Patron

    You have to loop through the ids in config and check each of them one by one.

    The best thing to do would be to load all of them into a List<Material> in your onEnable and then you can just do

    Code (Text):
    if(materials.contains(e.getRecipe().getResult().getType())) {}

    Example:
    Code (Text):
    private List<Material> materials;

    //In your onEnable

    materials = new ArrayList<Material>();
    for(String str : getConfig().getStringList("ids")) {
        try{
            int id = Integer.valueOf(str);
            Material mat = Material.getMaterial(id);
       
            if(mat != null) {
                materials.add(mat);
            } else {
                getLogger().warning("Invalid Id \"" + str + "\", can't find material");
            }
        } catch(NumberFormatException e) {
            getLogger().warning("Invalid id \"" + str + "\", must be integer");
        }
    }
    Written in browser, so may not be 100% correct, but should give you an idea of what to do.
     
    #6 sothatsit, Jun 22, 2015
    Last edited: Jun 22, 2015
  7. could you help me to add it to my code ?
    this is my code :
    Code (Text):
    package me.raymart;

    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.CraftItemEvent;

    public class CraftListener implements Listener {
        public static NoHopper plugin;
        public CraftListener(NoHopper instance) {
            plugin = instance;
        }
    @EventHandler(priority=EventPriority.HIGHEST)
      public void onCraft(CraftItemEvent e) {
          Player user = (Player)e.getWhoClicked();
        if (e.getRecipe().getResult().getType() == Material.WORKBENCH) {
          if(!user.hasPermission(plugin.getConfig().getString("Permission Node"))) {
          e.setCancelled(true);;
          if(plugin.getConfig().getBoolean("Warn If No Permission")) {
          user.sendMessage(plugin.getConfig().getString("NoPerm Message").replace("&", "§").replace("%player%", user.getName()));
          }else {
              user.sendMessage("");
          }
          }
        }
      }
    }
     
  8. sothatsit

    Patron

    No, if you can't figure it out from that then you shouldn't have it. Figure it out.