From 8c1a9f460defa29c9086ed467aeded8fbce1f661 Mon Sep 17 00:00:00 2001 From: ZacSharp <68165024+ZacSharp@users.noreply.github.com> Date: Mon, 18 Jan 2021 12:06:50 +0100 Subject: [PATCH] Simple blockstate preservation for buildSubstitutes --- .../api/schematic/SubstituteSchematic.java | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/api/java/baritone/api/schematic/SubstituteSchematic.java b/src/api/java/baritone/api/schematic/SubstituteSchematic.java index 675ccbff..fbc07580 100644 --- a/src/api/java/baritone/api/schematic/SubstituteSchematic.java +++ b/src/api/java/baritone/api/schematic/SubstituteSchematic.java @@ -19,8 +19,10 @@ package baritone.api.schematic; import baritone.api.utils.BlockOptionalMetaLookup; import net.minecraft.block.Block; +import net.minecraft.block.properties.IProperty; import net.minecraft.block.state.IBlockState; import net.minecraft.init.Blocks; +import java.util.Collection; import java.util.List; import java.util.Map; @@ -49,7 +51,7 @@ public class SubstituteSchematic extends AbstractSchematic { } List substitutes = substitutions.get(desiredBlock); if (substitutes.contains(current.getBlock()) && !current.getBlock().equals(Blocks.AIR)) {// don't preserve air, it's almost always there and almost never wanted - return current; + return withBlock(desired, current.getBlock()); } for (Block substitute : substitutes) { if (substitute.equals(Blocks.AIR)) { @@ -57,10 +59,25 @@ public class SubstituteSchematic extends AbstractSchematic { } for (IBlockState placeable : approxPlaceable) { if (substitute.equals(placeable.getBlock())) { - return placeable; + return withBlock(desired, placeable.getBlock()); } } } return substitutes.get(0).getDefaultState(); } + + private IBlockState withBlock(IBlockState state, Block block) { + Collection> properties = state.getPropertyKeys(); + IBlockState newState = block.getDefaultState(); + for (IProperty property : properties) { + try { + newState = copySingleProp(state, newState, property); + } catch (IllegalArgumentException e) { //property does not exist for target block + } + } + return newState; + } + private > IBlockState copySingleProp(IBlockState fromState, IBlockState toState, IProperty prop) { + return toState.withProperty(prop, fromState.getValue(prop)); + } }