fix block id world serialization
This commit is contained in:
parent
0f82ccb890
commit
264c7ffae3
4 changed files with 81 additions and 33 deletions
|
@ -22,7 +22,6 @@ import java.util.zip.InflaterInputStream;
|
|||
import common.block.Block;
|
||||
import common.collect.Lists;
|
||||
import common.collect.Maps;
|
||||
import common.collect.Sets;
|
||||
import common.entity.Entity;
|
||||
import common.init.BlockRegistry;
|
||||
import common.init.Blocks;
|
||||
|
@ -38,54 +37,80 @@ import common.world.State;
|
|||
|
||||
public class Region {
|
||||
private static boolean makeMap(TagObject tag) {
|
||||
Set<String> removed = Sets.newHashSet();
|
||||
for(String id : tag.keySet()) {
|
||||
if(tag.hasChar(id))
|
||||
removed.add(id);
|
||||
Map<Character, String> states = Maps.newTreeMap();
|
||||
Map<String, Character> assign = Maps.newHashMap();
|
||||
if(tag.hasStringArray("states")) {
|
||||
String[] ids = tag.getStringArray("states");
|
||||
for(char bid = 1; bid < ids.length; bid++) {
|
||||
String id = ids[bid];
|
||||
if(!id.isEmpty()) {
|
||||
states.put(bid, id);
|
||||
assign.put(id, bid);
|
||||
}
|
||||
}
|
||||
}
|
||||
Map<String, Character> mapping = Maps.newHashMap();
|
||||
Set<Character> taken = Sets.newHashSet();
|
||||
Map<Character, String> mapping = Maps.newHashMap();
|
||||
List<String> missing = Lists.newArrayList();
|
||||
char highest = 0;
|
||||
for(State state : BlockRegistry.states()) {
|
||||
if(state.getBlock() == Blocks.air)
|
||||
continue;
|
||||
String id = BlockRegistry.getName(state);
|
||||
if(tag.hasChar(id)) {
|
||||
char bid = tag.getChar(id);
|
||||
if(assign.containsKey(id)) {
|
||||
char bid = assign.get(id);
|
||||
if(bid == 0) {
|
||||
missing.add(id);
|
||||
continue;
|
||||
}
|
||||
mapping.put(id, bid);
|
||||
taken.add(bid);
|
||||
removed.remove(id);
|
||||
mapping.put(bid, id);
|
||||
highest = bid > highest ? bid : highest;
|
||||
states.remove(bid);
|
||||
Log.IO.debug("Bestehende Block-ID %d = %s", (int)bid, id);
|
||||
}
|
||||
else {
|
||||
missing.add(id);
|
||||
}
|
||||
}
|
||||
for(Entry<Character, String> entry : states.entrySet()) {
|
||||
char bid = entry.getKey();
|
||||
String oid = entry.getValue();
|
||||
State state = BlockRegistry.byName(oid, null);
|
||||
if(state != null) {
|
||||
String id = BlockRegistry.getName(state);
|
||||
mapping.put(bid, id);
|
||||
highest = bid > highest ? bid : highest;
|
||||
missing.remove(id);
|
||||
Log.IO.debug("Geänderte Block-ID %d = %s -> %s", (int)bid, oid, id);
|
||||
}
|
||||
else {
|
||||
Log.IO.debug("Entfernte Block-ID %d = %s", (int)bid, oid);
|
||||
}
|
||||
}
|
||||
char bid = 1;
|
||||
for(String id : missing) {
|
||||
while(taken.contains(bid)) {
|
||||
while(mapping.containsKey(bid)) {
|
||||
++bid;
|
||||
}
|
||||
mapping.put(id, bid);
|
||||
tag.setChar(id, bid);
|
||||
taken.add(bid);
|
||||
mapping.put(bid, id);
|
||||
highest = bid > highest ? bid : highest;
|
||||
Log.IO.debug("Neue Block-ID %d = %s", (int)bid, id);
|
||||
}
|
||||
for(Entry<String, Character> entry : mapping.entrySet()) {
|
||||
bid = entry.getValue();
|
||||
char ids = (char)BlockRegistry.getId(BlockRegistry.byName(entry.getKey(), null));
|
||||
for(Entry<Character, String> entry : mapping.entrySet()) {
|
||||
bid = entry.getKey();
|
||||
char ids = (char)BlockRegistry.getId(BlockRegistry.byName(entry.getValue(), null));
|
||||
DECODE_MAP[bid] = ids;
|
||||
ENCODE_MAP[ids] = bid;
|
||||
}
|
||||
for(String id : removed) {
|
||||
tag.remove(id);
|
||||
Log.IO.debug("Entfernte Block-ID %d = %s", (int)tag.getChar(id), id);
|
||||
if(!missing.isEmpty() || !states.isEmpty()) {
|
||||
String[] ids = new String[highest + 1];
|
||||
ids[0] = BlockRegistry.getName(Blocks.air.getState());
|
||||
for(bid = 1; bid < ids.length; bid++) {
|
||||
ids[bid] = mapping.getOrDefault(bid, "");
|
||||
}
|
||||
tag.setStringArray("states", ids);
|
||||
return true;
|
||||
}
|
||||
return !missing.isEmpty() || !removed.isEmpty();
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void loadMap() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue