Skip to content

Commit

Permalink
figldig
Browse files Browse the repository at this point in the history
  • Loading branch information
Kris-Driv committed Dec 13, 2016
1 parent c09ccf5 commit 5cfcc85
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 14 deletions.
Empty file modified LICENSE
100644 → 100755
Empty file.
Empty file modified README.md
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion src/dominate/Command.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public function addRequirement(Requirement $r) {
public function testRequirements(CommandSender $sender = null) : bool {
$sender = $sender ?? $this->sender;
foreach($this->requirements as $requirement) {
if(!$requirement->hasMet($r, false)) return false;
if(!$requirement->hasMet($sender, false)) return false;
}
return true;
}
Expand Down
41 changes: 29 additions & 12 deletions src/dominate/argument/Argument.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Argument {

const TYPE_STRING = 0x0;
const TYPE_INTEGER = 0x1;
const TYPE_NUMERIC = self::TYPE_INTEGER;
const TYPE_FLOAT = 0x2;
const TYPE_DOUBLE = self::TYPE_FLOAT;
const TYPE_REAL = self::TYPE_FLOAT;
Expand All @@ -36,18 +37,16 @@ class Argument {
/** @var string[] */
public static $ERROR_MESSAGES = [
self::TYPE_STRING => "argument.type-string-error",
self::TYPE_INTEGER => "argument.type-string-error",
self::TYPE_FLOAT => "argument.type-string-error",
self::TYPE_DOUBLE => "argument.type-string-error",
self::TYPE_BOOLEAN => "argument.type-string-error",
self::TYPE_INTEGER => "argument.type-integer-error",
self::TYPE_FLOAT => "argument.type-float-error",
self::TYPE_BOOLEAN => "argument.type-boolean-error",
self::TYPE_NULL => "argument.type-null-error"
];

const PRIMITIVE_TYPES = [
self::TYPE_STRING,
self::TYPE_INTEGER,
self::TYPE_FLOAT,
self::TYPE_DOUBLE,
self::TYPE_BOOLEAN,
self::TYPE_NULL,
];
Expand Down Expand Up @@ -125,18 +124,34 @@ public function getType() {
* Will do checks only on primitive data types
* @return bool
*/
public static function validateInputType(string $input, int $type) : bool {
public static function validateInputType($input, int $type) : bool {
if(!isset(self::PRIMITIVE_TYPES[$type])) return false;
echo "Validating primitive type".PHP_EOL;
switch ($type) {
case self::TYPE_STRING:
return is_string((string) $input);
return is_string($input);
case self::TYPE_BOOLEAN:
return is_bool((bool) $input);
switch(strtolower($input)) {
case '1':
case 'true':
case 'yes':
case 'y':
return true;
case '0':
case 'false':
case 'no':
case 'n':
return true;
default:
return false;
}
return false;
case self::TYPE_DOUBLE:
case self::TYPE_FLOAT:
return is_float((float) $input);
if(strpos($input, ".") === false) return false;
return is_numeric($input);
case self::TYPE_INTEGER:
return is_int((float) $input);
return is_numeric($input);
}
return false;
}
Expand All @@ -158,13 +173,13 @@ public function createErrorMessage(CommandSender $sender, string $value) : Trans
return new Translatable(self::$ERROR_MESSAGES[$this->type], [
"sender" => ($sender instanceof Player ? $sender->getDisplayName() : $sender->getName()),
"value" => $value,
"n" => $this->getIndex()
"n" => $this->getIndex() + 1 // Must make this readable, not everyone can program
]);
} else {
return new Translatable("argument.generic-error", [
"sender" => ($sender instanceof Player ? $sender->getDisplayName() : $sender->getName()),
"value" => $value,
"n" => $this->getIndex()
"n" => $this->getIndex() + 1
]);
}
}
Expand All @@ -186,7 +201,9 @@ public function isPrimitive() : bool {
public function read(string $input, CommandSender $sender = null) {
$silent = $sender ? false : true;
if($this->isPrimitive()) {
echo "Is primitive".PHP_EOL;
if(!self::validateInputType($input, $this->type)) {
echo "Input validation failed!".PHP_EOL;
if(!$silent) {
$sender->sendMessage($this->createErrorMessage($sender, $input));
}
Expand Down
Empty file modified src/dominate/requirement/Requirement.php
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion src/dominate/requirement/SimpleRequirement.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function __construct(int $type) {
}

public function createErrorMessage(CommandSender $sender) : Translatable {
return new Translatable(self::ERROR_MESSAGES[$this->type], [($sender instanceof Player) ? $sender->getDisplayName() : $sender->getName()]);
return new Translatable(self::$ERROR_MESSAGES[$this->type], [($sender instanceof Player) ? $sender->getDisplayName() : $sender->getName()]);
}

public function hasMet(CommandSender $sender, $silent = false) : bool {
Expand Down

0 comments on commit 5cfcc85

Please sign in to comment.