92 lines
		
	
	
		
			4.0 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
			
		
		
	
	
			92 lines
		
	
	
		
			4.0 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
| package net.knarcraft.paidsigns.command;
 | |
| 
 | |
| import net.knarcraft.paidsigns.PaidSigns;
 | |
| import net.knarcraft.paidsigns.container.PaidSign;
 | |
| import net.knarcraft.paidsigns.formatting.StringFormatter;
 | |
| import net.knarcraft.paidsigns.formatting.TranslatableMessage;
 | |
| 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;
 | |
| 
 | |
| /**
 | |
|  * A representation of the command for adding a new match condition for a sign
 | |
|  */
 | |
| public class AddConditionCommand 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 < 3) {
 | |
|             return false;
 | |
|         }
 | |
| 
 | |
|         String name = arguments.get(0);
 | |
|         short lineNumber;
 | |
|         try {
 | |
|             lineNumber = (short) (Short.parseShort(arguments.get(1)) - 1);
 | |
|             if (lineNumber < 0 || lineNumber > 3) {
 | |
|                 sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.ERROR_INVALID_NUMBER));
 | |
|                 return false;
 | |
|             }
 | |
|         } catch (NumberFormatException exception) {
 | |
|             sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.ERROR_INVALID_NUMBER));
 | |
|             return false;
 | |
|         }
 | |
|         String stringToMatch = arguments.get(2);
 | |
|         boolean executeRegEx = false;
 | |
|         if (argumentSize > 3) {
 | |
|             executeRegEx = Boolean.parseBoolean(arguments.get(3));
 | |
|             if (executeRegEx && isRegExInvalid(sender, stringToMatch)) {
 | |
|                 return false;
 | |
|             }
 | |
|         }
 | |
|         OptionState ignoreCase = OptionState.DEFAULT;
 | |
|         if (argumentSize > 4) {
 | |
|             ignoreCase = OptionState.getFromBoolean(Boolean.parseBoolean(arguments.get(4)));
 | |
|         }
 | |
|         OptionState ignoreColor = OptionState.DEFAULT;
 | |
|         if (argumentSize > 5) {
 | |
|             ignoreColor = OptionState.getFromBoolean(Boolean.parseBoolean(arguments.get(5)));
 | |
|         }
 | |
|         return addCondition(name, lineNumber, stringToMatch, executeRegEx, ignoreCase, ignoreColor, sender);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * Uses the given input to add a paid sign condition
 | |
|      *
 | |
|      * @param name          <p>The name of the paid sign to add the condition to</p>
 | |
|      * @param lineNumber    <p>The sign line the condition should check</p>
 | |
|      * @param stringToMatch <p>The string to look for on created signs</p>
 | |
|      * @param executeRegEx  <p>Whether to treat the match string as a regular expression</p>
 | |
|      * @param ignoreCase    <p>Whether to ignore case when matching</p>
 | |
|      * @param ignoreColor   <p>Whether to ignore color when matching</p>
 | |
|      * @param sender        <p>The command sender to notify when finished</p>
 | |
|      * @return <p>True if the condition was successfully added</p>
 | |
|      */
 | |
|     private boolean addCondition(String name, short lineNumber, String stringToMatch, boolean executeRegEx,
 | |
|                                  OptionState ignoreCase, OptionState ignoreColor, CommandSender sender) {
 | |
|         PaidSignManager signManager = PaidSigns.getInstance().getSignManager();
 | |
|         PaidSign sign = signManager.getPaidSign(name);
 | |
|         if (sign == null) {
 | |
|             sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.ERROR_PAID_SIGN_NOT_FOUND));
 | |
|             return false;
 | |
|         }
 | |
|         sign.addCondition(lineNumber, stringToMatch, executeRegEx, ignoreCase, ignoreColor);
 | |
|         try {
 | |
|             signManager.saveSigns();
 | |
|         } catch (IOException e) {
 | |
|             sender.sendMessage(StringFormatter.getTranslatedErrorMessage(TranslatableMessage.ERROR_EXCEPTION_OCCURRED));
 | |
|             return false;
 | |
|         }
 | |
|         sender.sendMessage(StringFormatter.getTranslatedInfoMessage(
 | |
|                 TranslatableMessage.SUCCESS_ADDED_PAID_SIGN_CONDITION));
 | |
|         return true;
 | |
|     }
 | |
| 
 | |
| }
 |