Item ids from config?

Discussion in 'Plugin Development' started by Gosintary, Apr 15, 2018.

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

    Gosintary

    The following code is to prevent items from being crafted, and these items should be gotten from the config.
    My current code
    Current Code (open)

    public class main extends JavaPlugin implements Listener {

    public void onEnable() {
    this.getConfig().options().copyDefaults(true);
    this.saveDefaultConfig();

    getServer().getPluginManager().registerEvents(this, this);
    }

    @EventHandler
    public void onCraft(CraftItemEvent e) {
    Player p = (Player) e.getWhoClicked();
    Material material = Material.getMaterial(getConfig().getString("Blocked"));
    if (e.getRecipe().getResult().getType() == material
    || e.getRecipe().getResult().getType() == Material.WOOD_SPADE) {
    p.sendMessage("You crafted a" + e.getRecipe().getResult().getType());
    if (!p.hasPermission("crb.*")) {
    e.setCancelled(true);
    }
    }
    }

    public boolean onCommand(Command cmd, String label, CommandSender s, String[] arg) {
    if (label.equalsIgnoreCase("blocklist")) {
    s.sendMessage(ChatColor.GREEN + "Blocked Recipies:");
    s.sendMessage(getConfig().getString("Blocked"));
    }
    return false;
    }
    }

    allows for
    Code:
    Blocked: STICK
    but not
    Code:
    Blocked:
    - STICK
    or
    Code:
    Blocked: 
    - 280
    My goal is to use
    Code:
    Blocked: 
    - 280
    and allow as many items as wanted.
    How can I do this? Thanks!
     
  2. Use config.getStringList() rather than getString(). You may have to change your code a bit to allow this to function correctly.
    If you're using Java 8, do something like List#stream#map(Material::fromName), then if(Stream#anyMatch(m->m.equals(e.getResult().etc())))


    Sent from my SM-G903F using Tapatalk
     
    Gosintary likes this.
  3. Offline

    Gosintary

Thread Status:
Not open for further replies.

Share This Page