Add an interface for the Jail Pay Manager.

Further API work.
This commit is contained in:
graywolf336 2015-05-21 14:30:49 -05:00
parent cf50e37910
commit 307a096acc
4 changed files with 76 additions and 42 deletions

View File

@ -13,6 +13,7 @@ import com.graywolf336.jail.command.CommandHandler;
import com.graywolf336.jail.command.JailHandler; import com.graywolf336.jail.command.JailHandler;
import com.graywolf336.jail.enums.Lang; import com.graywolf336.jail.enums.Lang;
import com.graywolf336.jail.enums.Settings; import com.graywolf336.jail.enums.Settings;
import com.graywolf336.jail.interfaces.IJailPayManager;
import com.graywolf336.jail.interfaces.IJailStickManager; import com.graywolf336.jail.interfaces.IJailStickManager;
import com.graywolf336.jail.legacy.LegacyManager; import com.graywolf336.jail.legacy.LegacyManager;
import com.graywolf336.jail.listeners.CacheListener; import com.graywolf336.jail.listeners.CacheListener;
@ -38,7 +39,7 @@ public class JailMain extends JavaPlugin {
private JailHandler jh; private JailHandler jh;
private JailIO io; private JailIO io;
private JailManager jm; private JailManager jm;
private JailPayManager jpm; private IJailPayManager jpm;
private IJailStickManager jsm; private IJailStickManager jsm;
private JailTimer jt; private JailTimer jt;
private JailVoteManager jvm; private JailVoteManager jvm;
@ -296,8 +297,8 @@ public class JailMain extends JavaPlugin {
return this.jm; return this.jm;
} }
/** Gets the {@link JailPayManager} instance. */ /** Gets an instance of the {@link IJailPayManager}. */
public JailPayManager getJailPayManager() { public IJailPayManager getJailPayManager() {
return this.jpm; return this.jpm;
} }
@ -306,7 +307,7 @@ public class JailMain extends JavaPlugin {
return this.pm; return this.pm;
} }
/** Gets the {@link JailStickManager} instance. */ /** Gets an instance of the {@link JailStickManager}. */
public IJailStickManager getJailStickManager() { public IJailStickManager getJailStickManager() {
return this.jsm; return this.jsm;
} }

View File

@ -9,8 +9,9 @@ import org.bukkit.plugin.RegisteredServiceProvider;
import com.graywolf336.jail.beans.Prisoner; import com.graywolf336.jail.beans.Prisoner;
import com.graywolf336.jail.enums.Settings; import com.graywolf336.jail.enums.Settings;
import com.graywolf336.jail.interfaces.IJailPayManager;
public class JailPayManager { public class JailPayManager implements IJailPayManager {
private Economy economy = null; private Economy economy = null;
private double minteCost, infiniteCost; private double minteCost, infiniteCost;
private Material item; private Material item;
@ -32,72 +33,42 @@ public class JailPayManager {
this.infinite = plugin.getConfig().getDouble(Settings.JAILPAYPRICEINFINITE.getPath()) != 0; this.infinite = plugin.getConfig().getDouble(Settings.JAILPAYPRICEINFINITE.getPath()) != 0;
} }
/** Checks if paying for infinite is enabled. */
public boolean isInfiniteEnabled() { public boolean isInfiniteEnabled() {
return this.infinite; return this.infinite;
} }
/** Checks if paying for timed is enabled. */
public boolean isTimedEnabled() { public boolean isTimedEnabled() {
return this.timed; return this.timed;
} }
/** Gets how much it cost per minute in string format. */
public String getCostPerMinute() { public String getCostPerMinute() {
return String.valueOf(this.minteCost); return String.valueOf(this.minteCost);
} }
/**
* Calculates how much players have to pay to get completely free.
*
* @param prisoner data of who we're calculating
* @return The economy cost the prisoner will need to pay to get completely free.
*/
public double calculateBill(Prisoner prisoner) { public double calculateBill(Prisoner prisoner) {
return prisoner.getRemainingTime() >= 0 ? prisoner.getRemainingTimeInMinutes() * this.minteCost : infiniteCost; return prisoner.getRemainingTime() >= 0 ? prisoner.getRemainingTimeInMinutes() * this.minteCost : infiniteCost;
} }
/** Gets how many minutes someone is paying for (rounds to the lowest number). */
public long getMinutesPayingFor(double amount) { public long getMinutesPayingFor(double amount) {
return (long) Math.floor(amount / this.minteCost); return (long) Math.floor(amount / this.minteCost);
} }
/** Returns if we are using items for payment instead of economy. */
public boolean usingItemsForPayment() { public boolean usingItemsForPayment() {
return this.item != Material.AIR; return this.item != Material.AIR;
} }
/**
* Gets the {@link Material} it costs for jail pay, will be air if using economy.
*
* @return The item type it costs, air if using virtual economy.
*/
public Material getItemItCost() { public Material getItemItCost() {
return this.item; return this.item;
} }
/**
* Checks if the player has enough money/items to pay what they have said they want to.
*
* @param p The player who is doing the paying.
* @param amt The amount to check they if they have.
* @return true if they have enough, false if not.
*/
public boolean hasEnoughToPay(Player p, double amt) { public boolean hasEnoughToPay(Player p, double amt) {
if(this.usingItemsForPayment()) { if(this.usingItemsForPayment()) {
return p.getInventory().contains(this.item, (int) Math.ceil(amt)); return p.getInventory().contains(this.item, (int) Math.ceil(amt));
}else { }else {
return this.economy.has(p, amt);
return this.economy.has(p.getName(), amt);
} }
} }
/**
* Pays the required fees from the given player, removing items or money from economy.
*
* @param p The player who is paying.
* @param amt The amount of items or money to withdraw from the player.
*/
public void pay(Player p, double amt) { public void pay(Player p, double amt) {
if(this.usingItemsForPayment()) { if(this.usingItemsForPayment()) {
int amtNeeded = (int) Math.ceil(amt); int amtNeeded = (int) Math.ceil(amt);
@ -123,11 +94,10 @@ public class JailPayManager {
if (amtNeeded == 0) break; if (amtNeeded == 0) break;
} }
}else { }else {
this.economy.withdrawPlayer(p.getName(), amt); this.economy.withdrawPlayer(p, amt);
} }
} }
/** Gets the name of the item in nice capitals. */
public String getCurrencyName() { public String getCurrencyName() {
if(this.usingItemsForPayment()) { if(this.usingItemsForPayment()) {
String name = item.toString().replaceAll("_", " "); String name = item.toString().replaceAll("_", " ");
@ -154,7 +124,6 @@ public class JailPayManager {
} }
} }
/** Returns the economy provider to do transaction with. */
public Economy getEconomy() { public Economy getEconomy() {
return this.economy; return this.economy;
} }

View File

@ -7,12 +7,12 @@ import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.graywolf336.jail.JailManager; import com.graywolf336.jail.JailManager;
import com.graywolf336.jail.JailPayManager;
import com.graywolf336.jail.beans.Prisoner; import com.graywolf336.jail.beans.Prisoner;
import com.graywolf336.jail.command.Command; import com.graywolf336.jail.command.Command;
import com.graywolf336.jail.command.CommandInfo; import com.graywolf336.jail.command.CommandInfo;
import com.graywolf336.jail.enums.Lang; import com.graywolf336.jail.enums.Lang;
import com.graywolf336.jail.enums.Settings; import com.graywolf336.jail.enums.Settings;
import com.graywolf336.jail.interfaces.IJailPayManager;
@CommandInfo( @CommandInfo(
maxArgs = 2, maxArgs = 2,
@ -25,7 +25,7 @@ import com.graywolf336.jail.enums.Settings;
public class JailPayCommand implements Command { public class JailPayCommand implements Command {
public boolean execute(JailManager jm, CommandSender sender, String... args) throws Exception { public boolean execute(JailManager jm, CommandSender sender, String... args) throws Exception {
if(jm.getPlugin().getConfig().getBoolean(Settings.JAILPAYENABLED.getPath())) { if(jm.getPlugin().getConfig().getBoolean(Settings.JAILPAYENABLED.getPath())) {
JailPayManager pm = jm.getPlugin().getJailPayManager(); IJailPayManager pm = jm.getPlugin().getJailPayManager();
switch(args.length) { switch(args.length) {
case 1: case 1:

View File

@ -0,0 +1,64 @@
package com.graywolf336.jail.interfaces;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import com.graywolf336.jail.beans.Prisoner;
public interface IJailPayManager {
/** Checks if paying for infinite is enabled. */
public boolean isInfiniteEnabled();
/** Checks if paying for timed is enabled. */
public boolean isTimedEnabled();
/** Gets how much it cost per minute in string format. */
public String getCostPerMinute();
/**
* Calculates how much players have to pay to get completely free.
*
* @param prisoner data of who we're calculating
* @return The economy cost the prisoner will need to pay to get completely free.
*/
public double calculateBill(Prisoner prisoner);
/** Gets how many minutes someone is paying for (rounds to the lowest number). */
public long getMinutesPayingFor(double amount);
/** Returns if we are using items for payment instead of economy. */
public boolean usingItemsForPayment();
/**
* Gets the {@link Material} it costs for jail pay, will be air if using economy.
*
* @return The item type it costs, air if using virtual economy.
*/
public Material getItemItCost();
/**
* Checks if the player has enough money/items to pay what they have said they want to.
*
* @param player The player who is doing the paying.
* @param amount The amount to check they if they have.
* @return true if they have enough, false if not.
*/
public boolean hasEnoughToPay(Player player, double amount);
/**
* Pays the required fees from the given player, removing items or money from economy.
*
* @param player The player who is paying.
* @param amount The amount of items or money to withdraw from the player.
*/
public void pay(Player player, double amount);
/** Gets the name of the item in nice capitals. */
public String getCurrencyName();
/** Returns the economy provider to do transaction with. */
public Economy getEconomy();
}