mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-18 03:44:54 -04:00
completely restructured Item save
This commit is contained in:
parent
76e17e0a24
commit
360eeee423
5
pom.xml
5
pom.xml
@ -50,6 +50,11 @@
|
||||
<artifactId>snakeyaml</artifactId>
|
||||
<version>1.25</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>29.0-jre</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -16,7 +16,7 @@ package de.bixilon.minosoft;
|
||||
import de.bixilon.minosoft.config.Configuration;
|
||||
import de.bixilon.minosoft.config.GameConfiguration;
|
||||
import de.bixilon.minosoft.game.datatypes.Player;
|
||||
import de.bixilon.minosoft.game.datatypes.entities.Items;
|
||||
import de.bixilon.minosoft.game.datatypes.entities.items.Items;
|
||||
import de.bixilon.minosoft.logging.Log;
|
||||
import de.bixilon.minosoft.logging.LogLevel;
|
||||
import de.bixilon.minosoft.mojang.api.MojangAccount;
|
||||
@ -30,6 +30,7 @@ import org.json.JSONObject;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
@ -132,13 +133,24 @@ public class Minosoft {
|
||||
// skip them, use mapping of 1.12
|
||||
continue;
|
||||
}
|
||||
String fileName;
|
||||
if (version.getVersionNumber() >= ProtocolVersion.VERSION_1_14_4.getVersionNumber()) {
|
||||
JSONObject data = Util.readJsonFromFile(Config.homeDir + String.format("assets/mapping/%s/registries.json", version.getVersionString()));
|
||||
Items.load(version, data.getJSONObject("minecraft:item").getJSONObject("entries"));
|
||||
fileName = Config.homeDir + String.format("assets/mapping/%s/registries.json", version.getVersionString());
|
||||
} else {
|
||||
fileName = Config.homeDir + String.format("assets/mapping/%s/items.json", version.getVersionString());
|
||||
}
|
||||
JSONObject data = Util.readJsonFromFile(fileName);
|
||||
for (Iterator<String> mods = data.keys(); mods.hasNext(); ) {
|
||||
// key = mod name
|
||||
String mod = mods.next();
|
||||
JSONObject modJSON = data.getJSONObject(mod);
|
||||
|
||||
if (version.getVersionNumber() >= ProtocolVersion.VERSION_1_14_4.getVersionNumber()) {
|
||||
Items.load(mod, modJSON.getJSONObject("item").getJSONObject("entries"), version);
|
||||
} else {
|
||||
// special rule: multiple files for registers
|
||||
Items.load(version, Util.readJsonFromFile(Config.homeDir + String.format("assets/mapping/%s/items.json", version.getVersionString())));
|
||||
|
||||
Items.load(mod, modJSON, version);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IOException | JSONException e) {
|
||||
|
@ -1,84 +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.entities;
|
||||
|
||||
import de.bixilon.minosoft.protocol.protocol.ProtocolVersion;
|
||||
import org.json.JSONObject;
|
||||
|
||||
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) {
|
||||
int itemId = id << 4;
|
||||
if (metaData > 0 && metaData <= 15) {
|
||||
itemId |= metaData;
|
||||
}
|
||||
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) {
|
||||
if (version.getVersionNumber() < ProtocolVersion.VERSION_1_12_2.getVersionNumber()) {
|
||||
version = ProtocolVersion.VERSION_1_12_2;
|
||||
}
|
||||
return itemProtocolMap.get(version).get(id);
|
||||
}
|
||||
|
||||
public static void load(ProtocolVersion version, JSONObject json) {
|
||||
HashMap<Integer, String> versionMapping = new HashMap<>();
|
||||
// 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");
|
||||
}
|
||||
} else {
|
||||
itemId = json.getJSONObject(identifier).getInt("protocol_id");
|
||||
}
|
||||
versionMapping.put(itemId, identifier);
|
||||
}
|
||||
itemProtocolMap.put(version, versionMapping);
|
||||
}
|
||||
|
||||
public static int getItemId(String identifier, ProtocolVersion version) {
|
||||
if (version.getVersionNumber() < ProtocolVersion.VERSION_1_12_2.getVersionNumber()) {
|
||||
version = ProtocolVersion.VERSION_1_12_2;
|
||||
}
|
||||
for (Map.Entry<Integer, String> identifierSet : itemProtocolMap.get(version).entrySet()) {
|
||||
if (identifierSet.getValue().equals(identifier)) {
|
||||
return identifierSet.getKey();
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* 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.entities.items;
|
||||
|
||||
public class Item {
|
||||
final String mod;
|
||||
final String identifier;
|
||||
|
||||
public Item(String mod, String identifier) {
|
||||
this.mod = mod;
|
||||
this.identifier = identifier;
|
||||
}
|
||||
|
||||
public String getMod() {
|
||||
return mod;
|
||||
}
|
||||
|
||||
public String getIdentifier() {
|
||||
return identifier;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%s:%s", getMod(), getIdentifier());
|
||||
}
|
||||
}
|
@ -0,0 +1,86 @@
|
||||
/*
|
||||
* 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.entities.items;
|
||||
|
||||
import com.google.common.collect.BiMap;
|
||||
import com.google.common.collect.HashBiMap;
|
||||
import de.bixilon.minosoft.protocol.protocol.ProtocolVersion;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
|
||||
public class Items {
|
||||
|
||||
static ArrayList<Item> itemList = new ArrayList<>();
|
||||
static HashMap<ProtocolVersion, BiMap<Integer, Item>> itemMap = new HashMap<>(); // version -> (protocolId > Item)
|
||||
|
||||
public static Item getItemByLegacy(int protocolId, int protocolMetaData, ProtocolVersion version) {
|
||||
int itemId = protocolId << 4;
|
||||
if (protocolMetaData > 0 && protocolMetaData <= 15) {
|
||||
itemId |= protocolMetaData;
|
||||
}
|
||||
Item item = getItem(itemId, version);
|
||||
if (item == null) {
|
||||
// ignore meta data?
|
||||
return getItem(protocolId << 4, version);
|
||||
}
|
||||
return item;
|
||||
}
|
||||
|
||||
public static Item getItem(int protocolId, ProtocolVersion version) {
|
||||
return itemMap.get(version).get(protocolId);
|
||||
}
|
||||
|
||||
public static void load(String mod, JSONObject json, ProtocolVersion version) {
|
||||
BiMap<Integer, Item> versionMapping = HashBiMap.create();
|
||||
for (Iterator<String> identifiers = json.keys(); identifiers.hasNext(); ) {
|
||||
String identifierName = identifiers.next();
|
||||
Item item = getItem(mod, identifierName);
|
||||
if (item == null) {
|
||||
// does not exist. create
|
||||
item = new Item(mod, identifierName);
|
||||
itemList.add(item);
|
||||
}
|
||||
JSONObject identifierJSON = json.getJSONObject(identifierName);
|
||||
int itemId;
|
||||
if (version.getVersionNumber() <= ProtocolVersion.VERSION_1_12_2.getVersionNumber()) {
|
||||
// old format (with metadata)
|
||||
itemId = identifierJSON.getInt("protocol_id") << 4;
|
||||
if (identifierJSON.has("protocol_meta")) {
|
||||
itemId |= identifierJSON.getInt("protocol_meta");
|
||||
}
|
||||
} else {
|
||||
itemId = identifierJSON.getInt("protocol_id");
|
||||
}
|
||||
versionMapping.put(itemId, item);
|
||||
}
|
||||
itemMap.put(version, versionMapping);
|
||||
}
|
||||
|
||||
public static Item getItem(String mod, String identifier) {
|
||||
for (Item item : itemList) {
|
||||
if (item.getMod().equals(mod) && item.getIdentifier().equals(identifier)) {
|
||||
return item;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static int getItemId(Item item, ProtocolVersion version) {
|
||||
return itemMap.get(version).inverse().get(item);
|
||||
}
|
||||
|
||||
}
|
@ -14,35 +14,36 @@
|
||||
package de.bixilon.minosoft.game.datatypes.inventory;
|
||||
|
||||
import de.bixilon.minosoft.game.datatypes.TextComponent;
|
||||
import de.bixilon.minosoft.game.datatypes.entities.Items;
|
||||
import de.bixilon.minosoft.game.datatypes.entities.items.Item;
|
||||
import de.bixilon.minosoft.game.datatypes.entities.items.Items;
|
||||
import de.bixilon.minosoft.nbt.tag.CompoundTag;
|
||||
import de.bixilon.minosoft.protocol.protocol.ProtocolVersion;
|
||||
|
||||
public class Slot {
|
||||
String identifier;
|
||||
Item item;
|
||||
int itemCount;
|
||||
short itemMetadata;
|
||||
CompoundTag nbt;
|
||||
|
||||
public Slot(String identifier, int itemCount, CompoundTag nbt) {
|
||||
this.identifier = identifier;
|
||||
public Slot(Item item, int itemCount, CompoundTag nbt) {
|
||||
this.item = item;
|
||||
this.itemCount = itemCount;
|
||||
this.nbt = nbt;
|
||||
}
|
||||
|
||||
public Slot(String identifier, byte itemCount, short itemMetadata, CompoundTag nbt) {
|
||||
this.identifier = identifier;
|
||||
public Slot(Item item, byte itemCount, short itemMetadata, CompoundTag nbt) {
|
||||
this.item = item;
|
||||
this.itemMetadata = itemMetadata;
|
||||
this.itemCount = itemCount;
|
||||
this.nbt = nbt;
|
||||
}
|
||||
|
||||
public String getIdentifier() {
|
||||
return this.identifier;
|
||||
public Item getItem() {
|
||||
return item;
|
||||
}
|
||||
|
||||
public int getItemId(ProtocolVersion version) {
|
||||
return Items.getItemId(identifier, version);
|
||||
return Items.getItemId(item, version);
|
||||
}
|
||||
|
||||
public int getItemCount() {
|
||||
@ -59,8 +60,8 @@ public class Slot {
|
||||
|
||||
public String getDisplayName() {
|
||||
if (nbt != null && nbt.containsKey("display") && nbt.getCompoundTag("display").containsKey("Name")) { // check if object has nbt data, and a custom display name
|
||||
return String.format("%s (%s)", new TextComponent(nbt.getCompoundTag("display").getStringTag("Name").getValue()).getColoredMessage(), identifier);
|
||||
return String.format("%s (%s)", new TextComponent(nbt.getCompoundTag("display").getStringTag("Name").getValue()).getColoredMessage(), item.toString());
|
||||
}
|
||||
return identifier; // ToDo display name per Item (from language file)
|
||||
return item.toString(); // ToDo display name per Item (from language file)
|
||||
}
|
||||
}
|
||||
|
@ -15,9 +15,9 @@ package de.bixilon.minosoft.protocol.protocol;
|
||||
|
||||
import de.bixilon.minosoft.game.datatypes.Direction;
|
||||
import de.bixilon.minosoft.game.datatypes.TextComponent;
|
||||
import de.bixilon.minosoft.game.datatypes.entities.Items;
|
||||
import de.bixilon.minosoft.game.datatypes.entities.Location;
|
||||
import de.bixilon.minosoft.game.datatypes.entities.Pose;
|
||||
import de.bixilon.minosoft.game.datatypes.entities.items.Items;
|
||||
import de.bixilon.minosoft.game.datatypes.entities.meta.EntityMetaData;
|
||||
import de.bixilon.minosoft.game.datatypes.inventory.Slot;
|
||||
import de.bixilon.minosoft.game.datatypes.particle.*;
|
||||
@ -277,10 +277,10 @@ public class InByteBuffer {
|
||||
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);
|
||||
return new Slot(Items.getItemByLegacy(id, metaData, version), count, metaData, nbt);
|
||||
case VERSION_1_13_2:
|
||||
if (readBoolean()) {
|
||||
return new Slot(Items.getIdentifier(readVarInt(), version), readByte(), readNBT());
|
||||
return new Slot(Items.getItem(readVarInt(), version), readByte(), readNBT());
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
@ -18,6 +18,7 @@ import de.bixilon.minosoft.game.datatypes.world.Chunk;
|
||||
import de.bixilon.minosoft.game.datatypes.world.ChunkNibble;
|
||||
import de.bixilon.minosoft.game.datatypes.world.ChunkNibbleLocation;
|
||||
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
|
||||
import de.bixilon.minosoft.protocol.protocol.ProtocolVersion;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
@ -158,7 +159,7 @@ public class ChunkUtil {
|
||||
if (bitsPerBlock < 4) {
|
||||
bitsPerBlock = 4;
|
||||
} else if (bitsPerBlock > 8) {
|
||||
bitsPerBlock = 13;
|
||||
bitsPerBlock = (byte) ((buffer.getVersion().getVersionNumber() >= ProtocolVersion.VERSION_1_13_2.getVersionNumber()) ? 14 : 13);
|
||||
}
|
||||
boolean usePalette = (bitsPerBlock <= 8);
|
||||
|
||||
@ -202,8 +203,13 @@ public class ChunkUtil {
|
||||
// you're probably reading light data instead
|
||||
blockId = palette[blockId];
|
||||
}
|
||||
|
||||
Blocks block = Blocks.byId(blockId >>> 4, blockId & 0xF);
|
||||
Blocks block;
|
||||
if (buffer.getVersion().getVersionNumber() >= ProtocolVersion.VERSION_1_13_2.getVersionNumber()) {
|
||||
// no meta data anymore
|
||||
block = Blocks.byId(blockId);
|
||||
} else {
|
||||
block = Blocks.byId(blockId >>> 4, blockId & 0xF);
|
||||
}
|
||||
if (block == Blocks.AIR) {
|
||||
continue;
|
||||
}
|
||||
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user