Skip to content

Commit

Permalink
add ability to set a one-time fee for a course
Browse files Browse the repository at this point in the history
  • Loading branch information
steve4744 committed Dec 7, 2022
1 parent d64fffe commit a97c73d
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 20 deletions.
2 changes: 1 addition & 1 deletion docs/files/parkourCommands.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
"permission": "parkour.admin.*",
"commandGroup": "5",
"consoleSyntax": "pac economy (info / setprize / setfee / add / deduct / amount)",
"autoTabSyntax": "[info,setprize,setfee,add,deduct,amount] {1:setprize=(course),1:setfee=(course),1:info=(nothing),*=(player)} {1:info=(nothing),1:amount=(nothing),*=(amount)}"
"autoTabSyntax": "[info,setprize,setfee,add,deduct,amount] {1:setprize=(course),1:setfee=(course),1:info=(nothing),*=(player)} {1:info=(nothing),1:amount=(nothing),*=(amount)} {1:setfee=(truefalse),*=(nothing)}"
},
{
"command": "editkit",
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/io/github/a5h73y/parkour/Parkour.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.github.a5h73y.parkour.other.PluginBackupUtil;
import io.github.a5h73y.parkour.plugin.BountifulApi;
import io.github.a5h73y.parkour.plugin.EconomyApi;
import io.github.a5h73y.parkour.plugin.PermissionVault;
import io.github.a5h73y.parkour.plugin.PlaceholderApi;
import io.github.a5h73y.parkour.type.Initializable;
import io.github.a5h73y.parkour.type.Teardownable;
Expand Down Expand Up @@ -88,6 +89,7 @@ public class Parkour extends JavaPlugin {

private BountifulApi bountifulApi;
private EconomyApi economyApi;
private PermissionVault permissionVault;
private PlaceholderApi placeholderApi;

/**
Expand Down Expand Up @@ -219,6 +221,7 @@ private AbstractPluginReceiver registerManager(AbstractPluginReceiver manager) {
private void setupPlugins() {
bountifulApi = new BountifulApi(this);
economyApi = new EconomyApi(this);
permissionVault = new PermissionVault(this);
placeholderApi = new PlaceholderApi(this);
}

Expand Down Expand Up @@ -388,6 +391,10 @@ public EconomyApi getEconomyApi() {
return economyApi;
}

public PermissionVault getPermissionVault() {
return permissionVault;
}

public PlaceholderApi getPlaceholderApi() {
return placeholderApi;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public boolean onCommand(@NotNull CommandSender sender,
if (!PermissionUtils.hasPermission(player, Permission.ADMIN_ALL)) {
return false;

} else if (!ValidationUtils.validateArgs(player, args, 2, 4)) {
} else if (!ValidationUtils.validateArgs(player, args, 2, 5)) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import io.github.a5h73y.parkour.type.course.ParkourEventType;
import io.github.a5h73y.parkour.utility.MaterialUtils;
import io.github.a5h73y.parkour.utility.TranslationUtils;

import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.conversations.BooleanPrompt;
Expand Down
36 changes: 22 additions & 14 deletions src/main/java/io/github/a5h73y/parkour/plugin/EconomyApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public void displayEconomyInformation(CommandSender commandSender) {
/**
* Validate and Charge the Player for joining Course.
* Check if there is a Join Fee for the Course, and that the player has sufficient funds.
* If there is a Fee, charge the Player and continue.
* If there is a Fee, check if it's a one-time Fee, charge the Player and continue.
*
* @param player requesting player
* @param courseName course name
Expand All @@ -159,17 +159,23 @@ public boolean validateAndChargeCourseJoin(Player player, String courseName) {
double joinFee = parkour.getConfigManager().getCourseConfig(courseName).getEconomyJoiningFee();

if (joinFee > 0) {
if (!hasAmount(player, joinFee)) {
player.sendMessage(TranslationUtils.getTranslation("Economy.Insufficient")
.replace(AMOUNT_PLACEHOLDER, joinFee + getCurrencyName())
.replace(COURSE_PLACEHOLDER, courseName));
allowed = false;

} else {
chargePlayer(player, joinFee);
player.sendMessage(TranslationUtils.getTranslation("Economy.Fee")
.replace(AMOUNT_PLACEHOLDER, joinFee + getCurrencyName())
.replace(COURSE_PLACEHOLDER, courseName));
boolean oneTimeFee = parkour.getConfigManager().getCourseConfig(courseName).isOneTimeFee();
if (!oneTimeFee || (oneTimeFee && !parkour.getPermissionVault().hasPaidOneTimeFee(player, courseName))) {
if (!hasAmount(player, joinFee)) {
player.sendMessage(TranslationUtils.getTranslation("Economy.Insufficient")
.replace(AMOUNT_PLACEHOLDER, joinFee + getCurrencyName())
.replace(COURSE_PLACEHOLDER, courseName));
allowed = false;

} else {
chargePlayer(player, joinFee);
player.sendMessage(TranslationUtils.getTranslation("Economy.Fee")
.replace(AMOUNT_PLACEHOLDER, joinFee + getCurrencyName())
.replace(COURSE_PLACEHOLDER, courseName));
if (oneTimeFee) {
parkour.getPermissionVault().setPaidOneTimeFee(player, courseName);
}
}
}
}
}
Expand Down Expand Up @@ -241,8 +247,8 @@ private void processSetPrizeCommand(CommandSender commandSender, String... args)
}

private void processSetFeeCommand(CommandSender commandSender, String... args) {
if (args.length != 4) {
TranslationUtils.sendInvalidSyntax(commandSender, "econ", "setfee (course) (amount)");
if (args.length < 4) {
TranslationUtils.sendInvalidSyntax(commandSender, "econ", "setfee (course) (amount) [one-time-fee]");
return;
}

Expand All @@ -256,7 +262,9 @@ private void processSetFeeCommand(CommandSender commandSender, String... args) {
return;
}

boolean value = args.length == 5 && args[4].equalsIgnoreCase("true") ? true : false;
parkour.getConfigManager().getCourseConfig(args[2]).setEconomyJoiningFee(Double.parseDouble(args[3]));
parkour.getConfigManager().getCourseConfig(args[2]).setEconomyOneTimeFee(value);
TranslationUtils.sendPropertySet(commandSender, "Join Fee", args[2], args[3]);
}

Expand Down
58 changes: 58 additions & 0 deletions src/main/java/io/github/a5h73y/parkour/plugin/PermissionVault.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package io.github.a5h73y.parkour.plugin;

import static org.bukkit.Bukkit.getServer;

import org.bukkit.entity.Player;
import org.bukkit.plugin.RegisteredServiceProvider;
import io.github.a5h73y.parkour.Parkour;
import io.github.a5h73y.parkour.utility.PluginUtils;
import net.milkbowl.vault.permission.Permission;

public class PermissionVault extends PluginWrapper {

public PermissionVault(Parkour parkour) {
super(parkour);
}

private Permission permission;
private static final String COURSE_PERMISSION = "parkour.course.";

@Override
protected void initialise() {
super.initialise();

if (isEnabled()) {
RegisteredServiceProvider<Permission> rsp = getServer().getServicesManager().getRegistration(Permission.class);

if (rsp == null) {
PluginUtils.log("[Permission] Failed to connect to Vault's Permission service.", 2);
setEnabled(false);
return;
}

permission = rsp.getProvider();
}
}

public Permission getPermissions() {
return permission;
}

public boolean isPermissions() {
return permission != null;
}

public boolean hasPaidOneTimeFee(Player player, String courseName) {
return player.hasPermission(COURSE_PERMISSION + courseName);
}

public void setPaidOneTimeFee(Player player, String courseName) {
permission.playerAdd(null, player, COURSE_PERMISSION + courseName);
}

@Override
public String getPluginName() {
return "Vault";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class CourseConfig extends Json {
public static final String DISPLAY_NAME = "DisplayName";
public static final String ECONOMY_FINISH_REWARD = "EconomyFinishReward";
public static final String ECONOMY_JOINING_FEE = "EconomyJoiningFee";
public static final String ECONOMY_ONETIME_FEE = "EconomyOneTimeFee";
public static final String HAS_FALL_DAMAGE = "HasFallDamage";
public static final String JOIN_ITEMS = "JoinItems";
public static final String LINKED_COURSE = "LinkedCourse";
Expand Down Expand Up @@ -436,6 +437,7 @@ public String getMaterialPrizeLabel() {
* The Material and Amount to be rewarded for finishing the Course.
* @param materialName prize material
* @param amount prize amount
* @param label display name
*/
public void setMaterialPrize(@NotNull String materialName, int amount, String label) {
this.set(PRIZE_MATERIAL, materialName);
Expand Down Expand Up @@ -812,7 +814,7 @@ public void setEconomyFinishReward(@Nullable Double finishReward) {
}

/**
* Get Economic Fee for joining Course.
* Get Economy Fee for joining Course.
* @return joining fee
*/
public double getEconomyJoiningFee() {
Expand All @@ -827,6 +829,22 @@ public boolean hasEconomyJoiningFee() {
return this.contains(ECONOMY_JOINING_FEE);
}

/**
* Check if the Economy Joining fee is a one-time fee.
* @return is one-time joining fee
*/
public boolean isOneTimeFee() {
return this.getBoolean(ECONOMY_ONETIME_FEE);
}

/**
* Set Economy one-time fee.
* @param is one-time joining fee
*/
public void setEconomyOneTimeFee(Boolean OneTimeFee) {
this.set(ECONOMY_ONETIME_FEE, OneTimeFee);
}

/**
* Set Economy Joining fee.
* @param joinFee join fee
Expand Down Expand Up @@ -1090,6 +1108,8 @@ public static void displayCourseInfo(@NotNull CommandSender commandSender,

if (parkour.getEconomyApi().isEnabled()) {
sendConditionalValue(commandSender, "Join Fee", config.getEconomyJoiningFee());
sendConditionalValue(commandSender, "One-Time Fee",
config.hasEconomyJoiningFee() && config.isOneTimeFee(), "true");
sendConditionalValue(commandSender, "Economy Reward", config.getEconomyFinishReward());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import java.util.Map;
import java.util.Set;
import de.leonhard.storage.sections.FlatFileSection;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.conversations.Conversable;
import org.bukkit.entity.Player;
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/parkourCommands.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
"permission": "parkour.admin.*",
"commandGroup": "5",
"consoleSyntax": "pac economy (info / setprize / setfee / add / deduct / amount)",
"autoTabSyntax": "[info,setprize,setfee,add,deduct,amount] {1:setprize=(course),1:setfee=(course),1:info=(nothing),*=(player)} {1:info=(nothing),1:amount=(nothing),*=(amount)}"
"autoTabSyntax": "[info,setprize,setfee,add,deduct,amount] {1:setprize=(course),1:setfee=(course),1:info=(nothing),*=(player)} {1:info=(nothing),1:amount=(nothing),*=(amount)} {1:setfee=(truefalse),*=(nothing)}"
},
{
"command": "editkit",
Expand Down

0 comments on commit a97c73d

Please sign in to comment.