mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-12 00:47:26 -04:00
fix some mappings problems, improve performance
This commit is contained in:
parent
d6fba287c2
commit
d7bf4f527a
@ -18,6 +18,7 @@ import de.bixilon.minosoft.config.Configuration;
|
||||
import de.bixilon.minosoft.config.GameConfiguration;
|
||||
import de.bixilon.minosoft.game.datatypes.Mappings;
|
||||
import de.bixilon.minosoft.game.datatypes.Player;
|
||||
import de.bixilon.minosoft.game.datatypes.objectLoader.blocks.Block;
|
||||
import de.bixilon.minosoft.game.datatypes.objectLoader.blocks.Blocks;
|
||||
import de.bixilon.minosoft.game.datatypes.objectLoader.enchantments.Enchantments;
|
||||
import de.bixilon.minosoft.game.datatypes.objectLoader.entities.Entities;
|
||||
@ -162,6 +163,8 @@ public class Minosoft {
|
||||
}
|
||||
Log.verbose(String.format("Loaded mappings for version %s in %dms (%s)", version, (System.currentTimeMillis() - startTime), version.getReleaseName()));
|
||||
}
|
||||
// end, we must set the nullBlock
|
||||
Blocks.nullBlock = new Block("minecraft", "air");
|
||||
} catch (IOException e) {
|
||||
Log.fatal("Error occurred while loading version mapping: " + e.getLocalizedMessage());
|
||||
System.exit(1);
|
||||
|
@ -13,20 +13,22 @@
|
||||
|
||||
package de.bixilon.minosoft.game.datatypes.objectLoader.blocks;
|
||||
|
||||
import java.util.HashSet;
|
||||
|
||||
public class Block {
|
||||
final String mod;
|
||||
final String identifier;
|
||||
final BlockRotation rotation;
|
||||
final BlockProperties[] properties;
|
||||
final HashSet<BlockProperties> properties;
|
||||
|
||||
public Block(String mod, String identifier, BlockProperties[] properties, BlockRotation rotation) {
|
||||
public Block(String mod, String identifier, HashSet<BlockProperties> properties, BlockRotation rotation) {
|
||||
this.mod = mod;
|
||||
this.identifier = identifier;
|
||||
this.properties = properties;
|
||||
this.rotation = rotation;
|
||||
}
|
||||
|
||||
public Block(String mod, String identifier, BlockProperties[] properties) {
|
||||
public Block(String mod, String identifier, HashSet<BlockProperties> properties) {
|
||||
this.mod = mod;
|
||||
this.identifier = identifier;
|
||||
this.properties = properties;
|
||||
@ -36,14 +38,14 @@ public class Block {
|
||||
public Block(String mod, String identifier, BlockRotation rotation) {
|
||||
this.mod = mod;
|
||||
this.identifier = identifier;
|
||||
this.properties = new BlockProperties[0];
|
||||
this.properties = new HashSet<>();
|
||||
this.rotation = rotation;
|
||||
}
|
||||
|
||||
public Block(String mod, String identifier) {
|
||||
this.mod = mod;
|
||||
this.identifier = identifier;
|
||||
this.properties = new BlockProperties[0];
|
||||
this.properties = new HashSet<>();
|
||||
this.rotation = BlockRotation.NONE;
|
||||
}
|
||||
|
||||
@ -59,7 +61,7 @@ public class Block {
|
||||
return rotation;
|
||||
}
|
||||
|
||||
public BlockProperties[] getProperties() {
|
||||
public HashSet<BlockProperties> getProperties() {
|
||||
return properties;
|
||||
}
|
||||
|
||||
@ -71,7 +73,7 @@ public class Block {
|
||||
out.append("rotation=");
|
||||
out.append(getRotation());
|
||||
}
|
||||
if (properties.length > 0) {
|
||||
if (properties.size() > 0) {
|
||||
if (out.length() > 0) {
|
||||
out.append(" ,");
|
||||
} else {
|
||||
@ -94,7 +96,11 @@ public class Block {
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return mod.hashCode() * identifier.hashCode();
|
||||
int ret = mod.hashCode() * identifier.hashCode() * rotation.hashCode();
|
||||
if (properties.size() > 0) {
|
||||
ret *= properties.hashCode();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -102,7 +108,10 @@ public class Block {
|
||||
if (super.equals(obj)) {
|
||||
return true;
|
||||
}
|
||||
if (hashCode() != obj.hashCode()) {
|
||||
return false;
|
||||
}
|
||||
Block their = (Block) obj;
|
||||
return getMod().equals(their.getMod()) && getIdentifier().equals(their.getIdentifier()) && getRotation() == their.getRotation() && Blocks.propertiesEquals(getProperties(), their.getProperties());
|
||||
return getIdentifier().equals(their.getIdentifier()) && getRotation() == their.getRotation() && getProperties().equals(their.getProperties()) && getMod().equals(their.getMod());
|
||||
}
|
||||
}
|
||||
|
@ -17,12 +17,12 @@ import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
import de.bixilon.minosoft.protocol.protocol.ProtocolVersion;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
|
||||
public class Blocks {
|
||||
public static Block nullBlock;
|
||||
static ArrayList<Block> blockList = new ArrayList<>();
|
||||
static HashSet<Block> blockList = new HashSet<>();
|
||||
static HashMap<ProtocolVersion, HashBiMap<Integer, Block>> blockMap = new HashMap<>(); // version -> (protocolId > block)
|
||||
static HashMap<String, HashMap<String, BlockProperties>> propertiesMapping = new HashMap<>();
|
||||
static HashMap<String, BlockRotation> rotationMapping = new HashMap<>();
|
||||
@ -460,6 +460,7 @@ public class Blocks {
|
||||
JsonArray statesArray = identifierJSON.getAsJsonArray("states");
|
||||
for (int i = 0; i < statesArray.size(); i++) {
|
||||
JsonObject statesJSON = statesArray.get(i).getAsJsonObject();
|
||||
Block block;
|
||||
if (statesJSON.has("properties")) {
|
||||
// properties are optional
|
||||
JsonObject propertiesJSON = statesJSON.getAsJsonObject("properties");
|
||||
@ -471,8 +472,7 @@ public class Blocks {
|
||||
rotation = rotationMapping.get(propertiesJSON.get("rotation").getAsString());
|
||||
propertiesJSON.remove("rotation");
|
||||
}
|
||||
BlockProperties[] properties = new BlockProperties[propertiesJSON.size()];
|
||||
int ii = 0;
|
||||
HashSet<BlockProperties> properties = new HashSet<>();
|
||||
for (String propertyName : propertiesJSON.keySet()) {
|
||||
if (propertiesMapping.get(propertyName) == null) {
|
||||
throw new RuntimeException(String.format("Unknown block property: %s (identifier=%s)", propertyName, identifierName));
|
||||
@ -480,51 +480,24 @@ public class Blocks {
|
||||
if (propertiesMapping.get(propertyName).get(propertiesJSON.get(propertyName).getAsString()) == null) {
|
||||
throw new RuntimeException(String.format("Unknown block property: %s -> %s (identifier=%s)", propertyName, propertiesJSON.get(propertyName).getAsString(), identifierName));
|
||||
}
|
||||
properties[ii] = propertiesMapping.get(propertyName).get(propertiesJSON.get(propertyName).getAsString());
|
||||
ii++;
|
||||
properties.add(propertiesMapping.get(propertyName).get(propertiesJSON.get(propertyName).getAsString()));
|
||||
}
|
||||
|
||||
Block block = new Block(mod, identifierName, properties, rotation);
|
||||
if (blockList.contains(block)) {
|
||||
block = blockList.get(blockList.indexOf(block));
|
||||
}
|
||||
|
||||
if (block == null) {
|
||||
// does not exist. create
|
||||
block = new Block(mod, identifierName, properties, rotation);
|
||||
blockList.add(block);
|
||||
}
|
||||
|
||||
// set nullBlock
|
||||
if (block.getIdentifier().equals("air")) {
|
||||
nullBlock = block;
|
||||
}
|
||||
|
||||
int blockId = getBlockId(statesJSON, version);
|
||||
checkAndCrashIfBlockIsIn(blockId, identifierName, versionMapping, version);
|
||||
versionMapping.put(blockId, block);
|
||||
block = new Block(mod, identifierName, properties, rotation);
|
||||
} else {
|
||||
// no properties, directly add block
|
||||
Block block = new Block(mod, identifierName);
|
||||
if (blockList.contains(block)) {
|
||||
block = blockList.get(blockList.indexOf(block));
|
||||
}
|
||||
|
||||
if (block == null) {
|
||||
// does not exist. create
|
||||
block = new Block(mod, identifierName);
|
||||
blockList.add(block);
|
||||
}
|
||||
|
||||
int blockId = getBlockId(statesJSON, version);
|
||||
checkAndCrashIfBlockIsIn(blockId, identifierName, versionMapping, version);
|
||||
versionMapping.put(blockId, block);
|
||||
block = new Block(mod, identifierName);
|
||||
}
|
||||
int blockId = getBlockId(statesJSON, version);
|
||||
checkAndCrashIfBlockIsIn(blockId, identifierName, versionMapping, version);
|
||||
versionMapping.put(blockId, block);
|
||||
blockList.add(block);
|
||||
}
|
||||
}
|
||||
blockMap.put(version, versionMapping);
|
||||
}
|
||||
|
||||
|
||||
private static int getBlockId(JsonObject json, ProtocolVersion version) {
|
||||
int blockId = json.get("id").getAsInt();
|
||||
if (version.getVersionNumber() <= ProtocolVersion.VERSION_1_12_2.getVersionNumber()) {
|
||||
@ -537,27 +510,6 @@ public class Blocks {
|
||||
return blockId;
|
||||
}
|
||||
|
||||
public static boolean propertiesEquals(BlockProperties[] one, BlockProperties[] two) {
|
||||
if (one.length != two.length) {
|
||||
return false;
|
||||
}
|
||||
for (BlockProperties property : one) {
|
||||
if (!containsElement(two, property)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean containsElement(BlockProperties[] arr, BlockProperties value) {
|
||||
for (BlockProperties property : arr) {
|
||||
if (property == value) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void checkAndCrashIfBlockIsIn(int blockId, String identifierName, HashBiMap<Integer, Block> versionMapping, ProtocolVersion version) {
|
||||
if (versionMapping.containsKey(blockId)) {
|
||||
String blockIdString;
|
||||
|
@ -40,6 +40,7 @@ public class Enchantment {
|
||||
if (super.equals(obj)) {
|
||||
return true;
|
||||
}
|
||||
return toString().equals(obj.toString());
|
||||
Enchantment their = (Enchantment) obj;
|
||||
return getIdentifier().equals(their.getIdentifier()) && getMod().equals(their.getMod());
|
||||
}
|
||||
}
|
||||
|
@ -17,15 +17,13 @@ import com.google.common.collect.HashBiMap;
|
||||
import com.google.gson.JsonObject;
|
||||
import de.bixilon.minosoft.protocol.protocol.ProtocolVersion;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class Enchantments {
|
||||
|
||||
static ArrayList<Enchantment> enchantmentList = new ArrayList<>();
|
||||
static HashMap<ProtocolVersion, HashBiMap<Integer, Enchantment>> enchantmentMap = new HashMap<>();
|
||||
|
||||
public static Enchantment getEnchantmentBdyId(int protocolId, ProtocolVersion version) {
|
||||
public static Enchantment getEnchantmentById(int protocolId, ProtocolVersion version) {
|
||||
if (version.getVersionNumber() < ProtocolVersion.VERSION_1_12_2.getVersionNumber()) {
|
||||
version = ProtocolVersion.VERSION_1_12_2;
|
||||
}
|
||||
@ -36,9 +34,6 @@ public class Enchantments {
|
||||
HashBiMap<Integer, Enchantment> versionMapping = HashBiMap.create();
|
||||
for (String identifierName : json.keySet()) {
|
||||
Enchantment enchantment = new Enchantment(mod, identifierName);
|
||||
if (enchantmentList.contains(enchantment)) {
|
||||
enchantment = enchantmentList.get(enchantmentList.indexOf(enchantment));
|
||||
}
|
||||
versionMapping.put(json.getAsJsonObject(identifierName).get("id").getAsInt(), enchantment);
|
||||
}
|
||||
enchantmentMap.put(version, versionMapping);
|
||||
|
@ -34,4 +34,21 @@ public class Item {
|
||||
public String toString() {
|
||||
return String.format("%s:%s", getMod(), getIdentifier());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return mod.hashCode() * identifier.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (super.equals(obj)) {
|
||||
return true;
|
||||
}
|
||||
if (hashCode() != obj.hashCode()) {
|
||||
return false;
|
||||
}
|
||||
Item their = (Item) obj;
|
||||
return getIdentifier().equals(their.getIdentifier()) && getMod().equals(their.getMod());
|
||||
}
|
||||
}
|
||||
|
@ -17,12 +17,12 @@ import com.google.common.collect.HashBiMap;
|
||||
import com.google.gson.JsonObject;
|
||||
import de.bixilon.minosoft.protocol.protocol.ProtocolVersion;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
|
||||
public class Items {
|
||||
|
||||
static ArrayList<Item> itemList = new ArrayList<>();
|
||||
static HashSet<Item> itemList = new HashSet<>();
|
||||
static HashMap<ProtocolVersion, HashBiMap<Integer, Item>> itemMap = new HashMap<>(); // version -> (protocolId > Item)
|
||||
|
||||
public static Item getItemByLegacy(int protocolId, int protocolMetaData) {
|
||||
@ -45,12 +45,8 @@ public class Items {
|
||||
public static void load(String mod, JsonObject json, ProtocolVersion version) {
|
||||
HashBiMap<Integer, Item> versionMapping = HashBiMap.create();
|
||||
for (String identifierName : json.keySet()) {
|
||||
Item item = getItem(mod, identifierName);
|
||||
if (item == null) {
|
||||
// does not exist. create
|
||||
item = new Item(mod, identifierName);
|
||||
itemList.add(item);
|
||||
}
|
||||
Item item = new Item(mod, identifierName);
|
||||
itemList.add(item);
|
||||
JsonObject identifierJSON = json.getAsJsonObject(identifierName);
|
||||
int itemId = identifierJSON.get("id").getAsInt();
|
||||
if (version.getVersionNumber() <= ProtocolVersion.VERSION_1_12_2.getVersionNumber()) {
|
||||
@ -66,12 +62,7 @@ public class Items {
|
||||
}
|
||||
|
||||
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;
|
||||
return new Item(mod, identifier);
|
||||
}
|
||||
|
||||
public static int getItemId(Item item, ProtocolVersion version) {
|
||||
|
@ -17,12 +17,10 @@ import com.google.common.collect.HashBiMap;
|
||||
import com.google.gson.JsonObject;
|
||||
import de.bixilon.minosoft.protocol.protocol.ProtocolVersion;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class Statistics {
|
||||
|
||||
static ArrayList<Statistic> statisticList = new ArrayList<>();
|
||||
static HashMap<ProtocolVersion, HashBiMap<Integer, Statistic>> statisticsIdMap = new HashMap<>();
|
||||
static HashMap<ProtocolVersion, HashBiMap<String, Statistic>> statisticsIdentifierMap = new HashMap<>();
|
||||
|
||||
@ -45,9 +43,6 @@ public class Statistics {
|
||||
HashBiMap<String, Statistic> versionIdentifierMapping = HashBiMap.create();
|
||||
for (String identifierName : json.keySet()) {
|
||||
Statistic statistic = new Statistic(mod, identifierName);
|
||||
if (statisticList.contains(statistic)) {
|
||||
statistic = statisticList.get(statisticList.indexOf(statistic));
|
||||
}
|
||||
if (json.getAsJsonObject(identifierName).has("id")) {
|
||||
versionIdMapping.put(json.getAsJsonObject(identifierName).get("id").getAsInt(), statistic);
|
||||
} else {
|
||||
|
@ -82,7 +82,7 @@ public class ChunkUtil {
|
||||
|
||||
// ToDo light, biome
|
||||
Block block = Blocks.getBlockByLegacy(singeBlockId, singleMeta);
|
||||
if (block == Blocks.nullBlock) {
|
||||
if (block.equals(Blocks.nullBlock)) {
|
||||
arrayPos++;
|
||||
continue;
|
||||
}
|
||||
@ -130,7 +130,7 @@ public class ChunkUtil {
|
||||
for (int nibbleZ = 0; nibbleZ < 16; nibbleZ++) {
|
||||
for (int nibbleX = 0; nibbleX < 16; nibbleX++) {
|
||||
Block block = Blocks.getBlockByLegacy(blockData[arrayPos]);
|
||||
if (block == Blocks.nullBlock) {
|
||||
if (block.equals(Blocks.nullBlock)) {
|
||||
arrayPos++;
|
||||
continue;
|
||||
}
|
||||
@ -186,7 +186,7 @@ public class ChunkUtil {
|
||||
blockId &= individualValueMask;
|
||||
|
||||
Block block = palette.byId(blockId);
|
||||
if (block == Blocks.nullBlock) {
|
||||
if (block.equals(Blocks.nullBlock)) {
|
||||
continue;
|
||||
}
|
||||
blockMap.put(new ChunkNibbleLocation(nibbleX, nibbleY, nibbleZ), block);
|
||||
|
Loading…
x
Reference in New Issue
Block a user