improvements, wip (1) PacketDeclareCommands

This commit is contained in:
Bixilon 2020-12-09 16:08:18 +01:00
parent bea841936b
commit abcbd3fa28
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
33 changed files with 897 additions and 23 deletions

View File

@ -0,0 +1,65 @@
/*
* 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;
import de.bixilon.minosoft.data.commands.parser.CommandParser;
import de.bixilon.minosoft.data.commands.parser.properties.ParserProperties;
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
import de.bixilon.minosoft.util.BitByte;
import javax.annotation.Nullable;
public class CommandArgumentNode extends CommandLiteralNode {
private final CommandParser parser;
private final ParserProperties properties;
private final SuggestionTypes suggestionType;
public CommandArgumentNode(byte flags, InByteBuffer buffer) {
super(flags, buffer);
parser = CommandParser.createInstance(buffer.readIdentifier());
properties = parser.readParserProperties(buffer);
if (BitByte.isBitMask(flags, 0x10)) {
String fullIdentifier = buffer.readIdentifier().getFullIdentifier();
suggestionType = switch (fullIdentifier) {
case "minecraft:ask_server" -> CommandArgumentNode.SuggestionTypes.ASK_SERVER;
case "minecraft:all_recipes " -> CommandArgumentNode.SuggestionTypes.ALL_RECIPES;
case "minecraft:available_sounds " -> CommandArgumentNode.SuggestionTypes.AVAILABLE_SOUNDS;
case "minecraft:summonable_entities " -> CommandArgumentNode.SuggestionTypes.SUMMONABLE_ENTITIES;
default -> throw new IllegalStateException("Unexpected value: " + fullIdentifier);
};
} else {
suggestionType = null;
}
}
public CommandParser getParser() {
return parser;
}
@Nullable
public ParserProperties getProperties() {
return properties;
}
public SuggestionTypes getSuggestionType() {
return suggestionType;
}
public enum SuggestionTypes {
ASK_SERVER,
ALL_RECIPES,
AVAILABLE_SOUNDS,
SUMMONABLE_ENTITIES
}
}

View File

@ -0,0 +1,29 @@
/*
* 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;
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
public class CommandLiteralNode extends CommandNode {
private final String name;
public CommandLiteralNode(byte flags, InByteBuffer buffer) {
super(flags, buffer);
name = buffer.readString();
}
public String getName() {
return name;
}
}

View File

@ -0,0 +1,74 @@
/*
* 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;
import com.google.errorprone.annotations.DoNotCall;
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
import de.bixilon.minosoft.util.BitByte;
import java.util.HashSet;
public abstract class CommandNode {
protected final boolean isExecutable;
protected final HashSet<CommandNode> children = new HashSet<>();
protected final int[] childrenIds;
protected final int redirectNodeId;
protected CommandNode redirectNode;
public CommandNode(byte flags, InByteBuffer buffer) {
this.isExecutable = BitByte.isBitMask(flags, 0x04);
childrenIds = buffer.readVarIntArray();
if (BitByte.isBitMask(flags, 0x08)) {
redirectNodeId = buffer.readVarInt();
} else {
redirectNodeId = -1;
}
}
public boolean isExecutable() {
return isExecutable;
}
public HashSet<CommandNode> getChildren() {
return children;
}
public CommandNode getRedirectNode() {
return redirectNode;
}
@DoNotCall
public void setRedirectNode(CommandNode redirectNode) {
if (this.redirectNode != null) {
throw new IllegalArgumentException("Object already initialized!");
}
this.redirectNode = redirectNode;
}
@DoNotCall
public int getRedirectNodeId() {
return redirectNodeId;
}
@DoNotCall
public int[] getChildrenIds() {
return childrenIds;
}
public enum NodeTypes {
ROOT,
LITERAL,
ARGUMENT
}
}

View File

@ -0,0 +1,22 @@
/*
* 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;
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
public class CommandRootNode extends CommandNode {
public CommandRootNode(byte flags, InByteBuffer buffer) {
super(flags, buffer);
}
}

View File

@ -0,0 +1,17 @@
/*
* 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;
public class BooleanParser extends CommandParser {
}

View File

@ -0,0 +1,48 @@
/*
* 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 com.google.common.collect.HashBiMap;
import de.bixilon.minosoft.data.commands.parser.properties.ParserProperties;
import de.bixilon.minosoft.data.mappings.ModIdentifier;
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
import javax.annotation.Nullable;
public abstract class CommandParser {
@Deprecated
private static final DummyParser dummyParser = new DummyParser();
public static HashBiMap<ModIdentifier, CommandParser> COMMAND_PARSERS = HashBiMap.create();
static {
COMMAND_PARSERS.put(new ModIdentifier("brigadier:bool"), new BooleanParser());
COMMAND_PARSERS.put(new ModIdentifier("brigadier:double"), new DoubleParser());
COMMAND_PARSERS.put(new ModIdentifier("brigadier:float"), new FloatParser());
COMMAND_PARSERS.put(new ModIdentifier("brigadier:integer"), new IntegerParser());
COMMAND_PARSERS.put(new ModIdentifier("brigadier:string"), new StringParser());
COMMAND_PARSERS.put(new ModIdentifier("entity"), new EntityParser());
COMMAND_PARSERS.put(new ModIdentifier("score_holder"), new ScoreHolderParser());
COMMAND_PARSERS.put(new ModIdentifier("range"), new RangeParser());
}
public static CommandParser createInstance(ModIdentifier identifier) {
return COMMAND_PARSERS.getOrDefault(identifier, dummyParser);
}
@Nullable
public ParserProperties readParserProperties(InByteBuffer buffer) {
return null;
}
}

View File

@ -0,0 +1,32 @@
/*
* 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.properties.DoubleParserProperties;
import de.bixilon.minosoft.data.commands.parser.properties.ParserProperties;
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
import javax.annotation.Nullable;
public class DoubleParser extends CommandParser {
public boolean isValidValue(DoubleParserProperties properties, double value) {
return value >= properties.getMinValue() && value <= properties.getMaxValue();
}
@Override
@Nullable
public ParserProperties readParserProperties(InByteBuffer buffer) {
return new DoubleParserProperties(buffer);
}
}

View File

@ -0,0 +1,18 @@
/*
* 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;
@Deprecated
public class DummyParser extends CommandParser {
}

View File

@ -0,0 +1,28 @@
/*
* 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.properties.EntityParserProperties;
import de.bixilon.minosoft.data.commands.parser.properties.ParserProperties;
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
import javax.annotation.Nullable;
public class EntityParser extends CommandParser {
@Override
@Nullable
public ParserProperties readParserProperties(InByteBuffer buffer) {
return new EntityParserProperties(buffer);
}
}

View File

@ -0,0 +1,32 @@
/*
* 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.properties.FloatParserProperties;
import de.bixilon.minosoft.data.commands.parser.properties.ParserProperties;
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
import javax.annotation.Nullable;
public class FloatParser extends CommandParser {
public boolean isValidValue(FloatParserProperties properties, float value) {
return value >= properties.getMinValue() && value <= properties.getMaxValue();
}
@Override
@Nullable
public ParserProperties readParserProperties(InByteBuffer buffer) {
return new FloatParserProperties(buffer);
}
}

View File

@ -0,0 +1,32 @@
/*
* 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.properties.IntegerParserProperties;
import de.bixilon.minosoft.data.commands.parser.properties.ParserProperties;
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
import javax.annotation.Nullable;
public class IntegerParser extends CommandParser {
public boolean isValidValue(IntegerParserProperties properties, int value) {
return value >= properties.getMinValue() && value <= properties.getMaxValue();
}
@Override
@Nullable
public ParserProperties readParserProperties(InByteBuffer buffer) {
return new IntegerParserProperties(buffer);
}
}

View File

@ -0,0 +1,28 @@
/*
* 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.properties.ParserProperties;
import de.bixilon.minosoft.data.commands.parser.properties.RangeParserProperties;
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
import javax.annotation.Nullable;
public class RangeParser extends CommandParser {
@Override
@Nullable
public ParserProperties readParserProperties(InByteBuffer buffer) {
return new RangeParserProperties(buffer);
}
}

View File

@ -0,0 +1,28 @@
/*
* 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.properties.ParserProperties;
import de.bixilon.minosoft.data.commands.parser.properties.ScoreHolderParserProperties;
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
import javax.annotation.Nullable;
public class ScoreHolderParser extends CommandParser {
@Override
@Nullable
public ParserProperties readParserProperties(InByteBuffer buffer) {
return new ScoreHolderParserProperties(buffer);
}
}

View File

@ -0,0 +1,28 @@
/*
* 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.properties.ParserProperties;
import de.bixilon.minosoft.data.commands.parser.properties.StringParserProperties;
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
import javax.annotation.Nullable;
public class StringParser extends CommandParser {
@Override
@Nullable
public ParserProperties readParserProperties(InByteBuffer buffer) {
return new StringParserProperties(buffer);
}
}

View File

@ -0,0 +1,44 @@
/*
* 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.properties;
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
import de.bixilon.minosoft.util.BitByte;
public class DoubleParserProperties implements ParserProperties {
private final double minValue;
private final double maxValue;
public DoubleParserProperties(InByteBuffer buffer) {
byte flags = buffer.readByte();
if (BitByte.isBitMask(flags, 0x01)) {
minValue = buffer.readDouble();
} else {
minValue = Double.MIN_VALUE;
}
if (BitByte.isBitMask(flags, 0x02)) {
maxValue = buffer.readDouble();
} else {
maxValue = Double.MAX_VALUE;
}
}
public double getMinValue() {
return minValue;
}
public double getMaxValue() {
return maxValue;
}
}

View File

@ -0,0 +1,36 @@
/*
* 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.properties;
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
import de.bixilon.minosoft.util.BitByte;
public class EntityParserProperties implements ParserProperties {
private final boolean onlySingleEntity;
private final boolean onlyPlayers;
public EntityParserProperties(InByteBuffer buffer) {
byte flags = buffer.readByte();
onlySingleEntity = BitByte.isBitMask(flags, 0x01);
onlyPlayers = BitByte.isBitMask(flags, 0x02);
}
public boolean isOnlySingleEntity() {
return onlySingleEntity;
}
public boolean isOnlyPlayers() {
return onlyPlayers;
}
}

View File

@ -0,0 +1,44 @@
/*
* 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.properties;
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
import de.bixilon.minosoft.util.BitByte;
public class FloatParserProperties implements ParserProperties {
private final float minValue;
private final float maxValue;
public FloatParserProperties(InByteBuffer buffer) {
byte flags = buffer.readByte();
if (BitByte.isBitMask(flags, 0x01)) {
minValue = buffer.readFloat();
} else {
minValue = Float.MIN_VALUE;
}
if (BitByte.isBitMask(flags, 0x02)) {
maxValue = buffer.readFloat();
} else {
maxValue = Float.MAX_VALUE;
}
}
public float getMinValue() {
return minValue;
}
public float getMaxValue() {
return maxValue;
}
}

View File

@ -0,0 +1,45 @@
/*
* 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.properties;
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
import de.bixilon.minosoft.util.BitByte;
public class IntegerParserProperties implements ParserProperties {
private final int minValue;
private final int maxValue;
public IntegerParserProperties(InByteBuffer buffer) {
byte flags = buffer.readByte();
if (BitByte.isBitMask(flags, 0x01)) {
minValue = buffer.readInt();
} else {
minValue = Integer.MIN_VALUE;
}
if (BitByte.isBitMask(flags, 0x02)) {
maxValue = buffer.readInt();
} else {
maxValue = Integer.MAX_VALUE;
}
}
public int getMinValue() {
return minValue;
}
public int getMaxValue() {
return maxValue;
}
}

View File

@ -0,0 +1,17 @@
/*
* 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.properties;
public interface ParserProperties {
}

View File

@ -0,0 +1,29 @@
/*
* 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.properties;
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
import de.bixilon.minosoft.util.BitByte;
public class RangeParserProperties implements ParserProperties {
private final boolean allowDecimals;
public RangeParserProperties(InByteBuffer buffer) {
allowDecimals = BitByte.isBitMask(buffer.readByte(), 0x01);
}
public boolean isAllowDecimals() {
return allowDecimals;
}
}

View File

@ -0,0 +1,29 @@
/*
* 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.properties;
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
import de.bixilon.minosoft.util.BitByte;
public class ScoreHolderParserProperties implements ParserProperties {
private final boolean allowMultiple;
public ScoreHolderParserProperties(InByteBuffer buffer) {
allowMultiple = BitByte.isBitMask(buffer.readByte(), 0x01);
}
public boolean isAllowMultiple() {
return allowMultiple;
}
}

View File

@ -0,0 +1,34 @@
/*
* 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.properties;
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
public class StringParserProperties implements ParserProperties {
private final StringSettings setting;
public StringParserProperties(InByteBuffer buffer) {
setting = StringSettings.values()[buffer.readVarInt()];
}
public StringSettings getSetting() {
return setting;
}
public enum StringSettings {
SINGLE_WORD,
QUOTABLE_PHRASE,
GREEDY_PHRASE
}
}

View File

@ -43,9 +43,13 @@ public class ModIdentifier {
return identifier;
}
public String getFullIdentifier() {
return String.format("%s:%s", mod, identifier);
}
@Override
public String toString() {
return String.format("%s:%s", mod, identifier);
return getFullIdentifier();
}
@Override

View File

@ -15,6 +15,7 @@ package de.bixilon.minosoft.data.mappings.recipes;
import com.google.common.collect.HashBiMap;
import de.bixilon.minosoft.data.inventory.Slot;
import de.bixilon.minosoft.data.mappings.ModIdentifier;
import java.util.ArrayList;
import java.util.Arrays;
@ -23,13 +24,13 @@ import java.util.HashSet;
public class Recipes {
final static ArrayList<Recipe> recipeList = new ArrayList<>();
final static HashBiMap<Integer, Recipe> recipeIdMap = HashBiMap.create(); // ids for version <= VERSION_1_12_2
final static HashBiMap<String, Recipe> recipeNameMap = HashBiMap.create();
final static HashBiMap<ModIdentifier, Recipe> recipeNameMap = HashBiMap.create();
public static Recipe getRecipeById(int id) {
return recipeIdMap.get(id);
}
public static Recipe getRecipe(String identifier) {
public static Recipe getRecipe(ModIdentifier identifier) {
return recipeNameMap.get(identifier);
}
@ -56,7 +57,7 @@ public class Recipes {
recipeNameMap.clear();
}
public static void registerCustomRecipes(HashBiMap<String, Recipe> recipes) {
public static void registerCustomRecipes(HashBiMap<ModIdentifier, Recipe> recipes) {
recipeNameMap.putAll(recipes);
}
}

View File

@ -16,6 +16,7 @@ package de.bixilon.minosoft.data.text;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import de.bixilon.minosoft.data.mappings.ModIdentifier;
import de.bixilon.minosoft.util.Util;
@ -27,10 +28,17 @@ public class HoverEvent {
public HoverEvent(JsonObject json) {
this.action = HoverEventActions.valueOf(json.get("action").getAsString().toUpperCase());
JsonElement data = json.get("value");
JsonElement data = null;
if (json.has("value")) {
data = json.get("value");
}
if (json.has("contents")) {
data = json.get("contents");
}
json.get("value");
value = switch (action) { // ToDo
case SHOW_TEXT -> ChatComponent.valueOf(data);
case SHOW_ENTITY -> EntityHoverData.deserialize(JsonParser.parseString(json.get("value").getAsString()).getAsJsonObject());
case SHOW_ENTITY -> EntityHoverData.deserialize(data);
default -> null;
};
}
@ -56,8 +64,14 @@ public class HoverEvent {
public static record EntityHoverData(UUID uuid, ModIdentifier identifier, ChatComponent name) {
public static EntityHoverData deserialize(JsonObject json) {
return new EntityHoverData(Util.getUUIDFromString(json.get("id").getAsString()), new ModIdentifier(json.get("type").getAsString()), ChatComponent.valueOf(json.get("name").getAsString()));
public static EntityHoverData deserialize(JsonElement data) {
JsonObject json;
if (data instanceof JsonPrimitive) {
json = JsonParser.parseString(data.getAsString()).getAsJsonObject();
} else {
json = (JsonObject) data;
}
return new EntityHoverData(Util.getUUIDFromString(json.get("id").getAsString()), new ModIdentifier(json.get("type").getAsString()), ChatComponent.valueOf(json.get("name")));
}
}
}

View File

@ -227,7 +227,7 @@ public class SocketNetwork implements Network {
e.printStackTrace();
}
} catch (Exception e) {
Log.printException(e, LogLevels.DEBUG);
Log.printException(e, LogLevels.VERBOSE);
Log.protocol(String.format("An error occurred while parsing a packet (%s): %s", packet, e));
}
}

View File

@ -0,0 +1,50 @@
/*
* 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.protocol.packets.clientbound.play;
import de.bixilon.minosoft.data.commands.CommandNode;
import de.bixilon.minosoft.logging.Log;
import de.bixilon.minosoft.protocol.packets.ClientboundPacket;
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
import de.bixilon.minosoft.protocol.protocol.PacketHandler;
public class PacketDeclareCommands implements ClientboundPacket {
private CommandNode[] nodes;
private CommandNode rootElement;
@Override
public boolean read(InByteBuffer buffer) throws Exception {
nodes = buffer.readCommandNodesArray();
rootElement = nodes[buffer.readVarInt()];
return true;
}
public CommandNode[] getNodes() {
return nodes;
}
public CommandNode getRootElement() {
return rootElement;
}
@Override
public void handle(PacketHandler h) {
h.handle(this);
}
@Override
public void log() {
Log.protocol("Received declare commands packets (nodes=%d)", nodes.length);
}
}

View File

@ -15,6 +15,7 @@ package de.bixilon.minosoft.protocol.packets.clientbound.play;
import com.google.common.collect.HashBiMap;
import de.bixilon.minosoft.data.inventory.Slot;
import de.bixilon.minosoft.data.mappings.ModIdentifier;
import de.bixilon.minosoft.data.mappings.recipes.Ingredient;
import de.bixilon.minosoft.data.mappings.recipes.Recipe;
import de.bixilon.minosoft.data.mappings.recipes.RecipeTypes;
@ -24,20 +25,20 @@ import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
import de.bixilon.minosoft.protocol.protocol.PacketHandler;
public class PacketDeclareRecipes implements ClientboundPacket {
final HashBiMap<String, Recipe> recipes = HashBiMap.create();
private final HashBiMap<ModIdentifier, Recipe> recipes = HashBiMap.create();
@Override
public boolean read(InByteBuffer buffer) {
int length = buffer.readVarInt();
for (int i = 0; i < length; i++) {
Recipe recipe;
String identifier;
ModIdentifier identifier;
String typeName;
if (buffer.getVersionId() >= 453) { // ToDo: find out version
typeName = buffer.readString();
identifier = buffer.readString();
identifier = buffer.readIdentifier();
} else {
identifier = buffer.readString();
identifier = buffer.readIdentifier();
typeName = buffer.readString();
}
RecipeTypes type = RecipeTypes.byName(typeName);
@ -93,7 +94,7 @@ public class PacketDeclareRecipes implements ClientboundPacket {
Log.protocol(String.format("[IN] Received declare recipe packet (recipeLength=%d)", recipes.size()));
}
public HashBiMap<String, Recipe> getRecipes() {
public HashBiMap<ModIdentifier, Recipe> getRecipes() {
return recipes;
}
}

View File

@ -14,6 +14,7 @@
package de.bixilon.minosoft.protocol.packets.clientbound.play;
import de.bixilon.minosoft.data.SoundCategories;
import de.bixilon.minosoft.data.mappings.ModIdentifier;
import de.bixilon.minosoft.logging.Log;
import de.bixilon.minosoft.protocol.packets.ClientboundPacket;
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
@ -22,13 +23,13 @@ import de.bixilon.minosoft.util.BitByte;
public class PacketStopSound implements ClientboundPacket {
SoundCategories category;
String soundIdentifier;
ModIdentifier soundIdentifier;
@Override
public boolean read(InByteBuffer buffer) {
if (buffer.getVersionId() < 343) { // ToDo: these 2 values need to be switched in before 1.12.2
category = SoundCategories.valueOf(buffer.readString().toUpperCase());
soundIdentifier = buffer.readString();
soundIdentifier = buffer.readIdentifier();
return true;
}
byte flags = buffer.readByte();
@ -36,7 +37,7 @@ public class PacketStopSound implements ClientboundPacket {
category = SoundCategories.byId(buffer.readVarInt());
}
if (BitByte.isBitMask(flags, 0x02)) {
soundIdentifier = buffer.readString();
soundIdentifier = buffer.readIdentifier();
}
return true;
}
@ -55,7 +56,7 @@ public class PacketStopSound implements ClientboundPacket {
return category;
}
public String getSoundIdentifier() {
public ModIdentifier getSoundIdentifier() {
return soundIdentifier;
}
}

View File

@ -57,7 +57,7 @@ public class PacketUnlockRecipes implements ClientboundPacket {
if (buffer.getVersionId() < 348) {
listed[i] = Recipes.getRecipeById(buffer.readVarInt());
} else {
listed[i] = Recipes.getRecipe(buffer.readString());
listed[i] = Recipes.getRecipe(buffer.readIdentifier());
}
}
if (action == UnlockRecipeActions.INITIALIZE) {
@ -66,7 +66,7 @@ public class PacketUnlockRecipes implements ClientboundPacket {
if (buffer.getVersionId() < 348) {
tagged[i] = Recipes.getRecipeById(buffer.readVarInt());
} else {
tagged[i] = Recipes.getRecipe(buffer.readString());
tagged[i] = Recipes.getRecipe(buffer.readIdentifier());
}
}
}

View File

@ -16,10 +16,15 @@ package de.bixilon.minosoft.protocol.protocol;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import de.bixilon.minosoft.data.Directions;
import de.bixilon.minosoft.data.commands.CommandArgumentNode;
import de.bixilon.minosoft.data.commands.CommandLiteralNode;
import de.bixilon.minosoft.data.commands.CommandNode;
import de.bixilon.minosoft.data.commands.CommandRootNode;
import de.bixilon.minosoft.data.entities.EntityMetaData;
import de.bixilon.minosoft.data.entities.Location;
import de.bixilon.minosoft.data.entities.Poses;
import de.bixilon.minosoft.data.inventory.Slot;
import de.bixilon.minosoft.data.mappings.ModIdentifier;
import de.bixilon.minosoft.data.mappings.particle.Particle;
import de.bixilon.minosoft.data.mappings.particle.data.BlockParticleData;
import de.bixilon.minosoft.data.mappings.particle.data.DustParticleData;
@ -156,8 +161,7 @@ public class InByteBuffer {
int byteCount = 0;
int result = 0;
byte read;
do
{
do {
read = readByte();
result |= (read & 0x7F) << (7 * byteCount);
byteCount++;
@ -436,6 +440,10 @@ public class InByteBuffer {
return ret;
}
public int[] readVarIntArray() {
return readVarIntArray(readVarInt());
}
public Ingredient readIngredient() {
return new Ingredient(readSlotArray(readVarInt()));
}
@ -466,4 +474,37 @@ public class InByteBuffer {
}
return readVarInt();
}
public CommandNode[] readCommandNodesArray() {
CommandNode[] nodes = new CommandNode[readVarInt()];
for (int i = 0; i < nodes.length; i++) {
nodes[i] = readCommandNode();
}
// resole ids
for (CommandNode node : nodes) {
// redirect
if (node.getRedirectNodeId() != -1) {
node.setRedirectNode(nodes[node.getRedirectNodeId()]);
}
// children
for (int id : node.getChildrenIds()) {
node.getChildren().add(nodes[id]);
}
}
return nodes;
}
private CommandNode readCommandNode() {
byte flags = readByte();
return switch (CommandNode.NodeTypes.values()[flags & 0x03]) {
case ROOT -> new CommandRootNode(flags, this);
case LITERAL -> new CommandLiteralNode(flags, this);
case ARGUMENT -> new CommandArgumentNode(flags, this);
};
}
public ModIdentifier readIdentifier() {
return new ModIdentifier(readString());
}
}

View File

@ -800,4 +800,7 @@ public class PacketHandler {
public void handle(PacketVehicleMovement pkg) {
}
public void handle(PacketDeclareCommands pkg) {
}
}

View File

@ -117,7 +117,7 @@ public class Packets {
PLAY_CHAT_MESSAGE(PacketChatMessageReceiving.class),
PLAY_MULTIBLOCK_CHANGE(PacketMultiBlockChange.class),
PLAY_TAB_COMPLETE(PacketTabCompleteReceiving.class),
PLAY_DECLARE_COMMANDS(null),
PLAY_DECLARE_COMMANDS(PacketDeclareCommands.class),
PLAY_WINDOW_CONFIRMATION(PacketConfirmTransactionReceiving.class),
PLAY_CLOSE_WINDOW(PacketCloseWindowReceiving.class),
PLAY_WINDOW_ITEMS(PacketWindowItems.class),