fix block id world serialization

This commit is contained in:
Sen 2025-06-28 14:01:46 +02:00
parent 0f82ccb890
commit 264c7ffae3
Signed by: sen
GPG key ID: 3AC50A6F47D1B722
4 changed files with 81 additions and 33 deletions

View file

@ -36,6 +36,7 @@ import common.world.AWorldServer;
public class BlockLeaves extends BlockLeavesBase public class BlockLeaves extends BlockLeavesBase
{ {
public static final PropertyBool DECAY = PropertyBool.create("decay"); public static final PropertyBool DECAY = PropertyBool.create("decay");
public static final PropertyBool BUSH = PropertyBool.create("bush");
public static final List<BlockLeaves> LEAVES = Lists.newArrayList(); public static final List<BlockLeaves> LEAVES = Lists.newArrayList();
private static final BlockLeaves[] MAPPING = new BlockLeaves[WoodType.values().length * LeavesType.values().length]; private static final BlockLeaves[] MAPPING = new BlockLeaves[WoodType.values().length * LeavesType.values().length];
@ -53,7 +54,7 @@ public class BlockLeaves extends BlockLeavesBase
super(Material.LEAVES); super(Material.LEAVES);
this.type = type; this.type = type;
this.subType = subType; this.subType = subType;
this.setDefaultState(this.getBaseState().withProperty(DECAY, Boolean.valueOf(true))); this.setDefaultState(this.getBaseState().withProperty(DECAY, true).withProperty(BUSH, false));
this.setTickRandomly(); this.setTickRandomly();
this.setTab(CheatTab.PLANTS); this.setTab(CheatTab.PLANTS);
this.setHardness(0.2F); this.setHardness(0.2F);
@ -87,7 +88,7 @@ public class BlockLeaves extends BlockLeavesBase
BlockPos blockpos = pos.add(j1, k1, l1); BlockPos blockpos = pos.add(j1, k1, l1);
State iblockstate = worldIn.getState(blockpos); State iblockstate = worldIn.getState(blockpos);
if (iblockstate.getBlock().getMaterial() == Material.LEAVES && !((Boolean)iblockstate.getValue(DECAY)).booleanValue()) if (iblockstate.getBlock().getMaterial() == Material.LEAVES && !iblockstate.getValue(BUSH) && !iblockstate.getValue(DECAY))
{ {
worldIn.setState(blockpos, iblockstate.withProperty(DECAY, Boolean.valueOf(true)), 4); worldIn.setState(blockpos, iblockstate.withProperty(DECAY, Boolean.valueOf(true)), 4);
} }
@ -100,7 +101,7 @@ public class BlockLeaves extends BlockLeavesBase
public void updateTick(AWorldServer worldIn, BlockPos pos, State state, Random rand) public void updateTick(AWorldServer worldIn, BlockPos pos, State state, Random rand)
{ {
if(Vars.seasonLeaves && this.subType != worldIn.getLeavesGen(pos)) { if(Vars.seasonLeaves && this.subType != worldIn.getLeavesGen(pos)) {
worldIn.setState(pos, getLeavesBlock(this.type, worldIn.getLeavesGen(pos)).getState().withProperty(DECAY, state.getValue(DECAY)), 2); worldIn.setState(pos, getLeavesBlock(this.type, worldIn.getLeavesGen(pos)).getState().withProperty(DECAY, state.getValue(DECAY)).withProperty(BUSH, state.getValue(BUSH)), 2);
return; return;
} }
if(Vars.leafDry && worldIn.getTemperatureC(pos) >= 50.0f) { if(Vars.leafDry && worldIn.getTemperatureC(pos) >= 50.0f) {
@ -109,7 +110,7 @@ public class BlockLeaves extends BlockLeavesBase
} }
// if (!worldIn.client) // if (!worldIn.client)
// { // {
else if (Vars.leavesDecay && ((Boolean)state.getValue(DECAY)).booleanValue()) else if (Vars.leavesDecay && !state.getValue(BUSH) && state.getValue(DECAY))
{ {
int i = 4; int i = 4;
int j = i + 1; int j = i + 1;
@ -305,7 +306,7 @@ public class BlockLeaves extends BlockLeavesBase
} }
protected Property[] getProperties() { protected Property[] getProperties() {
return new Property[] {DECAY}; return new Property[] {DECAY, BUSH};
} }
public void harvestBlock(World worldIn, EntityNPC player, BlockPos pos, State state, TileEntity te) { public void harvestBlock(World worldIn, EntityNPC player, BlockPos pos, State state, TileEntity te) {
@ -318,7 +319,7 @@ public class BlockLeaves extends BlockLeavesBase
} }
public State onBlockPlaced(World worldIn, BlockPos pos, Facing facing, float hitX, float hitY, float hitZ, EntityLiving placer) { public State onBlockPlaced(World worldIn, BlockPos pos, Facing facing, float hitX, float hitY, float hitZ, EntityLiving placer) {
return this.getState().withProperty(DECAY, false); return this.getState().withProperty(DECAY, false).withProperty(BUSH, true);
} }
public Model getModel(ModelProvider provider, String name, State state) { public Model getModel(ModelProvider provider, String name, State state) {
@ -327,7 +328,7 @@ public class BlockLeaves extends BlockLeavesBase
} }
public Property<?>[] getIgnoredProperties() { public Property<?>[] getIgnoredProperties() {
return new Property[] {DECAY}; return new Property[] {DECAY, BUSH};
} }
protected Item getItemToRegister() { protected Item getItemToRegister() {

View file

@ -38,7 +38,7 @@ public class BlockLog extends BlockRotatedPillar
{ {
State blk = worldIn.getState(bpos); State blk = worldIn.getState(bpos);
if (blk.getBlock().getMaterial() == Material.LEAVES && !((Boolean)blk.getValue(BlockLeaves.DECAY)).booleanValue()) if (blk.getBlock().getMaterial() == Material.LEAVES && !blk.getValue(BlockLeaves.BUSH) && !blk.getValue(BlockLeaves.DECAY))
{ {
worldIn.setState(bpos, blk.withProperty(BlockLeaves.DECAY, Boolean.valueOf(true)), 4); worldIn.setState(bpos, blk.withProperty(BlockLeaves.DECAY, Boolean.valueOf(true)), 4);
} }

View file

@ -142,6 +142,7 @@ import common.color.DyeColor;
import common.item.CheatTab; import common.item.CheatTab;
import common.log.Log; import common.log.Log;
import common.model.TextureAnimation; import common.model.TextureAnimation;
import common.properties.Property;
import common.util.Util; import common.util.Util;
import common.world.State; import common.world.State;
@ -190,9 +191,30 @@ public abstract class BlockRegistry {
State state = STATE_MAP.get(name); State state = STATE_MAP.get(name);
if(state != null) if(state != null)
return state; return state;
int idx = name.indexOf(","); int idx = name.indexOf(',');
Block block = BlockRegistry.byNameExact(idx >= 0 ? name.substring(0, idx) : name); Block block = BlockRegistry.byNameExact(idx >= 0 ? name.substring(0, idx) : name);
return block != null ? block.getState() : def; if(block == null)
return def;
state = block.getState();
if(idx < 0)
return state;
String[] tok = name.substring(idx + 1).split(",");
for(String str : tok) {
idx = str.indexOf('=');
if(idx <= 0 || idx == str.length() - 1)
continue;
String key = str.substring(0, idx);
String value = str.substring(idx + 1);
for(Property prop : block.getSavedProperties()) {
if(prop.getName().equals(key)) {
for(Object obj : prop.getStates()) {
if(prop.getName((Comparable)obj).equals(value))
state = state.withProperty(prop, (Comparable)obj);
}
}
}
}
return state;
} }
public static String getName(State state) { public static String getName(State state) {

View file

@ -22,7 +22,6 @@ import java.util.zip.InflaterInputStream;
import common.block.Block; import common.block.Block;
import common.collect.Lists; import common.collect.Lists;
import common.collect.Maps; import common.collect.Maps;
import common.collect.Sets;
import common.entity.Entity; import common.entity.Entity;
import common.init.BlockRegistry; import common.init.BlockRegistry;
import common.init.Blocks; import common.init.Blocks;
@ -38,54 +37,80 @@ import common.world.State;
public class Region { public class Region {
private static boolean makeMap(TagObject tag) { private static boolean makeMap(TagObject tag) {
Set<String> removed = Sets.newHashSet(); Map<Character, String> states = Maps.newTreeMap();
for(String id : tag.keySet()) { Map<String, Character> assign = Maps.newHashMap();
if(tag.hasChar(id)) if(tag.hasStringArray("states")) {
removed.add(id); 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(); Map<Character, String> mapping = Maps.newHashMap();
Set<Character> taken = Sets.newHashSet();
List<String> missing = Lists.newArrayList(); List<String> missing = Lists.newArrayList();
char highest = 0;
for(State state : BlockRegistry.states()) { for(State state : BlockRegistry.states()) {
if(state.getBlock() == Blocks.air) if(state.getBlock() == Blocks.air)
continue; continue;
String id = BlockRegistry.getName(state); String id = BlockRegistry.getName(state);
if(tag.hasChar(id)) { if(assign.containsKey(id)) {
char bid = tag.getChar(id); char bid = assign.get(id);
if(bid == 0) { if(bid == 0) {
missing.add(id); missing.add(id);
continue; continue;
} }
mapping.put(id, bid); mapping.put(bid, id);
taken.add(bid); highest = bid > highest ? bid : highest;
removed.remove(id); states.remove(bid);
Log.IO.debug("Bestehende Block-ID %d = %s", (int)bid, id); Log.IO.debug("Bestehende Block-ID %d = %s", (int)bid, id);
} }
else { else {
missing.add(id); 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; char bid = 1;
for(String id : missing) { for(String id : missing) {
while(taken.contains(bid)) { while(mapping.containsKey(bid)) {
++bid; ++bid;
} }
mapping.put(id, bid); mapping.put(bid, id);
tag.setChar(id, bid); highest = bid > highest ? bid : highest;
taken.add(bid);
Log.IO.debug("Neue Block-ID %d = %s", (int)bid, id); Log.IO.debug("Neue Block-ID %d = %s", (int)bid, id);
} }
for(Entry<String, Character> entry : mapping.entrySet()) { for(Entry<Character, String> entry : mapping.entrySet()) {
bid = entry.getValue(); bid = entry.getKey();
char ids = (char)BlockRegistry.getId(BlockRegistry.byName(entry.getKey(), null)); char ids = (char)BlockRegistry.getId(BlockRegistry.byName(entry.getValue(), null));
DECODE_MAP[bid] = ids; DECODE_MAP[bid] = ids;
ENCODE_MAP[ids] = bid; ENCODE_MAP[ids] = bid;
} }
for(String id : removed) { if(!missing.isEmpty() || !states.isEmpty()) {
tag.remove(id); String[] ids = new String[highest + 1];
Log.IO.debug("Entfernte Block-ID %d = %s", (int)tag.getChar(id), id); 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() { public static void loadMap() {