mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-04 23:47:59 +01:00
Merge branch 'master' into new-block-api
# Conflicts: # src/main/java/net/minestom/server/utils/ArrayUtils.java
This commit is contained in:
commit
22ac1c4998
@ -316,9 +316,9 @@ public final class CommandManager {
|
|||||||
final int literalNodeId = addCommandNameNode(literalNode, rootChildren, nodes);
|
final int literalNodeId = addCommandNameNode(literalNode, rootChildren, nodes);
|
||||||
|
|
||||||
// Contains the arguments of the already-parsed syntaxes
|
// Contains the arguments of the already-parsed syntaxes
|
||||||
List<Argument<?>[]> syntaxesArguments = new ArrayList<>();
|
Map<CommandSyntax, Argument<?>[]> syntaxesArguments = new HashMap<>();
|
||||||
// Contains the nodes of an argument
|
// Contains the nodes of an argument
|
||||||
Map<Argument<?>, List<DeclareCommandsPacket.Node[]>> storedArgumentsNodes = new HashMap<>();
|
Map<IndexedArgument, List<DeclareCommandsPacket.Node[]>> storedArgumentsNodes = new HashMap<>();
|
||||||
|
|
||||||
// Sort syntaxes by argument count. Brigadier requires it.
|
// Sort syntaxes by argument count. Brigadier requires it.
|
||||||
syntaxes = syntaxes.stream().sorted(Comparator.comparingInt(o -> -o.getArguments().length)).collect(Collectors.toList());
|
syntaxes = syntaxes.stream().sorted(Comparator.comparingInt(o -> -o.getArguments().length)).collect(Collectors.toList());
|
||||||
@ -347,14 +347,16 @@ public final class CommandManager {
|
|||||||
{
|
{
|
||||||
// Find shared part
|
// Find shared part
|
||||||
boolean foundSharedPart = false;
|
boolean foundSharedPart = false;
|
||||||
for (Argument<?>[] parsedArguments : syntaxesArguments) {
|
for (var entry : syntaxesArguments.entrySet()) {
|
||||||
|
final var parsedArguments = entry.getValue();
|
||||||
final int index = i + 1;
|
final int index = i + 1;
|
||||||
if (ArrayUtils.sameStart(arguments, parsedArguments, index)) {
|
if (ArrayUtils.sameStart(arguments, parsedArguments, index)) {
|
||||||
final Argument<?> sharedArgument = parsedArguments[i];
|
final Argument<?> sharedArgument = parsedArguments[i];
|
||||||
final List<DeclareCommandsPacket.Node[]> storedNodes = storedArgumentsNodes.get(sharedArgument);
|
final var indexed = new IndexedArgument(entry.getKey(), sharedArgument, i);
|
||||||
|
final List<DeclareCommandsPacket.Node[]> storedNodes = storedArgumentsNodes.get(indexed);
|
||||||
|
|
||||||
argChildren = new IntArrayList();
|
argChildren = new IntArrayList();
|
||||||
lastNodes = storedNodes.get(index);
|
lastNodes = storedNodes.get(storedNodes.size() >= index ? index : 1);
|
||||||
foundSharedPart = true;
|
foundSharedPart = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -369,7 +371,7 @@ public final class CommandManager {
|
|||||||
|
|
||||||
// Each node array represent a layer
|
// Each node array represent a layer
|
||||||
final List<DeclareCommandsPacket.Node[]> nodesLayer = nodeMaker.getNodes();
|
final List<DeclareCommandsPacket.Node[]> nodesLayer = nodeMaker.getNodes();
|
||||||
storedArgumentsNodes.put(argument, new ArrayList<>(nodesLayer));
|
storedArgumentsNodes.put(new IndexedArgument(syntax, argument, i), new ArrayList<>(nodesLayer));
|
||||||
for (int nodeIndex = lastArgumentNodeIndex; nodeIndex < nodesLayer.size(); nodeIndex++) {
|
for (int nodeIndex = lastArgumentNodeIndex; nodeIndex < nodesLayer.size(); nodeIndex++) {
|
||||||
final NodeMaker.ConfiguredNodes configuredNodes = nodeMaker.getConfiguredNodes().get(nodeIndex);
|
final NodeMaker.ConfiguredNodes configuredNodes = nodeMaker.getConfiguredNodes().get(nodeIndex);
|
||||||
final NodeMaker.Options options = configuredNodes.getOptions();
|
final NodeMaker.Options options = configuredNodes.getOptions();
|
||||||
@ -417,17 +419,18 @@ public final class CommandManager {
|
|||||||
|
|
||||||
nodeRequests.addAll(nodeMaker.getNodeRequests());
|
nodeRequests.addAll(nodeMaker.getNodeRequests());
|
||||||
|
|
||||||
syntaxesArguments.add(arguments);
|
syntaxesArguments.put(syntax, arguments);
|
||||||
}
|
}
|
||||||
|
|
||||||
storedArgumentsNodes.forEach((argument, argNodes) -> {
|
storedArgumentsNodes.forEach((indexedArgument, argNodes) -> {
|
||||||
int value = 0;
|
int value = 0;
|
||||||
for (DeclareCommandsPacket.Node[] n1 : argNodes) {
|
for (DeclareCommandsPacket.Node[] n1 : argNodes) {
|
||||||
for (DeclareCommandsPacket.Node n2 : n1) {
|
for (DeclareCommandsPacket.Node n2 : n1) {
|
||||||
value = nodes.indexOf(n2);
|
value = nodes.indexOf(n2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
argumentIdentityMap.put(argument, value);
|
// FIXME: add syntax for indexing
|
||||||
|
argumentIdentityMap.put(indexedArgument.argument, value);
|
||||||
});
|
});
|
||||||
|
|
||||||
literalNode.children = ArrayUtils.toArray(cmdChildren);
|
literalNode.children = ArrayUtils.toArray(cmdChildren);
|
||||||
@ -451,4 +454,38 @@ public final class CommandManager {
|
|||||||
nodes.add(commandNode);
|
nodes.add(commandNode);
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static class IndexedArgument {
|
||||||
|
private final CommandSyntax syntax;
|
||||||
|
private final Argument<?> argument;
|
||||||
|
private final int index;
|
||||||
|
|
||||||
|
public IndexedArgument(CommandSyntax syntax, Argument<?> argument, int index) {
|
||||||
|
this.syntax = syntax;
|
||||||
|
this.argument = argument;
|
||||||
|
this.index = index;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
IndexedArgument that = (IndexedArgument) o;
|
||||||
|
return index == that.index && Objects.equals(syntax, that.syntax) && Objects.equals(argument, that.argument);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(syntax, argument, index);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "IndexedArgument{" +
|
||||||
|
"syntax=" + syntax +
|
||||||
|
", argument=" + argument +
|
||||||
|
", index=" + index +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,30 +1,28 @@
|
|||||||
package net.minestom.server.utils;
|
package net.minestom.server.utils;
|
||||||
|
|
||||||
import it.unimi.dsi.fastutil.ints.IntList;
|
import it.unimi.dsi.fastutil.ints.IntList;
|
||||||
import net.minestom.server.utils.validate.Check;
|
import org.jetbrains.annotations.ApiStatus;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.function.Supplier;
|
import java.util.Objects;
|
||||||
|
|
||||||
|
@ApiStatus.Internal
|
||||||
public final class ArrayUtils {
|
public final class ArrayUtils {
|
||||||
|
|
||||||
private ArrayUtils() {
|
private ArrayUtils() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int[] concatenateIntArrays(@NotNull int[]... arrays) {
|
public static int[] concatenateIntArrays(int @NotNull []... arrays) {
|
||||||
int totalLength = 0;
|
int totalLength = 0;
|
||||||
for (int[] array : arrays) {
|
for (int[] array : arrays) {
|
||||||
totalLength += array.length;
|
totalLength += array.length;
|
||||||
}
|
}
|
||||||
int[] result = new int[totalLength];
|
int[] result = new int[totalLength];
|
||||||
|
|
||||||
int startingPos = 0;
|
int startingPos = 0;
|
||||||
for (int[] array : arrays) {
|
for (int[] array : arrays) {
|
||||||
System.arraycopy(array, 0, result, startingPos, array.length);
|
System.arraycopy(array, 0, result, startingPos, array.length);
|
||||||
startingPos += array.length;
|
startingPos += array.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -32,11 +30,6 @@ public final class ArrayUtils {
|
|||||||
System.arraycopy(arr, index + 1, arr, index, arr.length - 1 - index);
|
System.arraycopy(arr, index + 1, arr, index, arr.length - 1 - index);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void copyToDestination(short[] src, short[] dest) {
|
|
||||||
Check.argCondition(src.length != dest.length, "The two arrays need to have the same length.");
|
|
||||||
System.arraycopy(src, 0, dest, 0, src.length);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the differences between 2 arrays.
|
* Gets the differences between 2 arrays.
|
||||||
*
|
*
|
||||||
@ -44,8 +37,7 @@ public final class ArrayUtils {
|
|||||||
* @param b the second array
|
* @param b the second array
|
||||||
* @return an array containing a's indexes that aren't in b array
|
* @return an array containing a's indexes that aren't in b array
|
||||||
*/
|
*/
|
||||||
@NotNull
|
public static int @NotNull [] getDifferencesBetweenArray(long @NotNull [] a, long @NotNull [] b) {
|
||||||
public static int[] getDifferencesBetweenArray(@NotNull long[] a, @NotNull long[] b) {
|
|
||||||
int counter = 0;
|
int counter = 0;
|
||||||
int[] indexes = new int[Math.max(a.length, b.length)];
|
int[] indexes = new int[Math.max(a.length, b.length)];
|
||||||
|
|
||||||
@ -69,36 +61,28 @@ public final class ArrayUtils {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
public static int @NotNull [] toArray(@NotNull IntList list) {
|
||||||
public static int[] toArray(@NotNull IntList list) {
|
|
||||||
int[] array = new int[list.size()];
|
int[] array = new int[list.size()];
|
||||||
for (int i = 0; i < array.length; i++) {
|
list.getElements(0, array, 0, array.length);
|
||||||
array[i] = list.getInt(i);
|
|
||||||
}
|
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets if two arrays share the same start until {@code length}.
|
* Gets if two arrays share the same start until {@code length}.
|
||||||
*
|
*
|
||||||
* @param array1 the first array
|
* @param first the first array
|
||||||
* @param array2 the second array
|
* @param second the second array
|
||||||
* @param length the length to check (0-length)
|
* @param length the length to check (0-length)
|
||||||
* @param <T> the type of the arrays
|
* @param <T> the type of the arrays
|
||||||
* @return true if both arrays share the same start
|
* @return true if both arrays share the same start
|
||||||
*/
|
*/
|
||||||
public static <T> boolean sameStart(T[] array1, T[] array2, int length) {
|
public static <T> boolean sameStart(@NotNull T[] first, @NotNull T[] second, int length) {
|
||||||
if (length > array1.length || length > array2.length) {
|
if (Math.min(first.length, second.length) < length) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < length; i++) {
|
for (int i = 0; i < length; i++) {
|
||||||
final T value1 = array1[i];
|
if (!Objects.equals(first[i], second[i])) {
|
||||||
for (int j = 0; j < length; j++) {
|
return false;
|
||||||
final T value2 = array2[j];
|
|
||||||
if (!value1.equals(value2)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
Loading…
Reference in New Issue
Block a user