From 310252037269d0a5d3007d15d1ee515cfe38cad5 Mon Sep 17 00:00:00 2001 From: Unknown Date: Sat, 27 Apr 2019 13:18:38 +0200 Subject: [PATCH] Improved item model registration - more generic item handling - fix double item block registration --- .../warpdrive/block/BlockAbstractBase.java | 5 +--- .../block/BlockAbstractContainer.java | 4 +-- .../block/decoration/BlockAbstractLamp.java | 7 +++++ .../block/decoration/ItemBlockLamp.java | 30 +++++++++++++++++++ .../warpdrive/block/hull/BlockHullGlass.java | 5 +--- .../warpdrive/block/hull/BlockHullSlab.java | 5 +--- .../warpdrive/block/hull/BlockHullStairs.java | 5 +--- .../cr0s/warpdrive/client/ClientProxy.java | 22 ++++++++++---- 8 files changed, 58 insertions(+), 25 deletions(-) create mode 100644 src/main/java/cr0s/warpdrive/block/decoration/ItemBlockLamp.java diff --git a/src/main/java/cr0s/warpdrive/block/BlockAbstractBase.java b/src/main/java/cr0s/warpdrive/block/BlockAbstractBase.java index 4ad5baed..7feae0a0 100644 --- a/src/main/java/cr0s/warpdrive/block/BlockAbstractBase.java +++ b/src/main/java/cr0s/warpdrive/block/BlockAbstractBase.java @@ -3,13 +3,11 @@ package cr0s.warpdrive.block; import cr0s.warpdrive.Commons; import cr0s.warpdrive.WarpDrive; import cr0s.warpdrive.api.IBlockBase; -import cr0s.warpdrive.client.ClientProxy; import net.minecraft.block.Block; import net.minecraft.block.SoundType; import net.minecraft.block.material.Material; import net.minecraft.block.state.IBlockState; import net.minecraft.entity.EntityLivingBase; -import net.minecraft.item.Item; import net.minecraft.item.ItemBlock; import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumHand; @@ -55,8 +53,7 @@ public abstract class BlockAbstractBase extends Block implements IBlockBase { @SideOnly(Side.CLIENT) @Override public void modelInitialisation() { - final Item item = Item.getItemFromBlock(this); - ClientProxy.modelInitialisation(item); + // no operation /* // Force a single model through a custom state mapper diff --git a/src/main/java/cr0s/warpdrive/block/BlockAbstractContainer.java b/src/main/java/cr0s/warpdrive/block/BlockAbstractContainer.java index 5424e017..ec313d01 100644 --- a/src/main/java/cr0s/warpdrive/block/BlockAbstractContainer.java +++ b/src/main/java/cr0s/warpdrive/block/BlockAbstractContainer.java @@ -6,7 +6,6 @@ import cr0s.warpdrive.api.IBlockBase; import cr0s.warpdrive.api.IBlockUpdateDetector; import cr0s.warpdrive.api.IVideoChannel; import cr0s.warpdrive.api.WarpDriveText; -import cr0s.warpdrive.client.ClientProxy; import cr0s.warpdrive.config.WarpDriveConfig; import cr0s.warpdrive.data.EnumComponentType; import cr0s.warpdrive.data.EnumTier; @@ -81,8 +80,7 @@ public abstract class BlockAbstractContainer extends BlockContainer implements I @SideOnly(Side.CLIENT) @Override public void modelInitialisation() { - final Item item = Item.getItemFromBlock(this); - ClientProxy.modelInitialisation(item); + // no operation } @SuppressWarnings("deprecation") diff --git a/src/main/java/cr0s/warpdrive/block/decoration/BlockAbstractLamp.java b/src/main/java/cr0s/warpdrive/block/decoration/BlockAbstractLamp.java index 481d8064..25d25746 100644 --- a/src/main/java/cr0s/warpdrive/block/decoration/BlockAbstractLamp.java +++ b/src/main/java/cr0s/warpdrive/block/decoration/BlockAbstractLamp.java @@ -13,6 +13,7 @@ import net.minecraft.block.state.BlockStateContainer; import net.minecraft.block.state.IBlockState; import net.minecraft.client.util.ITooltipFlag; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemBlock; import net.minecraft.item.ItemStack; import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumHand; @@ -67,6 +68,12 @@ public class BlockAbstractLamp extends BlockAbstractBase { | (blockState.getValue(BlockProperties.FACING).getIndex()); } + @Nullable + @Override + public ItemBlock createItemBlock() { + return new ItemBlockLamp(this); + } + @SuppressWarnings("deprecation") @SideOnly(Side.CLIENT) @Override diff --git a/src/main/java/cr0s/warpdrive/block/decoration/ItemBlockLamp.java b/src/main/java/cr0s/warpdrive/block/decoration/ItemBlockLamp.java new file mode 100644 index 00000000..b413b28a --- /dev/null +++ b/src/main/java/cr0s/warpdrive/block/decoration/ItemBlockLamp.java @@ -0,0 +1,30 @@ +package cr0s.warpdrive.block.decoration; + +import cr0s.warpdrive.block.ItemBlockAbstractBase; + +import javax.annotation.Nonnull; + +import net.minecraft.block.Block; +import net.minecraft.client.renderer.block.model.ModelResourceLocation; +import net.minecraft.item.ItemStack; +import net.minecraft.util.ResourceLocation; + +import net.minecraftforge.fml.relauncher.Side; +import net.minecraftforge.fml.relauncher.SideOnly; + +public class ItemBlockLamp extends ItemBlockAbstractBase { + + public ItemBlockLamp(final Block block) { + super(block, true, false); + } + + @Nonnull + @SideOnly(Side.CLIENT) + @Override + public ModelResourceLocation getModelResourceLocation(final ItemStack itemStack) { + final ResourceLocation resourceLocation = getRegistryName(); + assert resourceLocation != null; + final String variant = "inventory"; + return new ModelResourceLocation(resourceLocation, variant); + } +} diff --git a/src/main/java/cr0s/warpdrive/block/hull/BlockHullGlass.java b/src/main/java/cr0s/warpdrive/block/hull/BlockHullGlass.java index 78632c42..fa222996 100644 --- a/src/main/java/cr0s/warpdrive/block/hull/BlockHullGlass.java +++ b/src/main/java/cr0s/warpdrive/block/hull/BlockHullGlass.java @@ -3,7 +3,6 @@ package cr0s.warpdrive.block.hull; import cr0s.warpdrive.WarpDrive; import cr0s.warpdrive.api.IBlockBase; import cr0s.warpdrive.api.IDamageReceiver; -import cr0s.warpdrive.client.ClientProxy; import cr0s.warpdrive.config.WarpDriveConfig; import cr0s.warpdrive.data.EnumTier; import cr0s.warpdrive.data.Vector3; @@ -16,7 +15,6 @@ import net.minecraft.block.BlockGlass; import net.minecraft.block.SoundType; import net.minecraft.block.material.EnumPushReaction; import net.minecraft.block.material.Material; -import net.minecraft.item.Item; import net.minecraft.item.ItemBlock; import net.minecraft.item.ItemStack; import net.minecraft.block.state.IBlockState; @@ -84,8 +82,7 @@ public class BlockHullGlass extends BlockColored implements IBlockBase, IDamageR @SideOnly(Side.CLIENT) @Override public void modelInitialisation() { - final Item item = Item.getItemFromBlock(this); - ClientProxy.modelInitialisation(item); + // no operation } @SuppressWarnings("deprecation") diff --git a/src/main/java/cr0s/warpdrive/block/hull/BlockHullSlab.java b/src/main/java/cr0s/warpdrive/block/hull/BlockHullSlab.java index ad1930b0..24b7d471 100644 --- a/src/main/java/cr0s/warpdrive/block/hull/BlockHullSlab.java +++ b/src/main/java/cr0s/warpdrive/block/hull/BlockHullSlab.java @@ -3,7 +3,6 @@ package cr0s.warpdrive.block.hull; import cr0s.warpdrive.WarpDrive; import cr0s.warpdrive.api.IBlockBase; import cr0s.warpdrive.api.IDamageReceiver; -import cr0s.warpdrive.client.ClientProxy; import cr0s.warpdrive.config.WarpDriveConfig; import cr0s.warpdrive.data.EnumTier; import cr0s.warpdrive.data.Vector3; @@ -25,7 +24,6 @@ import net.minecraft.block.state.IBlockState; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.entity.EntityLivingBase; import net.minecraft.item.EnumDyeColor; -import net.minecraft.item.Item; import net.minecraft.item.ItemBlock; import net.minecraft.item.ItemStack; import net.minecraft.util.DamageSource; @@ -316,8 +314,7 @@ public class BlockHullSlab extends BlockSlab implements IBlockBase, IDamageRecei @SideOnly(Side.CLIENT) @Override public void modelInitialisation() { - final Item item = Item.getItemFromBlock(this); - ClientProxy.modelInitialisation(item); + // no operation } @Override diff --git a/src/main/java/cr0s/warpdrive/block/hull/BlockHullStairs.java b/src/main/java/cr0s/warpdrive/block/hull/BlockHullStairs.java index 62486633..c5f99b38 100644 --- a/src/main/java/cr0s/warpdrive/block/hull/BlockHullStairs.java +++ b/src/main/java/cr0s/warpdrive/block/hull/BlockHullStairs.java @@ -3,7 +3,6 @@ package cr0s.warpdrive.block.hull; import cr0s.warpdrive.WarpDrive; import cr0s.warpdrive.api.IBlockBase; import cr0s.warpdrive.api.IDamageReceiver; -import cr0s.warpdrive.client.ClientProxy; import cr0s.warpdrive.config.WarpDriveConfig; import cr0s.warpdrive.data.EnumTier; import cr0s.warpdrive.data.Vector3; @@ -15,7 +14,6 @@ import net.minecraft.block.BlockStairs; import net.minecraft.block.material.EnumPushReaction; import net.minecraft.block.state.IBlockState; import net.minecraft.item.EnumDyeColor; -import net.minecraft.item.Item; import net.minecraft.item.ItemBlock; import net.minecraft.item.ItemStack; import net.minecraft.util.DamageSource; @@ -70,8 +68,7 @@ public class BlockHullStairs extends BlockStairs implements IBlockBase, IDamageR @SideOnly(Side.CLIENT) @Override public void modelInitialisation() { - final Item item = Item.getItemFromBlock(this); - ClientProxy.modelInitialisation(item); + // no operation } @Override diff --git a/src/main/java/cr0s/warpdrive/client/ClientProxy.java b/src/main/java/cr0s/warpdrive/client/ClientProxy.java index fc19c492..f9602fcb 100644 --- a/src/main/java/cr0s/warpdrive/client/ClientProxy.java +++ b/src/main/java/cr0s/warpdrive/client/ClientProxy.java @@ -12,7 +12,11 @@ import cr0s.warpdrive.render.*; import javax.annotation.Nonnull; +import java.util.Collection; + import net.minecraft.block.Block; +import net.minecraft.block.properties.IProperty; +import net.minecraft.block.state.IBlockState; import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.block.model.ModelResourceLocation; import net.minecraft.item.Item; @@ -84,7 +88,7 @@ public class ClientProxy extends CommonProxy { ResourceLocation resourceLocation = item.getRegistryName(); assert resourceLocation != null; - // reuse blockstate rendering for ItemBlocks + // reuse blockstate rendering for ItemBlocks when their blockstate have at least one property (typically colored blocks) if (item instanceof ItemBlock) { final int damage = itemStack.getItemDamage(); if (damage < 0 || damage > 15) { @@ -92,14 +96,20 @@ public class ClientProxy extends CommonProxy { damage, itemStack.getItem())); } final Block block = ((ItemBlock) item).getBlock(); - final String variant = block.getStateFromMeta(damage).toString().split("[\\[\\]]")[1]; - return new ModelResourceLocation(resourceLocation, variant); + final IBlockState blockState = block.getStateFromMeta(damage); + final Collection> properties = blockState.getPropertyKeys(); + if (!properties.isEmpty()) {// reuse defined properties + final String[] blockStateStrings = blockState.toString().split("[\\[\\]]"); + final String variant = blockStateStrings[1]; + return new ModelResourceLocation(resourceLocation, variant); + } } - // use damage value as suffix for pure items + // use damage value as suffix otherwise if (item.getHasSubtypes()) { resourceLocation = new ResourceLocation(resourceLocation.getNamespace(), resourceLocation.getPath() + "-" + itemStack.getItemDamage()); } + // defaults to inventory variant return new ModelResourceLocation(resourceLocation, "inventory"); } @@ -110,8 +120,8 @@ public class ClientProxy extends CommonProxy { } if (!item.getHasSubtypes()) { - assert item.getRegistryName() != null; - ModelLoader.setCustomModelResourceLocation(item, 0, new ModelResourceLocation(item.getRegistryName(), "inventory")); + final ModelResourceLocation modelResourceLocation = ((IItemBase) item).getModelResourceLocation(new ItemStack(item)); + ModelLoader.setCustomModelResourceLocation(item, 0, modelResourceLocation); } else { final NonNullList listItemStacks = NonNullList.create();