bspkrs

TreeRegistry.java

May 21st, 2013
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.81 KB | None | 0 0
  1. package bspkrs.treecapitator;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.LinkedList;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.Map.Entry;
  9.  
  10. import net.minecraft.nbt.NBTTagCompound;
  11. import net.minecraft.nbt.NBTTagList;
  12. import net.minecraft.nbt.NBTTagString;
  13. import bspkrs.util.BlockID;
  14. import bspkrs.util.Coord;
  15.  
  16. public class TreeRegistry
  17. {
  18.     private Map<String, TreeDefinition>       treeDefs;
  19.     private Map<BlockID, String>              logToStringMap;
  20.     private TreeDefinition                    masterDefinition;
  21.     private Map<String, ConfigTreeDefinition> vanTrees;
  22.     private List<Coord>                       blocksBeingChopped;
  23.    
  24.     private static TreeRegistry               instance;
  25.    
  26.     public static TreeRegistry instance()
  27.     {
  28.         if (instance == null)
  29.             new TreeRegistry();
  30.        
  31.         return instance;
  32.     }
  33.    
  34.     protected TreeRegistry()
  35.     {
  36.         instance = this;
  37.        
  38.         initMapsAndLists();
  39.         initVanillaTreeDefs();
  40.     }
  41.    
  42.     protected void initMapsAndLists()
  43.     {
  44.         treeDefs = new HashMap<String, TreeDefinition>();
  45.         logToStringMap = new HashMap<BlockID, String>();
  46.         masterDefinition = new TreeDefinition();
  47.         blocksBeingChopped = new ArrayList<Coord>();
  48.     }
  49.    
  50.     protected void initVanillaTreeDefs()
  51.     {
  52.         vanTrees = new HashMap<String, ConfigTreeDefinition>();
  53.         vanTrees.put(Strings.OAK, new ConfigTreeDefinition().addLogID(new BlockID(17, 0)).addLogID(new BlockID(17, 4))
  54.                 .addLogID(new BlockID(17, 8)).addLogID(new BlockID(17, 12))
  55.                 .addLeafID(new BlockID(18, 0)).addLeafID(new BlockID(18, 8)));
  56.         vanTrees.put(Strings.SPRUCE, new ConfigTreeDefinition().addLogID(new BlockID(17, 1)).addLogID(new BlockID(17, 5))
  57.                 .addLogID(new BlockID(17, 9)).addLogID(new BlockID(17, 13))
  58.                 .addLeafID(new BlockID(18, 1)).addLeafID(new BlockID(18, 9)));
  59.         vanTrees.put(Strings.BIRCH, new ConfigTreeDefinition().addLogID(new BlockID(17, 2)).addLogID(new BlockID(17, 6))
  60.                 .addLogID(new BlockID(17, 10)).addLogID(new BlockID(17, 14))
  61.                 .addLeafID(new BlockID(18, 2)).addLeafID(new BlockID(18, 10)));
  62.         vanTrees.put(Strings.JUNGLE, new ConfigTreeDefinition().addLogID(new BlockID(17, 3)).addLogID(new BlockID(17, 7))
  63.                 .addLogID(new BlockID(17, 11)).addLogID(new BlockID(17, 15))
  64.                 .addLeafID(new BlockID(18, 3)).addLeafID(new BlockID(18, 11))
  65.                 .addLeafID(new BlockID(18, 0)).addLeafID(new BlockID(18, 8))
  66.                 .setMaxLeafBreakDist(6).setRequireLeafDecayCheck(false));
  67.         vanTrees.put(Strings.MUSH_BROWN, new ConfigTreeDefinition().addLogID(new BlockID(99, 10)).addLogID(new BlockID(99, 15))
  68.                 .addLeafID(new BlockID(99, 1)).addLeafID(new BlockID(99, 2))
  69.                 .addLeafID(new BlockID(99, 3)).addLeafID(new BlockID(99, 4))
  70.                 .addLeafID(new BlockID(99, 5)).addLeafID(new BlockID(99, 6))
  71.                 .addLeafID(new BlockID(99, 7)).addLeafID(new BlockID(99, 8))
  72.                 .addLeafID(new BlockID(99, 9)).addLeafID(new BlockID(99, 14))
  73.                 .setMaxLeafBreakDist(6).setRequireLeafDecayCheck(false));
  74.         vanTrees.put(Strings.MUSH_RED, new ConfigTreeDefinition().addLogID(new BlockID(100, 10)).addLogID(new BlockID(100, 15))
  75.                 .addLeafID(new BlockID(100, 1)).addLeafID(new BlockID(100, 2))
  76.                 .addLeafID(new BlockID(100, 3)).addLeafID(new BlockID(100, 4))
  77.                 .addLeafID(new BlockID(100, 5)).addLeafID(new BlockID(100, 6))
  78.                 .addLeafID(new BlockID(100, 7)).addLeafID(new BlockID(100, 8))
  79.                 .addLeafID(new BlockID(100, 9)).addLeafID(new BlockID(100, 14))
  80.                 .setMaxLeafBreakDist(6).setRequireLeafDecayCheck(false));
  81.     }
  82.    
  83.     protected void registerVanillaTreeDefs()
  84.     {
  85.         for (Entry<String, ConfigTreeDefinition> e : vanTrees.entrySet())
  86.             this.registerTree(e.getKey(), e.getValue());
  87.     }
  88.    
  89.     /**
  90.      * Registers the given tree definition. If the new key already exists the existing definition is updated. If the new definition contains
  91.      * a log that is already part of a tree, the existing definition is merged into the new definition.
  92.      *
  93.      * @param newKey
  94.      * @param newTD
  95.      */
  96.     public void registerTree(String newKey, TreeDefinition newTD)
  97.     {
  98.         // Do NOT register null tree defs!
  99.         if (newTD != null)
  100.         {
  101.             // list of trees that have at least one log blockID common with this tree
  102.             List<String> sharedLogTrees = new LinkedList<String>();
  103.             // logToStringMap entries to add
  104.             Map<BlockID, String> toAdd = new HashMap<BlockID, String>();
  105.            
  106.             // Check each log to see if an existing definition already uses it
  107.             for (BlockID blockID : newTD.getLogList())
  108.                 if (!isRegistered(blockID))
  109.                 {
  110.                     // build the toAdd map of new log keys
  111.                     toAdd.put(blockID, newKey);
  112.                 }
  113.                 else if (logToStringMap.containsKey(blockID) && !sharedLogTrees.contains(logToStringMap.get(blockID)))
  114.                     // Whoa! this BlockID isn't new, we need to do some merging
  115.                     sharedLogTrees.add(logToStringMap.get(blockID));
  116.            
  117.             if (!newKey.trim().isEmpty() && !isRegistered(newKey) && sharedLogTrees.size() == 0)
  118.             {
  119.                 // New definition all around.  Easy.
  120.                 TCLog.info("Tree Definition \"%s\" is new.  Proceeding to insert new key.", newKey);
  121.                 // insert newTD
  122.                 treeDefs.put(newKey, newTD);
  123.                 // update logToStringMap
  124.                 logToStringMap.putAll(toAdd);
  125.             }
  126.             else if (!newKey.trim().isEmpty())
  127.             {
  128.                 if (sharedLogTrees.size() > 0)
  129.                 {
  130.                     // merge all shared log TreeDefinition objects with our new TreeDefinition
  131.                     for (String existingKey : sharedLogTrees)
  132.                     {
  133.                         TCLog.info("Tree Definition \"%s\" contains a log that is registered with an existing tree (%s).  " +
  134.                                 "The existing definition will be merged with the new tree.", newKey, existingKey);
  135.                        
  136.                         // append the existing definition to our new definition
  137.                         newTD.appendWithSettings(treeDefs.remove(existingKey));
  138.                     }
  139.                    
  140.                     // insert newTD
  141.                     treeDefs.put(newKey, newTD);
  142.                    
  143.                     // update logToStringMap for all logs in the new definition
  144.                     for (BlockID blockID : newTD.getLogList())
  145.                         logToStringMap.put(blockID, newKey);
  146.                 }
  147.                 else
  148.                 { // A tree is defined for that key; append the new definition to the existing tree
  149.                     TCLog.info("\"%s\" is already registered.  The new definition will be appended to the existing entry.", newKey);
  150.                     treeDefs.get(newKey).appendWithSettings(newTD);
  151.                     logToStringMap.putAll(toAdd);
  152.                 }
  153.             }
  154.             else
  155.             /* newKey is empty! */
  156.             {
  157.                 if (sharedLogTrees.size() == 1)
  158.                 {
  159.                     String existingTree = sharedLogTrees.remove(0);
  160.                     // append the new def to the existing tree
  161.                     treeDefs.get(existingTree).appendWithSettings(newTD);
  162.                    
  163.                     // update logToStringMap
  164.                     for (BlockID log : newTD.getLogList())
  165.                         if (!logToStringMap.containsKey(log))
  166.                             logToStringMap.put(log, existingTree);
  167.                    
  168.                     // update the master def
  169.                     masterDefinition.append(newTD);
  170.                    
  171.                 }
  172.                 else
  173.                 {
  174.                     TCLog.warning("TreeDefinition cannot be registered with an empty Key.");
  175.                     return;
  176.                 }
  177.             }
  178.             // Update our master tree definition
  179.            
  180.             if (!newKey.trim().isEmpty())
  181.                 masterDefinition.append(treeDefs.get(newKey));
  182.         }
  183.         else
  184.             TCLog.warning("TreeDefinition cannot be null when registering a tree!");
  185.     }
  186.    
  187.     public boolean trackTreeChopEventAt(Coord c)
  188.     {
  189.         if (!blocksBeingChopped.contains(c))
  190.         {
  191.             blocksBeingChopped.add(c);
  192.             return true;
  193.         }
  194.         return false;
  195.     }
  196.    
  197.     public void endTreeChopEventAt(Coord c)
  198.     {
  199.         if (blocksBeingChopped.contains(c))
  200.             blocksBeingChopped.remove(c);
  201.     }
  202.    
  203.     /**
  204.      * Gets a comma-delimited string with all generic log IDs (no metadata).
  205.      *
  206.      * @return
  207.      */
  208.     public String getMultiMineExclusionString()
  209.     {
  210.         String r = "";
  211.         List<Integer> processed = new ArrayList<Integer>();
  212.        
  213.         for (BlockID log : masterDefinition.logBlocks)
  214.         {
  215.             if (!processed.contains(log.id))
  216.             {
  217.                 processed.add(log.id);
  218.                 r += "," + log.id;
  219.             }
  220.         }
  221.        
  222.         return r.replaceFirst(", ", "");
  223.     }
  224.    
  225.     public TreeDefinition masterDefinition()
  226.     {
  227.         return masterDefinition;
  228.     }
  229.    
  230.     /**
  231.      * Checks if a given tree definition key has been previously defined.
  232.      *
  233.      * @param key
  234.      * @return
  235.      */
  236.     public boolean isRegistered(String key)
  237.     {
  238.         return treeDefs.containsKey(key);
  239.     }
  240.    
  241.     /**
  242.      * Checks if a given BlockID has been registered with any tree.
  243.      *
  244.      * @param log
  245.      * @return
  246.      */
  247.     public boolean isRegistered(BlockID log)
  248.     {
  249.         return masterDefinition.isLogBlock(log);
  250.     }
  251.    
  252.     public TreeDefinition get(String key)
  253.     {
  254.         if (isRegistered(key))
  255.             return treeDefs.get(key);
  256.         else
  257.             return null;
  258.     }
  259.    
  260.     public TreeDefinition get(BlockID blockID)
  261.     {
  262.         if (isRegistered(blockID))
  263.             if (TCSettings.useStrictBlockPairing)
  264.                 return get(logToStringMap.get(blockID));
  265.             else
  266.                 return masterDefinition;
  267.         else
  268.             return null;
  269.     }
  270.    
  271.     public List<BlockID> masterLogList()
  272.     {
  273.         return masterDefinition.getLogList();
  274.     }
  275.    
  276.     public List<BlockID> masterLeafList()
  277.     {
  278.         return masterDefinition.getLeafList();
  279.     }
  280.    
  281.     public Map<String, ConfigTreeDefinition> vanillaTrees()
  282.     {
  283.         return new HashMap<String, ConfigTreeDefinition>(vanTrees);
  284.     }
  285.    
  286.     protected void readFromNBT(NBTTagCompound ntc)
  287.     {
  288.         // treeDefs;
  289.         treeDefs = new HashMap<String, TreeDefinition>();
  290.         NBTTagList l = ntc.getTagList(Strings.TREES);
  291.         for (int i = 0; i < l.tagCount(); i++)
  292.         {
  293.             NBTTagCompound tree = (NBTTagCompound) l.tagAt(i);
  294.             treeDefs.put(tree.getName(), new TreeDefinition(tree));
  295.         }
  296.        
  297.         // logToStringMap;
  298.         logToStringMap = new HashMap<BlockID, String>();
  299.         l = ntc.getTagList(Strings.LOG_STR_MAP);
  300.         for (int i = 0; i < l.tagCount(); i++)
  301.         {
  302.             NBTTagString s = (NBTTagString) l.tagAt(i);
  303.             logToStringMap.put(new BlockID(s.getName(), ","), s.data);
  304.         }
  305.        
  306.         // masterDefinition;
  307.         masterDefinition = new TreeDefinition(ntc.getCompoundTag(Strings.MASTER_DEF));
  308.     }
  309.    
  310.     public void writeToNBT(NBTTagCompound ntc)
  311.     {
  312.         // treeDefs
  313.         NBTTagList trees = new NBTTagList();
  314.         trees.setName(Strings.TREES);
  315.         for (Entry<String, TreeDefinition> e : treeDefs.entrySet())
  316.         {
  317.             NBTTagCompound tree = new NBTTagCompound();
  318.             e.getValue().writeToNBT(tree);
  319.             tree.setName(e.getKey());
  320.             trees.appendTag(tree);
  321.         }
  322.         ntc.setTag(Strings.TREE_DEFS, trees);
  323.        
  324.         //        logToStringMap;  
  325.         NBTTagList entries = new NBTTagList();
  326.         entries.setName(Strings.LOG_STR_MAP);
  327.         for (Entry<BlockID, String> e : logToStringMap.entrySet())
  328.             entries.appendTag(new NBTTagString(e.getKey().toString(), e.getValue()).setName(e.getKey().toString()));
  329.         ntc.setTag(Strings.LOG_STR_MAP, entries);
  330.        
  331.         //        masterDefinition;
  332.         NBTTagCompound md = new NBTTagCompound();
  333.         masterDefinition.writeToNBT(md);
  334.         ntc.setTag(Strings.MASTER_DEF, md);
  335.     }
  336. }
Advertisement
Add Comment
Please, Sign In to add comment