Add items pickup while sitting

This commit is contained in:
Shevchik 2014-01-02 17:58:35 +04:00
parent 92188fd45e
commit e9ce76aca4
3 changed files with 60 additions and 9 deletions

View File

@ -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);
}
}

View File

@ -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<String> sitDisabledCommands = new HashSet<String>();
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);
@ -98,6 +102,7 @@ public class Chairs extends JavaPlugin {
ignoreList.save();
}
chairEffects.cancelHealing();
chairEffects.cancelPickup();
chairEffects = null;
log = null;
vehiclearrowclass = null;

View File

@ -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);
}