mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-15 02:15:34 -04:00
cssc: Check identifier against regex
This commit is contained in:
parent
7d6588378a
commit
096279efd9
@ -1,6 +0,0 @@
|
||||
version: "2" # required to adjust maintainability checks
|
||||
plugins:
|
||||
sonar-java:
|
||||
enabled: true
|
||||
config:
|
||||
sonar.java.source: "14"
|
@ -49,7 +49,7 @@ object CommandParsers {
|
||||
// swizzle
|
||||
// team
|
||||
// item_slot
|
||||
// resource_location
|
||||
ModIdentifier("resource_location") to IdentifierParser.IDENTIFIER_PARSER,
|
||||
ModIdentifier("mob_effect") to IdentifierListParser.MOB_EFFECT_PARSER,
|
||||
// function
|
||||
// entity_anchor
|
||||
|
@ -14,6 +14,7 @@ package de.bixilon.minosoft.data.commands.parser
|
||||
|
||||
import de.bixilon.minosoft.data.commands.parser.exceptions.CommandParseException
|
||||
import de.bixilon.minosoft.data.commands.parser.exceptions.EnchantmentNotFoundCommandParseException
|
||||
import de.bixilon.minosoft.data.commands.parser.exceptions.InvalidIdentifierCommandParseException
|
||||
import de.bixilon.minosoft.data.commands.parser.exceptions.MobEffectNotFoundCommandParseException
|
||||
import de.bixilon.minosoft.data.commands.parser.properties.ParserProperties
|
||||
import de.bixilon.minosoft.data.mappings.ModIdentifier
|
||||
@ -26,7 +27,13 @@ class IdentifierListParser : CommandParser() {
|
||||
override fun isParsable(connection: Connection, properties: ParserProperties?, stringReader: ImprovedStringReader) {
|
||||
val argument = stringReader.readUntilNextCommandArgument()
|
||||
|
||||
val identifier = ModIdentifier(argument)
|
||||
|
||||
val identifier: ModIdentifier
|
||||
try {
|
||||
identifier = ModIdentifier.getIdentifier(argument)
|
||||
} catch (exception: IllegalArgumentException) {
|
||||
throw InvalidIdentifierCommandParseException(stringReader, argument)
|
||||
}
|
||||
|
||||
if (this == ENCHANTMENT_PARSER) {
|
||||
if (!connection.mapping.doesEnchantmentExist(identifier)) {
|
||||
|
@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Minosoft
|
||||
* Copyright (C) 2020 Moritz Zwerger
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this program.If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
|
||||
*/
|
||||
package de.bixilon.minosoft.data.commands.parser
|
||||
|
||||
import de.bixilon.minosoft.data.commands.parser.exceptions.CommandParseException
|
||||
import de.bixilon.minosoft.data.commands.parser.exceptions.InvalidIdentifierCommandParseException
|
||||
import de.bixilon.minosoft.data.commands.parser.properties.ParserProperties
|
||||
import de.bixilon.minosoft.data.mappings.ModIdentifier
|
||||
import de.bixilon.minosoft.protocol.network.Connection
|
||||
import de.bixilon.minosoft.util.buffers.ImprovedStringReader
|
||||
|
||||
class IdentifierParser : CommandParser() {
|
||||
|
||||
@Throws(CommandParseException::class)
|
||||
override fun isParsable(connection: Connection, properties: ParserProperties?, stringReader: ImprovedStringReader) {
|
||||
val argument = stringReader.readUntilNextCommandArgument()
|
||||
|
||||
try {
|
||||
ModIdentifier.getIdentifier(argument)
|
||||
} catch (exception: IllegalArgumentException) {
|
||||
throw InvalidIdentifierCommandParseException(stringReader, argument)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
val IDENTIFIER_PARSER = IdentifierParser()
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
/*
|
||||
* Minosoft
|
||||
* Copyright (C) 2020 Moritz Zwerger
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with this program.If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
|
||||
*/
|
||||
package de.bixilon.minosoft.data.commands.parser.exceptions
|
||||
|
||||
import de.bixilon.minosoft.util.buffers.ImprovedStringReader
|
||||
|
||||
class InvalidIdentifierCommandParseException : CommandParseException {
|
||||
constructor(command: ImprovedStringReader, currentArgument: String) : super(ERROR_MESSAGE, command, currentArgument)
|
||||
|
||||
constructor(command: ImprovedStringReader, currentArgument: String, cause: Throwable) : super(ERROR_MESSAGE, command, currentArgument, cause)
|
||||
|
||||
companion object {
|
||||
private const val ERROR_MESSAGE = "Identifier is invalid!"
|
||||
}
|
||||
}
|
@ -16,6 +16,7 @@ package de.bixilon.minosoft.data.mappings;
|
||||
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
public class ModIdentifier {
|
||||
protected final String mod;
|
||||
@ -42,6 +43,16 @@ public class ModIdentifier {
|
||||
this.identifier = identifier.getIdentifier();
|
||||
}
|
||||
|
||||
public static ModIdentifier getIdentifier(String identifier) throws IllegalArgumentException {
|
||||
Matcher matcher = ProtocolDefinition.IDENTIFIER_PATTERN.matcher(identifier);
|
||||
if (!matcher.find() || !matcher.group().equals(identifier)) {
|
||||
throw new IllegalArgumentException(String.format("%s in not a valid identifier!", identifier));
|
||||
}
|
||||
|
||||
return new ModIdentifier(identifier);
|
||||
|
||||
}
|
||||
|
||||
public String getMod() {
|
||||
return this.mod;
|
||||
}
|
||||
|
@ -48,6 +48,7 @@ public final class ProtocolDefinition {
|
||||
public static final String COMMAND_SEPARATOR = " ";
|
||||
|
||||
public static final Pattern MINECRAFT_NAME_VALIDATOR = Pattern.compile("\\w{3,16}");
|
||||
public static final Pattern IDENTIFIER_PATTERN = Pattern.compile("([a-z_]+:)?[a-z_]+");
|
||||
|
||||
public static final int SECTION_WIDTH_X = 16;
|
||||
public static final int SECTION_WIDTH_Z = 16;
|
||||
|
Loading…
x
Reference in New Issue
Block a user