Advertisement
Guest User

Untitled

a guest
Oct 16th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.35 KB | None | 0 0
  1. dynping.json:
  2. {
  3.   "forge_marker": 1,
  4.   "variants": {
  5.     "inventory": {
  6.       "model": "ore_pings:ping",
  7.       "textures": {
  8.         "base": "forge:items/bucket_base",
  9.         "fluid": "forge:items/buckfet_fluid",
  10.         "cover": "forge:items/bucket_cover"
  11.       },
  12.       "transform": "forge:default-item",
  13.       "custom": {
  14.         "ore": "oreCoal"
  15.       }
  16.     }
  17.   }
  18. }
  19.  
  20. ModelDynPing:
  21. package com.kokolihapihvi.orepings.client.model;
  22.  
  23. import java.io.IOException;
  24. import java.util.Collection;
  25. import java.util.List;
  26. import java.util.Map;
  27.  
  28. import javax.vecmath.Matrix4f;
  29.  
  30. import net.minecraft.block.Block;
  31. import net.minecraft.block.state.IBlockState;
  32. import net.minecraft.client.Minecraft;
  33. import net.minecraft.client.renderer.block.model.BakedQuad;
  34. import net.minecraft.client.renderer.block.model.IBakedModel;
  35. import net.minecraft.client.renderer.block.model.ItemCameraTransforms;
  36. import net.minecraft.client.renderer.block.model.ItemCameraTransforms.TransformType;
  37. import net.minecraft.client.renderer.block.model.ItemOverride;
  38. import net.minecraft.client.renderer.block.model.ItemOverrideList;
  39. import net.minecraft.client.renderer.block.model.ModelResourceLocation;
  40. import net.minecraft.client.renderer.texture.TextureAtlasSprite;
  41. import net.minecraft.client.renderer.vertex.VertexFormat;
  42. import net.minecraft.client.resources.IResourceManager;
  43. import net.minecraft.entity.EntityLivingBase;
  44. import net.minecraft.item.ItemBlock;
  45. import net.minecraft.item.ItemStack;
  46. import net.minecraft.util.EnumFacing;
  47. import net.minecraft.util.ResourceLocation;
  48. import net.minecraft.world.World;
  49. import net.minecraftforge.client.model.ICustomModelLoader;
  50. import net.minecraftforge.client.model.IModel;
  51. import net.minecraftforge.client.model.IModelCustomData;
  52. import net.minecraftforge.client.model.IPerspectiveAwareModel;
  53. import net.minecraftforge.client.model.IRetexturableModel;
  54. import net.minecraftforge.client.model.ItemLayerModel;
  55. import net.minecraftforge.client.model.ItemTextureQuadConverter;
  56. import net.minecraftforge.client.model.SimpleModelState;
  57. import net.minecraftforge.common.model.IModelPart;
  58. import net.minecraftforge.common.model.IModelState;
  59. import net.minecraftforge.common.model.TRSRTransformation;
  60. import net.minecraftforge.oredict.OreDictionary;
  61.  
  62. import org.apache.commons.lang3.tuple.Pair;
  63.  
  64. import com.google.common.base.Function;
  65. import com.google.common.base.Optional;
  66. import com.google.common.collect.ImmutableList;
  67. import com.google.common.collect.ImmutableMap;
  68. import com.google.common.collect.ImmutableSet;
  69. import com.google.common.collect.Maps;
  70. import com.kokolihapihvi.orepings.OrePingsMod;
  71.  
  72. public class ModelDynPing implements IModel, IModelCustomData, IRetexturableModel {
  73.     public static final ModelResourceLocation LOCATION = new ModelResourceLocation(new ResourceLocation(OrePingsMod.MODID, "dynping"), "inventory");
  74.  
  75.     // minimal Z offset to prevent depth-fighting
  76.     private static final float NORTH_Z_BASE = 7.496f / 16f;
  77.     private static final float SOUTH_Z_BASE = 8.504f / 16f;
  78.     private static final float NORTH_Z_FLUID = 7.498f / 16f;
  79.     private static final float SOUTH_Z_FLUID = 8.502f / 16f;
  80.  
  81.     public static final IModel MODEL = new ModelDynPing();
  82.  
  83.     protected final ResourceLocation baseLocation;
  84.     protected final ResourceLocation oreLocation;
  85.     protected final ResourceLocation coverLocation;
  86.  
  87.     protected final Block ore;
  88.  
  89.     public ModelDynPing() {
  90.         this(null, null, null, null);
  91.     }
  92.  
  93.     public ModelDynPing(ResourceLocation baseLocation, ResourceLocation liquidLocation, ResourceLocation coverLocation, Block ore) {
  94.         this.baseLocation = baseLocation;
  95.         this.oreLocation = liquidLocation;
  96.         this.coverLocation = coverLocation;
  97.         this.ore = ore;
  98.     }
  99.  
  100.     @Override
  101.     public Collection<ResourceLocation> getDependencies() {
  102.         return ImmutableList.of();
  103.     }
  104.  
  105.     @Override
  106.     public Collection<ResourceLocation> getTextures() {
  107.         ImmutableSet.Builder<ResourceLocation> builder = ImmutableSet.builder();
  108.         if (baseLocation != null)
  109.             builder.add(baseLocation);
  110.         if (oreLocation != null)
  111.             builder.add(oreLocation);
  112.         if (coverLocation != null)
  113.             builder.add(coverLocation);
  114.  
  115.         return builder.build();
  116.     }
  117.  
  118.     @Override
  119.     public IBakedModel bake(IModelState state, VertexFormat format, Function<ResourceLocation, TextureAtlasSprite> bakedTextureGetter) {
  120.  
  121.         ImmutableMap<TransformType, TRSRTransformation> transformMap = IPerspectiveAwareModel.MapWrapper.getTransforms(state);
  122.  
  123.         TRSRTransformation transform = state.apply(Optional.<IModelPart> absent()).or(TRSRTransformation.identity());
  124.         TextureAtlasSprite oreSprite = null;
  125.         ImmutableList.Builder<BakedQuad> builder = ImmutableList.builder();
  126.  
  127.         if (ore != null) {
  128.             oreSprite = Minecraft.getMinecraft().getBlockRendererDispatcher().getModelForState(ore.getDefaultState()).getParticleTexture();
  129.         }
  130.  
  131.         if (baseLocation != null) {
  132.             // build base (insidest)
  133.             IBakedModel model = (new ItemLayerModel(ImmutableList.of(baseLocation))).bake(state, format, bakedTextureGetter);
  134.             builder.addAll(model.getQuads(null, null, 0));
  135.         }
  136.         if (oreLocation != null && oreSprite != null) {
  137.             // build ore layer (inside)
  138.             builder.add(ItemTextureQuadConverter.genQuad(format, transform, 0, 0, 16, 16, NORTH_Z_FLUID, oreSprite, EnumFacing.NORTH, 0xffffffff));
  139.             builder.add(ItemTextureQuadConverter.genQuad(format, transform, 0, 0, 16, 16, SOUTH_Z_FLUID, oreSprite, EnumFacing.SOUTH, 0xffffffff));
  140.         }
  141.         if (coverLocation != null) {
  142.             // cover (the actual item around the other two)
  143.             TextureAtlasSprite base = bakedTextureGetter.apply(coverLocation);
  144.             builder.add(ItemTextureQuadConverter.genQuad(format, transform, 0, 0, 16, 16, NORTH_Z_BASE, base, EnumFacing.NORTH, 0xffffffff));
  145.             builder.add(ItemTextureQuadConverter.genQuad(format, transform, 0, 0, 16, 16, SOUTH_Z_BASE, base, EnumFacing.SOUTH, 0xffffffff));
  146.         }
  147.  
  148.         return new BakedDynPing(this, builder.build(), oreSprite, format, Maps.immutableEnumMap(transformMap), Maps.<String, IBakedModel> newHashMap());
  149.     }
  150.  
  151.     @Override
  152.     public IModelState getDefaultState() {
  153.         return TRSRTransformation.identity();
  154.     }
  155.  
  156.     /**
  157.      * Sets the liquid in the model. fluid - Name of the fluid in the
  158.      * FluidRegistry flipGas - If "true" the model will be flipped upside down
  159.      * if the liquid is a gas. If "false" it wont
  160.      * <p/>
  161.      * If the fluid can't be found, water is used
  162.      */
  163.     @Override
  164.     public ModelDynPing process(ImmutableMap<String, String> customData) {
  165.         String oreName = customData.get("ore");
  166.        
  167.         List<ItemStack> ores = OreDictionary.getOres(oreName);
  168.         Block ore = null;
  169.        
  170.         if(ores.size() > 0)
  171.             ore = ((ItemBlock) ores.get(0).getItem()).block;
  172.  
  173.         if (ore == null)
  174.             ore = this.ore;
  175.  
  176.         // create new model with correct liquid
  177.         return new ModelDynPing(baseLocation, oreLocation, coverLocation, ore);
  178.     }
  179.  
  180.     /**
  181.      * Allows to use different textures for the model. There are 3 layers: base
  182.      * - The empty bucket/container fluid - A texture representing the liquid
  183.      * portion. Non-transparent = liquid cover - An overlay that's put over the
  184.      * liquid (optional)
  185.      * <p/>
  186.      * If no liquid is given a hardcoded variant for the bucket is used.
  187.      */
  188.     @Override
  189.     public ModelDynPing retexture(ImmutableMap<String, String> textures) {
  190.  
  191.         ResourceLocation base = baseLocation;
  192.         ResourceLocation oreMask = oreLocation;
  193.         ResourceLocation cover = coverLocation;
  194.  
  195.         if (textures.containsKey("base"))
  196.             base = new ResourceLocation(textures.get("base"));
  197.         if (textures.containsKey("fluid"))
  198.             oreMask = new ResourceLocation(textures.get("fluid"));
  199.         if (textures.containsKey("cover"))
  200.             cover = new ResourceLocation(textures.get("cover"));
  201.  
  202.         return new ModelDynPing(base, oreMask, cover, ore);
  203.     }
  204.  
  205.     public enum LoaderDynPing implements ICustomModelLoader {
  206.         instance;
  207.  
  208.         @Override
  209.         public boolean accepts(ResourceLocation modelLocation) {
  210.             return modelLocation.getResourceDomain().equals(OrePingsMod.MODID) && modelLocation.getResourcePath().contains("ping");
  211.         }
  212.  
  213.         @Override
  214.         public IModel loadModel(ResourceLocation modelLocation) throws IOException {
  215.             return MODEL;
  216.         }
  217.  
  218.         @Override
  219.         public void onResourceManagerReload(IResourceManager resourceManager) {
  220.             // no need to clear cache since we create a new model instance
  221.         }
  222.     }
  223.  
  224.     private static final class BakedDynPingOverrideHandler extends ItemOverrideList {
  225.  
  226.         public static final BakedDynPingOverrideHandler INSTANCE = new BakedDynPingOverrideHandler();
  227.  
  228.         private BakedDynPingOverrideHandler() {
  229.             super(ImmutableList.<ItemOverride> of());
  230.         }
  231.  
  232.         @Override
  233.         public IBakedModel handleItemState(IBakedModel originalModel, ItemStack stack, World world, EntityLivingBase entity) {
  234.             BakedDynPing model = (BakedDynPing) originalModel;
  235.  
  236.             String type = "UNKNOWN";
  237.  
  238.             if (stack.getTagCompound() != null) {
  239.                 if (stack.getTagCompound().getCompoundTag("OrePing") != null) {
  240.                     type = stack.getTagCompound().getCompoundTag("OrePing").getString("ore");
  241.                 }
  242.             }
  243.  
  244.             String name = type;
  245.  
  246.             if (!model.cache.containsKey(name)) {
  247.                 IModel parent = model.parent.process(ImmutableMap.of("ore", name));
  248.                 Function<ResourceLocation, TextureAtlasSprite> textureGetter;
  249.                 textureGetter = new Function<ResourceLocation, TextureAtlasSprite>() {
  250.                     public TextureAtlasSprite apply(ResourceLocation location) {
  251.                         return Minecraft.getMinecraft().getTextureMapBlocks().getAtlasSprite(location.toString());
  252.                     }
  253.                 };
  254.  
  255.                 IBakedModel bakedModel = parent.bake(new SimpleModelState(model.transforms), model.format, textureGetter);
  256.                 model.cache.put(name, bakedModel);
  257.                 return bakedModel;
  258.             }
  259.  
  260.             return model.cache.get(name);
  261.         }
  262.  
  263.     }
  264.  
  265.     // the dynamic bucket is based on the empty bucket
  266.     private static final class BakedDynPing implements IPerspectiveAwareModel {
  267.  
  268.         private final ModelDynPing parent;
  269.         private final Map<String, IBakedModel> cache; // contains all the baked
  270.                                                         // models since they'll
  271.                                                         // never change
  272.         private final ImmutableMap<TransformType, TRSRTransformation> transforms;
  273.         private final ImmutableList<BakedQuad> quads;
  274.         private final TextureAtlasSprite particle;
  275.         private final VertexFormat format;
  276.  
  277.         public BakedDynPing(ModelDynPing parent, ImmutableList<BakedQuad> quads, TextureAtlasSprite particle, VertexFormat format, ImmutableMap<ItemCameraTransforms.TransformType, TRSRTransformation> transforms, Map<String, IBakedModel> cache) {
  278.             this.quads = quads;
  279.             this.particle = particle;
  280.             this.format = format;
  281.             this.parent = parent;
  282.             this.transforms = transforms;
  283.             this.cache = cache;
  284.         }
  285.  
  286.         @Override
  287.         public ItemOverrideList getOverrides() {
  288.             return BakedDynPingOverrideHandler.INSTANCE;
  289.         }
  290.  
  291.         @Override
  292.         public Pair<? extends IBakedModel, Matrix4f> handlePerspective(TransformType cameraTransformType) {
  293.             return IPerspectiveAwareModel.MapWrapper.handlePerspective(this, transforms, cameraTransformType);
  294.         }
  295.  
  296.         @Override
  297.         public List<BakedQuad> getQuads(IBlockState state, EnumFacing side, long rand) {
  298.             if (side == null)
  299.                 return quads;
  300.             return ImmutableList.of();
  301.         }
  302.  
  303.         public boolean isAmbientOcclusion() {
  304.             return true;
  305.         }
  306.  
  307.         public boolean isGui3d() {
  308.             return false;
  309.         }
  310.  
  311.         public boolean isBuiltInRenderer() {
  312.             return false;
  313.         }
  314.  
  315.         public TextureAtlasSprite getParticleTexture() {
  316.             return particle;
  317.         }
  318.  
  319.         public ItemCameraTransforms getItemCameraTransforms() {
  320.             return ItemCameraTransforms.DEFAULT;
  321.         }
  322.     }
  323. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement