Skip to content

Commit

Permalink
Use new fastutil dependencies and remove Trove.
Browse files Browse the repository at this point in the history
  • Loading branch information
SupremeMortal committed Mar 13, 2020
1 parent c2f48c1 commit 5013b4b
Show file tree
Hide file tree
Showing 90 changed files with 388 additions and 352 deletions.
12 changes: 9 additions & 3 deletions bedrock/bedrock-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,15 @@
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.nukkitx</groupId>
<artifactId>fastutil-lite</artifactId>
<version>8.1.1</version>
<groupId>com.nukkitx.fastutil</groupId>
<artifactId>fastutil-long-common</artifactId>
<version>8.3.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.nukkitx.fastutil</groupId>
<artifactId>fastutil-long-object-maps</artifactId>
<version>8.3.1</version>
<scope>compile</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
import com.nukkitx.protocol.bedrock.packet.PacketHeader;
import com.nukkitx.protocol.bedrock.packet.UnknownPacket;
import com.nukkitx.protocol.serializer.PacketSerializer;
import com.nukkitx.protocol.util.TIntHashBiMap;
import gnu.trove.iterator.TIntObjectIterator;
import gnu.trove.map.TIntObjectMap;
import gnu.trove.map.hash.TIntObjectHashMap;
import com.nukkitx.protocol.util.Int2ObjectBiMap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.util.ReferenceCountUtil;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -33,7 +32,7 @@ public final class BedrockPacketCodec {
@Getter
private final String minecraftVersion;
private final PacketSerializer<BedrockPacket>[] serializers;
private final TIntHashBiMap<Class<? extends BedrockPacket>> idBiMap;
private final Int2ObjectBiMap<Class<? extends BedrockPacket>> idBiMap;
private final PacketSerializer<PacketHeader> headerSerializer;

public static Builder builder() {
Expand Down Expand Up @@ -104,8 +103,8 @@ public int getId(Class<? extends BedrockPacket> clazz) {
@SuppressWarnings("unchecked")
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public static class Builder {
private final TIntObjectMap<PacketSerializer<BedrockPacket>> serializers = new TIntObjectHashMap<>();
private final TIntHashBiMap<Class<? extends BedrockPacket>> idBiMap = new TIntHashBiMap<>((Class) UnknownPacket.class);
private final Int2ObjectMap<PacketSerializer<BedrockPacket>> serializers = new Int2ObjectOpenHashMap<>();
private final Int2ObjectBiMap<Class<? extends BedrockPacket>> idBiMap = new Int2ObjectBiMap<>(UnknownPacket.class);
private int protocolVersion = -1;
private String minecraftVersion = null;
private PacketSerializer<PacketHeader> headerSerializer = null;
Expand Down Expand Up @@ -146,19 +145,16 @@ public BedrockPacketCodec build() {
Preconditions.checkNotNull(minecraftVersion, "No Minecraft version defined");
Preconditions.checkNotNull(headerSerializer, "headerSerializer cannot be null");
int largestId = -1;
for (int id : serializers.keys()) {
for (int id : serializers.keySet()) {
if (id > largestId) {
largestId = id;
}
}
Preconditions.checkArgument(largestId > -1, "Must have at least one packet registered");
PacketSerializer<BedrockPacket>[] serializers = new PacketSerializer[largestId + 1];

TIntObjectIterator<PacketSerializer<BedrockPacket>> iterator = this.serializers.iterator();

while (iterator.hasNext()) {
iterator.advance();
serializers[iterator.key()] = iterator.value();
for (Int2ObjectMap.Entry<PacketSerializer<BedrockPacket>> entry : this.serializers.int2ObjectEntrySet()) {
serializers[entry.getIntKey()] = entry.getValue();
}
return new BedrockPacketCodec(protocolVersion, minecraftVersion, serializers, idBiMap, headerSerializer);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.nukkitx.protocol.bedrock.data;

import gnu.trove.map.TIntObjectMap;
import gnu.trove.map.hash.TIntObjectHashMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;

public enum ContainerType {
UNTRACKED_UI_INTERACTION(-9),
Expand Down Expand Up @@ -29,7 +29,7 @@ public enum ContainerType {
MATERIAL_REDUCER(22),
LAB_TABLE(23);

public static final TIntObjectMap<ContainerType> BY_ID = new TIntObjectHashMap<>();
public static final Int2ObjectMap<ContainerType> BY_ID = new Int2ObjectOpenHashMap<>();

static {
for (ContainerType type : values()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package com.nukkitx.protocol.bedrock.data;

import com.nukkitx.network.util.Preconditions;
import com.nukkitx.protocol.util.TIntHashBiMap;
import com.nukkitx.protocol.util.Int2ObjectBiMap;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import lombok.ToString;

import javax.annotation.Nonnull;
import java.util.HashSet;
import java.util.Set;

@ToString
public class EntityFlags {
private static final InternalLogger log = InternalLoggerFactory.getInstance(EntityFlags.class);

private final Set<EntityFlag> flags = new HashSet<>();
private final Set<EntityFlag> flags = new ObjectOpenHashSet<>();

/**
* Set {@link EntityFlag} value
Expand Down Expand Up @@ -47,7 +47,7 @@ public boolean getFlag(@Nonnull EntityFlag flag) {
return flags.contains(flag);
}

public long get(int index, TIntHashBiMap<EntityFlag> flagMappings) {
public long get(int index, Int2ObjectBiMap<EntityFlag> flagMappings) {
long value = 0;
final int lower = index * 64;
final int upper = lower + 64;
Expand All @@ -60,7 +60,7 @@ public long get(int index, TIntHashBiMap<EntityFlag> flagMappings) {
return value;
}

public void set(long value, int index, TIntHashBiMap<EntityFlag> flagMappings) {
public void set(long value, int index, Int2ObjectBiMap<EntityFlag> flagMappings) {
final int lower = index * 64;
final int upper = lower + 64;
for (int i = lower; i < upper; i++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.nukkitx.protocol.bedrock.data;

import com.nukkitx.network.util.Preconditions;
import gnu.trove.map.TIntObjectMap;
import gnu.trove.map.hash.TIntObjectHashMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.Value;
Expand Down Expand Up @@ -60,7 +60,7 @@ public enum Type {
UNTRACKED_INTERACTION_UI(100),
NON_IMPLEMENTED_TODO(99999);

private static final TIntObjectMap<Type> BY_ID = new TIntObjectHashMap<>(6);
private static final Int2ObjectMap<Type> BY_ID = new Int2ObjectOpenHashMap<>(6);

static {
for (Type type : values()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
import com.nukkitx.protocol.bedrock.data.CommandPermission;
import com.nukkitx.protocol.bedrock.data.PlayerPermission;
import com.nukkitx.protocol.bedrock.handler.BedrockPacketHandler;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.HashSet;
import java.util.Set;

@Data
@EqualsAndHashCode(doNotUseGetters = true, callSuper = false)
public class AdventureSettingsPacket extends BedrockPacket {
private final Set<Flag> flags = new HashSet<>();
private final Set<Flag> flags = new ObjectOpenHashSet<>();
private CommandPermission commandPermission;
private PlayerPermission playerPermission;
private long uniqueEntityId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.nukkitx.protocol.bedrock.BedrockPacket;
import com.nukkitx.protocol.bedrock.BedrockPacketType;
import com.nukkitx.protocol.bedrock.handler.BedrockPacketHandler;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import lombok.Data;
import lombok.EqualsAndHashCode;

Expand All @@ -19,7 +20,7 @@ public class UpdateBlockPacket extends BedrockPacket {
new HashSet<>(Arrays.asList(Flag.NEIGHBORS, Flag.NETWORK)));
public static final Set<Flag> FLAG_ALL_PRIORITY = Collections.unmodifiableSet(
new HashSet<>(Arrays.asList(Flag.NEIGHBORS, Flag.NETWORK, Flag.PRIORITY)));
private final Set<Flag> flags = new HashSet<>();
private final Set<Flag> flags = new ObjectOpenHashSet<>();
private Vector3i blockPosition;
private int runtimeId;
private int dataLayer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import com.nukkitx.protocol.bedrock.packet.ResourcePacksInfoPacket;
import com.nukkitx.protocol.bedrock.util.LittleEndianByteBufOutputStream;
import com.nukkitx.protocol.bedrock.v291.serializer.GameRulesChangedSerializer_v291;
import com.nukkitx.protocol.util.TIntHashBiMap;
import com.nukkitx.protocol.util.Int2ObjectBiMap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufInputStream;
import io.netty.util.AsciiString;
Expand All @@ -34,9 +34,9 @@
@UtilityClass
public final class BedrockUtils {
private static final InternalLogger log = InternalLoggerFactory.getInstance(BedrockUtils.class);
private static final TIntHashBiMap<EntityData> METADATAS = new TIntHashBiMap<>();
private static final TIntHashBiMap<EntityFlag> METADATA_FLAGS = new TIntHashBiMap<>();
private static final TIntHashBiMap<EntityData.Type> METADATA_TYPES = new TIntHashBiMap<>(9);
private static final Int2ObjectBiMap<EntityData> METADATAS = new Int2ObjectBiMap<>();
private static final Int2ObjectBiMap<EntityFlag> METADATA_FLAGS = new Int2ObjectBiMap<>();
private static final Int2ObjectBiMap<Type> METADATA_TYPES = new Int2ObjectBiMap<>(9);

static {
METADATAS.put(0, FLAGS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import com.nukkitx.protocol.bedrock.data.PlayerPermission;
import com.nukkitx.protocol.bedrock.packet.AdventureSettingsPacket;
import com.nukkitx.protocol.serializer.PacketSerializer;
import gnu.trove.map.TObjectIntMap;
import gnu.trove.map.hash.TObjectIntHashMap;
import io.netty.buffer.ByteBuf;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

Expand All @@ -25,8 +25,8 @@ public class AdventureSettingsSerializer_v291 implements PacketSerializer<Advent
private static final AdventureSettingsPacket.Flag[] FLAGS_1 = {IMMUTABLE_WORLD, NO_PVP, NO_PVM, null, NO_MVP, AUTO_JUMP, MAY_FLY, NO_CLIP, WORLD_BUILDER, FLYING, MUTE};
private static final AdventureSettingsPacket.Flag[] FLAGS_2 = {MINE, DOORS_AND_SWITCHES, OPEN_CONTAINERS, ATTACK_PLAYERS, ATTACK_MOBS, OP, null, TELEPORT, BUILD, SET_DEFAULT};

private static final TObjectIntMap<AdventureSettingsPacket.Flag> FLAGS_TO_BIT_1 = new TObjectIntHashMap<>();
private static final TObjectIntMap<AdventureSettingsPacket.Flag> FLAGS_TO_BIT_2 = new TObjectIntHashMap<>();
private static final Object2IntMap<AdventureSettingsPacket.Flag> FLAGS_TO_BIT_1 = new Object2IntOpenHashMap<>();
private static final Object2IntMap<AdventureSettingsPacket.Flag> FLAGS_TO_BIT_2 = new Object2IntOpenHashMap<>();

static {
FLAGS_TO_BIT_1.put(IMMUTABLE_WORLD, 0x1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.nukkitx.network.VarInts;
import com.nukkitx.protocol.bedrock.packet.AnimatePacket;
import com.nukkitx.protocol.serializer.PacketSerializer;
import com.nukkitx.protocol.util.TIntHashBiMap;
import com.nukkitx.protocol.util.Int2ObjectBiMap;
import io.netty.buffer.ByteBuf;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
Expand All @@ -13,7 +13,7 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class AnimateSerializer_v291 implements PacketSerializer<AnimatePacket> {
public static final AnimateSerializer_v291 INSTANCE = new AnimateSerializer_v291();
private static final TIntHashBiMap<AnimatePacket.Action> types = new TIntHashBiMap<>();
private static final Int2ObjectBiMap<Action> types = new Int2ObjectBiMap<>();

static {
types.put(0, Action.NO_ACTION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@
import com.nukkitx.protocol.bedrock.packet.AvailableCommandsPacket;
import com.nukkitx.protocol.bedrock.v291.BedrockUtils;
import com.nukkitx.protocol.serializer.PacketSerializer;
import com.nukkitx.protocol.util.TIntHashBiMap;
import com.nukkitx.protocol.util.Int2ObjectBiMap;
import io.netty.buffer.ByteBuf;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.function.ObjIntConsumer;
import java.util.function.ToIntFunction;

Expand All @@ -29,7 +33,7 @@ public class AvailableCommandsSerializer_v291 implements PacketSerializer<Availa
private static final ToIntFunction<ByteBuf> READ_BYTE = ByteBuf::readUnsignedByte;
private static final ToIntFunction<ByteBuf> READ_SHORT = ByteBuf::readUnsignedShortLE;
private static final ToIntFunction<ByteBuf> READ_INT = ByteBuf::readIntLE;
private static final TIntHashBiMap<CommandParamData.Type> PARAM_TYPES = new TIntHashBiMap<>();
private static final Int2ObjectBiMap<CommandParamData.Type> PARAM_TYPES = new Int2ObjectBiMap<>();

static {
PARAM_TYPES.put(1, INT);
Expand All @@ -51,10 +55,10 @@ public class AvailableCommandsSerializer_v291 implements PacketSerializer<Availa

@Override
public void serialize(ByteBuf buffer, AvailableCommandsPacket packet) {
Set<String> enumValuesSet = new HashSet<>();
Set<String> postfixSet = new HashSet<>();
Set<CommandEnumData> enumsSet = new HashSet<>();
Set<CommandEnumData> softEnumsSet = new HashSet<>();
Set<String> enumValuesSet = new ObjectOpenHashSet<>();
Set<String> postfixSet = new ObjectOpenHashSet<>();
Set<CommandEnumData> enumsSet = new ObjectOpenHashSet<>();
Set<CommandEnumData> softEnumsSet = new ObjectOpenHashSet<>();

// Get all enum values
for (CommandData data : packet.getCommands()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.nukkitx.protocol.bedrock.packet.BookEditPacket;
import com.nukkitx.protocol.bedrock.v291.BedrockUtils;
import com.nukkitx.protocol.serializer.PacketSerializer;
import com.nukkitx.protocol.util.TIntHashBiMap;
import com.nukkitx.protocol.util.Int2ObjectBiMap;
import io.netty.buffer.ByteBuf;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
Expand All @@ -13,7 +13,7 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class BookEditSerializer_v291 implements PacketSerializer<BookEditPacket> {
public static final BookEditSerializer_v291 INSTANCE = new BookEditSerializer_v291();
private static final TIntHashBiMap<Action> types = new TIntHashBiMap<>();
private static final Int2ObjectBiMap<Action> types = new Int2ObjectBiMap<>();

static {
types.put(0, Action.REPLACE_PAGE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void serialize(ByteBuf buffer, ClientboundMapItemDataPacket packet) {

if ((type & 0x8) != 0) {
VarInts.writeUnsignedInt(buffer, trackedEntityIds.size());
for (long trackedEntityId : trackedEntityIds.toArray()) {
for (long trackedEntityId : trackedEntityIds) {
VarInts.writeLong(buffer, trackedEntityId);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.nukkitx.network.VarInts;
import com.nukkitx.protocol.bedrock.packet.ContainerSetDataPacket;
import com.nukkitx.protocol.serializer.PacketSerializer;
import com.nukkitx.protocol.util.TIntHashBiMap;
import io.netty.buffer.ByteBuf;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.nukkitx.protocol.bedrock.data.EntityEventType;
import com.nukkitx.protocol.bedrock.packet.EntityEventPacket;
import com.nukkitx.protocol.serializer.PacketSerializer;
import com.nukkitx.protocol.util.TIntHashBiMap;
import com.nukkitx.protocol.util.Int2ObjectBiMap;
import io.netty.buffer.ByteBuf;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
Expand All @@ -16,7 +16,7 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class EntityEventSerializer_v291 implements PacketSerializer<EntityEventPacket> {
public static final EntityEventSerializer_v291 INSTANCE = new EntityEventSerializer_v291();
private static final TIntHashBiMap<EntityEventType> events = new TIntHashBiMap<>();
private static final Int2ObjectBiMap<EntityEventType> events = new Int2ObjectBiMap<>();
private static final InternalLogger log = InternalLoggerFactory.getInstance(EntityEventSerializer_v291.class);

static {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
import com.nukkitx.protocol.bedrock.packet.GameRulesChangedPacket;
import com.nukkitx.protocol.bedrock.v291.BedrockUtils;
import com.nukkitx.protocol.serializer.PacketSerializer;
import gnu.trove.map.TObjectIntMap;
import gnu.trove.map.hash.TObjectIntHashMap;
import io.netty.buffer.ByteBuf;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class GameRulesChangedSerializer_v291 implements PacketSerializer<GameRulesChangedPacket> {
public static final GameRulesChangedSerializer_v291 INSTANCE = new GameRulesChangedSerializer_v291();

public static final TObjectIntMap<Class> RULE_TYPES = new TObjectIntHashMap<>(3, 0.5f, -1);
public static final Object2IntMap<Class<?>> RULE_TYPES = new Object2IntOpenHashMap<>(3, 0.5f);

static {
RULE_TYPES.defaultReturnValue(-1);
RULE_TYPES.put(Boolean.class, 1);
RULE_TYPES.put(Integer.class, 2);
RULE_TYPES.put(Float.class, 3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.nukkitx.protocol.bedrock.packet.LevelEventPacket;
import com.nukkitx.protocol.bedrock.v291.BedrockUtils;
import com.nukkitx.protocol.serializer.PacketSerializer;
import com.nukkitx.protocol.util.TIntHashBiMap;
import com.nukkitx.protocol.util.Int2ObjectBiMap;
import io.netty.buffer.ByteBuf;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
Expand All @@ -19,7 +19,7 @@ public class LevelEventSerializer_v291 implements PacketSerializer<LevelEventPac
public static final LevelEventSerializer_v291 INSTANCE = new LevelEventSerializer_v291();

private static final InternalLogger log = InternalLoggerFactory.getInstance(LevelEventSerializer_v291.class);
private static final TIntHashBiMap<LevelEventType> events = new TIntHashBiMap<>();
private static final Int2ObjectBiMap<LevelEventType> events = new Int2ObjectBiMap<>();

static {
events.put(1000, SOUND_CLICK);
Expand Down
Loading

0 comments on commit 5013b4b

Please sign in to comment.