forked from ProtocolSupport/ProtocolSupport
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
570 additions
and
9 deletions.
There are no files selected for viewing
111 changes: 103 additions & 8 deletions
111
src/protocolsupport/protocol/packet/middle/base/clientbound/play/MiddleDeclareCommands.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,123 @@ | ||
package protocolsupport.protocol.packet.middle.base.clientbound.play; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import protocolsupport.protocol.codec.ArrayCodec; | ||
import protocolsupport.protocol.codec.MiscDataCodec; | ||
import protocolsupport.protocol.packet.middle.MiddlePacketCancelException; | ||
import protocolsupport.protocol.codec.StringCodec; | ||
import protocolsupport.protocol.codec.VarNumberCodec; | ||
import protocolsupport.protocol.packet.middle.base.clientbound.ClientBoundMiddlePacket; | ||
import protocolsupport.protocol.utils.ProtocolVersionsHelper; | ||
import protocolsupport.protocol.types.command.CommandNode; | ||
import protocolsupport.protocol.types.command.CommandNodeDoubleProperties; | ||
import protocolsupport.protocol.types.command.CommandNodeEntityProperties; | ||
import protocolsupport.protocol.types.command.CommandNodeIntegerProperties; | ||
import protocolsupport.protocol.types.command.CommandNodeLongProperties; | ||
import protocolsupport.protocol.types.command.CommandNodeProperties; | ||
import protocolsupport.protocol.types.command.CommandNodeRangeProperties; | ||
import protocolsupport.protocol.types.command.CommandNodeResourceOrTagProperties; | ||
import protocolsupport.protocol.types.command.CommandNodeResourceProperties; | ||
import protocolsupport.protocol.types.command.CommandNodeScoreHolderProperties; | ||
import protocolsupport.protocol.types.command.CommandNodeSimpleProperties; | ||
import protocolsupport.protocol.types.command.CommandNodeStringProperties; | ||
import protocolsupport.utils.BitUtils; | ||
|
||
public abstract class MiddleDeclareCommands extends ClientBoundMiddlePacket { | ||
|
||
protected MiddleDeclareCommands(IMiddlePacketInit init) { | ||
super(init); | ||
} | ||
|
||
//TODO: structure | ||
protected ByteBuf data; | ||
protected CommandNode[] nodes; | ||
protected int rootNodeIndex; | ||
|
||
@Override | ||
protected void decode(ByteBuf serverdata) { | ||
data = MiscDataCodec.readAllBytesSlice(serverdata); | ||
nodes = ArrayCodec.readVarIntTArray(serverdata, CommandNode.class, MiddleDeclareCommands::readNode); | ||
rootNodeIndex = VarNumberCodec.readVarInt(serverdata); | ||
} | ||
|
||
protected static final int NODE_FLAGS_TYPE_MASK = BitUtils.createIBitMaskFromBits(new int[] {0, 1}); | ||
protected static final int NODE_FLAGS_HAS_REDIRECT_BIT = 3; | ||
protected static final int NODE_FLAGS_HAS_SUGGESTIONS_TYPE = 4; | ||
|
||
protected static final int NODE_TYPE_ROOT = 0; | ||
protected static final int NODE_TYPE_LITERAL = 1; | ||
protected static final int NODE_TYPE_ARGUMENT = 2; | ||
|
||
//TODO: remove after implementing | ||
if (version.isBefore(ProtocolVersionsHelper.LATEST_PC)) { | ||
throw MiddlePacketCancelException.INSTANCE; | ||
protected static final Map<String, Function<ByteBuf, CommandNodeProperties>> propertiesDeserializer = new HashMap<>(); | ||
static { | ||
propertiesDeserializer.put("brigadier:double", data -> { | ||
int flags = data.readByte(); | ||
double min = BitUtils.isIBitSet(flags, 0) ? data.readDouble() : Double.MIN_VALUE; | ||
double max = BitUtils.isIBitSet(flags, 1) ? data.readDouble() : Double.MAX_VALUE; | ||
return new CommandNodeDoubleProperties(flags, min, max); | ||
}); | ||
propertiesDeserializer.put("brigadier:float", data -> { | ||
int flags = data.readByte(); | ||
float min = BitUtils.isIBitSet(flags, 0) ? data.readFloat() : Float.MIN_VALUE; | ||
float max = BitUtils.isIBitSet(flags, 1) ? data.readFloat() : Float.MAX_VALUE; | ||
return new CommandNodeDoubleProperties(flags, min, max); | ||
}); | ||
propertiesDeserializer.put("brigadier:long", data -> { | ||
int flags = data.readByte(); | ||
long min = BitUtils.isIBitSet(flags, 0) ? data.readLong() : Long.MIN_VALUE; | ||
long max = BitUtils.isIBitSet(flags, 1) ? data.readLong() : Long.MAX_VALUE; | ||
return new CommandNodeLongProperties(flags, min, max); | ||
}); | ||
propertiesDeserializer.put("brigadier:integer", data -> { | ||
int flags = data.readByte(); | ||
int min = BitUtils.isIBitSet(flags, 0) ? data.readInt() : Integer.MIN_VALUE; | ||
int max = BitUtils.isIBitSet(flags, 1) ? data.readInt() : Integer.MAX_VALUE; | ||
return new CommandNodeIntegerProperties(flags, min, max); | ||
}); | ||
propertiesDeserializer.put("brigadier:string", data -> { | ||
CommandNodeStringProperties.Type type = MiscDataCodec.readVarIntEnum(data, CommandNodeStringProperties.Type.CONSTANT_LOOKUP); | ||
return new CommandNodeStringProperties(type); | ||
}); | ||
propertiesDeserializer.put("minecraft:entity", data -> { | ||
int flags = data.readByte(); | ||
return new CommandNodeEntityProperties(flags); | ||
}); | ||
propertiesDeserializer.put("minecraft:score_holder", data -> { | ||
int flags = data.readByte(); | ||
return new CommandNodeScoreHolderProperties(flags); | ||
}); | ||
propertiesDeserializer.put("minecraft:range", data -> { | ||
boolean allowDecimals = data.readBoolean(); | ||
return new CommandNodeRangeProperties(allowDecimals); | ||
}); | ||
propertiesDeserializer.put("minecraft:resource_or_tag", data -> { | ||
String identifier = StringCodec.readVarIntUTF8String(data); | ||
return new CommandNodeResourceOrTagProperties(identifier); | ||
}); | ||
propertiesDeserializer.put("minecraft:resource", data -> { | ||
String identifier = StringCodec.readVarIntUTF8String(data); | ||
return new CommandNodeResourceProperties(identifier); | ||
}); | ||
} | ||
|
||
protected static CommandNode readNode(ByteBuf data) { | ||
byte flags = data.readByte(); | ||
int nodeType = flags & NODE_FLAGS_TYPE_MASK; | ||
int[] childNodesIndexes = ArrayCodec.readVarIntVarIntArray(data); | ||
int redirectNodeIndex = BitUtils.isIBitSet(flags, NODE_FLAGS_HAS_REDIRECT_BIT) ? VarNumberCodec.readVarInt(data) : -1; | ||
String name = (nodeType == NODE_TYPE_LITERAL) || (nodeType == NODE_TYPE_ARGUMENT) ? StringCodec.readVarIntUTF8String(data) : null; | ||
String parser = nodeType == NODE_TYPE_ARGUMENT ? StringCodec.readVarIntUTF8String(data) : null; | ||
CommandNodeProperties properties = null; | ||
if (parser != null) { | ||
Function<ByteBuf, CommandNodeProperties> propertiesFunc = propertiesDeserializer.get(parser); | ||
if (propertiesFunc != null) { | ||
properties = propertiesFunc.apply(data); | ||
} else { | ||
properties = new CommandNodeSimpleProperties(parser); | ||
} | ||
} | ||
String suggestionsType = BitUtils.isIBitSet(flags, NODE_FLAGS_HAS_SUGGESTIONS_TYPE) ? StringCodec.readVarIntUTF8String(data) : null; | ||
return new CommandNode(flags, childNodesIndexes, redirectNodeIndex, name, parser, properties, suggestionsType); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/protocolsupport/protocol/typeremapper/legacy/LegacyCommandDataRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package protocolsupport.protocol.typeremapper.legacy; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
import java.util.function.UnaryOperator; | ||
|
||
import protocolsupport.api.ProtocolVersion; | ||
import protocolsupport.protocol.typeremapper.legacy.LegacyCommandDataRegistry.LegacyCommandDataMappingTable; | ||
import protocolsupport.protocol.typeremapper.utils.MappingRegistry; | ||
import protocolsupport.protocol.typeremapper.utils.MappingTable; | ||
import protocolsupport.protocol.types.command.CommandNodeProperties; | ||
import protocolsupport.protocol.types.command.CommandNodeResourceOrTagProperties; | ||
import protocolsupport.protocol.types.command.CommandNodeResourceProperties; | ||
import protocolsupport.protocol.types.command.CommandNodeStringProperties; | ||
import protocolsupport.protocol.utils.ProtocolVersionsHelper; | ||
import protocolsupportbuildprocessor.Preload; | ||
|
||
@Preload | ||
public class LegacyCommandDataRegistry extends MappingRegistry<LegacyCommandDataMappingTable> { | ||
|
||
public static final LegacyCommandDataRegistry INSTANCE = new LegacyCommandDataRegistry(); | ||
|
||
public LegacyCommandDataRegistry() { | ||
register(CommandNodeResourceOrTagProperties.class, properties -> new CommandNodeStringProperties(CommandNodeStringProperties.Type.SINGLE_WORD), ProtocolVersionsHelper.DOWN_1_18); | ||
register(CommandNodeResourceProperties.class, properties -> new CommandNodeStringProperties(CommandNodeStringProperties.Type.SINGLE_WORD), ProtocolVersionsHelper.DOWN_1_18); | ||
} | ||
|
||
protected <T extends CommandNodeProperties> void register(Class<T> clazz, Function<T, CommandNodeProperties> func, ProtocolVersion... versions) { | ||
for (ProtocolVersion version : versions) { | ||
getTable(version).set(clazz, func); | ||
} | ||
} | ||
|
||
@Override | ||
protected LegacyCommandDataMappingTable createTable() { | ||
return new LegacyCommandDataMappingTable(); | ||
} | ||
|
||
public static class LegacyCommandDataMappingTable extends MappingTable { | ||
|
||
protected final Map<Class<? extends CommandNodeProperties>, Function<? extends CommandNodeProperties, CommandNodeProperties>> table = new HashMap<>(); | ||
|
||
public <T extends CommandNodeProperties> void set(Class<T> clazz, Function<T, CommandNodeProperties> func) { | ||
table.put(clazz, func); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public <T extends CommandNodeProperties> Function<T, CommandNodeProperties> get(Class<? extends T> clazz) { | ||
return (Function<T, CommandNodeProperties>) table.getOrDefault(clazz, UnaryOperator.identity()); | ||
} | ||
|
||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
src/protocolsupport/protocol/types/command/CommandNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package protocolsupport.protocol.types.command; | ||
|
||
public class CommandNode { | ||
|
||
protected final int flags; | ||
protected final int[] childNodesIndexes; | ||
protected final int redirectNodeIndex; | ||
protected final String name; | ||
protected final String parser; | ||
protected final CommandNodeProperties properties; | ||
protected final String suggestType; | ||
|
||
public CommandNode(int flags, int[] childNodesIndexes, int redirectNodeIndex, String name, String parser, CommandNodeProperties properties, String suggestType) { | ||
this.flags = flags; | ||
this.childNodesIndexes = childNodesIndexes; | ||
this.redirectNodeIndex = redirectNodeIndex; | ||
this.name = name; | ||
this.parser = parser; | ||
this.properties = properties; | ||
this.suggestType = suggestType; | ||
} | ||
|
||
public int getFlags() { | ||
return flags; | ||
} | ||
|
||
|
||
public int[] getChildNodesIndexes() { | ||
return childNodesIndexes; | ||
} | ||
|
||
|
||
public int getRedirectNodeIndex() { | ||
return redirectNodeIndex; | ||
} | ||
|
||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
|
||
public String getParser() { | ||
return parser; | ||
} | ||
|
||
|
||
public CommandNodeProperties getProperties() { | ||
return properties; | ||
} | ||
|
||
|
||
public String getSuggestType() { | ||
return suggestType; | ||
} | ||
|
||
} |
Oops, something went wrong.