Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map.Entry;
- import org.bukkit.Bukkit;
- import org.bukkit.Location;
- import org.bukkit.World;
- import org.bukkit.configuration.file.FileConfiguration;
- import org.bukkit.configuration.file.YamlConfiguration;
- import org.bukkit.entity.Player;
- public class CmdBlockManager {
- private HashMap<Location, CmdBlock> cmds = new HashMap<Location, CmdBlock>();
- public void clickedBlock(Player p, Location loc){
- if(cmds.containsKey(loc) && !cmds.get(loc).isDeleted()){
- if(cmds.get(loc).isConsole()){
- Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), cmds.get(loc).getCmd().replace("%p%", p.getName()));
- }else{
- p.performCommand(cmds.get(loc).getCmd());
- }
- }
- }
- public void addCmdBlock(String id, Location location, String cmd, boolean console){
- // Sicher gehen, dass die Location nicht doppelt benutzt wird
- if(cmds.containsKey(location)){
- cmds.remove(location);
- }
- // Sicher gehen, dass die ID nicht doppelt benutzt wird
- Location l = null;
- for(Location loc : cmds.keySet()){
- if(cmds.get(loc).getId().equals(id)){
- l = loc;
- break;
- }
- }
- if(l != null){
- cmds.remove(l);
- }
- cmds.put(location, new CmdBlock(id, cmd, console, false));
- }
- public void removeCmdBlock(Location location){
- if(location == null)return;
- if(cmds.containsKey(location)){
- cmds.get(location).setDeleted(true);
- }
- }
- public void removeCmdBlock(String id){
- Location l = null;
- for(Location loc : cmds.keySet()){
- if(cmds.get(loc).getId().equals(id)){
- l = loc;
- break;
- }
- }
- removeCmdBlock(l);
- }
- public CmdBlock getCmdBlock(String id){
- for(Location loc : cmds.keySet()){
- if(cmds.get(loc).getId().equals(id) && !cmds.get(loc).isDeleted()){
- return cmds.get(loc);
- }
- }
- return null;
- }
- public CmdBlock getCmdBlock(Location l){
- if(cmds.containsKey(l) && !cmds.get(l).isDeleted()){
- return cmds.get(l);
- }
- return null;
- }
- public Location getLocation(String id){
- for(Location loc : cmds.keySet()){
- if(cmds.get(loc).getId().equals(id) && !cmds.get(loc).isDeleted()){
- return loc;
- }
- }
- return null;
- }
- public List<String> listIds(){
- List<String> list = new ArrayList<String>();
- for(Location loc : cmds.keySet()){
- if(!cmds.get(loc).isDeleted()){
- list.add(cmds.get(loc).getId());
- }
- }
- return list;
- }
- public void load() {
- // Config erstellen
- FileConfiguration cfg = getConfigFileConfiguration();
- cfg.options().copyDefaults(true);
- try {
- cfg.save(getConfigFile());
- } catch (IOException e) {
- e.printStackTrace();
- }
- // Inhalte der Config in die HashMap laden
- for(String path : cfg.getKeys(false)){
- try{
- Location l;
- String cmd;
- boolean console;
- World w = Bukkit.getWorld(cfg.getString(path + ".world"));
- double x = cfg.getDouble(path + ".x");
- double y = cfg.getDouble(path + ".y");
- double z = cfg.getDouble(path + ".z");
- l = new Location(w,x,y,z);
- cmd = cfg.getString(path + ".cmd");
- console = cfg.getBoolean(path + ".console");
- addCmdBlock(path,l,cmd, console);
- }catch(Exception e){
- }
- }
- }
- public void save() {
- FileConfiguration cfg = getConfigFileConfiguration();
- // Inhalte der HashMap in die Config laden
- for(Entry<Location, CmdBlock> e : this.cmds.entrySet()){
- if(!e.getValue().isDeleted()){
- cfg.set(e.getValue().getId() + ".world", e.getKey().getWorld().getName());
- cfg.set(e.getValue().getId() + ".x", e.getKey().getX());
- cfg.set(e.getValue().getId() + ".y", e.getKey().getY());
- cfg.set(e.getValue().getId() + ".z", e.getKey().getZ());
- cfg.set(e.getValue().getId() + ".cmd", e.getValue().getCmd());
- cfg.set(e.getValue().getId() + ".console", e.getValue().isConsole());
- }else{
- cfg.set(e.getValue().getId() + ".world", null);
- cfg.set(e.getValue().getId() + ".x", null);
- cfg.set(e.getValue().getId() + ".y", null);
- cfg.set(e.getValue().getId() + ".z", null);
- cfg.set(e.getValue().getId() + ".cmd", null);
- cfg.set(e.getValue().getId() + ".console", null);
- cfg.set(e.getValue().getId(), null);
- }
- }
- try {
- cfg.save(getConfigFile());
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- }
- public static File getConfigFile() {
- return new File("plugins/cmdblock", "commands.yml");
- }
- public static FileConfiguration getConfigFileConfiguration() {
- return YamlConfiguration.loadConfiguration(getConfigFile());
- }
- public class CmdBlock{
- private String id;
- private String cmd;
- private boolean deleted;
- private boolean console;
- public CmdBlock(String id, String cmd, boolean console, boolean deleted){
- this.setId(id);
- this.setCmd(cmd);
- this.console = console;
- this.setDeleted(deleted);
- }
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public String getCmd() {
- return cmd;
- }
- public void setCmd(String cmd) {
- this.cmd = cmd;
- }
- public boolean isDeleted() {
- return deleted;
- }
- public void setDeleted(boolean deleted) {
- this.deleted = deleted;
- }
- public boolean isConsole() {
- return console;
- }
- public void setConsole(boolean console) {
- this.console = console;
- }
- }
- }
Add Comment
Please, Sign In to add comment