Résolu ItemBlock, problème de texture
-
Bonjour à tous, aujourd’hui je voulais passer mon mod en 1.12 et le temps était venue pour moi de refaire l’entier système d’enregistrement des blocs et des items, tout ce passé très bien jusqu’à ce que j’essaye d’enregistrer un bloc à metadata. Voilà le problème quand je lance Minecraft la console m’indique qu’il manque le fichier json test_meta_block qui et le nom normal de mon bloc or il devrait utiliser ces variantes: test_meta_block_red, test_meta_block_green et test_meta_block_blue pour les items. Dans mon inventaires je vois bien ces gros cubes violet et noir (qui sont bien nommé) et une fois que je les places les textures sont correct… Je vous donne mes class:
- Classe des blocs -
package fr.emotion.updatingmod.init; import java.util.HashSet; import fr.emotion.updatingmod.block.TestBlock; import fr.emotion.updatingmod.block.TestMetaBlock; import fr.emotion.updatingmod.block.item.UDTItemBlock; import fr.emotion.updatingmod.util.Reference; import net.minecraft.block.Block; import net.minecraft.item.Item; import net.minecraft.item.ItemBlock; import net.minecraft.util.ResourceLocation; /** * @author EmotionFox */ public final class EmotionBlock { public static final HashSet <block>BLOCKS = new HashSet<block>(); public static final HashSet BLOCKS_ITEMS = new HashSet(); private static final Block TEST_BLOCK = new TestBlock(); private static final Block TEST_META_BLOCK = new TestMetaBlock(); public static final void init() { addBlock(TEST_BLOCK, "test_block"); addBlock(TEST_META_BLOCK, new UDTItemBlock(TEST_META_BLOCK), "test_meta_block"); } /** * Adding block without proper item. * * @param block * @param name */ private static final void addBlock(Block block, String name) { ItemBlock itemBlock = new ItemBlock(block); itemBlock.setRegistryName(new ResourceLocation(Reference.MOD_ID, name)); BLOCKS_ITEMS.add(itemBlock); block.setUnlocalizedName(name); block.setRegistryName(new ResourceLocation(Reference.MOD_ID, name)); BLOCKS.add(block); } /** * Adding block with proper item, always instance of MetaBlock class. * * @param block * @param itemBlock * @param name */ private static final void addBlock(Block block, ItemBlock itemBlock, String name) { itemBlock.setRegistryName(new ResourceLocation(Reference.MOD_ID, name)); BLOCKS_ITEMS.add(itemBlock); block.setUnlocalizedName(name); block.setRegistryName(new ResourceLocation(Reference.MOD_ID, name)); BLOCKS.add(block); } /** * Get all blocks added. * * @return Block[] */ public static final Block[] getBlocks() { final Block[] blocks = new Block[BLOCKS.size()]; int i = 0; for (Block block : BLOCKS) { blocks* = block; i++; } return blocks; } /** * Get all block items added. * * @return Item[] */ public static final Item[] getItems() { final Item[] items = new Item[BLOCKS_ITEMS.size()]; int i = 0; for (Item item : BLOCKS_ITEMS) { items* = item; i++; } return items; } }
- Classe UDTItemBlock -
package fr.emotion.updatingmod.block.item; import net.minecraft.block.Block; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemBlock; import net.minecraft.item.ItemStack; import net.minecraft.util.ActionResult; import net.minecraft.util.EnumHand; import net.minecraft.world.World; /** * @author EmotionFox */ public class UDTItemBlock extends ItemBlock { public UDTItemBlock(Block block) { super(block); if (!(block instanceof MetaBlock)) throw new IllegalArgumentException(String.format("The given Block %s is not an instance of MetaBlock!", block.getUnlocalizedName())); this.setHasSubtypes(true); } @Override public String getUnlocalizedName(ItemStack stack) { return ((MetaBlock) this.block).getName(stack.getMetadata()); } public int getMetadata(int damage) { return damage; } }
- Class des enregistrements -
package fr.emotion.updatingmod.init; import fr.emotion.updatingmod.block.item.MetaBlock; import fr.emotion.updatingmod.util.Reference; import net.minecraft.block.Block; import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.block.model.ModelResourceLocation; import net.minecraft.item.Item; import net.minecraft.util.ResourceLocation; import net.minecraftforge.event.RegistryEvent; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; /** * @author EmotionFox */ @Mod.EventBusSubscriber(modid = Reference.MOD_ID) public final class RegistrationHandler { // Called by Forge. @SubscribeEvent public static void registerBlock(RegistryEvent.Register <block>e) { e.getRegistry().registerAll(EmotionBlock.getBlocks()); } // Called by proxy. public static void registerBlockRender() { for (Block block : EmotionBlock.BLOCKS) { // Check if this block have many meta. if (block instanceof MetaBlock) { MetaBlock metaBlock = (MetaBlock) block; for (int i = 0; i <= metaBlock.getMaxMeta(); i++) { System.out.println("Registering render for: " + metaBlock.getName(i).substring(5)); Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(Item.getItemFromBlock(block), i, new ModelResourceLocation(new ResourceLocation(Reference.MOD_ID, metaBlock.getName(i).substring(5)), "inventory")); } } else { Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(Item.getItemFromBlock(block), 0, new ModelResourceLocation(new ResourceLocation(Reference.MOD_ID, block.getUnlocalizedName().substring(5)), "inventory")); } } } // Called by Forge. @SubscribeEvent public static void registerItem(RegistryEvent.Register e) { e.getRegistry().registerAll(EmotionItem.getItems()); e.getRegistry().registerAll(EmotionBlock.getItems()); } // Called by proxy. public static void registerItemRender() { for (Item item : EmotionItem.ITEMS) { Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(item, 0, new ModelResourceLocation(new ResourceLocation(Reference.MOD_ID, item.getUnlocalizedName().substring(5)), "inventory")); } } }
Et pour finir si cela et nécessaire;
- Class TestMetaBlock -
package fr.emotion.updatingmod.block; import java.util.Collection; import javax.annotation.Nullable; import com.google.common.base.Predicate; import com.google.common.collect.Collections2; import com.google.common.collect.Lists; import fr.emotion.updatingmod.block.item.MetaBlock; import net.minecraft.block.Block; import net.minecraft.block.BlockFlower; import net.minecraft.block.material.Material; import net.minecraft.block.properties.IProperty; import net.minecraft.block.properties.PropertyEnum; import net.minecraft.block.state.BlockStateContainer; import net.minecraft.block.state.IBlockState; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.item.ItemStack; import net.minecraft.util.IStringSerializable; import net.minecraft.util.NonNullList; /** * @author EmotionFox */ public class TestMetaBlock extends Block implements MetaBlock { protected static final PropertyEnum <testmetablock.enumtype>VARIANT = PropertyEnum.<testmetablock.enumtype>create("variant", TestMetaBlock.EnumType.class); public TestMetaBlock() { super(Material.ROCK); this.setCreativeTab(CreativeTabs.BUILDING_BLOCKS); this.setDefaultState(this.blockState.getBaseState().withProperty(VARIANT, TestMetaBlock.EnumType.RED)); } @Override public void getSubBlocks(CreativeTabs itemIn, NonNullList <itemstack>items) { for (int i = 0; i < TestMetaBlock.EnumType.values().length; i++) { items.add(new ItemStack(this, 1, i)); } } @Override protected BlockStateContainer createBlockState() { return new BlockStateContainer(this, new IProperty[] { VARIANT }); } @Override public int getMetaFromState(IBlockState state) { TestMetaBlock.EnumType variant = (TestMetaBlock.EnumType) state.getValue(VARIANT); return variant.getMetadata(); } @Override public IBlockState getStateFromMeta(int meta) { return meta > getMaxMeta() ? this.getDefaultState().withProperty(VARIANT, TestMetaBlock.EnumType.values()[0]) : this.getDefaultState().withProperty(VARIANT, TestMetaBlock.EnumType.values()[meta]); } @Override public int damageDropped(IBlockState state) { return getMetaFromState(state); } public static enum EnumType implements IStringSerializable { RED(0, "red"), GREEN(1, "green"), BLUE(2, "blue"); private static final TestMetaBlock.EnumType[] METADATA = new TestMetaBlock.EnumType[values().length]; private final String name; private final int meta; private EnumType(int metadata, String name) { this.meta = metadata; this.name = name; names.add(name); } public int getMetadata() { return meta; } @Override public String toString() { return name; } @Override public String getName() { return name; } } }
- Interface MetaBlock -
package fr.emotion.updatingmod.block.item; import java.util.ArrayList; import net.minecraft.block.Block; /** * @author EmotionFox */ public interface MetaBlock { public ArrayList <string>names = new ArrayList<string>(); /** * Return the unlocalized name of the block by meta. * * @param meta * @return String */ public default String getName(int meta) { return meta > getMaxMeta() ? ((Block) this).getUnlocalizedName() + "_" + names.get(0) : ((Block) this).getUnlocalizedName() + "_" + names.get(meta); } /** * Return the number of name (variants) available for this block therefore * his maximum meta. * * @return Integer */ public default int getMaxMeta() { // -1 because size return the number of elements but meta start at 0. return names.size() - 1; } }
Merci d’avance à tous, pour vos précieuses réponses qui me permettrons enfin d’avancer sur tout ça.
</string></string></itemstack></testmetablock.enumtype></testmetablock.enumtype></block></block></block>
-
les blocks avec meta ont les mêmes jsons et pas des json séparés si je me souvient bien
-
Alors oui SpyMan, les blocs ont le même json dans “blockstates” qui va lister ces variantes mais normalement chaque variante devrait avoir un json dans “models\block” et “models\item” or dans mon cas et pour les items seulement il ne cherche que test_meta_block à la place des ces variances (test_meta_block_red, test_meta_block_green et test_meta_block_blue).
-
envoi le blockstat
-
- Json test_meta_block.json -
{ "variants": { "variant=red" : {"model": "udtmod:test_meta_block_red"}, "variant=green" : {"model": "udtmod:test_meta_block_green"}, "variant=blue" : {"model": "udtmod:test_meta_block_blue"} } }
-
Bizzar
-
Tu dois enregistrer tes rendus dans l’event ModelRegistryEvent ou quelque chose du genre
-
Bonjour BrokenSwing, j’enregistre tous mes rendues dans la class RegistrationHandler:
package fr.emotion.updatingmod.init; import fr.emotion.updatingmod.block.item.MetaBlock; import fr.emotion.updatingmod.block.item.UDTItemBlock; import fr.emotion.updatingmod.util.Reference; import net.minecraft.block.Block; import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.ItemModelMesher; import net.minecraft.client.renderer.block.model.ModelResourceLocation; import net.minecraft.item.Item; import net.minecraft.item.ItemBlock; import net.minecraft.util.ResourceLocation; import net.minecraftforge.event.RegistryEvent; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; /** * @author EmotionFox */ @Mod.EventBusSubscriber(modid = Reference.MOD_ID) public final class RegistrationHandler { private final ItemModelMesher renderItem = Minecraft.getMinecraft().getRenderItem().getItemModelMesher(); // Called by Forge. @SubscribeEvent public static void registerBlock(RegistryEvent.Register <block>e) { e.getRegistry().registerAll(EmotionBlock.getBlocks()); } // Called by proxy. public static void registerBlockRender() { ItemModelMesher renderItem = Minecraft.getMinecraft().getRenderItem().getItemModelMesher(); for (Block block : EmotionBlock.BLOCKS) { // Check if this block have many meta. if (block instanceof MetaBlock) { MetaBlock metaBlock = (MetaBlock) block; for (int i = 0; i <= metaBlock.getMaxMeta(); i++) { System.out.println("Registering render for: " + metaBlock.getName(i).substring(5)); renderItem.register(Item.getItemFromBlock(block), i, new ModelResourceLocation(new ResourceLocation(Reference.MOD_ID, metaBlock.getName(i).substring(5)), "inventory")); } } else { System.out.println("Registering render for: " + block.getUnlocalizedName().substring(5)); renderItem.register(Item.getItemFromBlock(block), 0, new ModelResourceLocation(new ResourceLocation(Reference.MOD_ID, block.getUnlocalizedName().substring(5)), "inventory")); } } } // Called by Forge. @SubscribeEvent public static void registerItem(RegistryEvent.Register e) { e.getRegistry().registerAll(EmotionItem.getItems()); e.getRegistry().registerAll(EmotionBlock.getItems()); } // Called by proxy. public static void registerItemRender() { ItemModelMesher renderItem = Minecraft.getMinecraft().getRenderItem().getItemModelMesher(); for (Item item : EmotionItem.ITEMS) { System.out.println("Registering render for: " + item.getUnlocalizedName().substring(5)); renderItem.register(item, 0, new ModelResourceLocation(new ResourceLocation(Reference.MOD_ID, item.getUnlocalizedName().substring(5)), "inventory")); } } }
Et ça marche pour mes items et les autres blocs et l’évent que tu me propose ne semble pas correspondre dans cette situation (j’ai aussi regarder si aucun autre évent ne pourrais m’aider mais je n’ai rien trouvé):
@SubscribeEvent public static void renderEvent(ModelRegistryEvent e) {} ```</block>
-
il faut faire comme cela :
@SubscribeEvent public static void registerItemTexture(ModelRegistryEvent event) { ModelLoader.setCustomModelResourceLocation(tonItem, metadata, new ModelResourceLocation(tonItem.getRegistryName(), "inventory")); }
-
Merci beaucoup Robin, t’es un chef ,) Et merci à toi aussi BrokenSwing!