Advertisement
tomdodd4598

Untitled

Apr 28th, 2021
868
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.45 KB | None | 0 0
  1. package crafttweaker.runtime;
  2.  
  3. import crafttweaker.api.network.NetworkSide;
  4. import crafttweaker.preprocessor.IPreprocessor;
  5. import crafttweaker.runtime.providers.ScriptIteratorZip;
  6.  
  7. import java.io.*;
  8. import java.util.*;
  9.  
  10. public class ScriptFile {
  11.     private IScriptIterator script;
  12.     private List<IPreprocessor> affectingPreprocessors = new ArrayList<>();
  13.    
  14.     private ITweaker tweaker;
  15.    
  16.     /** Priority which can be assigned to script, within the same priority the load order is affected by filename*/
  17.     private int priority = 0;
  18.    
  19.     /** In case no loaders are provided, default to crafttweaker loader*/
  20.     private static final String[] LOADER_NAMES_DEFAULT = {"crafttweaker"};
  21.    
  22.     /** Loader names get provided so that it can only load specific scripts, not all of them*/
  23.     private String[] loaderNames = LOADER_NAMES_DEFAULT;
  24.    
  25.     private boolean isExecutionBlocked = false;
  26.     private boolean isParsingBlocked = false;
  27.     private boolean isCompileBlocked = false;
  28.    
  29.     private boolean isDebugEnabled = false;
  30.     private boolean ignoreBracketErrors = false;
  31.    
  32.     private boolean isSyntaxCommand;
  33.    
  34.     private NetworkSide networkSide;
  35.    
  36.     public ScriptFile(ITweaker tweaker, IScriptIterator script, boolean isSyntaxCommand) {
  37.         this.tweaker = tweaker;
  38.         this.script = script;
  39.         this.isSyntaxCommand = isSyntaxCommand;
  40.     }
  41.    
  42.     public IScriptIterator getScript() {
  43.         return script;
  44.     }
  45.    
  46.     /**
  47.      * Group name is with relative path to the file or ZIP file name
  48.      */
  49.     public String getGroupName() {
  50.         return script.getGroupName();
  51.     }
  52.    
  53.     /**
  54.      * Actual name of the zs file
  55.      */
  56.     public String getName() {
  57.         return script.getName();
  58.     }
  59.    
  60.     /**
  61.      * Gets the effective name that has every info in it that it can, like zip files don't use only the zip file name
  62.      */
  63.     public String getEffectiveName(){
  64.         if (script instanceof ScriptIteratorZip) {
  65.             ScriptIteratorZip scriptIterator = (ScriptIteratorZip) script;
  66.             return getGroupName() + File.separator + scriptIterator.getCurrentName().replace('/', File.separatorChar);
  67.         }else {
  68.             return getGroupName();
  69.         }
  70.     }
  71.    
  72.     /**
  73.      * Gives InputStream of the script,
  74.      * must be closed again or might cause problems down the line
  75.      * @return Input stream of the file
  76.      * @throws IOException
  77.      */
  78.     public InputStream open() throws IOException {
  79.         return script.open();
  80.     }
  81.    
  82.     /**
  83.      * Delegates to the preprocessor List
  84.      */
  85.     public boolean add(IPreprocessor preprocessor) {
  86.         return affectingPreprocessors.add(preprocessor);
  87.     }
  88.    
  89.     public boolean addAll(Collection<? extends IPreprocessor> c) {
  90.         return affectingPreprocessors.addAll(c);
  91.     }
  92.    
  93.     public List<IPreprocessor> getAffectingPreprocessors() {
  94.         return affectingPreprocessors;
  95.     }
  96.    
  97.     /**
  98.      * Change the priority
  99.      */
  100.     public int getPriority() {
  101.         return priority;
  102.     }
  103.    
  104.     public void setPriority(int priority) {
  105.         this.priority = priority;
  106.     }
  107.    
  108.     public String[] getLoaderNames() {
  109.         if (loaderNames.length == 0) {
  110.             return LOADER_NAMES_DEFAULT;
  111.         }
  112.         else {
  113.             return loaderNames;
  114.         }
  115.     }
  116.    
  117.     /**
  118.      * Change the loader names to change whether it should be loaded or not
  119.      */
  120.     public void setLoaderNames(String... loaderNames) {
  121.         this.loaderNames = loaderNames;
  122.     }
  123.    
  124.     @Deprecated
  125.     public String getLoaderName() {
  126.         return getLoaderNames()[0];
  127.     }
  128.    
  129.     @Deprecated
  130.     public void setLoaderName(String loaderName) {
  131.         loaderNames = new String[] {loaderName};
  132.     }
  133.    
  134.     /**
  135.      * Gets the tweaker which handles the loading of the current file
  136.      * @return ITweaker instance of the current tweaker
  137.      */
  138.     public ITweaker getTweaker() {
  139.         return tweaker;
  140.     }
  141.    
  142.     /**
  143.      * Getters and setters which alter the loading of the code
  144.      */
  145.     public boolean isExecutionBlocked() {
  146.         return isExecutionBlocked;
  147.     }
  148.    
  149.     public void setExecutionBlocked(boolean executionBlocked) {
  150.         isExecutionBlocked = executionBlocked;
  151.     }
  152.    
  153.     public boolean isParsingBlocked() {
  154.         return isParsingBlocked;
  155.     }
  156.    
  157.     public void setParsingBlocked(boolean parsingBlocked) {
  158.         isParsingBlocked = parsingBlocked;
  159.     }
  160.    
  161.     public boolean isCompileBlocked() {
  162.         return isCompileBlocked;
  163.     }
  164.    
  165.     public void setCompileBlocked(boolean compileBlocked) {
  166.         isCompileBlocked = compileBlocked;
  167.     }
  168.    
  169.     /**
  170.      * Whether it is a by syntax command. Syntax commands only load the script, but don't execute it
  171.      */
  172.     public boolean isSyntaxCommand() {
  173.         return isSyntaxCommand;
  174.     }
  175.    
  176.     @Override
  177.     public String toString() {
  178.         return "{[" + priority + ":" + loaderNamesToString() + "]: " + getEffectiveName() + (networkSide == null ? "" : "[Side: " + networkSide + "]")+ "}";
  179.     }
  180.    
  181.     private String loaderNamesToString() {
  182.         String[] names = getLoaderNames();
  183.         if (names.length == 1) {
  184.             return names[0];
  185.         }
  186.         else {
  187.             return Arrays.toString(names);
  188.         }
  189.     }
  190.    
  191.     /**
  192.      * Change whether you want to have bracket errors show up for the specified file
  193.      */
  194.     public boolean areBracketErrorsIgnored() {
  195.         return ignoreBracketErrors;
  196.     }
  197.    
  198.     public void setIgnoreBracketErrors(boolean ignoreBracketErrors) {
  199.         this.ignoreBracketErrors = ignoreBracketErrors;
  200.     }
  201.    
  202.     /**
  203.      * Change whether you want to enable debug mode for the specified file
  204.      */
  205.     public boolean isDebugEnabled() {
  206.         return isDebugEnabled;
  207.     }
  208.    
  209.     public void setDebugEnabled(boolean debugEnabled) {
  210.         isDebugEnabled = debugEnabled;
  211.     }
  212.    
  213.     /**
  214.      * Registers the file to be only loaded on this network side
  215.      */
  216.     public void setNetworkSide(NetworkSide networkSide){
  217.         this.networkSide = networkSide;
  218.     }
  219.    
  220.     /**
  221.      * Returns whether it should be loaded on the side it was asked to be loaded on
  222.      */
  223.     public boolean shouldBeLoadedOn(NetworkSide proposedSide) {
  224.         return networkSide == null || networkSide.equals(proposedSide);
  225.     }
  226.    
  227.    
  228. }
  229.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement