Browse Source

Added several configuration options and fixed coupon generation issue.

master
Stefatorus 4 years ago
parent
commit
0cd35db6d1
  1. 24
      ERCoupons/resources/server.yml
  2. 17
      ERCoupons/src/com/entryrise/coupons/Data.java
  3. 2
      ERCoupons/src/com/entryrise/coupons/Main.java
  4. 33
      ERCoupons/src/com/entryrise/coupons/cmd/CommandListener.java
  5. 36
      ERCoupons/src/com/entryrise/coupons/utils/CSUtils.java
  6. 2
      ERCoupons/src/com/entryrise/coupons/utils/Chat.java

24
ERCoupons/resources/server.yml

@ -1,5 +1,29 @@
# ______ _____ _____
# | ____| __ \ / ____|
# | |__ | |__) | | ___ _ _ _ __ ___ _ __ ___
# | __| | _ /| | / _ \| | | | '_ \ / _ \| '_ \/ __|
# | |____| | \ \| |___| (_) | |_| | |_) | (_) | | | \__ \
# |______|_| \_\\_____\___/ \__,_| .__/ \___/|_| |_|___/
# | |
# |_|
# Plugin config # Plugin config
# You can use any craftingstore server api key. You need at least silver plan.
api-key: "CRAFTINGSTORE KEY HERE" api-key: "CRAFTINGSTORE KEY HERE"
settings:
# Settings for redeeming credits for store money.
redeem:
# What is the minimum credits to redeem in a coupon?
min: 100
# What is the maximum credits to redeem in a coupon?
max: 50000
# Settings for virtual coupon players can use for safe trading, bragging, etc.
virtual:
# What is the minimum currency to hold in an item?
min: 50
# What is the maximum currency to hold in an item?
max: 5000
# Version is used for future updates # Version is used for future updates
version: 1 version: 1

17
ERCoupons/src/com/entryrise/coupons/Data.java

@ -80,21 +80,24 @@ public class Data {
final UUID u = p.getUniqueId(); final UUID u = p.getUniqueId();
final long available = getCredits(u); final long available = getCredits(u);
if (credits > 5000) {
p.sendMessage("§2§lER§f§lCoupons §e» §fYou can make a coupon holding a maximum of 5000 credits");
int min = Main.config.getInt("settings.virtual.min");
int max = Main.config.getInt("settings.virtual.max");
if (credits > max) {
p.sendMessage(Main.PREFIX + "You can make a coupon holding a maximum of " + max + " credits");
return; return;
} }
if (credits < 50) {
p.sendMessage("§2§lER§f§lCoupons §e» §fYou can't make a coupon holding under 50 credits");
if (credits < min) {
p.sendMessage(Main.PREFIX + "You can't make a coupon holding under " + min + " credits");
return; return;
} }
if (available < credits) { if (available < credits) {
p.sendMessage("§2§lER§f§lCoupons §e» §fYou need another " + (available - credits)
p.sendMessage(Main.PREFIX + "You need another " + (available - credits)
+ " credits to afford creating this coupon."); + " credits to afford creating this coupon.");
return; return;
} }
if (p.getInventory().firstEmpty() == -1) { if (p.getInventory().firstEmpty() == -1) {
p.sendMessage("§2§lER§f§lCoupons §e» §fYou need to make space in your inventory to create a coupon.");
p.sendMessage(Main.PREFIX + "You need to make space in your inventory to create a coupon.");
return; return;
} }
@ -174,7 +177,7 @@ public class Data {
} }
long current = getCredits(u); long current = getCredits(u);
setCredits(u, current + credits); setCredits(u, current + credits);
p.sendMessage("§2§lER§f§lCoupons §e» §fYou have redeemed a coupon worth " + credits + " credits. Current: "
p.sendMessage(Main.PREFIX + "You have redeemed a coupon worth " + credits + " credits. Current: "
+ (current + credits)); + (current + credits));
return true; return true;
} }

2
ERCoupons/src/com/entryrise/coupons/Main.java

@ -17,7 +17,7 @@ import com.entryrise.coupons.utils.Others;
public class Main extends JavaPlugin implements Listener public class Main extends JavaPlugin implements Listener
{ {
public static String USER = "%%__USER__%%"; public static String USER = "%%__USER__%%";
public static final String PREFIX = c§lTemplate§f§lPlugin §e» §f";
public static final String PREFIX = 2§lER§f§lCoupons §e» §f";
public static JavaPlugin p; public static JavaPlugin p;

33
ERCoupons/src/com/entryrise/coupons/cmd/CommandListener.java

@ -9,6 +9,8 @@ import java.util.UUID;
import com.entryrise.coupons.utils.CSUtils; import com.entryrise.coupons.utils.CSUtils;
import com.entryrise.coupons.utils.MathUtils; import com.entryrise.coupons.utils.MathUtils;
import com.entryrise.coupons.Data; import com.entryrise.coupons.Data;
import com.entryrise.coupons.Main;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -20,20 +22,25 @@ public class CommandListener implements CommandExecutor {
final Player p = (sender instanceof Player) ? (Player) sender : null; final Player p = (sender instanceof Player) ? (Player) sender : null;
if (args.length == 0) { if (args.length == 0) {
if (p == null) { if (p == null) {
sender.sendMessage("§2§lER§f§lCoupons §e» §f");
sender.sendMessage(Main.PREFIX + "");
return true; return true;
} }
sender.sendMessage("§2§lER§f§lCoupons §e» §fYou have " + Data.getCredits(p.getUniqueId())
sender.sendMessage(Main.PREFIX + "You have " + Data.getCredits(p.getUniqueId())
+ " credits in your account."); + " credits in your account.");
return true; return true;
} else { } else {
if (args.length == 1) {
final int count = MathUtils.isInt(args[0]) ? Integer.valueOf(args[0]) : -1;
if (args.length == 1 && MathUtils.isInt(args[0])) {
final int count = Integer.valueOf(args[0]);
Data.createCoupon(p, count); Data.createCoupon(p, count);
return true; return true;
}
if (args.length == 2) {
if (args[0].equalsIgnoreCase("store")) {
} else if (args.length == 1 && args[0].equalsIgnoreCase("version")) {
sender.sendMessage(Main.PREFIX + "You are using ERCoupons v" + Main.p.getDescription().getVersion() + " by EntryRise: https://git.entryrise.com/EntryRise/ERCoupons");
return true;
} else if (args.length == 1) {
sender.sendMessage(Main.PREFIX + "To create a virtual coupon, please use /coupons (AMOUNT)");
return true;
} else if (args.length == 2) {
if (args[0].equalsIgnoreCase("redeem")) {
final int count = MathUtils.isInt(args[1]) ? Integer.valueOf(args[1]) : -1; final int count = MathUtils.isInt(args[1]) ? Integer.valueOf(args[1]) : -1;
CSUtils.redeemStore(p, (long) count); CSUtils.redeemStore(p, (long) count);
return true; return true;
@ -46,9 +53,9 @@ public class CommandListener implements CommandExecutor {
} catch (Exception e) { } catch (Exception e) {
u = Bukkit.getOfflinePlayer(args[1]).getUniqueId(); u = Bukkit.getOfflinePlayer(args[1]).getUniqueId();
} }
final int amount = MathUtils.isInt(args[2]) ? Integer.valueOf(args[2]) : 0;
int amount = MathUtils.isInt(args[2]) ? Integer.valueOf(args[2]) : 0;
if (amount == 0) { if (amount == 0) {
sender.sendMessage("§2§lER§f§lCoupons §e» §fMUST BE NEGATIVE OR POSITIVE NUMBER.");
sender.sendMessage(Main.PREFIX + "MUST BE NEGATIVE OR POSITIVE NUMBER.");
return true; return true;
} }
Data.setCredits(u, Data.getCredits(u) + amount); Data.setCredits(u, Data.getCredits(u) + amount);
@ -60,12 +67,12 @@ public class CommandListener implements CommandExecutor {
} catch (Exception e) { } catch (Exception e) {
u = Bukkit.getOfflinePlayer(args[1]).getUniqueId(); u = Bukkit.getOfflinePlayer(args[1]).getUniqueId();
} }
final double amount2 = MathUtils.isDouble(args[2]) ? Double.valueOf(args[2]) : -1.0;
if (amount2 == -1.0) {
sender.sendMessage("§2§lER§f§lCoupons §e» §fMUST BE NEGATIVE OR POSITIVE NUMBER.");
double amount = MathUtils.isDouble(args[2]) ? Double.valueOf(args[2]) : -1.0;
if (amount == -1.0) {
sender.sendMessage(Main.PREFIX + "MUST BE NEGATIVE OR POSITIVE NUMBER.");
return true; return true;
} }
Data.setCredits(u, Data.getCredits(u) + (long) (amount2 * 100.0));
Data.setCredits(u, Data.getCredits(u) + (long) (amount * 100.0));
return true; return true;
} }
} }

36
ERCoupons/src/com/entryrise/coupons/utils/CSUtils.java

@ -33,24 +33,33 @@ public class CSUtils {
return; return;
} }
if (credits < 1L) { if (credits < 1L) {
p.sendMessage("§2§lER§f§lCoupons §e» §fYou need to specify the amount of credits to redeem your credits.");
p.sendMessage(Main.PREFIX + "You need to specify the amount of credits to redeem your credits.");
return; return;
} }
if (credits % 100L != 0L) { if (credits % 100L != 0L) {
p.sendMessage("§2§lER§f§lCoupons §e» §fYou can only redeem multiples of 100 credits.");
p.sendMessage(Main.PREFIX + "You can only redeem multiples of 100 credits.");
return; return;
} }
int max = Main.config.getInt("settings.redeem.max");
int min = Main.config.getInt("settings.redeem.min");
if (min > credits || max < credits) {
p.sendMessage(
Main.PREFIX + "You can only redeem coupons valuing between " + min + " and " + max + " coupons.");
return;
}
final UUID u = p.getUniqueId(); final UUID u = p.getUniqueId();
final long available = Data.getCredits(u); final long available = Data.getCredits(u);
final long rem = available - credits; final long rem = available - credits;
if (rem < 0L) { if (rem < 0L) {
p.sendMessage("§2§lER§f§lCoupons §e» §fYou need " + -rem
+ " more credits to be able to create this store giftcard");
p.sendMessage(Main.PREFIX + "You need " + -rem + " more credits to be able to create this store giftcard");
return; return;
} }
Data.setCredits(u, rem); Data.setCredits(u, rem);
final long dollars = credits / 100L; final long dollars = credits / 100L;
p.sendMessage("§2§lER§f§lCoupons §e» §fWe're currently contacting the store and creating your coupon...");
p.sendMessage(Main.PREFIX + "We're currently contacting the store and creating your coupon...");
Bukkit.getScheduler().runTaskAsynchronously(Main.p, () -> CSUtils.giveCoupon(p, dollars)); Bukkit.getScheduler().runTaskAsynchronously(Main.p, () -> CSUtils.giveCoupon(p, dollars));
} }
@ -81,16 +90,17 @@ public class CSUtils {
final String stg = (conn.getResponseCode() == 200) ? getInputStreamString(conn.getInputStream()) final String stg = (conn.getResponseCode() == 200) ? getInputStreamString(conn.getInputStream())
: getInputStreamString(conn.getErrorStream()); : getInputStreamString(conn.getErrorStream());
final JsonParser parser = new JsonParser();
final JsonObject o = parser.parse(stg).getAsJsonObject();
final String code = ((JsonObject) o.get("data")).get("code").getAsString();
System.out.println(stg);
p.sendMessage("§2§lER§f§lCoupons §e» §fGIFT-CARD Succesfully printed below.");
p.spigot().sendMessage((BaseComponent) Chat.genHoverAndSuggestTextComponent(
"§2§lER§f§lCoupons §e» §f§c" + code, "Click, CTRL+A, CTRL+C, and paste with CTRL+V", code));
JsonParser parser = new JsonParser();
JsonObject o = parser.parse(stg).getAsJsonObject();
String code = ((JsonObject) o.get("data")).get("code").getAsString();
p.sendMessage(Main.PREFIX + "GIFT-CARD Succesfully printed below.");
p.spigot().sendMessage((BaseComponent) Chat.genHoverAndSuggestTextComponent(Main.PREFIX + "§c" + code,
"Click, CTRL+A, CTRL+C, and paste with CTRL+V", code));
} catch (Exception e) { } catch (Exception e) {
p.sendMessage(
"§2§lER§f§lCoupons §e» §fSomething went wrong with creating your store coupon. Urgently contact staff. "
p.sendMessage(Main.PREFIX + "Something went wrong with creating your store coupon. Urgently contact staff. "
+ transactionid.toString()); + transactionid.toString());
Bukkit.getLogger() Bukkit.getLogger()
.warning("STORE TRANSACTION " + transactionid.toString() + " with " + p.getName() + " WENT WRONG!"); .warning("STORE TRANSACTION " + transactionid.toString() + " with " + p.getName() + " WENT WRONG!");

2
ERCoupons/src/com/entryrise/coupons/utils/Chat.java

@ -10,7 +10,7 @@ public class Chat {
public static TextComponent genHoverAndSuggestTextComponent(String show, String hover, String click) { public static TextComponent genHoverAndSuggestTextComponent(String show, String hover, String click) {
TextComponent msg = new TextComponent(show); TextComponent msg = new TextComponent(show);
msg.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(hover).create())); msg.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(hover).create()));
msg.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/" + click));
msg.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, click));
return msg; return msg;
} }

Loading…
Cancel
Save