fix enchantment reading in lower versions, other fixes

Remove nullBlock (replace with null)
chunk block replacement fixes
Create NumberTag in NBT
This commit is contained in:
Bixilon 2020-12-02 17:46:11 +01:00
parent 2bf78b63da
commit e38e45c20b
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
12 changed files with 100 additions and 24 deletions

View File

@ -27,7 +27,7 @@ import java.util.HashMap;
public class Slot {
final Item item;
final HashMap<Enchantment, Short> enchantments = new HashMap<>();
final HashMap<Enchantment, Integer> enchantments = new HashMap<>();
final ArrayList<ChatComponent> lore = new ArrayList<>();
int itemCount;
short itemMetadata;
@ -90,11 +90,11 @@ public class Slot {
if (nbt.containsKey("Enchantments")) {
for (CompoundTag enchantment : nbt.getListTag("Enchantments").<CompoundTag>getValue()) {
String[] spilittedIdentifier = enchantment.getStringTag("id").getValue().split(":");
enchantments.put(new Enchantment(spilittedIdentifier[0], spilittedIdentifier[1]), enchantment.getShortTag("lvl").getValue());
enchantments.put(new Enchantment(spilittedIdentifier[0], spilittedIdentifier[1]), enchantment.getNumberTag("lvl").getAsInt());
}
} else if (nbt.containsKey("ench")) {
for (CompoundTag enchantment : nbt.getListTag("ench").<CompoundTag>getValue()) {
enchantments.put(mapping.getEnchantmentById(enchantment.getShortTag("id").getValue()), enchantment.getShortTag("lvl").getValue());
enchantments.put(mapping.getEnchantmentById(enchantment.getNumberTag("id").getAsInt()), enchantment.getNumberTag("lvl").getAsInt());
}
}
}
@ -130,7 +130,7 @@ public class Slot {
enchantments.forEach((id, level) -> {
CompoundTag tag = new CompoundTag();
tag.writeTag("id", new StringTag(id.toString()));
tag.writeTag("lvl", new ShortTag(level));
tag.writeTag("lvl", new ShortTag(level.shortValue()));
enchantmentList.getValue().add(tag);
});
nbt.writeTag("Enchantments", enchantmentList);
@ -139,7 +139,7 @@ public class Slot {
enchantments.forEach((id, level) -> {
CompoundTag tag = new CompoundTag();
tag.writeTag("id", new ShortTag((short) (int) mapping.getIdByEnchantment(id)));
tag.writeTag("lvl", new ShortTag(level));
tag.writeTag("lvl", new ShortTag(level.shortValue()));
enchantmentList.getValue().add(tag);
});
nbt.writeTag("ench", enchantmentList);
@ -257,7 +257,7 @@ public class Slot {
return BitByte.isBitSet(hideFlags, 6);
}
public HashMap<Enchantment, Short> getEnchantments() {
public HashMap<Enchantment, Integer> getEnchantments() {
return enchantments;
}

View File

@ -21,7 +21,6 @@ import de.bixilon.minosoft.config.StaticConfiguration;
import java.util.HashSet;
public class Blocks {
public static final Block nullBlock = new Block("air");
public static HashBiMap<Integer, Block> load(String mod, JsonObject json, boolean metaData) {
HashBiMap<Integer, Block> versionMapping = HashBiMap.create();

View File

@ -55,7 +55,13 @@ public class ChunkSection {
public void setBlock(InChunkSectionLocation location, Block block) {
Block current = blocks.get(location);
if (current == null || current.equals(block)) {
if (block == null) {
blocks.remove(location);
blockEntityMeta.remove(location);
return;
}
if (current.equals(block)) {
blockEntityMeta.remove(location);
return;
}
blocks.put(location, block);

View File

@ -17,8 +17,8 @@ import de.bixilon.minosoft.data.entities.block.BlockEntityMetaData;
import de.bixilon.minosoft.data.entities.entities.Entity;
import de.bixilon.minosoft.data.mappings.Dimension;
import de.bixilon.minosoft.data.mappings.blocks.Block;
import de.bixilon.minosoft.data.mappings.blocks.Blocks;
import javax.annotation.Nullable;
import java.util.HashMap;
/**
@ -35,12 +35,13 @@ public class World {
return chunks;
}
@Nullable
public Block getBlock(BlockPosition pos) {
ChunkLocation loc = pos.getChunkLocation();
if (getChunk(loc) != null) {
return getChunk(loc).getBlock(pos.getInChunkLocation());
}
return Blocks.nullBlock;
return null;
}
public Chunk getChunk(ChunkLocation loc) {

View File

@ -18,7 +18,6 @@ import de.bixilon.minosoft.config.ConfigurationPaths;
import de.bixilon.minosoft.data.GameModes;
import de.bixilon.minosoft.data.entities.entities.Entity;
import de.bixilon.minosoft.data.entities.entities.player.PlayerEntity;
import de.bixilon.minosoft.data.mappings.blocks.Blocks;
import de.bixilon.minosoft.data.mappings.recipes.Recipes;
import de.bixilon.minosoft.data.mappings.versions.Version;
import de.bixilon.minosoft.data.mappings.versions.Versions;
@ -247,7 +246,11 @@ public class PacketHandler {
if (connection.fireEvent(event)) {
return;
}
Log.game("[CHAT] " + event.getMessage());
Log.game(switch (pkg.getPosition()) {
case SYSTEM_MESSAGE -> "[SYSTEM] ";
case ABOVE_HOTBAR -> "[HOTBAR] ";
default -> "[CHAT] ";
} + event.getMessage());
}
public void handle(PacketDisconnect pkg) {
@ -553,7 +556,7 @@ public class PacketHandler {
int y = ((int) pkg.getLocation().getY()) + record[1];
int z = ((int) pkg.getLocation().getZ()) + record[2];
BlockPosition blockPosition = new BlockPosition(x, (short) y, z);
connection.getPlayer().getWorld().setBlock(blockPosition, Blocks.nullBlock);
connection.getPlayer().getWorld().setBlock(blockPosition, null);
}
// ToDo: motion support
}

View File

@ -14,7 +14,6 @@
package de.bixilon.minosoft.util;
import de.bixilon.minosoft.data.mappings.blocks.Block;
import de.bixilon.minosoft.data.mappings.blocks.Blocks;
import de.bixilon.minosoft.data.world.Chunk;
import de.bixilon.minosoft.data.world.ChunkSection;
import de.bixilon.minosoft.data.world.InChunkSectionLocation;
@ -76,11 +75,12 @@ public final class ChunkUtil {
}
}
// ToDo light, biome
Block block = buffer.getConnection().getMapping().getBlockById((singeBlockId << 4) | singleMeta);
if (block.equals(Blocks.nullBlock)) {
int fullBlockId = (singeBlockId << 4) | singleMeta;
if (fullBlockId == ProtocolDefinition.NULL_BLOCK_ID) {
arrayPos++;
continue;
}
Block block = buffer.getConnection().getMapping().getBlockById(fullBlockId);
blockMap.put(new InChunkSectionLocation(nibbleX, nibbleY, nibbleZ), block);
arrayPos++;
}
@ -129,7 +129,7 @@ public final class ChunkUtil {
Log.warn("Unknown block: %d", blockId);
}
*/
if (block == null || block.equals(Blocks.nullBlock)) {
if (block == null) {
arrayPos++;
continue;
}
@ -177,6 +177,9 @@ public final class ChunkUtil {
Block block = palette.byId(blockId);
if (block == null) {
if (blockId == ProtocolDefinition.NULL_BLOCK_ID) {
continue;
}
String blockName;
if (buffer.getVersionId() <= ProtocolDefinition.FLATTING_VERSION_ID) {
blockName = String.format("%d:%d", blockId >> 4, blockId & 0xF);
@ -186,9 +189,6 @@ public final class ChunkUtil {
Log.warn(String.format("Server sent unknown block: %s", blockName));
continue;
}
if (block.equals(Blocks.nullBlock)) {
continue;
}
blockMap.put(new InChunkSectionLocation(nibbleX, nibbleY, nibbleZ), block);
}
}

View File

@ -16,7 +16,7 @@ package de.bixilon.minosoft.util.nbt.tag;
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
import de.bixilon.minosoft.protocol.protocol.OutByteBuffer;
public class ByteTag extends NBTTag {
public class ByteTag extends NumberTag {
final byte value;
public ByteTag(byte value) {
@ -49,4 +49,9 @@ public class ByteTag extends NBTTag {
public String toString() {
return value + "b";
}
@Override
public long getAsLong() {
return value;
}
}

View File

@ -133,6 +133,10 @@ public class CompoundTag extends NBTTag {
return (CompoundTag) data.get(key);
}
public NumberTag getNumberTag(String key) {
return (NumberTag) data.get(key);
}
public NBTTag getTag(String key) {
return data.get(key);
}

View File

@ -16,7 +16,7 @@ package de.bixilon.minosoft.util.nbt.tag;
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
import de.bixilon.minosoft.protocol.protocol.OutByteBuffer;
public class IntTag extends NBTTag {
public class IntTag extends NumberTag {
final int value;
public IntTag(int value) {
@ -45,4 +45,9 @@ public class IntTag extends NBTTag {
public String toString() {
return String.valueOf(value);
}
@Override
public long getAsLong() {
return value;
}
}

View File

@ -16,7 +16,7 @@ package de.bixilon.minosoft.util.nbt.tag;
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
import de.bixilon.minosoft.protocol.protocol.OutByteBuffer;
public class LongTag extends NBTTag {
public class LongTag extends NumberTag {
final long value;
public LongTag(long value) {
@ -45,4 +45,9 @@ public class LongTag extends NBTTag {
public String toString() {
return value + "l";
}
@Override
public long getAsLong() {
return value;
}
}

View File

@ -0,0 +1,43 @@
/*
* Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program.If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.util.nbt.tag;
public abstract class NumberTag extends NBTTag {
public byte getAsByte() {
return (byte) getAsLong();
}
public short getAsShort() {
return (short) getAsLong();
}
public int getAsInt() {
return (int) getAsLong();
}
public abstract long getAsLong();
public int getAsUnsignedByte() {
return getAsShort();
}
public int getAsUnsignedShort() {
return getAsShort();
}
public long getAsUnsignedInt() {
return getAsInt();
}
}

View File

@ -16,7 +16,7 @@ package de.bixilon.minosoft.util.nbt.tag;
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
import de.bixilon.minosoft.protocol.protocol.OutByteBuffer;
public class ShortTag extends NBTTag {
public class ShortTag extends NumberTag {
final short value;
public ShortTag(short value) {
@ -45,4 +45,9 @@ public class ShortTag extends NBTTag {
public String toString() {
return value + "s";
}
@Override
public long getAsLong() {
return value;
}
}