Advertisement
peacestorm

Example addon for ModPE.

Apr 2nd, 2017
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // IN MAIN FILE
  2. const ScriptableObject_ = org.mozilla.javascript.ScriptableObject;
  3.  
  4. var ScriptManager_ = com.mcbox.pesdk.mcpelauncher.ScriptManager;
  5. var ScriptManager__ = net.zhuoweizhang.mcpelauncher.ScriptManager;
  6. var ctx = com.mojang.minecraftpe.MainActivity.currentMainActivity.get();
  7. var addons = [];
  8.  
  9. var Launcher = {
  10.     isBlockLauncher: function() {
  11.         return (ctx.getPackageName() == "net.zhuoweizhang.mcpelauncher" || ctx.getPackageName() == "net.zhuoweizhang.mcpelauncher.pro");
  12.     },
  13.     isToolbox: function() {
  14.         return ctx.getPackageName() == "io.mrarm.mctoolbox";
  15.     },
  16.     isMcpeMaster: function() {
  17.         return ctx.getPackageName() == "com.mcbox.pesdkb.mcpelauncher";
  18.     }
  19. };
  20.  
  21. var mainScriptName = "IceAttack"; //example
  22. (function loadAddons() {
  23.     if(Launcher.isBlockLauncher() || Launcher.isToolbox()) {
  24.         ScriptManager__.callScriptMethod("addonLoadHook", [mainScriptName]);
  25.     }
  26.     if(Launcher.isMcpeMaster()) {
  27.         ScriptManager_.callScriptMethod("addonLoadHook", [mainScriptName]);
  28.     }
  29. })();
  30.  
  31. function registerAddon(name, desc, current_version, target_version, author) {
  32.     var shouldMessage = true;
  33.     try {
  34.         let scripts;
  35.         if(Launcher.isBlockLauncher() || Launcher.isToolbox()) {
  36.             scripts = ScriptManager__.scripts;
  37.         } else {
  38.             scripts = ScriptManager_.scripts;
  39.         }
  40.         let scriptName;
  41.         for(var i = 0; i < scripts.size(); i++) {
  42.             var script = scripts.get(i);
  43.             var scope = script.scope;
  44.             if(ScriptableObject_.hasProperty(scope, "ADDON_NAME") && ScriptableObject_.hasProperty(scope, "ADDON_VERSION")) {
  45.                 if(ScriptableObject_.getProperty(scope, "ADDON_NAME") == name && ScriptableObject_.getProperty(scope, "ADDON_VERSION") == current_version) {
  46.                     scriptName = script.name;
  47.                     break;
  48.                 }
  49.             }
  50.         }
  51.         addons.push({
  52.             name: name,
  53.             desc: desc,
  54.             author: author==null?"Unknown":author,
  55.             current_version: current_version,
  56.             target_version: target_version,
  57.             scriptName: scriptName
  58.         });
  59.     } catch(e) {
  60.         shouldMessage = false;
  61.         print("[" + mainScriptName + "] An error occurred while loading addons: " + e);
  62.     }
  63.    
  64.     if(shouldMessage) {
  65.         print("[" + mainScriptName + "] Successfully loaded the addon with name " + name + "!");
  66.     }
  67. }
  68.  
  69. /*
  70.  
  71.     Afterwards, the main script will have all the loaded addons stored in the addons array,
  72.     which makes it possible to use a for loop on it. For example you can do addons[i].name
  73.     which returns the name of the addon at index i in the addons array.
  74.    
  75. */
  76.  
  77. // IN ADDON FILE
  78.  
  79. const ADDON_NAME = "BestAddon for IceAttack!";
  80. const ADDON_DESC = "It's just the best.";
  81. const ADDON_AUTHOR = "Smasher & peacestorm ;)."; //<< examples
  82. const ADDON_VERSION = "1.0"
  83. const TARGET_VERSION = "1.0"; //this means the addon is compatible with version 1.0 of the main script
  84.  
  85. var ScriptManager_ = com.mcbox.pesdk.mcpelauncher.ScriptManager;
  86. var ScriptManager__ = net.zhuoweizhang.mcpelauncher.ScriptManager;
  87. var ctx = com.mojang.minecraftpe.MainActivity.currentMainActivity.get();
  88.  
  89. var Launcher = {
  90.     isBlockLauncher: function() {
  91.         return (ctx.getPackageName() == "net.zhuoweizhang.mcpelauncher" || ctx.getPackageName() == "net.zhuoweizhang.mcpelauncher.pro");
  92.     },
  93.     isToolbox: function() {
  94.         return ctx.getPackageName() == "io.mrarm.mctoolbox";
  95.     },
  96.     isMcpeMaster: function() {
  97.         return ctx.getPackageName() == "com.mcbox.pesdkb.mcpelauncher";
  98.     }
  99. };
  100.  
  101. function addonLoadHook(mainScriptName) {
  102.     if(mainScriptName == "IceAttack") { //insert main script name
  103.         if(Launcher.isBlockLauncher() || Launcher.isToolbox()) {
  104.             net.zhuoweizhang.mcpelauncher.ScriptManager.callScriptMethod("registerAddon", [ADDON_NAME, ADDON_DESC, ADDON_VERSION, TARGET_VERSION, ADDON_AUTHOR]);
  105.         }
  106.         if(Launcher.isMcpeMaster()) {
  107.             com.mcbox.pesdk.mcpelauncher.ScriptManager.callScriptMethod("registerAddon", [ADDON_NAME, ADDON_DESC, ADDON_VERSION, TARGET_VERSION, ADDON_AUTHOR]);
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement