Fix some 1.8 bugs

Fix chunk reading bug
Fix InventorySlots::byId bug
VersionMapping NullPointerException
This commit is contained in:
Bixilon 2020-11-25 21:49:18 +01:00
parent e515b019e4
commit 36548e9004
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
12 changed files with 42 additions and 248 deletions

View File

@ -110,7 +110,11 @@ public class InventorySlots {
@Override
public int getId(int versionId) {
return valueMap.get(versionId);
Integer value = valueMap.get(versionId);
if (value == null) {
return Integer.MIN_VALUE;
}
return value;
}
}

View File

@ -38,23 +38,23 @@ public class VersionMapping {
private final HashSet<Mappings> loaded = new HashSet<>();
private Version version;
private VersionMapping parentMapping;
private HashBiMap<String, Motive> motiveIdentifierMap;
private HashBiMap<String, Particle> particleIdentifierMap;
private HashBiMap<String, Statistic> statisticIdentifierMap;
private HashBiMap<Integer, Item> itemMap;
private HashBiMap<Integer, Motive> motiveIdMap;
private HashBiMap<Integer, MobEffect> mobEffectMap;
private HashBiMap<Integer, Dimension> dimensionMap;
private final HashBiMap<Class<? extends Entity>, EntityInformation> entityInformationMap = HashBiMap.create();
private final HashMap<EntityMetaDataFields, Integer> entityMetaIndexMap = new HashMap<>();
private final HashMap<String, Pair<String, Integer>> entityMetaIndexOffsetParentMapping = new HashMap<>();
private final HashBiMap<Integer, Class<? extends Entity>> entityIdClassMap = HashBiMap.create();
private HashBiMap<String, Motive> motiveIdentifierMap = HashBiMap.create();
private HashBiMap<String, Particle> particleIdentifierMap = HashBiMap.create();
private HashBiMap<String, Statistic> statisticIdentifierMap = HashBiMap.create();
private HashMap<String, HashBiMap<String, Dimension>> dimensionIdentifierMap = new HashMap<>();
private HashBiMap<Integer, Block> blockMap;
private HashBiMap<Integer, BlockId> blockIdMap;
private HashBiMap<Integer, Enchantment> enchantmentMap;
private HashBiMap<Integer, Particle> particleIdMap;
private HashBiMap<Integer, Statistic> statisticIdMap;
private HashBiMap<Class<? extends Entity>, EntityInformation> entityInformationMap;
private HashMap<EntityMetaDataFields, Integer> entityMetaIndexMap;
private HashMap<String, Pair<String, Integer>> entityMetaIndexOffsetParentMapping;
private HashBiMap<Integer, Class<? extends Entity>> entityIdClassMap;
private HashBiMap<Integer, Item> itemMap = HashBiMap.create();
private HashBiMap<Integer, Motive> motiveIdMap = HashBiMap.create();
private HashBiMap<Integer, MobEffect> mobEffectMap = HashBiMap.create();
private HashBiMap<Integer, Dimension> dimensionMap = HashBiMap.create();
private HashBiMap<Integer, Block> blockMap = HashBiMap.create();
private HashBiMap<Integer, BlockId> blockIdMap = HashBiMap.create();
private HashBiMap<Integer, Enchantment> enchantmentMap = HashBiMap.create();
private HashBiMap<Integer, Particle> particleIdMap = HashBiMap.create();
private HashBiMap<Integer, Statistic> statisticIdMap = HashBiMap.create();
public VersionMapping(Version version) {
this.version = version;
@ -153,7 +153,11 @@ public class VersionMapping {
return dimensionMap.get(versionId);
}
@Nullable
public Block getBlockById(int versionId) {
if (versionId == ProtocolDefinition.NULL_BLOCK_ID) {
return null;
}
if (parentMapping != null) {
Block block = parentMapping.getBlockById(versionId);
if (block != null) {
@ -302,16 +306,6 @@ public class VersionMapping {
dimensionMap = Versions.PRE_FLATTENING_MAPPING.dimensionMap;
break;
}
itemMap = HashBiMap.create();
enchantmentMap = HashBiMap.create();
statisticIdMap = HashBiMap.create();
statisticIdentifierMap = HashBiMap.create();
blockIdMap = HashBiMap.create();
motiveIdMap = HashBiMap.create();
motiveIdentifierMap = HashBiMap.create();
particleIdMap = HashBiMap.create();
particleIdentifierMap = HashBiMap.create();
mobEffectMap = HashBiMap.create();
if (data == null) {
break;
@ -387,17 +381,11 @@ public class VersionMapping {
}
if (data == null) {
blockMap = HashBiMap.create();
break;
}
blockMap = Blocks.load(mod, data, !version.isFlattened());
}
case ENTITIES -> {
entityInformationMap = HashBiMap.create();
entityMetaIndexMap = new HashMap<>();
entityMetaIndexOffsetParentMapping = new HashMap<>();
entityIdClassMap = HashBiMap.create();
if (data == null) {
break;
}

View File

@ -41,8 +41,8 @@ public class PacketChunkData implements ClientboundPacket {
if (buffer.getVersionId() < 23) {
this.location = new ChunkLocation(buffer.readInt(), buffer.readInt());
boolean groundUpContinuous = buffer.readBoolean();
short sectionBitMask = buffer.readShort();
short addBitMask = buffer.readShort();
int sectionBitMask = buffer.readUnsignedShort();
int addBitMask = buffer.readUnsignedShort();
// decompress chunk data
InByteBuffer decompressed;
@ -60,15 +60,14 @@ public class PacketChunkData implements ClientboundPacket {
boolean groundUpContinuous = buffer.readBoolean();
int sectionBitMask;
if (buffer.getVersionId() < 60) {
sectionBitMask = buffer.readShort();
sectionBitMask = buffer.readUnsignedShort();
} else {
sectionBitMask = buffer.readInt();
}
int size = buffer.readVarInt();
int lastPos = buffer.getPosition();
chunk = ChunkUtil.readChunkPacket(buffer, sectionBitMask, 0, groundUpContinuous, containsSkyLight);
buffer.setPosition(size + lastPos);
chunk = ChunkUtil.readChunkPacket(buffer, (short) sectionBitMask, (short) 0, groundUpContinuous, containsSkyLight);
return true;
}
this.location = new ChunkLocation(buffer.readInt(), buffer.readInt());
@ -99,7 +98,7 @@ public class PacketChunkData implements ClientboundPacket {
int lastPos = buffer.getPosition();
if (size > 0) {
chunk = ChunkUtil.readChunkPacket(buffer, (short) sectionBitMask, (short) 0, groundUpContinuous, containsSkyLight);
chunk = ChunkUtil.readChunkPacket(buffer, sectionBitMask, 0, groundUpContinuous, containsSkyLight);
// set position of the byte buffer, because of some reasons HyPixel makes some weird stuff and sends way to much 0 bytes. (~ 190k), thanks @pokechu22
buffer.setPosition(size + lastPos);
}

View File

@ -29,22 +29,15 @@ public class PacketEntityEquipment implements ClientboundPacket {
@Override
public boolean read(InByteBuffer buffer) {
if (buffer.getVersionId() < 7) {
entityId = buffer.readInt();
slots.put(InventorySlots.EntityInventorySlots.byId(buffer.readShort(), buffer.getVersionId()), buffer.readSlot());
return true;
}
entityId = buffer.readEntityId();
if (buffer.getVersionId() < 49) {
entityId = buffer.readVarInt();
slots.put(InventorySlots.EntityInventorySlots.byId(buffer.readShort(), buffer.getVersionId()), buffer.readSlot());
return true;
}
if (buffer.getVersionId() < 732) {
entityId = buffer.readVarInt();
slots.put(InventorySlots.EntityInventorySlots.byId(buffer.readVarInt(), buffer.getVersionId()), buffer.readSlot());
return true;
}
entityId = buffer.readVarInt();
boolean slotAvailable = true;
while (slotAvailable) {
int slotId = buffer.readByte();

View File

@ -123,8 +123,8 @@ public class InByteBuffer {
return readByte() == 1;
}
public short[] readLEShorts(int num) {
short[] ret = new short[num];
public int[] readUnsignedLEShorts(int num) {
int[] ret = new int[num];
for (int i = 0; i < ret.length; i++) {
ret[i] = (short) (readByte() & 0xFF);
ret[i] |= (readByte() & 0xFF) << 8;

View File

@ -39,6 +39,8 @@ public final class ProtocolDefinition {
public static final int DEFAULT_BUFFER_SIZE = 4096;
public static final int NULL_BLOCK_ID = 0;
static {
// java does (why ever) not allow to directly assign a null
InetAddress temp;

View File

@ -23,10 +23,10 @@ public final class BitByte {
return ((in & mask) == mask);
}
public static byte getBitCount(short input) {
public static byte getBitCount(int input) {
byte ret = 0;
for (byte i = 0; i < Short.BYTES * 8; i++) { // bytes to bits
if (isBitSetShort(input, i)) {
if (isBitSet(input, i)) {
ret++;
}
}

View File

@ -26,7 +26,7 @@ import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition;
import java.util.HashMap;
public final class ChunkUtil {
public static Chunk readChunkPacket(InByteBuffer buffer, short sectionBitMask, short addBitMask, boolean groundUpContinuous, boolean containsSkyLight) {
public static Chunk readChunkPacket(InByteBuffer buffer, int sectionBitMask, int addBitMask, boolean groundUpContinuous, boolean containsSkyLight) {
if (buffer.getVersionId() < 23) {
if (sectionBitMask == 0x00 && groundUpContinuous) {
// unload chunk
@ -100,7 +100,7 @@ public final class ChunkUtil {
int totalBlocks = 4096 * sections; // 16 * 16 * 16 * sections; Section Width * Section Height * Section Width * sections
int halfBytes = totalBlocks / 2; // half bytes
short[] blockData = buffer.readLEShorts(totalBlocks); // blocks >>> 4, data & 0xF
int[] blockData = buffer.readUnsignedLEShorts(totalBlocks); // blocks >>> 4, data & 0xF
byte[] light = buffer.readBytes(halfBytes);
byte[] skyLight = null;
@ -122,9 +122,9 @@ public final class ChunkUtil {
for (int nibbleY = 0; nibbleY < 16; nibbleY++) {
for (int nibbleZ = 0; nibbleZ < 16; nibbleZ++) {
for (int nibbleX = 0; nibbleX < 16; nibbleX++) {
int blockId = blockData[arrayPos] & 0xFFFF;
int blockId = blockData[arrayPos];
Block block = buffer.getConnection().getMapping().getBlockById(blockId);
if (block.equals(Blocks.nullBlock)) {
if (block == null || block.equals(Blocks.nullBlock)) {
arrayPos++;
continue;
}

File diff suppressed because one or more lines are too long