From db1912445e8a4acad4efa04d71676fc5fe7dd487 Mon Sep 17 00:00:00 2001 From: cnaude Date: Fri, 15 Mar 2013 13:55:09 -0700 Subject: [PATCH] Added healing while sitting. Updated for CB1.5.0-R0.1 --- src/config.yml | 8 ++- src/net/spoothie/chairs/ChairEffects.java | 57 ++++++++++++++++++++++ src/net/spoothie/chairs/ChairWatcher.java | 4 +- src/net/spoothie/chairs/Chairs.java | 28 ++++++++++- src/net/spoothie/chairs/ChairsCommand.java | 1 + src/plugin.yml | 2 +- 6 files changed, 94 insertions(+), 6 deletions(-) create mode 100644 src/net/spoothie/chairs/ChairEffects.java diff --git a/src/config.yml b/src/config.yml index fed1e23..3f36584 100644 --- a/src/config.yml +++ b/src/config.yml @@ -42,4 +42,10 @@ upside-down-check: true seat-occupied-check: true upper-step-check: true per-item-perms: true -ops-override-perms: false \ No newline at end of file +ops-override-perms: false +sit-effects: + enabled: false + interval: 20 + healing: + amount: 1 + max-percent: 100 diff --git a/src/net/spoothie/chairs/ChairEffects.java b/src/net/spoothie/chairs/ChairEffects.java new file mode 100644 index 0000000..dec7410 --- /dev/null +++ b/src/net/spoothie/chairs/ChairEffects.java @@ -0,0 +1,57 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package net.spoothie.chairs; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; + +/** + * + * @author cnaude + */ +public class ChairEffects { + + Chairs plugin; + int taskID; + + public ChairEffects(Chairs plugin) { + this.plugin = plugin; + effectsTask(); + } + + public void cancel() { + plugin.getServer().getScheduler().cancelTask(taskID); + taskID = 0; + } + + public void restart() { + this.cancel(); + this.effectsTask(); + } + + private void effectsTask() { + taskID = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() { + @Override + public void run() { + for (Player p : Bukkit.getOnlinePlayers()) { + String pName = p.getName(); + if (plugin.sit.containsKey(pName)) { + if (p.hasPermission("chairs.sit.health")) { + double pHealthPcnt = (double) p.getHealth() / (double) p.getMaxHealth() * 100d; + if ((pHealthPcnt < plugin.sitMaxHealth) + && (p.getHealth() < p.getMaxHealth())) { + int newHealth = plugin.sitHealthPerInterval + p.getHealth(); + if (newHealth > p.getMaxHealth()) { + newHealth = p.getMaxHealth(); + } + p.setHealth(newHealth); + } + } + } + } + } + }, plugin.sitEffectInterval, plugin.sitEffectInterval); + } +} diff --git a/src/net/spoothie/chairs/ChairWatcher.java b/src/net/spoothie/chairs/ChairWatcher.java index 70df9aa..93b4ca6 100644 --- a/src/net/spoothie/chairs/ChairWatcher.java +++ b/src/net/spoothie/chairs/ChairWatcher.java @@ -5,8 +5,8 @@ package net.spoothie.chairs; import java.util.ArrayList; -import net.minecraft.server.v1_4_R1.DataWatcher; -import net.minecraft.server.v1_4_R1.WatchableObject; +import net.minecraft.server.v1_5_R1.DataWatcher; +import net.minecraft.server.v1_5_R1.WatchableObject; /** diff --git a/src/net/spoothie/chairs/Chairs.java b/src/net/spoothie/chairs/Chairs.java index 5532327..ff63905 100644 --- a/src/net/spoothie/chairs/Chairs.java +++ b/src/net/spoothie/chairs/Chairs.java @@ -6,12 +6,12 @@ import java.util.HashMap; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; -import net.minecraft.server.v1_4_R1.Packet40EntityMetadata; +import net.minecraft.server.v1_5_R1.Packet40EntityMetadata; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.Material; -import org.bukkit.craftbukkit.v1_4_R1.entity.CraftPlayer; +import org.bukkit.craftbukkit.v1_5_R1.entity.CraftPlayer; import org.bukkit.entity.Player; import org.bukkit.permissions.Permission; import org.bukkit.permissions.PermissionDefault; @@ -20,12 +20,17 @@ import org.bukkit.plugin.java.JavaPlugin; public class Chairs extends JavaPlugin { private static Chairs instance = null; + public static ChairEffects chairEffects; public List allowedBlocks = new ArrayList(); public List validSigns = new ArrayList(); public boolean sneaking, autoRotate, signCheck, permissions, notifyplayer, opsOverridePerms; public boolean invertedStairCheck, seatOccupiedCheck, invertedStepCheck, perItemPerms; + public boolean sitEffectsEnabled; public double sittingHeight, sittingHeightAdj, distance; public int maxChairWidth; + public int sitMaxHealth; + public int sitHealthPerInterval; + public int sitEffectInterval; private File pluginFolder; private File configFile; public byte metadata; @@ -50,11 +55,24 @@ public class Chairs extends JavaPlugin { loadConfig(); getServer().getPluginManager().registerEvents(new EventListener(this, ignoreList), this); getCommand("chairs").setExecutor(new ChairsCommand(this, ignoreList)); + if (sitEffectsEnabled) { + logInfo("Enabling sitting effects."); + chairEffects = new ChairEffects(this); + } } @Override public void onDisable() { ignoreList.save(); + if (chairEffects != null) { + chairEffects.cancel(); + } + } + + public void restartEffectsTask() { + if (chairEffects != null) { + chairEffects.restart(); + } } private void createConfig() { @@ -90,6 +108,11 @@ public class Chairs extends JavaPlugin { invertedStepCheck = getConfig().getBoolean("upper-step-check"); perItemPerms = getConfig().getBoolean("per-item-perms"); opsOverridePerms = getConfig().getBoolean("ops-override-perms"); + + sitEffectsEnabled = getConfig().getBoolean("sit-effects.enabled", false); + sitEffectInterval = getConfig().getInt("sit-effects.interval",20); + sitMaxHealth = getConfig().getInt("sit-effects.healing.max-percent",100); + sitHealthPerInterval = getConfig().getInt("sit-effects.healing.amount",1); for (String s : getConfig().getStringList("allowed-blocks")) { String type; @@ -137,6 +160,7 @@ public class Chairs extends JavaPlugin { perms.add("chairs.sit"); perms.add("chairs.reload"); perms.add("chairs.self"); + perms.add("chairs.sit.health"); for (String s : perms) { if (pm.getPermission(s) != null) { pm.removePermission(s); diff --git a/src/net/spoothie/chairs/ChairsCommand.java b/src/net/spoothie/chairs/ChairsCommand.java index a556081..cc29c66 100644 --- a/src/net/spoothie/chairs/ChairsCommand.java +++ b/src/net/spoothie/chairs/ChairsCommand.java @@ -33,6 +33,7 @@ public class ChairsCommand implements CommandExecutor { if (sender.hasPermission("chairs.reload") || !(sender instanceof Player)) { plugin.reloadConfig(); plugin.loadConfig(); + plugin.restartEffectsTask(); sender.sendMessage("Chairs configuration file reloaded."); } else { sender.sendMessage(ChatColor.GRAY + "No permission to do this!"); diff --git a/src/plugin.yml b/src/plugin.yml index 912d6bf..62667de 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -1,5 +1,5 @@ name: Chairs -version: 1.18.1 +version: 1.20.0 description: Let players sit on blocks. authors: - spoothie