From e9ce76aca48c1da1bab82ab34e97e763ede2407e Mon Sep 17 00:00:00 2001 From: Shevchik Date: Thu, 2 Jan 2014 17:58:35 +0400 Subject: [PATCH] Add items pickup while sitting --- src/com/cnaude/chairs/ChairEffects.java | 55 +++++++++++++++++++++--- src/com/cnaude/chairs/Chairs.java | 9 +++- src/com/cnaude/chairs/ChairsCommand.java | 5 +++ 3 files changed, 60 insertions(+), 9 deletions(-) diff --git a/src/com/cnaude/chairs/ChairEffects.java b/src/com/cnaude/chairs/ChairEffects.java index 8a91e56..c48f1a6 100644 --- a/src/com/cnaude/chairs/ChairEffects.java +++ b/src/com/cnaude/chairs/ChairEffects.java @@ -5,6 +5,8 @@ package com.cnaude.chairs; import org.bukkit.Bukkit; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Item; import org.bukkit.entity.Player; /** @@ -13,20 +15,24 @@ import org.bukkit.entity.Player; */ public class ChairEffects { - Chairs plugin; - int taskID; + private Chairs plugin; + private int healTaskID = -1; + private int pickupTaskID = -1; + public ChairEffects(Chairs plugin) { this.plugin = plugin; } public void startHealing() { - effectsTask(); + healEffectsTask(); } public void cancelHealing() { - plugin.getServer().getScheduler().cancelTask(taskID); - taskID = 0; + if (healTaskID != -1) { + plugin.getServer().getScheduler().cancelTask(healTaskID); + healTaskID = -1; + } } public void restartHealing() { @@ -34,8 +40,8 @@ public class ChairEffects { startHealing(); } - private void effectsTask() { - taskID = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() { + private void healEffectsTask() { + healTaskID = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() { @Override public void run() { for (Player p : Bukkit.getOnlinePlayers()) { @@ -55,4 +61,39 @@ public class ChairEffects { } }, plugin.sitHealInterval, plugin.sitHealInterval); } + + public void startPickup() { + pickupEffectsTask(); + } + + public void cancelPickup() { + if (pickupTaskID != -1) + plugin.getServer().getScheduler().cancelTask(pickupTaskID); + pickupTaskID = -1; + } + + public void restartPickup() { + cancelPickup(); + startPickup(); + } + + private void pickupEffectsTask() { + pickupTaskID = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() { + public void run() { + for (Player p : Bukkit.getOnlinePlayers()) { + if (plugin.getPlayerSitData().isSitting(p)) { + for (Entity entity : p.getNearbyEntities(1, 1, 1)) { + if (entity instanceof Item) { + if (p.getInventory().firstEmpty() != -1) { + p.getInventory().addItem(Item.class.cast(entity).getItemStack()); + entity.remove(); + } + } + } + } + } + } + },0,1); + } + } diff --git a/src/com/cnaude/chairs/Chairs.java b/src/com/cnaude/chairs/Chairs.java index 22b92fb..97c2a81 100644 --- a/src/com/cnaude/chairs/Chairs.java +++ b/src/com/cnaude/chairs/Chairs.java @@ -32,6 +32,7 @@ public class Chairs extends JavaPlugin { public int sitMaxHealth; public int sitHealthPerInterval; public int sitHealInterval; + public boolean sitPickupEnabled; public boolean sitDisableAllCommands = false; public HashSet sitDisabledCommands = new HashSet(); private Logger log; @@ -74,13 +75,16 @@ public class Chairs extends JavaPlugin { chairEffects = new ChairEffects(this); ignoreList = new ChairsIgnoreList(this); ignoreList.load(); + psitdata = new PlayerSitData(this); getConfig().options().copyDefaults(true); saveConfig(); loadConfig(); if (sitHealEnabled) { chairEffects.startHealing(); } - psitdata = new PlayerSitData(this); + if (sitPickupEnabled) { + chairEffects.startPickup(); + } getServer().getPluginManager().registerEvents(new TrySitEventListener(this, ignoreList), this); getServer().getPluginManager().registerEvents(new TryUnsitEventListener(this), this); getServer().getPluginManager().registerEvents(new CommandRestrict(this), this); @@ -97,7 +101,8 @@ public class Chairs extends JavaPlugin { if (ignoreList != null) { ignoreList.save(); } - chairEffects.cancelHealing(); + chairEffects.cancelHealing(); + chairEffects.cancelPickup(); chairEffects = null; log = null; vehiclearrowclass = null; diff --git a/src/com/cnaude/chairs/ChairsCommand.java b/src/com/cnaude/chairs/ChairsCommand.java index 8361780..0ec2764 100644 --- a/src/com/cnaude/chairs/ChairsCommand.java +++ b/src/com/cnaude/chairs/ChairsCommand.java @@ -44,6 +44,11 @@ public class ChairsCommand implements CommandExecutor { } else { plugin.chairEffects.cancelHealing(); } + if (plugin.sitPickupEnabled) { + plugin.chairEffects.restartPickup(); + } else { + plugin.chairEffects.cancelPickup(); + } if (!plugin.msgReloaded.isEmpty()) { sender.sendMessage(plugin.msgReloaded); }