3
0
Fork 0

ADD: voucher command

This commit is contained in:
BuildTools 2022-11-06 11:35:34 +01:00
parent 559ae95fed
commit 9e9ce74c55
8 changed files with 70 additions and 24 deletions

View file

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>nl.iobyte</groupId> <groupId>nl.iobyte</groupId>
<artifactId>themeparkconnector</artifactId> <artifactId>themeparkconnector</artifactId>
<version>3.1.0</version> <version>3.1.1</version>
<build> <build>
<defaultGoal>clean package</defaultGoal> <defaultGoal>clean package</defaultGoal>
<resources> <resources>

View file

@ -12,7 +12,7 @@
<groupId>nl.iobyte</groupId> <groupId>nl.iobyte</groupId>
<artifactId>themeparkconnector</artifactId> <artifactId>themeparkconnector</artifactId>
<version>3.1.0</version> <version>3.1.1</version>
<repositories> <repositories>
<repository> <repository>

View file

@ -25,6 +25,10 @@ public class ShowService {
if(uuid == null || voucher == null) if(uuid == null || voucher == null)
return "Invalid parameters"; return "Invalid parameters";
Player player = Bukkit.getPlayer(uuid);
if(player == null)
return "Invalid player";
try { try {
ArrayList<Map<String, Object>> resultSet = ThemePark.getInstance().getAPI().getDatabaseService().executeQuery( ArrayList<Map<String, Object>> resultSet = ThemePark.getInstance().getAPI().getDatabaseService().executeQuery(
"remote", "remote",
@ -93,7 +97,6 @@ public class ShowService {
NBTCompound compound = item.addCompound("data"); NBTCompound compound = item.addCompound("data");
compound.setString("ticket", g.toJson(ticket, Ticket.class)); compound.setString("ticket", g.toJson(ticket, Ticket.class));
Player player = Bukkit.getPlayer(uuid);
player.getInventory().addItem(item.getItem()); player.getInventory().addItem(item.getItem());
return "true"; return "true";
} catch(Exception e) { } catch(Exception e) {

View file

@ -10,17 +10,20 @@ import nl.iobyte.themeparkconnector.commands.subcommands.*;
import nl.iobyte.themeparkconnector.commands.subcommands.operator.OperatorCommands; import nl.iobyte.themeparkconnector.commands.subcommands.operator.OperatorCommands;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.Objects;
public class ThemeParkConnectorCommand { public class ThemeParkConnectorCommand {
//Load command data //Load command data
public ThemeParkConnectorCommand() { public ThemeParkConnectorCommand() {
CommandFactory factory = new CommandFactory("themeparkconnector"); CommandFactory factory = new CommandFactory("themeparkconnector");
factory.addSubCommand(new HelpCommand(factory)) factory.addSubCommand(new HelpCommand(factory))
.addSubCommand(new ConnectCommand()); .addSubCommand(new ConnectCommand())
.addSubCommand(new VoucherCommand());
//Admin //Admin
factory.addSubCommand(new StatusCommand()) factory.addSubCommand(new StatusCommand())
.addSubCommand(new NotificationCommand()) .addSubCommand(new VoucherCommand())
.addSubCommand(new DisconnectCommand()); .addSubCommand(new DisconnectCommand());
//Operator //Operator
@ -30,7 +33,7 @@ public class ThemeParkConnectorCommand {
factory.addMiddleware(new PermissionMiddleware()); factory.addMiddleware(new PermissionMiddleware());
//Register command //Register command
ThemeParkConnector.getInstance().getCommand(factory.getName()).setExecutor((sender, command, s, args) -> { Objects.requireNonNull(ThemeParkConnector.getInstance().getCommand(factory.getName())).setExecutor((sender, command, s, args) -> {
ICommandExecutor executor = new ConsoleExecutor(sender); ICommandExecutor executor = new ConsoleExecutor(sender);
if(sender instanceof Player) if(sender instanceof Player)
executor = new PlayerExecutor((Player) sender); executor = new PlayerExecutor((Player) sender);

View file

@ -0,0 +1,39 @@
package nl.iobyte.themeparkconnector.commands.subcommands;
import nl.iobyte.commandapi.arguments.StringArgument;
import nl.iobyte.commandapi.interfaces.ICommandExecutor;
import nl.iobyte.commandapi.objects.SubCommand;
import nl.iobyte.themeparkconnector.ThemeParkConnector;
import nl.iobyte.themeparkconnector.api.message.Text;
import org.bukkit.entity.Player;
import java.util.List;
public class VoucherCommand extends SubCommand {
public VoucherCommand() {
super(new String[]{"redeem"});
addSyntax("/themeparkconnector redeem <voucher>")
.addArgument(new StringArgument())
.setAllowConsole(false);
}
//Get Token and redeem ticket
public void onCommand(ICommandExecutor sender, List<Object> objects, int i) {
String voucher = (String) objects.get(0);
String result = ThemeParkConnector.getInstance().getAPI().getShowService().redeemTicket(
((Player) sender.getOriginal()).getUniqueId(),
voucher
);
if(result.equals("true")) {
sender.sendMessage(Text.color("&6Redeemed ticket with voucher: &L&f"+voucher));
return;
}
ThemeParkConnector.getInstance().getLogger().warning(result);
sender.sendMessage(Text.color("&4Invalid voucher: &L&f"+voucher));
}
}

View file

@ -25,6 +25,7 @@ import org.bukkit.inventory.ItemStack;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.Objects;
public class TicketListener implements Listener { public class TicketListener implements Listener {
@ -50,7 +51,7 @@ public class TicketListener implements Listener {
} }
String date = e.getLine(2); String date = e.getLine(2);
if(isValidFormat("HH:mm", date) && isValidFormat("HH:mm dd-MM-yyyy", date)) { if(isInvalidFormat("HH:mm", date) && isInvalidFormat("HH:mm dd-MM-yyyy", date)) {
player.sendMessage(Text.color("&4Can't make a TicketScanner with incorrect date, example: 20:00 or 20:00 04-04-2019")); player.sendMessage(Text.color("&4Can't make a TicketScanner with incorrect date, example: 20:00 or 20:00 04-04-2019"));
return; return;
} }
@ -88,24 +89,24 @@ public class TicketListener implements Listener {
} }
String id = ChatColor.stripColor(sign.getLine(1)); String id = ChatColor.stripColor(sign.getLine(1));
if(id == null || id.isEmpty()) if(id.isEmpty())
return; return;
String date = ChatColor.stripColor(sign.getLine(2)); String date = ChatColor.stripColor(sign.getLine(2));
if(date == null || date.isEmpty()) if(date.isEmpty())
return; return;
if(isValidFormat("HH:mm", date) && isValidFormat("HH:mm dd-MM-yyyy", date)) { if(isInvalidFormat("HH:mm", date) && isInvalidFormat("HH:mm dd-MM-yyyy", date)) {
System.out.println("Invalid date format"); ThemeParkConnector.getInstance().getLogger().warning("Invalid date format");
return; return;
} }
if(isValidFormat("HH:mm dd-MM-yyyy", date)) if(isInvalidFormat("HH:mm dd-MM-yyyy", date))
date = date + " " + format.format(new Date()); date = date + " " + format.format(new Date());
Location location = LocationUtil.fromString(sign.getLocation().getWorld().getName() + ":" + sign.getLine(3)); Location location = LocationUtil.fromString(Objects.requireNonNull(sign.getLocation().getWorld()).getName() + ":" + sign.getLine(3));
if(location == null) { if(location == null) {
System.out.println("Invalid Location"); ThemeParkConnector.getInstance().getLogger().warning("Invalid Location");
return; return;
} }
@ -118,30 +119,30 @@ public class TicketListener implements Listener {
NBTItem it = new NBTItem(item); NBTItem it = new NBTItem(item);
if(!it.hasKey("data")) { if(!it.hasKey("data")) {
System.out.println("Missing data key"); ThemeParkConnector.getInstance().getLogger().warning("Missing data key");
return; return;
} }
NBTCompound compound = it.getCompound("data"); NBTCompound compound = it.getCompound("data");
if(!compound.hasKey("ticket")) { if(!compound.hasKey("ticket")) {
System.out.println("Missing ticket key"); ThemeParkConnector.getInstance().getLogger().warning("Missing ticket key");
return; return;
} }
Gson g = new Gson(); Gson g = new Gson();
Ticket ticket = g.fromJson(it.getCompound("data").getString("ticket"), Ticket.class); Ticket ticket = g.fromJson(it.getCompound("data").getString("ticket"), Ticket.class);
if(ticket == null) { if(ticket == null) {
System.out.println("Couldn't get ticket class using Gson"); ThemeParkConnector.getInstance().getLogger().warning("Couldn't get ticket class using Gson");
return; return;
} }
if(!id.equals(ticket.getShowID())) { if(!id.equals(ticket.getShowID())) {
System.out.println("Different show id"); ThemeParkConnector.getInstance().getLogger().warning("Different show id");
return; return;
} }
if(!date.equals(new SimpleDateFormat("HH:mm dd-MM-yyyy").format(ticket.getShowDate()))) { if(!date.equals(new SimpleDateFormat("HH:mm dd-MM-yyyy").format(ticket.getShowDate()))) {
System.out.println("Different date"); ThemeParkConnector.getInstance().getLogger().warning("Different date");
return; return;
} }
@ -152,7 +153,7 @@ public class TicketListener implements Listener {
Bukkit.getPluginManager().callEvent(event); Bukkit.getPluginManager().callEvent(event);
int i = player.getInventory().getHeldItemSlot(); int i = player.getInventory().getHeldItemSlot();
player.getInventory().remove(player.getInventory().getItem(i)); player.getInventory().remove(Objects.requireNonNull(player.getInventory().getItem(i)));
if(!b) { if(!b) {
player.sendMessage(Text.color("&4Invalid ticket.")); player.sendMessage(Text.color("&4Invalid ticket."));
return; return;
@ -161,10 +162,10 @@ public class TicketListener implements Listener {
if(!event.isCancelled()) if(!event.isCancelled())
player.teleport(location); player.teleport(location);
player.sendMessage(Text.color("&6Used ticket for Show: " + item.getItemMeta().getDisplayName())); player.sendMessage(Text.color("&6Used ticket for Show: " + Objects.requireNonNull(item.getItemMeta()).getDisplayName()));
} }
private static boolean isValidFormat(String format, String value) { private static boolean isInvalidFormat(String format, String value) {
try { try {
SimpleDateFormat sdf = new SimpleDateFormat(format); SimpleDateFormat sdf = new SimpleDateFormat(format);
Date date = sdf.parse(value); Date date = sdf.parse(value);

View file

@ -1,4 +1,4 @@
version: 1.0 version: 1.1
prefix: '&6&lThemeParkConnectorMC &r&f➢' prefix: '&6&lThemeParkConnectorMC &r&f➢'

View file

@ -1,5 +1,5 @@
name: ThemeParkConnector name: ThemeParkConnector
version: 3.0.2 version: ${project.version}
author: IOByte author: IOByte
website: 'https://www.iobyte.nl' website: 'https://www.iobyte.nl'
main: nl.iobyte.themeparkconnector.ThemeParkConnector main: nl.iobyte.themeparkconnector.ThemeParkConnector