From 264c7ffae3b5e0caf0b3d90e4a5b1040ac6d7149 Mon Sep 17 00:00:00 2001 From: Sen Date: Sat, 28 Jun 2025 14:01:46 +0200 Subject: [PATCH] fix block id world serialization --- .../common/block/foliage/BlockLeaves.java | 15 ++-- .../java/common/block/foliage/BlockLog.java | 2 +- .../main/java/common/init/BlockRegistry.java | 26 ++++++- server/src/main/java/server/world/Region.java | 71 +++++++++++++------ 4 files changed, 81 insertions(+), 33 deletions(-) diff --git a/common/src/main/java/common/block/foliage/BlockLeaves.java b/common/src/main/java/common/block/foliage/BlockLeaves.java index 7fec8c86..ed543aa9 100755 --- a/common/src/main/java/common/block/foliage/BlockLeaves.java +++ b/common/src/main/java/common/block/foliage/BlockLeaves.java @@ -36,6 +36,7 @@ import common.world.AWorldServer; public class BlockLeaves extends BlockLeavesBase { public static final PropertyBool DECAY = PropertyBool.create("decay"); + public static final PropertyBool BUSH = PropertyBool.create("bush"); public static final List LEAVES = Lists.newArrayList(); 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); this.type = type; 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.setTab(CheatTab.PLANTS); this.setHardness(0.2F); @@ -87,7 +88,7 @@ public class BlockLeaves extends BlockLeavesBase BlockPos blockpos = pos.add(j1, k1, l1); 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); } @@ -100,7 +101,7 @@ public class BlockLeaves extends BlockLeavesBase public void updateTick(AWorldServer worldIn, BlockPos pos, State state, Random rand) { 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; } if(Vars.leafDry && worldIn.getTemperatureC(pos) >= 50.0f) { @@ -109,7 +110,7 @@ public class BlockLeaves extends BlockLeavesBase } // 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 j = i + 1; @@ -305,7 +306,7 @@ public class BlockLeaves extends BlockLeavesBase } 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) { @@ -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) { - return this.getState().withProperty(DECAY, false); + return this.getState().withProperty(DECAY, false).withProperty(BUSH, true); } public Model getModel(ModelProvider provider, String name, State state) { @@ -327,7 +328,7 @@ public class BlockLeaves extends BlockLeavesBase } public Property[] getIgnoredProperties() { - return new Property[] {DECAY}; + return new Property[] {DECAY, BUSH}; } protected Item getItemToRegister() { diff --git a/common/src/main/java/common/block/foliage/BlockLog.java b/common/src/main/java/common/block/foliage/BlockLog.java index eca7705e..8ff716f5 100755 --- a/common/src/main/java/common/block/foliage/BlockLog.java +++ b/common/src/main/java/common/block/foliage/BlockLog.java @@ -38,7 +38,7 @@ public class BlockLog extends BlockRotatedPillar { 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); } diff --git a/common/src/main/java/common/init/BlockRegistry.java b/common/src/main/java/common/init/BlockRegistry.java index 5d2ce292..f4ed4473 100755 --- a/common/src/main/java/common/init/BlockRegistry.java +++ b/common/src/main/java/common/init/BlockRegistry.java @@ -142,6 +142,7 @@ import common.color.DyeColor; import common.item.CheatTab; import common.log.Log; import common.model.TextureAnimation; +import common.properties.Property; import common.util.Util; import common.world.State; @@ -190,9 +191,30 @@ public abstract class BlockRegistry { State state = STATE_MAP.get(name); if(state != null) return state; - int idx = name.indexOf(","); + int idx = name.indexOf(','); 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) { diff --git a/server/src/main/java/server/world/Region.java b/server/src/main/java/server/world/Region.java index 52f84ff6..c63bf57c 100755 --- a/server/src/main/java/server/world/Region.java +++ b/server/src/main/java/server/world/Region.java @@ -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 removed = Sets.newHashSet(); - for(String id : tag.keySet()) { - if(tag.hasChar(id)) - removed.add(id); + Map states = Maps.newTreeMap(); + Map 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 mapping = Maps.newHashMap(); - Set taken = Sets.newHashSet(); + Map mapping = Maps.newHashMap(); List 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 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 entry : mapping.entrySet()) { - bid = entry.getValue(); - char ids = (char)BlockRegistry.getId(BlockRegistry.byName(entry.getKey(), null)); + for(Entry 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() {