Skip to content

Commit

Permalink
Various fixes to packet encoding, decoding and equals/hashCode
Browse files Browse the repository at this point in the history
  • Loading branch information
SupremeMortal committed Mar 3, 2020
1 parent c154d00 commit c2f48c1
Show file tree
Hide file tree
Showing 206 changed files with 406 additions and 408 deletions.
6 changes: 6 additions & 0 deletions bedrock/bedrock-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
<version>4.41.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.nukkitx</groupId>
<artifactId>fastutil-lite</artifactId>
<version>8.1.1</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ public String toString() {
overloads.append("]\r\n");

StringBuilder builder = new StringBuilder("CommandData(\r\n");
List objects = Arrays.asList("name=" + name, "description=" + description, "flags=" + Arrays.toString(flags.toArray()), "permission=" + permission, "aliases=" + aliases, "overloads=" + overloads);
List<?> objects = Arrays.asList("name=" + name, "description=" + description,
"flags=" + Arrays.toString(flags.toArray()), "permission=" + permission, "aliases=" + aliases,
"overloads=" + overloads);

for (Object object : objects) {
builder.append(" ").append(Objects.toString(object).replaceAll("\r\n", "\r\n ")).append("\r\n");
}
return builder.toString();
return builder.append(")").toString();
}

@Value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public enum Type {
INT_RANGE,
STRING,
POSITION,
BLOCK_POSITION,
MESSAGE,
TEXT,
JSON,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,15 @@ public EntityDataMap putVector3f(EntityData key, Vector3f value) {
return this;
}

@Nonnull
public EntityFlags getOrCreateFlags() {
EntityFlags flags = this.getFlags();
if (flags == null) {
this.putFlags(flags = new EntityFlags());
}
return flags;
}

public EntityFlags getFlags() {
return (EntityFlags) this.map.get(FLAGS);
}
Expand Down Expand Up @@ -242,6 +251,19 @@ public Set<Entry<EntityData, Object>> entrySet() {
return this.map.entrySet();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
EntityDataMap that = (EntityDataMap) o;
return this.map.equals(that.map);
}

@Override
public int hashCode() {
return this.map.hashCode();
}

@Override
public String toString() {
return map.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

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

@ToString
Expand All @@ -17,24 +16,6 @@ public class EntityFlags {

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

public static EntityFlags create(long value, int index, TIntHashBiMap<EntityFlag> flagMappings) {
EntityFlags flags = new EntityFlags();
final int lower = index * 64;
final int upper = lower + 64;
for (int i = lower; i < upper; i++) {
int idx = i & 0x3f;
if ((value & (1L << idx)) != 0) {
EntityFlag flag = flagMappings.get(i);
if (flag != null) {
flags.flags.add(flag);
} else {
log.debug("Unknown Metadata flag index {} detected", i);
}
}
}
return flags;
}

/**
* Set {@link EntityFlag} value
*
Expand Down Expand Up @@ -79,12 +60,28 @@ public long get(int index, TIntHashBiMap<EntityFlag> flagMappings) {
return value;
}

public void set(long value, int index, TIntHashBiMap<EntityFlag> flagMappings) {
final int lower = index * 64;
final int upper = lower + 64;
for (int i = lower; i < upper; i++) {
int idx = i & 0x3f;
if ((value & (1L << idx)) != 0) {
EntityFlag flag = flagMappings.get(i);
if (flag != null) {
flags.add(flag);
} else {
log.debug("Unknown entity flag index {} detected", i);
}
}
}
}

@Override
public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof EntityFlags)) return false;
EntityFlags that = (EntityFlags) o;
return Objects.equals(this.flags, that.flags);
return this.flags.equals(that.flags);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@
public class GameRuleData<T> {
private final String name;
private final T value;

@Override
public String toString() {
return this.name + '=' + this.value;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.nukkitx.protocol.bedrock.data;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import lombok.*;

import java.awt.*;
import java.awt.image.BufferedImage;
Expand All @@ -12,6 +9,7 @@

@Getter
@ToString(exclude = {"image"})
@EqualsAndHashCode(doNotUseGetters = true)
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
public class ImageData {
public static final ImageData EMPTY = new ImageData(0, 0, new byte[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = false)
@EqualsAndHashCode(doNotUseGetters = true, callSuper = false)
public class AddBehaviorTreePacket extends BedrockPacket {
private String behaviorTreeJson;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import java.util.List;

@Data
@EqualsAndHashCode(callSuper = false)
@EqualsAndHashCode(doNotUseGetters = true, callSuper = false)
public class AddEntityPacket extends BedrockPacket {
private final List<Attribute> attributes = new ArrayList<>();
private final EntityDataMap metadata = new EntityDataMap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = false)
@EqualsAndHashCode(doNotUseGetters = true, callSuper = false)
public class AddHangingEntityPacket extends BedrockPacket {
private long uniqueEntityId;
private long runtimeEntityId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = false)
@EqualsAndHashCode(doNotUseGetters = true, callSuper = false)
public class AddItemEntityPacket extends BedrockPacket {
private final EntityDataMap metadata = new EntityDataMap();
private long uniqueEntityId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import lombok.ToString;

@Data
@EqualsAndHashCode(callSuper = false)
@EqualsAndHashCode(doNotUseGetters = true, callSuper = false)
@ToString(callSuper = false)
public class AddPaintingPacket extends AddHangingEntityPacket {
private String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import java.util.UUID;

@Data
@EqualsAndHashCode(callSuper = false)
@EqualsAndHashCode(doNotUseGetters = true, callSuper = false)
public class AddPlayerPacket extends BedrockPacket {
private final EntityDataMap metadata = new EntityDataMap();
private final List<EntityLink> entityLinks = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.util.Set;

@Data
@EqualsAndHashCode(callSuper = false)
@EqualsAndHashCode(doNotUseGetters = true, callSuper = false)
public class AdventureSettingsPacket extends BedrockPacket {
private final Set<Flag> flags = new HashSet<>();
private CommandPermission commandPermission;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = false)
@EqualsAndHashCode(doNotUseGetters = true, callSuper = false)
public class AnimatePacket extends BedrockPacket {
private float rowingTime;
private Action action;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = false)
@EqualsAndHashCode(doNotUseGetters = true, callSuper = false)
public class AnvilDamagePacket extends BedrockPacket {
private int damage;
private Vector3i position;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = false)
@EqualsAndHashCode(doNotUseGetters = true, callSuper = false)
public class AutomationClientConnectPacket extends BedrockPacket {
private String address;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import java.util.List;

@Data
@EqualsAndHashCode(callSuper = false)
@EqualsAndHashCode(doNotUseGetters = true, callSuper = false)
public class AvailableCommandsPacket extends BedrockPacket {
private final List<CommandData> commands = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = false)
@EqualsAndHashCode(doNotUseGetters = true, callSuper = false)
public class AvailableEntityIdentifiersPacket extends BedrockPacket {
private Tag<?> tag;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = false)
@EqualsAndHashCode(doNotUseGetters = true, callSuper = false)
public class BiomeDefinitionListPacket extends BedrockPacket {
private Tag<?> tag;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = false)
@EqualsAndHashCode(doNotUseGetters = true, callSuper = false)
public class BlockEntityDataPacket extends BedrockPacket {
private Vector3i blockPosition;
private Tag<?> data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
*
**/
@Data
@EqualsAndHashCode(callSuper = false)
@EqualsAndHashCode(doNotUseGetters = true, callSuper = false)
public class BlockEventPacket extends BedrockPacket {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = false)
@EqualsAndHashCode(doNotUseGetters = true, callSuper = false)
public class BlockPickRequestPacket extends BedrockPacket {
private Vector3i blockPosition;
private boolean addUserData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = false)
@EqualsAndHashCode(doNotUseGetters = true, callSuper = false)
public class BookEditPacket extends BedrockPacket {
private Action action;
private int inventorySlot;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = false)
@EqualsAndHashCode(doNotUseGetters = true, callSuper = false)
public class BossEventPacket extends BedrockPacket {
private long bossUniqueEntityId;
private Action action;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = false)
@EqualsAndHashCode(doNotUseGetters = true, callSuper = false)
public class CameraPacket extends BedrockPacket {
private long cameraUniqueEntityId;
private long playerUniqueEntityId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = false)
@EqualsAndHashCode(doNotUseGetters = true, callSuper = false)
public class ChangeDimensionPacket extends BedrockPacket {
private int dimension;
private Vector3f position;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = false)
@EqualsAndHashCode(doNotUseGetters = true, callSuper = false)
public class ChunkRadiusUpdatedPacket extends BedrockPacket {
private int radius;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
import com.nukkitx.protocol.bedrock.BedrockPacket;
import com.nukkitx.protocol.bedrock.BedrockPacketType;
import com.nukkitx.protocol.bedrock.handler.BedrockPacketHandler;
import gnu.trove.list.TLongList;
import gnu.trove.list.array.TLongArrayList;
import it.unimi.dsi.fastutil.longs.LongArrayList;
import it.unimi.dsi.fastutil.longs.LongList;
import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = false)
@EqualsAndHashCode(doNotUseGetters = true, callSuper = false)
public class ClientCacheBlobStatusPacket extends BedrockPacket {
private final TLongList acks = new TLongArrayList();
private final TLongList naks = new TLongArrayList();
private final LongList acks = new LongArrayList();
private final LongList naks = new LongArrayList();

@Override
public boolean handle(BedrockPacketHandler handler) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
import com.nukkitx.protocol.bedrock.BedrockPacket;
import com.nukkitx.protocol.bedrock.BedrockPacketType;
import com.nukkitx.protocol.bedrock.handler.BedrockPacketHandler;
import gnu.trove.map.TLongObjectMap;
import gnu.trove.map.hash.TLongObjectHashMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = false)
@EqualsAndHashCode(doNotUseGetters = true, callSuper = false)
public class ClientCacheMissResponsePacket extends BedrockPacket {
private final TLongObjectMap<byte[]> blobs = new TLongObjectHashMap<>();
private final Long2ObjectMap<byte[]> blobs = new Long2ObjectOpenHashMap<>();

@Override
public boolean handle(BedrockPacketHandler handler) {
Expand Down
Loading

0 comments on commit c2f48c1

Please sign in to comment.