Skip to content
This repository has been archived by the owner on Dec 19, 2021. It is now read-only.

Commit

Permalink
* Fixed thread to use TaskTimer which is much safer
Browse files Browse the repository at this point in the history
+ Added messages to config - now configurable
* Fixed crappy project structure
  • Loading branch information
bartenbach committed Oct 23, 2016
1 parent 955a015 commit 785a22d
Show file tree
Hide file tree
Showing 20 changed files with 338 additions and 240 deletions.
37 changes: 17 additions & 20 deletions src/co/proxa/durabilitynotify/DurabilityNotify.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,30 @@

package co.proxa.durabilitynotify;

import co.proxa.durabilitynotify.file.ConfigHandler;
import co.proxa.durabilitynotify.file.Paths;
import co.proxa.durabilitynotify.handler.*;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import co.proxa.durabilitynotify.file.FileHandler;
import co.proxa.durabilitynotify.listeners.*;
import co.proxa.durabilitynotify.threads.ReminderThread;
import co.proxa.durabilitynotify.runnable.ReminderRunnable;

public class DurabilityNotify extends JavaPlugin {

private final LiveNotify ln = new LiveNotify();
private Armor a;
private ReminderThread rt;
private final LiveNotifyHandler ln = new LiveNotifyHandler();
private final FileHandler fh = new FileHandler(this);
private final ConfigHandler lm = new ConfigHandler(this);
private final PermissionsHandler ph = new PermissionsHandler(this);
private ArmorHandler a;

@Override
public void onEnable() {
final FileHandler fh = new FileHandler(this);
final ListManager lm = new ListManager(this);
final Permissions p = new Permissions(this);
final Tool t = new Tool(lm);
a = new Armor(lm);
final ToolHandler t = new ToolHandler(lm);
a = new ArmorHandler(lm);
final BlockBreakListener bbl = new BlockBreakListener();
final BowListener bl = new BowListener(lm);
final FishingListener fl = new FishingListener(lm);
Expand All @@ -58,7 +59,7 @@ public void onEnable() {
final FlintAndSteelListener fasl = new FlintAndSteelListener(lm);
final ShearListener sl = new ShearListener(lm);
final PluginManager pm = getServer().getPluginManager();
final Notify n = new Notify();
final NotifyHandler n = new NotifyHandler();

this.checkReminderThread(a);
fh.checkFiles();
Expand All @@ -73,33 +74,29 @@ public void onEnable() {
pm.registerEvents(sl, this);
}

private void checkReminderThread(Armor a) {
private void checkReminderThread(ArmorHandler a) {
if (this.getConfig().getBoolean(Paths.reminderEnabled)) {
int minutes = this.getConfig().getInt(Paths.reminderMinutes);
rt = new ReminderThread(this, a, minutes);
rt.startThread();
int minutes = this.getConfig().getInt(Paths.reminderMinutes) * 20 * 60;
getServer().getScheduler().runTaskTimer(this, new ReminderRunnable(this), minutes, minutes);
}
}

@Override
public void onDisable() {
if (rt != null) {
rt.stopThread();
}
ln.clearMap();
}

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (sender instanceof Player && Permissions.hasToolPerms((Player) sender) && label.equalsIgnoreCase("dura")) {
if (sender instanceof Player && PermissionsHandler.hasToolPerms((Player) sender) && label.equalsIgnoreCase("dura")) {
if (ln.onMap((Player)sender)) {
ln.toggleNotify((Player)sender);
} else {
LiveNotify.putPlayerOnMap((Player)sender);
LiveNotifyHandler.putPlayerOnMap((Player)sender);
}
ln.sendMessage((Player)sender);
return true;
} else if (sender instanceof Player && Permissions.hasArmorPerms((Player) sender) && label.startsWith("arm")) {
} else if (sender instanceof Player && PermissionsHandler.hasArmorPerms((Player) sender) && label.startsWith("arm")) {
a.checkArmorCommand((Player) sender);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package co.proxa.durabilitynotify;
package co.proxa.durabilitynotify.file;

import co.proxa.durabilitynotify.file.Paths;
import co.proxa.durabilitynotify.DurabilityNotify;

import java.util.List;

public class ListManager {
public class ConfigHandler {

private DurabilityNotify dn;
private List<Integer> woodenPickaxe;
Expand Down Expand Up @@ -73,8 +73,22 @@ public class ListManager {
private List<Integer> diamondChestplate;
private List<Integer> diamondLeggings;
private List<Integer> diamondBoots;

public ListManager(DurabilityNotify dn) {
public static String reminderMsg;
public static String liveNotifyOnMsg;
public static String liveNotifyOffMsg;
public static String infoMsg;
public static String toolWarningMsg;
public static String toolWarningBroken;
public static String armorWarningMsg;
public static String armorWarningBroken;
public static String infoBroken;
public static String improperToolMsg;
public static String grammarSingular;
public static String grammarPlural;
public static String grammar2Singular;
public static String grammar2Plural;

public ConfigHandler(DurabilityNotify dn) {
this.dn = dn;
}

Expand Down Expand Up @@ -129,6 +143,20 @@ public void initializeLists() {
diamondChestplate = dn.getConfig().getIntegerList(Paths.diamondChestplate);
diamondLeggings = dn.getConfig().getIntegerList(Paths.diamondLeggings);
diamondBoots = dn.getConfig().getIntegerList(Paths.diamondBoots);
reminderMsg = dn.getConfig().getString(Paths.reminderMsg);
liveNotifyOffMsg = dn.getConfig().getString(Paths.liveDuraOffMsg);
liveNotifyOnMsg = dn.getConfig().getString(Paths.liveDuraOnMsg);
infoMsg = dn.getConfig().getString(Paths.infoMsg);
infoBroken = dn.getConfig().getString(Paths.infoBrokenMsg);
toolWarningMsg = dn.getConfig().getString(Paths.toolWarningMsg);
toolWarningBroken = dn.getConfig().getString(Paths.toolWarningBrokenMsg);
armorWarningMsg = dn.getConfig().getString(Paths.armorWarningMsg);
armorWarningBroken = dn.getConfig().getString(Paths.armorWarningBrokenMsg);
improperToolMsg = dn.getConfig().getString(Paths.improperToolMsg);
grammarSingular = dn.getConfig().getString(Paths.grammarSingular);
grammarPlural = dn.getConfig().getString(Paths.grammarPlural);
grammar2Singular = dn.getConfig().getString(Paths.grammar2Singular);
grammar2Plural = dn.getConfig().getString(Paths.grammar2Plural);
}

public List<Integer> getWoodenPickaxe() {
Expand Down
2 changes: 2 additions & 0 deletions src/co/proxa/durabilitynotify/file/FileHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

public class FileHandler {


private DurabilityNotify dn;


public FileHandler(DurabilityNotify dn) {
this.dn = dn;
}
Expand Down
56 changes: 36 additions & 20 deletions src/co/proxa/durabilitynotify/file/Paths.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,40 @@ public class Paths {
public final static String flintAndSteel = "DurabilityNotify Configuration.Tools.Flint and Steel";
public final static String shears = "DurabilityNotify Configuration.Tools.Shears";
public final static String carrotStick = "DurabilityNotify Configuration.Tools.CarrotStick";
public final static String leatherHelmet = "DurabilityNotify Configuration.Armor.Leather.Helmet";
public final static String leatherChestplate = "DurabilityNotify Configuration.Armor.Leather.Chestplate";
public final static String leatherLeggings = "DurabilityNotify Configuration.Armor.Leather.Leggings";
public final static String leatherBoots = "DurabilityNotify Configuration.Armor.Leather.Boots";
public final static String goldHelmet = "DurabilityNotify Configuration.Armor.Gold.Helmet";
public final static String goldChestplate = "DurabilityNotify Configuration.Armor.Gold.Chestplate";
public final static String goldLeggings = "DurabilityNotify Configuration.Armor.Gold.Leggings";
public final static String goldBoots = "DurabilityNotify Configuration.Armor.Gold.Boots";
public final static String chainmailHelmet = "DurabilityNotify Configuration.Armor.Chainmail.Helmet";
public final static String chainmailChestplate = "DurabilityNotify Configuration.Armor.Chainmail.Chestplate";
public final static String chainmailLeggings = "DurabilityNotify Configuration.Armor.Chainmail.Leggings";
public final static String chainmailBoots = "DurabilityNotify Configuration.Armor.Chainmail.Boots";
public final static String ironHelmet = "DurabilityNotify Configuration.Armor.Iron.Helmet";
public final static String ironChestplate = "DurabilityNotify Configuration.Armor.Iron.Chestplate";
public final static String ironLeggings = "DurabilityNotify Configuration.Armor.Iron.Leggings";
public final static String ironBoots = "DurabilityNotify Configuration.Armor.Iron.Boots";
public final static String diamondHelmet = "DurabilityNotify Configuration.Armor.Diamond.Helmet";
public final static String diamondChestplate = "DurabilityNotify Configuration.Armor.Diamond.Chestplate";
public final static String diamondLeggings = "DurabilityNotify Configuration.Armor.Diamond.Leggings";
public final static String diamondBoots = "DurabilityNotify Configuration.Armor.Diamond.Boots";
public final static String leatherHelmet = "DurabilityNotify Configuration.ArmorHandler.Leather.Helmet";
public final static String leatherChestplate = "DurabilityNotify Configuration.ArmorHandler.Leather.Chestplate";
public final static String leatherLeggings = "DurabilityNotify Configuration.ArmorHandler.Leather.Leggings";
public final static String leatherBoots = "DurabilityNotify Configuration.ArmorHandler.Leather.Boots";
public final static String goldHelmet = "DurabilityNotify Configuration.ArmorHandler.Gold.Helmet";
public final static String goldChestplate = "DurabilityNotify Configuration.ArmorHandler.Gold.Chestplate";
public final static String goldLeggings = "DurabilityNotify Configuration.ArmorHandler.Gold.Leggings";
public final static String goldBoots = "DurabilityNotify Configuration.ArmorHandler.Gold.Boots";
public final static String chainmailHelmet = "DurabilityNotify Configuration.ArmorHandler.Chainmail.Helmet";
public final static String chainmailChestplate = "DurabilityNotify Configuration.ArmorHandler.Chainmail.Chestplate";
public final static String chainmailLeggings = "DurabilityNotify Configuration.ArmorHandler.Chainmail.Leggings";
public final static String chainmailBoots = "DurabilityNotify Configuration.ArmorHandler.Chainmail.Boots";
public final static String ironHelmet = "DurabilityNotify Configuration.ArmorHandler.Iron.Helmet";
public final static String ironChestplate = "DurabilityNotify Configuration.ArmorHandler.Iron.Chestplate";
public final static String ironLeggings = "DurabilityNotify Configuration.ArmorHandler.Iron.Leggings";
public final static String ironBoots = "DurabilityNotify Configuration.ArmorHandler.Iron.Boots";
public final static String diamondHelmet = "DurabilityNotify Configuration.ArmorHandler.Diamond.Helmet";
public final static String diamondChestplate = "DurabilityNotify Configuration.ArmorHandler.Diamond.Chestplate";
public final static String diamondLeggings = "DurabilityNotify Configuration.ArmorHandler.Diamond.Leggings";
public final static String diamondBoots = "DurabilityNotify Configuration.ArmorHandler.Diamond.Boots";
public final static String liveDuraOnMsg = "DurabilityNotify Configuration.Messages.Live Durability on";
public final static String liveDuraOffMsg = "DurabilityNotify Configuration.Messages.Live Durability off";
public final static String toolWarningMsg = "DurabilityNotify Configuration.Messages.ToolWarning";
public final static String toolWarningBrokenMsg = "DurabilityNotify Configuration.Messages.ToolWarningBroken";
public final static String armorWarningMsg = "DurabilityNotify Configuration.Messages.ArmorWarning";
public final static String armorWarningBrokenMsg = "DurabilityNotify Configuration.Messages.ArmorWarningBroken";
public final static String infoMsg = "DurabilityNotify Configuration.Messages.Info";
public final static String infoBrokenMsg = "DurabilityNotify Configuration.Messages.InfoBroken";
public final static String reminderMsg = "DurabilityNotify Configuration.Messages.Reminder";
public final static String improperToolMsg = "DurabilityNotify Configuration.Messages.Improper ToolHandler Message";
public final static String grammarSingular = "DurabilityNotify Configuration.Messages.Grammar Singular";
public final static String grammarPlural = "DurabilityNotify Configuration.Messages.Grammar Plural";
public final static String grammar2Singular = "DurabilityNotify Configuration.Messages.Grammar2 Singular";
public final static String grammar2Plural = "DurabilityNotify Configuration.Messages.Grammar2 Plural";


}
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
package co.proxa.durabilitynotify;
package co.proxa.durabilitynotify.handler;

import co.proxa.durabilitynotify.file.ConfigHandler;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.util.List;

public class Armor {
public class ArmorHandler {

private ListManager lm;
private static ConfigHandler lm;

public Armor(ListManager lm) {
public ArmorHandler(ConfigHandler lm) {
this.lm = lm;
}

private double getPercentDurabilityLeft(ItemStack is) {
private static double getPercentDurabilityLeft(ItemStack is) {
return (100 - (((double)is.getDurability() / (double)is.getType().getMaxDurability()) * 100.00));
}

private boolean isApproximatePercentage(double currentDurability, List<Integer> configList) {
private static boolean isApproximatePercentage(double currentDurability, List<Integer> configList) {
for (double d : configList) {
if (currentDurability >= (d - 1.0) && currentDurability <= (d + 1.0)) {
return true;
Expand All @@ -34,20 +35,20 @@ public void checkArmor(Player player) {
ItemStack boots = player.getInventory().getBoots();

if (helmet != null && notifyHelmet(helmet, getPercentDurabilityLeft(helmet))) {
Notify.createArmorWarning(player, helmet, (int) getPercentDurabilityLeft(helmet));
NotifyHandler.createArmorWarning(player, helmet, (int) getPercentDurabilityLeft(helmet));
}
if (chestplate != null && notifyChestplate(chestplate, getPercentDurabilityLeft(chestplate))) {
Notify.createArmorWarning(player, chestplate, (int) getPercentDurabilityLeft(chestplate));
NotifyHandler.createArmorWarning(player, chestplate, (int) getPercentDurabilityLeft(chestplate));
}
if (leggings != null && notifyLeggings(leggings, getPercentDurabilityLeft(leggings))) {
Notify.createArmorWarning(player, leggings, (int) getPercentDurabilityLeft(leggings));
NotifyHandler.createArmorWarning(player, leggings, (int) getPercentDurabilityLeft(leggings));
}
if (boots != null && notifyBoots(boots, getPercentDurabilityLeft(boots))) {
Notify.createArmorWarning(player, boots, (int) getPercentDurabilityLeft(boots));
NotifyHandler.createArmorWarning(player, boots, (int) getPercentDurabilityLeft(boots));
}
}

private boolean notifyHelmet(ItemStack helmet, double durability) {
private static boolean notifyHelmet(ItemStack helmet, double durability) {
boolean isNotifyTime = false;
switch (helmet.getType()) {
case LEATHER_HELMET:
Expand All @@ -69,7 +70,7 @@ private boolean notifyHelmet(ItemStack helmet, double durability) {
return isNotifyTime;
}

private boolean notifyChestplate(ItemStack chestplate, double durability) {
private static boolean notifyChestplate(ItemStack chestplate, double durability) {
boolean isNotifyTime = false;
switch (chestplate.getType()) {
case LEATHER_CHESTPLATE:
Expand All @@ -91,7 +92,7 @@ private boolean notifyChestplate(ItemStack chestplate, double durability) {
return isNotifyTime;
}

private boolean notifyLeggings(ItemStack leggings, double durability) {
private static boolean notifyLeggings(ItemStack leggings, double durability) {
boolean isNotifyTime = false;
switch (leggings.getType()) {
case LEATHER_LEGGINGS:
Expand All @@ -113,7 +114,7 @@ private boolean notifyLeggings(ItemStack leggings, double durability) {
return isNotifyTime;
}

private boolean notifyBoots(ItemStack boots, double durability) {
private static boolean notifyBoots(ItemStack boots, double durability) {
boolean isNotifyTime = false;
switch (boots.getType()) {
case LEATHER_BOOTS:
Expand All @@ -135,23 +136,23 @@ private boolean notifyBoots(ItemStack boots, double durability) {
return isNotifyTime;
}

public void checkArmorForReminder(Player player) {
public static void checkArmorForReminder(Player player) {
ItemStack helmet = player.getInventory().getHelmet();
ItemStack chestplate = player.getInventory().getChestplate();
ItemStack leggings = player.getInventory().getLeggings();
ItemStack boots = player.getInventory().getBoots();

if (helmet != null && notifyHelmet(helmet, getPercentDurabilityLeft(helmet))) {
Notify.createArmorReminder(player, helmet, (int) getPercentDurabilityLeft(helmet));
NotifyHandler.createArmorReminder(player, helmet, (int) getPercentDurabilityLeft(helmet));
}
if (chestplate != null && notifyChestplate(chestplate, getPercentDurabilityLeft(chestplate))) {
Notify.createArmorReminder(player, chestplate, (int) getPercentDurabilityLeft(chestplate));
NotifyHandler.createArmorReminder(player, chestplate, (int) getPercentDurabilityLeft(chestplate));
}
if (leggings != null && notifyLeggings(leggings, getPercentDurabilityLeft(leggings))) {
Notify.createArmorReminder(player, leggings, (int) getPercentDurabilityLeft(leggings));
NotifyHandler.createArmorReminder(player, leggings, (int) getPercentDurabilityLeft(leggings));
}
if (boots != null && notifyBoots(boots, getPercentDurabilityLeft(boots))) {
Notify.createArmorReminder(player, boots, (int) getPercentDurabilityLeft(boots));
NotifyHandler.createArmorReminder(player, boots, (int) getPercentDurabilityLeft(boots));
}
}

Expand Down Expand Up @@ -194,22 +195,22 @@ public void checkArmorCommand(Player player) {

player.sendMessage(ChatColor.WHITE + "--------------------------");
if (helmet != null) {
Notify.sendArmorCommandMessage(player, helmet, getPercentDurabilityLeft(helmet));
NotifyHandler.sendArmorCommandMessage(player, helmet, getPercentDurabilityLeft(helmet));
} else {
player.sendMessage(ChatColor.GRAY + "Helmet " + ChatColor.WHITE + "-" + ChatColor.GRAY + " none");
}
if (chestplate != null) {
Notify.sendArmorCommandMessage(player, chestplate, getPercentDurabilityLeft(chestplate));
NotifyHandler.sendArmorCommandMessage(player, chestplate, getPercentDurabilityLeft(chestplate));
} else {
player.sendMessage(ChatColor.GRAY + "Chestplate " + ChatColor.WHITE + "-" + ChatColor.GRAY + " none");
}
if (leggings != null) {
Notify.sendArmorCommandMessage(player, leggings, getPercentDurabilityLeft(leggings));
NotifyHandler.sendArmorCommandMessage(player, leggings, getPercentDurabilityLeft(leggings));
} else {
player.sendMessage(ChatColor.GRAY + "Leggings " + ChatColor.WHITE + "-" + ChatColor.GRAY + " none");
}
if (boots != null) {
Notify.sendArmorCommandMessage(player, boots, getPercentDurabilityLeft(boots));
NotifyHandler.sendArmorCommandMessage(player, boots, getPercentDurabilityLeft(boots));
} else {
player.sendMessage(ChatColor.GRAY + "Boots " + ChatColor.WHITE + "-" + ChatColor.GRAY + " none");
}
Expand Down
Loading

0 comments on commit 785a22d

Please sign in to comment.