001/* 002 * PlotSquared, a land and world management plugin for Minecraft. 003 * Copyright (C) IntellectualSites <https://intellectualsites.com> 004 * Copyright (C) IntellectualSites team and contributors 005 * 006 * This program is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * This program is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with this program. If not, see <https://www.gnu.org/licenses/>. 018 */ 019package com.plotsquared.core.command; 020 021import com.plotsquared.core.plot.PlotId; 022 023public abstract class Argument<T> { 024 025 public static final Argument<Integer> Integer = new Argument<>("int", 16) { 026 @Override 027 public Integer parse(String in) { 028 Integer value = null; 029 try { 030 value = java.lang.Integer.parseInt(in); 031 } catch (Exception ignored) { 032 } 033 return value; 034 } 035 }; 036 public static final Argument<Boolean> Boolean = new Argument<>("boolean", true) { 037 @Override 038 public Boolean parse(String in) { 039 Boolean value = null; 040 if (in.equalsIgnoreCase("true") || in.equalsIgnoreCase("Yes") || in 041 .equalsIgnoreCase("1")) { 042 value = true; 043 } else if (in.equalsIgnoreCase("false") || in.equalsIgnoreCase("No") || in 044 .equalsIgnoreCase("0")) { 045 value = false; 046 } 047 return value; 048 } 049 }; 050 public static final Argument<String> String = new Argument<>("String", "Example") { 051 @Override 052 public String parse(String in) { 053 return in; 054 } 055 }; 056 public static final Argument<String> PlayerName = 057 new Argument<>("PlayerName", "<player | *>") { 058 @Override 059 public String parse(String in) { 060 return in.length() <= 16 ? in : null; 061 } 062 }; 063 public static final Argument<PlotId> PlotID = 064 new Argument<>("PlotID", PlotId.of(-6, 3)) { 065 @Override 066 public PlotId parse(String in) { 067 return PlotId.fromString(in); 068 } 069 }; 070 private final String name; 071 private final T example; 072 073 public Argument(String name, T example) { 074 this.name = name; 075 this.example = example; 076 } 077 078 public abstract T parse(String in); 079 080 @Override 081 public final String toString() { 082 return this.getName(); 083 } 084 085 public final String getName() { 086 return this.name; 087 } 088 089 public final T getExample() { 090 return this.example; 091 } 092 093}