gt22

Untitled

Nov 2nd, 2016
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.53 KB | None | 0 0
  1. package com.gt22.gt22core.texturegen;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.File;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import java.lang.reflect.Field;
  9. import java.util.ArrayList;
  10. import java.util.HashSet;
  11. import java.util.List;
  12. import java.util.Set;
  13. import java.util.stream.Collectors;
  14. import java.util.stream.IntStream;
  15.  
  16. import org.apache.logging.log4j.Level;
  17.  
  18. import com.google.common.collect.Lists;
  19. import com.gt22.gt22core.core.Core;
  20. import com.gt22.gt22core.texturegen.ICustomJson.JsonType;
  21. import com.gt22.gt22core.utils.FileUtils;
  22.  
  23. import net.minecraft.block.Block;
  24. import net.minecraft.block.properties.IProperty;
  25. import net.minecraft.crash.CrashReport;
  26. import net.minecraft.item.Item;
  27. import net.minecraft.util.EnumFacing;
  28. import net.minecraft.util.ReportedException;
  29. import net.minecraft.util.ResourceLocation;
  30. import net.minecraftforge.fml.common.FMLLog;
  31.  
  32. public class TextureGenRegistry {
  33.     private static class TextureStorage {
  34.         public Class<?> reg;
  35.         public String modid;
  36.  
  37.         public TextureStorage(Class clazz, String modid) {
  38.             this.reg = clazz;
  39.             this.modid = modid;
  40.         }
  41.     }
  42.  
  43.     private static List<TextureStorage> registries = Core.isDev() ? new ArrayList<TextureStorage>() : null;
  44.  
  45.     /**
  46.      * Use this method to add class containing Items/Blocks field (Like
  47.      * {@link net.minecraft.init.Items} or {@link net.minecraft.init.Items} for
  48.      * vanilla) After registering and jsons will appear in
  49.      * rundir/GeneratedTextures, Just copy assets forlder in your resources
  50.      * source forlder to add all generated jsons. Texture generating works <strong>ONLY</strong>
  51.      * in dev enviroment, if trying to use this in normal minecraft it will be
  52.      * just ignored. Also see: {@link DoNotApplyTexture} {@link ICustomJson}
  53.      * {@link IMultitextureBlock}
  54.      *
  55.      * @param registry
  56.      * @param modid
  57.      */
  58.     public static void reigster(Class registry, String modid) {
  59.         if (Core.isDev()) {
  60.             registries.add(new TextureStorage(registry, modid));
  61.         }
  62.     }
  63.  
  64.     /**
  65.      * Called interanly
  66.      */
  67.     public static void generateTextures() {
  68.         Set<String> addedGenericFor = new HashSet<String>();
  69.         for (TextureStorage reg : registries) {
  70.             if (!addedGenericFor.contains(reg.modid)) {
  71.                 createGeneric(reg.modid);
  72.                 addedGenericFor.add(reg.modid);
  73.             }
  74.             Field[] fields = reg.reg.getFields();
  75.             for (Field f : fields) {
  76.                 if (!f.isAnnotationPresent(DoNotApplyTexture.class)) {
  77.                     try {
  78.                         if (Item.class.isAssignableFrom(f.getType())) {
  79.                             generateItemTexture(reg.modid, (Item) f.get(null));
  80.  
  81.                         } else if (Block.class.isAssignableFrom(f.getType())) {
  82.                             generateBlockTexture(reg.modid, (Block) f.get(null));
  83.                         }
  84.                     } catch (IllegalAccessException e) {
  85.                         throw new ReportedException(CrashReport.makeCrashReport(e, "Unable to get value from registry. Are you using PRIVATE FIELDS IN REGISTRY!!??"));
  86.                     }
  87.                     catch(NullPointerException e)
  88.                     {
  89.                         FMLLog.log(Core.modid, Level.WARN, "Item/Block field %s was not be initialized, skipping", f.getName());
  90.                     }
  91.                 }
  92.             }
  93.         }
  94.         registries = null;
  95.     }
  96.  
  97.     private static void createGeneric(String modid) {
  98.         File generic = new File(getDirForItem(modid), "ItemGeneric.json");
  99.         try (BufferedReader in = new BufferedReader(new InputStreamReader(TextureGenRegistry.class.getResourceAsStream("/ItemGeneric.json"), "UTF-8")); BufferedWriter out = FileUtils.createWriter(generic, false)) {
  100.             for (String buf = in.readLine(); buf != null; buf = in.readLine()) {
  101.                 out.write(buf);
  102.             }
  103.         } catch (IOException e) {
  104.             throw new ReportedException(CrashReport.makeCrashReport(e, "Unable to create ItemGeneric.json for mod " + modid));
  105.         }
  106.     }
  107.  
  108.     private static File getCoreModDir(String modid) {
  109.         return new File("GeneratedTextures/assets/" + modid);
  110.     }
  111.  
  112.     private static File getDirForMod(String modid) {
  113.         return new File(getCoreModDir(modid), "models/");
  114.     }
  115.  
  116.     private static File getDirForItem(String modid) {
  117.         return new File(getDirForMod(modid), "item");
  118.     }
  119.  
  120.     private static File getDirForBlock(String modid) {
  121.         return new File(getDirForMod(modid), "block");
  122.     }
  123.  
  124.     public static File getDirForBlockstate(String modid) {
  125.         return new File(getCoreModDir(modid), "blockstates");
  126.     }
  127.  
  128.     private static void generateItemTexture(String modid, Item item) {
  129.         File itemfile = new File(getDirForItem(modid), item.getRegistryName().getResourcePath() + ".json");
  130.         try (BufferedWriter out = FileUtils.createWriter(itemfile, false)) {
  131.             out.write(formatItemJson(modid, item));
  132.         } catch (IOException e) {
  133.             throw new ReportedException(CrashReport.makeCrashReport(e, "Unable to generate texture for item " + item.getUnlocalizedName()));
  134.         }
  135.     }
  136.  
  137.     private static void generateBlockTexture(String modid, Block block) {
  138.         File blockfile = new File(getDirForBlock(modid), block.getRegistryName().getResourcePath() + ".json");
  139.         File statefile = new File(getDirForBlockstate(modid), block.getRegistryName().getResourcePath() + ".json");
  140.         File itemfile = new File(getDirForItem(modid), block.getRegistryName().getResourcePath() + ".json");
  141.         try (BufferedWriter outblock = FileUtils.createWriter(blockfile, false); BufferedWriter outstate = FileUtils.createWriter(statefile, false); BufferedWriter outitem = FileUtils.createWriter(itemfile, false)) {
  142.             outblock.write(formatBlockJson(modid, block));
  143.             outstate.write(formatBlockStateJson(modid, block));
  144.             outitem.write(formatItemBlockJson(modid, block));
  145.         } catch (IOException e) {
  146.             throw new ReportedException(CrashReport.makeCrashReport(e, "Unable to generate texture for block " + block.getUnlocalizedName()));
  147.         }
  148.     }
  149.  
  150.     //@formatter:off
  151.     private static String formatItemJson(String modid, Item item) {
  152.         if(item instanceof ICustomJson && ((ICustomJson) item).getJson(JsonType.ITEM) != null)
  153.         {
  154.             return ((ICustomJson) item).getJson(JsonType.ITEM);
  155.         }
  156.         if(item.isFull3D())
  157.         {
  158.             return String.format("{\n"
  159.                                     +"\"parent\": \"item/handheld\",\n"
  160.                                     +"\"textures\": {\n"
  161.                                     + "\"layer0\": \"%s:items/%s\"\n"
  162.                                     +"}\n"
  163.                                 +"}", modid, item.getRegistryName().getResourcePath());
  164.         }
  165.         else
  166.         {
  167.             return String.format(
  168.                       "{\n"
  169.                     + "\"parent\": \"%s:item/ItemGeneric\",\n"
  170.                     + "\"textures\": {\n"
  171.                     + "\"layer0\": \"%s:items/%s\"\n"
  172.                     + "}\n"
  173.                     + "}",
  174.                     modid, modid, item.getRegistryName().getResourcePath());
  175.             }
  176.         }
  177.    
  178.     private static String formatItemBlockJson(String modid, Block block) {
  179.         if(block instanceof ICustomJson && ((ICustomJson) block).getJson(JsonType.ITEM) != null)
  180.         {
  181.             return ((ICustomJson) block).getJson(JsonType.ITEM);
  182.         }
  183.         return String.format(
  184.                 "{\n"
  185.                     +"\"parent\":\"%s:block/%s\",\n"
  186.                     +"\"display\": {\n"
  187.                         +"\"thirdperson\": {\n"
  188.                             +"\"rotation\": [ 10, -45, 170 ],\n"
  189.                             +"\"translation\": [ 0, 1.5, -2.75 ],\n"
  190.                             +"\"scale\": [ 0.375, 0.375, 0.375 ]\n"
  191.                        +"}\n"
  192.                     +"}\n"
  193.                 +"}",
  194.                 modid, block.getRegistryName().getResourcePath());
  195.     }
  196.    
  197.     private static String formatBlockJson(String modid, Block block) {
  198.         if(block instanceof ICustomJson && ((ICustomJson) block).getJson(JsonType.BLOCK) != null)
  199.         {
  200.             return ((ICustomJson) block).getJson(JsonType.BLOCK);
  201.         }
  202.         if(block instanceof IMultitextureBlock)
  203.         {
  204.             IMultitextureBlock bl = (IMultitextureBlock) block;
  205.             return String.format("{\n"
  206.                     + "\"parent\": \"block/cube\",\n"
  207.                     + "\"textures\": {\n"
  208.                         + "\"particle\": \"%s\",\n"
  209.                         + "\"up\": \"%s\",\n"
  210.                         + "\"down\": \"%s\",\n"
  211.                         + "\"north\": \"%s\",\n"
  212.                         + "\"east\": \"%s\",\n"
  213.                         + "\"south\": \"%s\",\n"
  214.                         + "\"west\": \"%s\"\n"
  215.                     + "}\n"
  216.                 + "}",
  217.                 bl.getParticleTexture(), bl.getTexture(EnumFacing.UP), bl.getTexture(EnumFacing.DOWN), bl.getTexture(EnumFacing.NORTH), bl.getTexture(EnumFacing.EAST), bl.getTexture(EnumFacing.SOUTH), bl.getTexture(EnumFacing.WEST));
  218.         }
  219.             return String.format("{\n"
  220.                                     + "\"parent\": \"block/cube_all\",\n"
  221.                                     + "\"textures\": {\n"
  222.                                         + "\"all\": \"%s:blocks/%s\"\n"
  223.                                     + "}\n"
  224.                                 + "}",
  225.                                 modid, block.getRegistryName().getResourcePath());
  226.     }
  227.    
  228.     private static String formatBlockStateJson(String modid, Block block) {
  229.         if(block instanceof ICustomJson && ((ICustomJson) block).getJson(JsonType.BLOCKSTATE) != null)
  230.         {
  231.             return ((ICustomJson) block).getJson(JsonType.BLOCKSTATE);
  232.         }
  233.         if(block instanceof IMultiBlockStateBlock)
  234.         {
  235.             return formatMultiStateJson(modid, block);
  236.         }
  237.         return String.format("{\n"
  238.                                 + "\t\"variants\": {\n"
  239.                                 + "\t\"normal\": { \"model\":\"%s\" }\n"
  240.                             + "}\n"
  241.                             + "}", block.getRegistryName());
  242.     }
  243.    
  244.     private static String formatMultiStateJson(String modid, Block block)
  245.     {
  246.         IMultiBlockStateBlock states = (IMultiBlockStateBlock) block;
  247.         IProperty<?>[] props = states.getProps();
  248.         StringBuilder sb = new StringBuilder();
  249.         for(IProperty<?> prop : props)
  250.         {
  251.             getJsonsForProps(prop, states, sb);
  252.         }
  253.         return String.format(""
  254.                 + "{\n"
  255.                     + "\t\"forge_marker\": 1,\n"
  256.                     + "\t\"defaults\": {\n"
  257.                         + "\t\t%s\n"
  258.                         + "\t\t\"model\":\"%s\"\n"
  259.                     + "\t}\n"
  260.                     + "\t\"variants\": {\n"
  261.                         + "\t\t\"normal\": [{}]\n"
  262.                         + "%s"
  263.                     + "\t}\n"
  264.                 + "}"      
  265.                 ,states.getDefaultJson(), block.getRegistryName(),  sb);
  266.     }
  267.    
  268.     private static <T extends Comparable<T>> void getJsonsForProps(IProperty<T> prop, IMultiBlockStateBlock states, StringBuilder sb)
  269.     {
  270.         sb.append(String.format("\t\t\"%s\":{\n", prop.getName()));
  271.         for(T val : prop.getAllowedValues())
  272.         {
  273.             sb.append(String.format("\t\t\t\"%s\":{\n", prop.getName(val)));
  274.             sb.append(states.getJson(prop, val));
  275.             sb.append(getTexturesForBlock(states.getTextures(prop, val), 4));
  276.             sb.append("\t\t\t}\n");
  277.         }
  278.         sb.append("\t\t}\n");
  279.     }
  280.    
  281.     private static String getTexturesForBlock(Block bl, int tabs)
  282.     {
  283.         return getTexturesForBlock(bl instanceof IMultitextureBlock ? (IMultitextureBlock) bl : side -> bl.getRegistryName(), tabs);
  284.     }
  285.    
  286.     private static String getTexturesForBlock(IMultitextureBlock bl, int tabs)
  287.     {
  288.         return String.format(
  289.                 tabs(tabs) + "\"textures\": {\n"
  290.                         + tabs(++tabs) + "\"particle\": \"%s\",\n"
  291.                         + tabs(tabs) + "\"up\": \"%s\",\n"
  292.                         + tabs(tabs) + "\"down\": \"%s\",\n"
  293.                         + tabs(tabs) + "\"north\": \"%s\",\n"
  294.                         + tabs(tabs) + "\"east\": \"%s\",\n"
  295.                         + tabs(tabs) + "\"south\": \"%s\",\n"
  296.                         + tabs(tabs) + "\"west\": \"%s\"\n"
  297.                 + tabs(--tabs) + "}\n"
  298.                 ,bl.getParticleTexture(), bl.getTexture(EnumFacing.UP), bl.getTexture(EnumFacing.DOWN), bl.getTexture(EnumFacing.NORTH), bl.getTexture(EnumFacing.EAST), bl.getTexture(EnumFacing.SOUTH), bl.getTexture(EnumFacing.WEST));
  299.     }
  300.    
  301.     private static String tabs(int tabs)
  302.     {
  303.         String ret = "";
  304.         for(int i = 0; i < tabs; i++)
  305.         {
  306.             ret += '\t';
  307.         }
  308.         return ret;
  309.     }
  310.    
  311.     //@formatter:on
  312. }
Advertisement
Add Comment
Please, Sign In to add comment