Improve new Items system

This commit is contained in:
Bixilon 2020-07-12 12:43:46 +02:00
parent 904c0c6a19
commit a2ca7646d0
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
3 changed files with 27 additions and 35 deletions

View File

@ -1,17 +0,0 @@
/*
* Codename 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.game.datatypes.blocks;
public class Block {
}

View File

@ -16,13 +16,12 @@ package de.bixilon.minosoft.game.datatypes.entities;
import de.bixilon.minosoft.protocol.protocol.ProtocolVersion;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.*;
public class Items {
// ProtocolVersion-> itemId << 4 | metaData, Identifier
static List<String> identifiers = new ArrayList<>();
static HashMap<ProtocolVersion, HashMap<Integer, String>> itemProtocolMap = new HashMap<>();
public static String getIdentifierByLegacy(int id, int metaData, ProtocolVersion version) {
@ -30,7 +29,12 @@ public class Items {
if (metaData > 0 && metaData <= 15) {
itemId |= metaData;
}
return getIdentifier(itemId, version);
String identifier = getIdentifier(itemId, version);
if (identifier == null) {
// ignore meta data?
return getIdentifier(id << 4, version);
}
return identifier;
}
public static String getIdentifier(int id, ProtocolVersion version) {
@ -42,22 +46,25 @@ public class Items {
public static void load(ProtocolVersion version, JSONObject json) {
HashMap<Integer, String> versionMapping = new HashMap<>();
if (version.getVersionNumber() <= ProtocolVersion.VERSION_1_12_2.getVersionNumber()) {
// old format (with metadata
for (Iterator<String> it = json.keys(); it.hasNext(); ) {
String identifier = it.next();
JSONObject identifierJSON = json.getJSONObject(identifier);
int itemId = identifierJSON.getInt("protocol_id") << 4;
// old format (with metadata
for (Iterator<String> it = json.keys(); it.hasNext(); ) {
String identifier = it.next();
if (identifiers.contains(identifier)) {
identifier = identifiers.get(identifiers.indexOf(identifier));
} else {
identifiers.add(identifier);
}
JSONObject identifierJSON = json.getJSONObject(identifier);
int itemId;
if (version.getVersionNumber() <= ProtocolVersion.VERSION_1_12_2.getVersionNumber()) {
itemId = identifierJSON.getInt("protocol_id") << 4;
if (identifierJSON.has("protocol_meta")) {
itemId |= identifierJSON.getInt("protocol_meta");
}
versionMapping.put(itemId, identifier);
}
} else {
for (Iterator<String> it = json.keys(); it.hasNext(); ) {
String identifier = it.next();
versionMapping.put(json.getJSONObject(identifier).getInt("protocol_id"), identifier);
} else {
itemId = json.getJSONObject(identifier).getInt("protocol_id");
}
versionMapping.put(itemId, identifier);
}
itemProtocolMap.put(version, versionMapping);
}

View File

@ -274,10 +274,12 @@ public class InByteBuffer {
if (id == -1) {
return null;
}
return new Slot(Items.getIdentifier(id, version), readByte(), readShort(), readNBT(version == ProtocolVersion.VERSION_1_7_10)); // compression
byte count = readByte();
short metaData = readShort();
CompoundTag nbt = readNBT(version == ProtocolVersion.VERSION_1_7_10);
return new Slot(Items.getIdentifierByLegacy(id, metaData, version), count, metaData, nbt);
case VERSION_1_13_2:
if (readBoolean()) {
// nothing here
return new Slot(Items.getIdentifier(readVarInt(), version), readByte(), readNBT());
}
}