From 311f090934f524ed49abf7c74766b7dd27a1da74 Mon Sep 17 00:00:00 2001 From: cnaude Date: Tue, 1 Jan 2013 18:35:17 -0700 Subject: [PATCH] Added the off on command --- src/net/spoothie/chairs/Chairs.java | 39 ++++------ src/net/spoothie/chairs/ChairsCommand.java | 62 +++++++++++++++ src/net/spoothie/chairs/ChairsIgnoreList.java | 77 +++++++++++++++++++ src/net/spoothie/chairs/EventListener.java | 8 +- src/plugin.yml | 16 +++- 5 files changed, 172 insertions(+), 30 deletions(-) create mode 100644 src/net/spoothie/chairs/ChairsCommand.java create mode 100644 src/net/spoothie/chairs/ChairsIgnoreList.java diff --git a/src/net/spoothie/chairs/Chairs.java b/src/net/spoothie/chairs/Chairs.java index aeee2d3..32e9d43 100644 --- a/src/net/spoothie/chairs/Chairs.java +++ b/src/net/spoothie/chairs/Chairs.java @@ -11,8 +11,6 @@ import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.Material; -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; import org.bukkit.craftbukkit.v1_4_6.entity.CraftPlayer; import org.bukkit.entity.Player; import org.bukkit.permissions.Permission; @@ -21,7 +19,7 @@ import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.java.JavaPlugin; public class Chairs extends JavaPlugin { - + private static Chairs instance = null; public List allowedBlocks = new ArrayList(); public boolean sneaking, autoRotate, signCheck, permissions, notifyplayer, opsOverridePerms; public boolean invertedStairCheck, seatOccupiedCheck, invertedStepCheck, perItemPerms; @@ -35,9 +33,12 @@ public class Chairs extends JavaPlugin { public static final String LOG_HEADER = "[" + PLUGIN_NAME + "]"; static final Logger log = Logger.getLogger("Minecraft"); public PluginManager pm; + public static ChairsIgnoreList ignoreList; @Override public void onEnable() { + instance = this; + ignoreList = new ChairsIgnoreList(); pm = this.getServer().getPluginManager(); pluginFolder = getDataFolder(); configFile = new File(pluginFolder, "config.yml"); @@ -45,12 +46,13 @@ public class Chairs extends JavaPlugin { this.getConfig().options().copyDefaults(true); saveConfig(); loadConfig(); - EventListener eventListener = new EventListener(this); - getServer().getPluginManager().registerEvents(eventListener, this); + getServer().getPluginManager().registerEvents(new EventListener(this, ignoreList), this); + getCommand("chairs").setExecutor(new ChairsCommand(this, ignoreList)); } @Override public void onDisable() { + ignoreList.save(); } private void createConfig() { @@ -71,7 +73,7 @@ public class Chairs extends JavaPlugin { } } - private void loadConfig() { + public void loadConfig() { autoRotate = getConfig().getBoolean("auto-rotate"); sneaking = getConfig().getBoolean("sneaking"); signCheck = getConfig().getBoolean("sign-check"); @@ -107,26 +109,7 @@ public class Chairs extends JavaPlugin { } else { pm.addPermission(new Permission("chairs.sit","Allows players to sit on blocks",PermissionDefault.FALSE)); } - } - - @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - if (command.getName().equalsIgnoreCase("chairs")) { - if (sender instanceof Player && !((Player) sender).hasPermission("chairs.reload")) { - return true; - } - - if (args.length > 0 && args[0].equalsIgnoreCase("reload")) { - reloadConfig(); - loadConfig(); - sender.sendMessage("Chairs configuration file reloaded."); - } else { - sender.sendMessage("Use '/chairs reload' to reload the configuration file."); - } - } - - return true; - } + } // Send sit packet to all online players public void sendSit(Player p) { @@ -169,4 +152,8 @@ public class Chairs extends JavaPlugin { log.log(Level.SEVERE, String.format("%s %s", LOG_HEADER, _message)); } + public static Chairs get() { + return instance; + } + } diff --git a/src/net/spoothie/chairs/ChairsCommand.java b/src/net/spoothie/chairs/ChairsCommand.java new file mode 100644 index 0000000..6f6a124 --- /dev/null +++ b/src/net/spoothie/chairs/ChairsCommand.java @@ -0,0 +1,62 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package net.spoothie.chairs; + +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +/** + * + * @author cnaude + */ +public class ChairsCommand implements CommandExecutor { + + private final Chairs plugin; + public ChairsIgnoreList ignoreList; + + public ChairsCommand(Chairs instance, ChairsIgnoreList ignoreList) { + this.plugin = instance; + this.ignoreList = ignoreList; + } + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (args.length == 0) { + return false; + } + if (args[0].equalsIgnoreCase("reload")) { + if (sender.hasPermission("chairs.reload")) { + plugin.reloadConfig(); + plugin.loadConfig(); + sender.sendMessage("Chairs configuration file reloaded."); + } else { + sender.sendMessage(ChatColor.GRAY + "No permission to do this!"); + } + } + if (sender instanceof Player) { + Player p = (Player) sender; + if (args[0].equalsIgnoreCase("on")) { + if (p.hasPermission("chairs.self")) { + ignoreList.removePlayer(p.getName()); + p.sendMessage(ChatColor.GRAY + "You have enabled chairs for yourself!"); + } else { + p.sendMessage(ChatColor.GRAY + "No permission to do this!"); + } + } + if (args[0].equalsIgnoreCase("off")) { + if (p.hasPermission("chairs.self")) { + ignoreList.addPlayer(p.getName()); + p.sendMessage(ChatColor.GRAY + "You have disabled chairs for yourself!"); + } else { + p.sendMessage(ChatColor.GRAY + "No permission to do this!"); + } + } + } + return true; + } +} diff --git a/src/net/spoothie/chairs/ChairsIgnoreList.java b/src/net/spoothie/chairs/ChairsIgnoreList.java new file mode 100644 index 0000000..83fc5aa --- /dev/null +++ b/src/net/spoothie/chairs/ChairsIgnoreList.java @@ -0,0 +1,77 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package net.spoothie.chairs; + +import java.io.*; +import java.util.ArrayList; + +/** + * + * @author naudec + */ +@SuppressWarnings("serial") +public class ChairsIgnoreList implements Serializable{ + private static ArrayList ignoreList = new ArrayList(); + private static final String IGNORE_FILE = "plugins/Chairs/ignores.ser"; + + public void ChairsIgnoreList() { + this.load(); + } + + @SuppressWarnings("unchecked") + public void load() { + File file = new File(IGNORE_FILE); + if (!file.exists()) { + Chairs.get().logInfo("Ignore file '"+file.getAbsolutePath()+"' does not exist."); + return; + } + try { + FileInputStream f_in = new FileInputStream(file); + ObjectInputStream obj_in = new ObjectInputStream (f_in); + ignoreList = (ArrayList) obj_in.readObject(); + obj_in.close(); + Chairs.get().logInfo("Loaded ignore list. (Count = "+ignoreList.size()+")"); + } + catch(Exception e) { + Chairs.get().logError(e.getMessage()); + } + } + + public void save() { + try { + File file = new File(IGNORE_FILE); + FileOutputStream f_out = new FileOutputStream (file); + ObjectOutputStream obj_out = new ObjectOutputStream (f_out); + obj_out.writeObject (ignoreList); + obj_out.close(); + Chairs.get().logInfo("Saved ignore list. (Count = "+ignoreList.size()+")"); + } + catch(Exception e) { + Chairs.get().logError(e.getMessage()); + } + } + + public void addPlayer(String s) { + if (ignoreList.contains(s)) { + return; + } + Chairs.get().logInfo("Adding " + s + " to ignore list."); + ignoreList.add(s); + } + + public void removePlayer(String s) { + Chairs.get().logInfo("Removing " + s + " from ignore list."); + ignoreList.remove(s); + } + + public boolean isIgnored(String s) { + if (ignoreList.contains(s)) { + return true; + } + else { + return false; + } + } +} \ No newline at end of file diff --git a/src/net/spoothie/chairs/EventListener.java b/src/net/spoothie/chairs/EventListener.java index 5c05c0e..f68450f 100644 --- a/src/net/spoothie/chairs/EventListener.java +++ b/src/net/spoothie/chairs/EventListener.java @@ -27,9 +27,11 @@ import org.bukkit.permissions.PermissionDefault; public class EventListener implements Listener { public Chairs plugin; + public ChairsIgnoreList ignoreList; - public EventListener(Chairs plugin) { + public EventListener(Chairs plugin, ChairsIgnoreList ignoreList) { this.plugin = plugin; + this.ignoreList = ignoreList; } @EventHandler @@ -96,6 +98,7 @@ public class EventListener implements Listener { @EventHandler public void onPlayerInteract(PlayerInteractEvent event) { if (event.hasBlock() && event.getAction() == Action.RIGHT_CLICK_BLOCK) { + Block block = event.getClickedBlock(); Stairs stairs = null; Step step = null; @@ -108,6 +111,9 @@ public class EventListener implements Listener { sh += 1.0; } Player player = event.getPlayer(); + if (ignoreList.isIgnored(player.getName())) { + return; + } // Permissions Check if (plugin.permissions) { if (!player.hasPermission("chairs.sit")) { diff --git a/src/plugin.yml b/src/plugin.yml index 1d26b38..dfa57af 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -1,5 +1,5 @@ name: Chairs -version: 1.13.5 +version: 1.15.0 description: Let players sit on blocks. authors: - spoothie @@ -7,5 +7,15 @@ authors: main: net.spoothie.chairs.Chairs commands: chairs: - description: Reloads the Chairs configuration file. - usage: /chairs reload \ No newline at end of file + description: Various commands for Chairs. + usage: | + / reload - reloads the configuration + / off - disable chairs for self + / on - enable chairs for self +permissions: + chairs.reload: + description: Reload configuration + default: op + chairs.self: + description: Allow player to turn chairs off and on + default: false \ No newline at end of file