From 991f9817a9f63487cb14625f4d2340f1ecaa0ed8 Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 2 Jan 2019 20:43:07 +0100 Subject: [PATCH] Added a default transformer for EnumFacing properties --- .../cr0s/warpdrive/api/IBlockTransformer.java | 47 +++++++++++++++++++ .../warpdrive/compat/CompatGalacticraft.java | 41 +--------------- .../java/cr0s/warpdrive/data/JumpBlock.java | 3 ++ 3 files changed, 52 insertions(+), 39 deletions(-) diff --git a/src/main/java/cr0s/warpdrive/api/IBlockTransformer.java b/src/main/java/cr0s/warpdrive/api/IBlockTransformer.java index a7d2bcbc..afc7342b 100644 --- a/src/main/java/cr0s/warpdrive/api/IBlockTransformer.java +++ b/src/main/java/cr0s/warpdrive/api/IBlockTransformer.java @@ -1,11 +1,14 @@ package cr0s.warpdrive.api; import net.minecraft.block.Block; +import net.minecraft.block.properties.IProperty; +import net.minecraft.block.properties.PropertyEnum; import net.minecraft.block.state.IBlockState; import net.minecraft.init.Blocks; import net.minecraft.nbt.NBTBase; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.EnumFacing; import net.minecraft.util.ResourceLocation; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; @@ -55,4 +58,48 @@ public interface IBlockTransformer { } return block; } + + // default rotation using EnumFacing properties used in many blocks + @SuppressWarnings("unchecked") + static int rotateFirstEnumFacingProperty(final Block block, final int metadata, final byte rotationSteps) { + // find first property using EnumFacing + final IBlockState blockState = block.getStateFromMeta(metadata); + PropertyEnum propertyFacing = null; + for (final IProperty propertyKey : blockState.getPropertyKeys()) { + if ( propertyKey instanceof PropertyEnum + && propertyKey.getValueClass() == EnumFacing.class ) { + propertyFacing = (PropertyEnum) propertyKey; + break; + } + } + if (propertyFacing != null) { + final EnumFacing facingOld = blockState.getValue(propertyFacing); + // skip vertical facings + if ( facingOld == EnumFacing.DOWN + || facingOld == EnumFacing.UP ) { + return metadata; + } + + // turn horizontal facings + final EnumFacing facingNew; + switch (rotationSteps) { + case 1: + facingNew = facingOld.rotateY(); + break; + case 2: + facingNew = facingOld.rotateY().rotateY(); + break; + case 3: + facingNew = facingOld.rotateY().rotateY().rotateY(); + break; + default: + facingNew = facingOld; + break; + } + final IBlockState blockStateNew = blockState.withProperty(propertyFacing, facingNew); + return block.getMetaFromState(blockStateNew); + } + + return metadata; + } } \ No newline at end of file diff --git a/src/main/java/cr0s/warpdrive/compat/CompatGalacticraft.java b/src/main/java/cr0s/warpdrive/compat/CompatGalacticraft.java index 85b9981f..02d11960 100644 --- a/src/main/java/cr0s/warpdrive/compat/CompatGalacticraft.java +++ b/src/main/java/cr0s/warpdrive/compat/CompatGalacticraft.java @@ -326,45 +326,8 @@ D- micdoodle8.mods.galacticraft.planets.mars.BlockTelemetry } } - // generic rotation based on EnumFacing properties - final IBlockState blockState = block.getStateFromMeta(metadata); - PropertyEnum propertyFacing = null; - for (final IProperty propertyKey : blockState.getPropertyKeys()) { - if ( propertyKey instanceof PropertyEnum - && propertyKey.getValueClass() == EnumFacing.class ) { - propertyFacing = (PropertyEnum) propertyKey; - break; - } - } - if (propertyFacing != null) { - final EnumFacing facingOld = blockState.getValue(propertyFacing); - // skip vertical facings - if ( facingOld == EnumFacing.DOWN - || facingOld == EnumFacing.UP ) { - return metadata; - } - - // turn horizontal facings - final EnumFacing facingNew; - switch (rotationSteps) { - case 1: - facingNew = facingOld.rotateY(); - break; - case 2: - facingNew = facingOld.rotateY().rotateY(); - break; - case 3: - facingNew = facingOld.rotateY().rotateY().rotateY(); - break; - default: - facingNew = facingOld; - break; - } - final IBlockState blockStateNew = blockState.withProperty(propertyFacing, facingNew); - return block.getMetaFromState(blockStateNew); - } - - return metadata; + // apply default transformer + return IBlockTransformer.rotateFirstEnumFacingProperty(block, metadata, rotationSteps); } @Override diff --git a/src/main/java/cr0s/warpdrive/data/JumpBlock.java b/src/main/java/cr0s/warpdrive/data/JumpBlock.java index d3026215..e0ff9d7e 100644 --- a/src/main/java/cr0s/warpdrive/data/JumpBlock.java +++ b/src/main/java/cr0s/warpdrive/data/JumpBlock.java @@ -259,6 +259,9 @@ public class JumpBlock { default: break; } + } else { + // apply default transformer + return IBlockTransformer.rotateFirstEnumFacingProperty(block, blockMeta, rotationSteps); } switch (rotationSteps) {