From b3c5a1fcc9479e3be046288874223fc56a85ab1e Mon Sep 17 00:00:00 2001 From: Shevchik Date: Thu, 2 Jan 2014 17:42:06 +0400 Subject: [PATCH] Add command to clear arrows, fix healing effects. --- src/com/cnaude/chairs/ChairEffects.java | 11 ++++--- src/com/cnaude/chairs/Chairs.java | 27 +++++---------- src/com/cnaude/chairs/ChairsCommand.java | 42 ++++++++++++++++++++++-- src/com/cnaude/chairs/PlayerSitData.java | 11 +++++++ src/config.yml | 4 +-- 5 files changed, 67 insertions(+), 28 deletions(-) diff --git a/src/com/cnaude/chairs/ChairEffects.java b/src/com/cnaude/chairs/ChairEffects.java index 66ec1e3..0c136c4 100644 --- a/src/com/cnaude/chairs/ChairEffects.java +++ b/src/com/cnaude/chairs/ChairEffects.java @@ -18,17 +18,20 @@ public class ChairEffects { public ChairEffects(Chairs plugin) { this.plugin = plugin; + } + + public void startHealing() { effectsTask(); } - public void cancel() { + public void cancelHealing() { plugin.getServer().getScheduler().cancelTask(taskID); taskID = 0; } - public void restart() { - this.cancel(); - this.effectsTask(); + public void restartHealing() { + cancelHealing(); + startHealing(); } private void effectsTask() { diff --git a/src/com/cnaude/chairs/Chairs.java b/src/com/cnaude/chairs/Chairs.java index 9017bbf..7e896d1 100644 --- a/src/com/cnaude/chairs/Chairs.java +++ b/src/com/cnaude/chairs/Chairs.java @@ -71,11 +71,15 @@ public class Chairs extends JavaPlugin { getServer().getPluginManager().disablePlugin(this); return; } + chairEffects = new ChairEffects(this); ignoreList = new ChairsIgnoreList(this); ignoreList.load(); getConfig().options().copyDefaults(true); saveConfig(); loadConfig(); + if (sitEffectsEnabled) { + chairEffects.startHealing(); + } psitdata = new PlayerSitData(this); getServer().getPluginManager().registerEvents(new TrySitEventListener(this, ignoreList), this); getServer().getPluginManager().registerEvents(new TryUnsitEventListener(this), this); @@ -93,20 +97,12 @@ public class Chairs extends JavaPlugin { if (ignoreList != null) { ignoreList.save(); } - if (chairEffects != null) { - chairEffects.cancel(); - } + chairEffects.cancelHealing(); + chairEffects = null; log = null; vehiclearrowclass = null; psitdata = null; } - - public void restartEffectsTask() { - if (chairEffects != null) { - chairEffects.restart(); - } - } - public void loadConfig() { FileConfiguration config = YamlConfiguration.loadConfiguration(new File(this.getDataFolder(),"config.yml")); @@ -119,17 +115,10 @@ public class Chairs extends JavaPlugin { disabledRegions = new HashSet(config.getStringList("disabledWGRegions")); - sitEffectsEnabled = config.getBoolean("sit-effects.enabled", false); - sitEffectInterval = config.getInt("sit-effects.interval",20); + sitEffectsEnabled = config.getBoolean("sit-effects.healing.enabled", false); + sitEffectInterval = config.getInt("sit-effects.healing.interval",20); sitMaxHealth = config.getInt("sit-effects.healing.max-percent",100); sitHealthPerInterval = config.getInt("sit-effects.healing.amount",1); - if (sitEffectsEnabled) { - if (chairEffects != null) { - chairEffects.cancel(); - } - logInfo("Enabling sitting effects."); - chairEffects = new ChairEffects(this); - } sitDisableAllCommands = config.getBoolean("sit-restrictions.commands.all"); sitDisabledCommands = new HashSet(config.getStringList("sit-restrictions.commands.list")); diff --git a/src/com/cnaude/chairs/ChairsCommand.java b/src/com/cnaude/chairs/ChairsCommand.java index b56aee0..384cb6a 100644 --- a/src/com/cnaude/chairs/ChairsCommand.java +++ b/src/com/cnaude/chairs/ChairsCommand.java @@ -4,9 +4,17 @@ */ package com.cnaude.chairs; +import java.util.Iterator; + +import org.bukkit.Bukkit; +import org.bukkit.World; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; +import org.bukkit.command.ConsoleCommandSender; +import org.bukkit.command.RemoteConsoleCommandSender; +import org.bukkit.entity.Arrow; +import org.bukkit.entity.Entity; import org.bukkit.entity.Player; /** @@ -30,9 +38,8 @@ public class ChairsCommand implements CommandExecutor { } if (args[0].equalsIgnoreCase("reload")) { if (sender.hasPermission("chairs.reload") || !(sender instanceof Player)) { - //plugin.reloadConfig(); plugin.loadConfig(); - plugin.restartEffectsTask(); + plugin.chairEffects.restartHealing(); if (!plugin.msgReloaded.isEmpty()) { sender.sendMessage(plugin.msgReloaded); } @@ -42,6 +49,20 @@ public class ChairsCommand implements CommandExecutor { } } } + if (args[0].equalsIgnoreCase("removearrows")) { + if (sender instanceof ConsoleCommandSender || sender instanceof RemoteConsoleCommandSender || (sender instanceof Player && sender.hasPermission("chairs.removearrows"))) { + if (args.length == 2) { + World world = Bukkit.getWorld(args[1]); + int removed = removeArrows(world); + sender.sendMessage("Removed "+removed+" unused arrows"); + } else if (args.length == 1) { + for (World world : Bukkit.getWorlds()) { + int removed = removeArrows(world); + sender.sendMessage("Removed "+removed+" unused arrows"); + } + } + } + } if (sender instanceof Player) { Player p = (Player) sender; if (args[0].equalsIgnoreCase("on")) { @@ -68,8 +89,23 @@ public class ChairsCommand implements CommandExecutor { } } } - //plugin.sendSit(p,Integer.parseInt(args[0])); } return true; } + + private int removeArrows(World world) { + Iterator entityit = world.getEntities().iterator(); + int removed = 0; + while (entityit.hasNext()) { + Entity entity = entityit.next(); + if (entity instanceof Arrow) { + if (!plugin.getPlayerSitData().isAroowOccupied(entity)) { + entity.remove(); + removed++; + } + } + } + return removed; + } + } diff --git a/src/com/cnaude/chairs/PlayerSitData.java b/src/com/cnaude/chairs/PlayerSitData.java index d63318d..bfd14ac 100644 --- a/src/com/cnaude/chairs/PlayerSitData.java +++ b/src/com/cnaude/chairs/PlayerSitData.java @@ -29,6 +29,17 @@ public class PlayerSitData { { return sit.containsKey(player.getName()); } + protected boolean isAroowOccupied(Entity entity) + { + for (Entity usedentity : sit.values()) + { + if (usedentity.getEntityId() == entity.getEntityId()) + { + return true; + } + } + return false; + } protected boolean isBlockOccupied(Block block) { return sitblock.containsKey(block); diff --git a/src/config.yml b/src/config.yml index 20926f3..3db5270 100644 --- a/src/config.yml +++ b/src/config.yml @@ -39,9 +39,9 @@ ignore-if-item-in-hand: false disabledWGRegions: - exampleregionname sit-effects: - enabled: false - interval: 20 healing: + enabled: false + interval: 20 amount: 1 max-percent: 100 sit-restrictions: