package net.knarcraft.paidsigns.command; import net.knarcraft.paidsigns.PaidSigns; import net.knarcraft.paidsigns.container.PaidSign; import net.knarcraft.paidsigns.manager.PaidSignManager; import net.knarcraft.paidsigns.property.OptionState; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; import java.io.IOException; import java.util.Arrays; import java.util.logging.Level; import java.util.logging.Logger; /** * A representation of the command for adding a new paid sign */ public class AddCommand extends TokenizedCommand { @Override public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { super.onCommand(sender, command, label, args); if (argumentSize < 2) { return false; } String signName = arguments.get(0).trim(); double cost; try { cost = Double.parseDouble(arguments.get(1)); } catch (NumberFormatException exception) { sender.sendMessage("You provided an invalid number"); return false; } String permission = ""; if (argumentSize > 2) { permission = arguments.get(2); } OptionState ignoreCase = OptionState.DEFAULT; OptionState ignoreColor = OptionState.DEFAULT; if (argumentSize > 3) { ignoreCase = OptionState.fromString(arguments.get(3)); } if (argumentSize > 4) { ignoreColor = OptionState.fromString(arguments.get(4)); } return createPaidSign(sender, signName, cost, permission, ignoreCase, ignoreColor); } /** * Creates a new paid sign with the given user input * * @param sender

The command sender that called the add command

* @param signName

The name of the new paid sign

* @param cost

The cost of the new paid sign

* @param permission

The permission required for creating the sign represented by the paid sign

* @param ignoreCase

Whether to ignore case for the paid sign's conditions

* @param ignoreColor

Whether to ignore color for the paid sign's conditions

* @return

True if the paid sign was successfully created and registered

*/ private boolean createPaidSign(CommandSender sender, String signName, double cost, String permission, OptionState ignoreCase, OptionState ignoreColor) { PaidSignManager manager = PaidSigns.getInstance().getSignManager(); try { PaidSign sign = new PaidSign(signName, cost, permission, ignoreCase, ignoreColor); if (manager.getPaidSign(signName) != null) { sender.sendMessage("A paid sign with the same name already exists"); return false; } manager.addPaidSign(sign); sender.sendMessage("Successfully added new paid sign"); return true; } catch (IOException e) { Logger logger = PaidSigns.getInstance().getLogger(); logger.log(Level.SEVERE, "Exception encountered while trying to write " + "to the data file"); logger.log(Level.SEVERE, Arrays.toString(e.getStackTrace())); sender.sendMessage("An exception occurred. Please notify the server administrator or check the server log."); return false; } catch (IllegalArgumentException e) { sender.sendMessage("Invalid input: " + e.getMessage()); } return false; } }