1.16.5 [Problem] Add lines to config.yml (spigot 1.16.5)

Discussion in 'Spigot Plugin Development' started by jukebowaz, Feb 13, 2021.

Thread Status:
Not open for further replies.
  1. Could you post the code where you load them, too?
    And maybe use [code=Java]//CODE HERE[/code].
     
  2. please don't spoonfeed
     
  3. Yeah, that was a bit much...
    But It can get worse:

    Try something like this:
    Code (Java):
      @Override
      public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (!(sender instanceof Player)) {
          //PlayerOnly
          return false;
        }
        if (args.length != 2) {
          //Wrong usage
          return false;
        }
        String cmd_left = args[0];
        if (cmd_left.isEmpty() || cmd_left.isBlank()) {
          //No valid cmd_left
          return false;
        }
        String cmd_right = args[1];
        if (cmd_right.isEmpty() || cmd_right.isBlank()) {
          //No valid cmd_right
          return false;
        }
        Player p = (Player) sender;
        ItemStack iStack = p.getInventory().getItemInMainHand();
        if (iStack == null) {
          //Hand is empty
          return false;
        }
        if ((iStack.getType() == Material.AIR) || (iStack.getType() == Material.VOID_AIR)) {
          //Hand is empty
          return false;
        }
        if (!iStack.hasItemMeta()) {
          //No Displayename
          return false;
        }
        if (!iStack.getItemMeta().hasDisplayName()) {
          //No Displayname
          return false;
        }
        String displayname = iStack.getItemMeta().getDisplayName();
        if (displayname.isEmpty() || displayname.isBlank()) {
          //No valid Displayname
          return false;
        }

        String path = "items." + iStack.getType().getKey().getNamespace() + "-" + displayname + ".";

        //Replace '_' to ' ' for more commands
        Main.getInstance().getConfig().set(path + "cmd-left", cmd_left.replace("_", " "));
        Main.getInstance().getConfig().set(path + "cmd-right", cmd_right.replace("_", " "));

        Main.getInstance().saveConfig();
        Main.getInstance().reloadConfig();

        p.sendMessage(ChatColor.translateAlternateColorCodes('&', "&aItem added!"));

        return true;
      }

      @EventHandler
      public void onPlayerUse(PlayerInteractEvent e) {
        ItemStack iStack = e.getItem();
        if (!iStack.hasItemMeta()) {
          //No valid Item
          return;
        }
        if (iStack.getType() == null) {
          //Hand is empty
          return;
        }
        if ((iStack.getType() == Material.AIR) || (iStack.getType() == Material.VOID_AIR)) {
          //Hand is empty
          return;
        }

        boolean exist = false;
        String finaldisplayname;
        String cmd_left;
        String cmd_right;

        //Searching config for item
        if (!Main.getInstance().getConfig().isConfigurationSection("items")) {
          //No Items saved
          return;
        }
        for (String key : getConfig().getConfigurationSection("items").getKeys(false)) {
          String[] splitkey = key.split("-");
          if (splitkey.length != 2) {
            //Corrupted config
            continue;
          }
          String materialString = splitkey[0];
          Material material = Material.getMaterial(materialString);
          if (material == null) {
            //Not existing Material?
           continue;
          }
          String displayname = splitkey[1];
          if ((iStack.getType() == material) && (iStack.getItemMeta().getDisplayName() == displayname)) {
            //Same item
            exist = true;
            finaldisplayname = displayname;
            cmd_left = getConfig().getString("items." + key + ".cmd-left");
            cmd_right = getConfig().getString("items." + key + ".cmd-right");
            break;
          }
        }

        if (!exist) {
          //Item not in config
          return;
        }

        String left_papi = PlaceholderAPI.setPlaceholders(e.getPlayer(), cmd_left);
        String right_papi = PlaceholderAPI.setPlaceholders(e.getPlayer(), cmd_right);

        if ((e.getAction() == Action.LEFT_CLICK_AIR) || (e.getAction() == Action.LEFT_CLICK_BLOCK)) {
          Main.getInstance().getServer().dispatchCommand(e.getPlayer(), left_papi);
        }
        else if ((e.getAction() == Action.RIGHT_CLICK_AIR) || (e.getAction() == Action.RIGHT_CLICK_BLOCK)) {
          Main.getInstance().getServer().dispatchCommand(e.getPlayer(), right_papi);
        }

      }
     
    #63 _xXSchrandXx_, Feb 19, 2021
    Last edited: Feb 19, 2021
  4. oh god, please don't answer questions if you don't even know basic java
     
  5. How did you know? :eek:

    Well, @illuminator3 isn't wrong, but you cloud try the fixxed one:
    Code (Java):
      @Override
      public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
        if (!(sender instanceof Player)) {
          //PlayerOnly
          return false;
        }
        if (args.length != 2) {
          //Wrong usage
          return false;
        }
        String cmd_left = args[0];
        if (cmd_left.isEmpty() || cmd_left.isBlank()) {
          //No valid cmd_left
          return false;
        }
        String cmd_right = args[1];
        if (cmd_right.isEmpty() || cmd_right.isBlank()) {
          //No valid cmd_right
          return false;
        }
        Player p = (Player) sender;
        ItemStack iStack = p.getInventory().getItemInMainHand();
        if (iStack == null) {
          //Hand is empty
          return false;
        }
        if (iStack.getType().equals(Material.AIR) || iStack.getType().equals(Material.VOID_AIR)) {
          //Hand is empty
          return false;
        }
        if (!iStack.hasItemMeta()) {
          //No Displayename
          return false;
        }
        if (!iStack.getItemMeta().hasDisplayName()) {
          //No Displayname
          return false;
        }
        String displayname = iStack.getItemMeta().getDisplayName();
        if (displayname.isEmpty() || displayname.isBlank()) {
          //No valid Displayname
          return false;
        }

        String path = "items." + iStack.getType().getKey().getKey() + "-" + displayname + ".";

        //Replace '_' to ' ' for more commands
        Main.getInstance().getConfig().set(path + "cmd-left", cmd_left.replace("_", " "));
        Main.getInstance().getConfig().set(path + "cmd-right", cmd_right.replace("_", " "));

        Main.getInstance().saveConfig();
        Main.getInstance().reloadConfig();

        p.sendMessage(ChatColor.translateAlternateColorCodes('&', "&aItem added!"));

        return true;
      }
    Code (Java):
      @EventHandler
      public void onPlayerUse(PlayerInteractEvent e) {
        if (!e.hasItem()) {
          //No Item involved
          return;
        }
        ItemStack iStack = e.getItem();
        if (!iStack.hasItemMeta()) {
          //No valid Item
          return;
        }
        if (iStack.getType() == null) {
          //Hand is empty
          return;
        }
        if (iStack.getType().equals(Material.AIR) || iStack.getType().equals(Material.VOID_AIR)) {
          //Hand is empty
          return;
        }

        String cmd_left = null;
        String cmd_right = null;

        //Searching config for item
        if (!Main.getInstance().getConfig().contains("items")) {
          //No Items saved
          return;
        }
        for (String key : Main.getInstance().getConfig().getConfigurationSection("items").getKeys(false)) {
          String[] splitkey = key.split("-");
          if (splitkey.length != 2) {
            continue;
          }
          String materialString = splitkey[0];
          Material material = Material.getMaterial(materialString.toUpperCase());
          if (material == null) {
            //Not existing Material?
            continue;
          }
          String displayname = splitkey[1];
          if (iStack.getType().equals(material) && iStack.getItemMeta().getDisplayName().equals(displayname)) {
            //Same item
            cmd_left = Main.getInstance().getConfig().getString("items." + key + ".cmd-left");
            cmd_right = Main.getInstance().getConfig().getString("items." + key + ".cmd-right");
            break;
          }
        }

        if (cmd_left == null || cmd_right == null) {
          //Item not in config
          return;
        }

        e.setCancelled(true);

        String left_papi = PlaceholderAPI.setPlaceholders(e.getPlayer(), cmd_left);
        String right_papi = PlaceholderAPI.setPlaceholders(e.getPlayer(), cmd_right);

        if (e.getAction().equals(Action.LEFT_CLICK_AIR) || e.getAction().equals(Action.LEFT_CLICK_BLOCK)) {
          Main.getInstance().getServer().dispatchCommand(e.getPlayer(), left_papi);
        }
        else if (e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
          Main.getInstance().getServer().dispatchCommand(e.getPlayer(), right_papi);
        }

      }
     
  6. oh god your code looks so wrong
     
  7. so how can i do it?
     
  8. Then give some constructive criticism. ;)
    Tell us what's on your mind.
     
  9. your variable names, your conditionals and your code is lacking objects
     
    • Useful Useful x 1
  10. Ok, the code worked in my test. Which objects do you think are missing?
    And since this is just an example and the style is a personal matter, I'll ignore that until @jukebowaz wants it.
     
  11. Code (Java):

    public class TestPlugin extends JavaPlugin implements Listener {

      private static TestPlugin instance;

      public static TestPlugin getInstance() {
        return instance;
      }

      public void onEnable() {
        instance = this;
        getServer().getPluginManager().registerEvents(this, this);
        getCommand("test").setExecutor(this);
      }

      @Override
      public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
        if (!(sender instanceof Player)) {
          //PlayerOnly
          sender.sendMessage("You have to be a Player.");
          return true;
        }
        Player p = (Player) sender;
        if (args.length != 2) {
          //Wrong usage
          p.sendMessage("Usage: /test <cmd-right> <cmd-left>");
          return true;
        }
        String cmd_left = args[0];
        if (cmd_left.isEmpty() || cmd_left.isBlank()) {
          //No valid cmd_left
          p.sendMessage("cmd-left cannot be empty or blank.");
          return true;
        }
        String cmd_right = args[1];
        if (cmd_right.isEmpty() || cmd_right.isBlank()) {
          //No valid cmd_right
          p.sendMessage("cmd-right cannot be empty or blank.");
          return true;
        }
        ItemStack iStack = p.getInventory().getItemInMainHand();
        if (iStack == null) {
          //Hand is empty
          p.sendMessage("You have to hold an item in your mainhand.");
          return true;
        }
        if (iStack.getType().equals(Material.AIR) || iStack.getType().equals(Material.VOID_AIR)) {
          //Hand is empty
          p.sendMessage("You have to hold an valid item in your mainhand.");
          return true;
        }
        if (!iStack.hasItemMeta()) {
          //No Displayename
          p.sendMessage("The item in your mainhand has to have a custom name.");
          return true;
        }
        if (!iStack.getItemMeta().hasDisplayName()) {
          //No Displayname
          p.sendMessage("The item in your mainhand has to have a custom name.");
          return true;
        }
        String displayname = iStack.getItemMeta().getDisplayName();
        if (displayname.isEmpty() || displayname.isBlank()) {
          //No valid Displayname
          p.sendMessage("The custom name cannot be empty of blank.");
          return true;
        }

        String path = "items." + iStack.getType().getKey().getKey() + "-" + displayname + ".";

        //Replace '_' to ' ' for more commands
        TestPlugin.getInstance().getConfig().set(path + "cmd-left", cmd_left.replace("_", " "));
        TestPlugin.getInstance().getConfig().set(path + "cmd-right", cmd_right.replace("_", " "));

        TestPlugin.getInstance().saveConfig();
        TestPlugin.getInstance().reloadConfig();

        p.sendMessage(ChatColor.translateAlternateColorCodes('&', "&aItem added!"));

        return true;
      }

      @EventHandler
      public void onPlayerUse(PlayerInteractEvent e) {
        if (!e.hasItem()) {
          //No Item involved
          return;
        }
        ItemStack iStack = e.getItem();
        if (!iStack.hasItemMeta()) {
          //No valid Item
          return;
        }
        if (iStack.getType() == null) {
          //Hand is empty
          return;
        }
        if (iStack.getType().equals(Material.AIR) || iStack.getType().equals(Material.VOID_AIR)) {
          //Hand is empty
          return;
        }

        String cmd_left = null;
        String cmd_right = null;

        //Searching config for item
        if (!TestPlugin.getInstance().getConfig().contains("items")) {
          //No Items saved
          return;
        }
        for (String key : TestPlugin.getInstance().getConfig().getConfigurationSection("items").getKeys(false)) {
          String[] splitkey = key.split("-");
          if (splitkey.length != 2) {
            continue;
          }
          String materialString = splitkey[0];
          Material material = Material.getMaterial(materialString.toUpperCase());
          if (material == null) {
            //Not existing Material?
            continue;
          }
          String displayname = splitkey[1];
          if (iStack.getType().equals(material) && iStack.getItemMeta().getDisplayName().equals(displayname)) {
            //Same item
            cmd_left = TestPlugin.getInstance().getConfig().getString("items." + key + ".cmd-left");
            cmd_right = TestPlugin.getInstance().getConfig().getString("items." + key + ".cmd-right");
            break;
          }
        }

        if (cmd_left == null || cmd_right == null) {
          //Item not in config
          return;
        }

        e.setCancelled(true);

        String left_papi = cmd_left; //PlaceholderAPI.setPlaceholders(e.getPlayer(), cmd_left);
        String right_papi = cmd_right; //PlaceholderAPI.setPlaceholders(e.getPlayer(), cmd_right);

        if (e.getAction().equals(Action.LEFT_CLICK_AIR) || e.getAction().equals(Action.LEFT_CLICK_BLOCK)) {
          TestPlugin.getInstance().getServer().dispatchCommand(e.getPlayer(), left_papi);
        }
        else if (e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
          TestPlugin.getInstance().getServer().dispatchCommand(e.getPlayer(), right_papi);
        }

      }

    }

     
     
  12. ok
     
  13. I was wrong to write something .. thanks for everything! Excuse me, since I would like to use this plugin on a server, can I use the part of the code you wrote me along with what I did?
     
  14. Of course not, you should never use something that you do not understand or cannot create yourself.
    But unfortunately, nobody can track which plugins you are using on your server (unless you forget to hide it).
    And as @illuminator3 already wrote, it's "just working" code. It should never be used on productive server.
     
  15. ok thanks for all!
     
  16. Would be nice, if you mark this as solved
     
Thread Status:
Not open for further replies.