Getting Command Arguments From A Config

Discussion in 'Plugin Development' started by toxiccoke, Nov 23, 2015.

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

    toxiccoke

    Hi

    So im just playing around with some code and just trying to make it so villagers will not be push-able im doing it by checking if a player has moved it if so it will teleport it back to the location set in the config. i have it so when you enter the command /npc create (name) (test) it will get the name and it will save it in the config as npc.(name enter on command/args[1]).(x/y/z) but im having trouble working out how to then get the name from the config to check if it has been moved.
    Heres my code:
    Code (open)

    Code:
    package me.toxiccoke.test;
    
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Villager;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    public class Main extends JavaPlugin implements Listener {
       
          public void onEnable(){
            getServer().getPluginManager().registerEvents(this, this);
            saveDefaultConfig();
          }
         
          public void onDisable(){
            saveConfig();
          }
         
         
          public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            Player p = (Player)sender;
           
            if(cmd.getName().equalsIgnoreCase("npc")){
                if(args.length == 0){
                    p.sendMessage("nope1");
                   
                }else{
                    if((args.length == 1) && (args[0].equalsIgnoreCase("create"))){
                        p.sendMessage("nope2");
                       
                    }else{
                        if(args.length == 2){
                            p.sendMessage("nope3");
                           
                        }else{
                            if(args.length == 3){
                                p.sendMessage("nice");
                                  Villager npc = (Villager)p.getWorld().spawnCreature(p.getLocation(), EntityType.VILLAGER);
                                  npc.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 1000000000, 1000000000));
                                  npc.setCustomName(args[1]);
                                  getConfig().set("npc.name." + args[1], args[2]);
                                  getConfig().set("npc." + args[1] + ".x", (p.getLocation().getX()));
                                  getConfig().set("npc." + args[1] + ".y", (p.getLocation().getY()));
                                  getConfig().set("npc." + args[1] + ".z", (p.getLocation().getZ()));
                                  saveConfig();
                                 
                            }
                        }
                    }
                }
            }
           
            return false;
          }
         
          @EventHandler
          public void onMove(PlayerMoveEvent e)
          {
            for (Entity entity : e.getPlayer().getNearbyEntities(3.0D, 3.0D, 3.0D)) {
              if (entity.getType().equals(EntityType.VILLAGER))
              {
                Villager npc = (Villager)entity;
                World w = npc.getWorld();
                double x = getConfig().getDouble("npc." + args[1] + ".x");
                double y = getConfig().getDouble("npc." + args[1] + ".y");
                double z = getConfig().getDouble("npc." + args[1] + ".z");
                npc.teleport(new Location(w, x, npc.getLocation().getY(), z));
              }
            }
          }
    }


    Any thing else that you may need please post below and i will get it on the post asap.
    Thanks
    toxiccoke
     
  2. Offline

    Lordloss

    you can get the coords back with int x = getConfig().getInt("npc.name.x"); Which will return 0 if it doesnt exist i think. You can check if it exists with getConfig().contains("npc.name");
     
  3. Offline

    toxiccoke

    @Lordloss it would not be name it would be the name of the villager that the player has entered so how would i then get that from the config because every villager name is going to be diffrent
     
Thread Status:
Not open for further replies.

Share This Page