Lobby/src/eu/univento/lobby/commands/SetNPC.java

118 lines
3.2 KiB
Java
Raw Blame History

package eu.univento.lobby.commands;
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import eu.univento.core.api.AutoCommand;
import eu.univento.core.api.CustomPlayer;
import eu.univento.core.api.Messages;
import eu.univento.core.api.NPC;
import eu.univento.core.api.Perms.Ranks;
import eu.univento.lobby.Lobby;
/**
* spawns NPCs
* @author joethei
* @version 1.0
*/
public class SetNPC extends AutoCommand<Lobby>{
/**
* configuration file
*/
static File file = new File("plugins/Lobby", "NPCs.yml");
/**
* configuration
*/
static FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);
/**
* @param plugin main class
* @param command command to execute
* @param description describes the command
* @param aliases aliases of command
*/
public SetNPC(Lobby plugin, String command, String description, String[] aliases) {
super(plugin, command, description, aliases);
}
@Override
public boolean execute(CommandSender sender, String label, String[] args) {
if(sender instanceof Player) {
CustomPlayer p = CustomPlayer.getPlayer(sender.getName());
if(p.isAllowed(Ranks.Admin)) {
if (args.length == 1) {
cfg.set(args[0] + ".World", p.getWorld().getName());
cfg.set(args[0] + ".X", Double.valueOf(p.getLocation().getX()));
cfg.set(args[0] + ".Y", Double.valueOf(p.getLocation().getY()));
cfg.set(args[0] + ".Z", Double.valueOf(p.getLocation().getY()));
try {
cfg.save(file);
} catch (IOException e) {
e.printStackTrace();
}
spawnNPC(args[0], p.getLocation());
}
p.sendMessage("<EFBFBD>cNutze /setnpc <Name>");
}else {
sender.sendMessage(Messages.NO_PERMS);
}
}else {
sender.sendMessage(Messages.NOT_A_PLAYER);
}
return false;
}
@Override
public List<String> tabComplete(CommandSender sender, String label, String[] args) {
return null;
}
/**
* @param name name of npc
* @param loc location to spawn
*/
static void spawnNPC(String name, Location loc) {
Bukkit.getScheduler().runTaskAsynchronously(Lobby.getInstance(), new Runnable() {
@Override
public void run() {
NPC npc = new NPC(name, loc, true);
npc.setItemInHand(Material.IRON_SWORD);
npc.spawn();
}
});
}
/**
* @param what name of the npc
*/
public static void startNPCs(String what) {
String world = cfg.getString(what + ".World");
double x = cfg.getDouble(what + ".X");
double y = cfg.getDouble(what + ".Y");
double z = cfg.getDouble(what + ".Z");
World w = Bukkit.getWorld(world);
Location loc = new Location(w, x, y, z);
Chunk chunk = loc.getChunk();
chunk.load();
spawnNPC(what, loc);
}
}