Advertisement
Guest User

Untitled

a guest
Sep 12th, 2014
843
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.60 KB | None | 0 0
  1. /*
  2. Too Many Items mod by @MCMrARM
  3. Do *NOT* redistribute source code or biniares, do *NOT* make software based on it.
  4. */
  5.  
  6. #include <jni.h>
  7. #include "mcpelauncher.h"
  8. #include "UILib.cpp"
  9. #include "colors.h"
  10. #include "elements/GUILabel.cpp"
  11. #include "elements/GUIButton.cpp"
  12. #include "elements/GUIToggleButton.cpp"
  13. #include "elements/GUIHeader.cpp"
  14. #include "elements/GUIItem.cpp"
  15. #include <android/log.h>
  16. #include <dlfcn.h>
  17. #include <string>
  18. #include <sstream>
  19. #include <stdlib.h>
  20. #include <cmath>
  21.  
  22. typedef void* MCPE_Inventory;
  23. typedef void* MCPE_Player;
  24.  
  25. MCPE_ItemInstance* createItemInstance(int id, int count, int damage) {
  26. MCPE_ItemInstance* item = new MCPE_ItemInstance();
  27. item->damage = damage;
  28. item->count = count;
  29. MCPE_ItemInstance_setId(item, id);
  30. return item;
  31. }
  32.  
  33. MCPE_Inventory* MCPE_Inventory_instance;
  34. static void (*MCPE_Inventory_add)(MCPE_Inventory*, MCPE_ItemInstance*);
  35.  
  36. MCPE_Item** MCPE_Item_items;
  37.  
  38. JavaVM* jvm;
  39.  
  40. int page;
  41. int pages;
  42. GUILabel* pageText;
  43.  
  44. void changePage(int btn) {
  45. if(btn == 1) {
  46. if(page <= 0) return;
  47. page--;
  48. }else if(btn == 2) {
  49. if(page+1 >= pages) return;
  50. page++;
  51. }
  52.  
  53. std::stringstream newText;
  54. newText << "Page " << (page+1) << "/" << pages;
  55. pageText->setMsg(newText.str());
  56. }
  57.  
  58. void fireBackEvent(int anything) {
  59. if(currentScreen != NULL && currentScreenCustom) {
  60. GUIScreen* screen = (GUIScreen*) currentScreen;
  61. screen->handleBackEvent();
  62. }
  63. }
  64.  
  65. int currentId = 0;
  66. GUILabel* lblItemDmgVal = NULL;
  67. GUILabel* check = NULL;
  68. int currentDmg = 0;
  69. void updateDmg(int id) {
  70. int dmg = id-2;
  71. std::stringstream newText;
  72. newText << dmg;
  73. lblItemDmgVal->setMsg(newText.str());
  74. check->setX(12+dmg*17);
  75. currentDmg = dmg;
  76. }
  77.  
  78. GUILabel* lblItemCountVal = NULL;
  79. int currentCount = 1;
  80. void countBtn(int id) {
  81. if(id == 25) {
  82. // add
  83. currentCount++;
  84. if(currentCount > 1000) currentCount = 1000;
  85. }else if(id == 26) {
  86. // rem
  87. currentCount--;
  88. if(currentCount < 1) currentCount = 1;
  89. }else if(id == 27) {
  90. // 1
  91. currentCount = 1;
  92. }else if(id == 28) {
  93. // 64
  94. currentCount = 64;
  95. }
  96. std::stringstream newText;
  97. newText << currentCount;
  98. lblItemCountVal->setMsg(newText.str());
  99. }
  100.  
  101. void addItem(int anything) {
  102. fireBackEvent(anything);
  103. MCPE_Inventory_add(MCPE_Inventory_instance, createItemInstance(currentId, currentCount, currentDmg));
  104. }
  105.  
  106. class AddItemScreen: public GUIScreen {
  107. int itemId;
  108.  
  109. public:
  110. AddItemScreen(int itemId) : GUIScreen(BACKGROUND_GAME_DARK) {
  111. this->itemId = itemId;
  112.  
  113. currentId = itemId;
  114. GUIHeader* header = new GUIHeader(0, 0, 0, getScreenWidth(), 26, "Add item");
  115. addElement(header);
  116. GUIButton* back = new GUIButton(1, 4, 4, 38, 18, "Back");
  117. back->setClick(fireBackEvent);
  118. addElement(back);
  119. GUILabel* lblItemID = new GUILabel(4, 30, 35, COLOR_WHITE, "Item ID:", true);
  120. addElement(lblItemID);
  121. std::stringstream sid;
  122. sid << itemId;
  123. GUILabel* lblItemIDVal = new GUILabel(42, 30, 50, COLOR_GREEN, sid.str(), true);
  124. addElement(lblItemIDVal);
  125. sid.str("");
  126. sid.clear();
  127. GUILabel* lblItemDmg = new GUILabel(4, 45, 70, COLOR_WHITE, "Item damage:", true);
  128. addElement(lblItemDmg);
  129. currentDmg = 0;
  130. lblItemDmgVal = new GUILabel(68, 45, 50, COLOR_GREEN, "0", true);
  131. addElement(lblItemDmgVal);
  132. for(int ix=0;ix<(getScreenWidth()-8)/17;ix++) {
  133. GUIItem* itemGui = new GUIItem(2+ix, 4+ix*17, 55, itemId, ix);
  134. itemGui->setClick(updateDmg);
  135. addElement(itemGui);
  136. }
  137. check = new GUILabel(12, 63, 20, COLOR_GREEN, "\u2713", true);
  138. addElement(check);
  139. currentCount = 1;
  140. GUILabel* lblItemCount = new GUILabel(4, 78, 30, COLOR_WHITE, "Count:", true);
  141. addElement(lblItemCount);
  142. lblItemCountVal = new GUILabel(36, 78, 50, COLOR_GREEN, "1", true);
  143. addElement(lblItemCountVal);
  144. GUIButton* addCount = new GUIButton(25, 60, 73, 18, 18, "+");
  145. addCount->setClick(countBtn);
  146. addElement(addCount);
  147. GUIButton* remCount = new GUIButton(26, 80, 73, 18, 18, "-");
  148. remCount->setClick(countBtn);
  149. addElement(remCount);
  150. GUIButton* count1 = new GUIButton(27, 105, 73, 18, 18, "1");
  151. count1->setClick(countBtn);
  152. addElement(count1);
  153. GUIButton* count64 = new GUIButton(28, 125, 73, 24, 18, "64");
  154. count64->setClick(countBtn);
  155. addElement(count64);
  156. GUIButton* addBtn = new GUIButton(30, getScreenWidth()/4, getScreenHeight()-40, getScreenWidth()/4*2, 30, "Add");
  157. addBtn->setClick(addItem);
  158. addElement(addBtn);
  159. }
  160.  
  161. bool handleBackEvent();
  162. };
  163.  
  164. jclass mainClass;
  165. void aboutBtn(int id) {
  166. JNIEnv* env;
  167. int envStatus = jvm->GetEnv((void**) &env, JNI_VERSION_1_6);
  168. if (envStatus == JNI_EDETACHED) {
  169. jvm->AttachCurrentThread(&env, NULL);
  170. }
  171.  
  172. const char* url = "https://twitter.com/MCMrARM";
  173. if(id == 4){
  174. url = "http://www.minecraftforum.net/forums/minecraft-pocket-edition/mcpe-mods-tools/1987484-too-many-items-mod";
  175. }
  176.  
  177. jclass uriClass = env->FindClass("android/net/Uri");
  178. jmethodID uriParseID = env->GetStaticMethodID(uriClass, "parse", "(Ljava/lang/String;)Landroid/net/Uri;");
  179. jobject uri = env->CallStaticObjectMethod(uriClass, uriParseID, env->NewStringUTF(url));
  180.  
  181. jclass intentClass = env->FindClass("android/content/Intent");
  182. jmethodID intentConstructor = env->GetMethodID(intentClass, "<init>", "(Ljava/lang/String;Landroid/net/Uri;)V");
  183. jobject intent = env->NewObject(intentClass, intentConstructor, env->NewStringUTF("android.intent.action.VIEW"), uri);
  184.  
  185. jfieldID currentMainField = env->GetStaticFieldID(mainClass, "currentMainActivity", "Ljava/lang/ref/WeakReference;");
  186. jobject mainInstanceWeak = env->GetStaticObjectField(mainClass, currentMainField);
  187.  
  188. jclass refClass = env->FindClass("java/lang/ref/Reference");
  189. jmethodID refGetID = env->GetMethodID(refClass, "get", "()Ljava/lang/Object;");
  190. jobject mainInstance = env->CallObjectMethod(mainInstanceWeak, refGetID);
  191.  
  192. jmethodID startActivityID = env->GetMethodID(mainClass, "startActivity", "(Landroid/content/Intent;)V");
  193. env->CallVoidMethod(mainInstance, startActivityID, intent);
  194.  
  195. if (envStatus == JNI_EDETACHED) {
  196. jvm->DetachCurrentThread();
  197. }
  198. }
  199.  
  200. class AboutScreen: public GUIScreen {
  201. public:
  202. AboutScreen() : GUIScreen(BACKGROUND_GAME_DARK) {
  203. GUIHeader* header = new GUIHeader(0, 0, 0, getScreenWidth(), 26, "About");
  204. addElement(header);
  205. GUIButton* back = new GUIButton(1, 4, 4, 38, 18, "Back");
  206. back->setClick(fireBackEvent);
  207. addElement(back);
  208. GUILabel* lblAbout = new GUILabel(5, 30, getScreenWidth()-10, COLOR_GREEN, "Too Many Items", true);
  209. addElement(lblAbout);
  210. GUILabel* lblAbout2 = new GUILabel(5, 42, getScreenWidth()-10, COLOR_WHITE, "version 8", true);
  211. addElement(lblAbout2);
  212. GUILabel* lblAbout3 = new GUILabel(5, 54, getScreenWidth()-10, COLOR_WHITE, "made by MrARM", true);
  213. addElement(lblAbout3);
  214. GUIButton* twitter = new GUIButton(3, 5, getScreenHeight()-25, (getScreenWidth()-20)/2, 20, "Twitter");
  215. twitter->setClick(aboutBtn);
  216. addElement(twitter);
  217. GUIButton* mcforums = new GUIButton(4, getScreenWidth()/2+5, getScreenHeight()-25, (getScreenWidth()-20)/2, 20, "Minecraft Forums thread");
  218. mcforums->setClick(aboutBtn);
  219. addElement(mcforums);
  220. }
  221.  
  222. bool handleBackEvent();
  223. };
  224.  
  225. void showAbout(int id) {
  226. setScreen(new AboutScreen);
  227. }
  228.  
  229. static std::string (*MCPE_Item_getDescriptionId)(MCPE_Item*);
  230.  
  231. class ListScreen: public GUIScreen {
  232. public:
  233. ListScreen() : GUIScreen(BACKGROUND_GAME_DARK) {
  234. page = 0;
  235. pages = 0;
  236. }
  237.  
  238. void init() {
  239. GUIScreen::init();
  240. GUILabel* lbl = new GUILabel(2, 2, 100, COLOR_GREEN, "Too Many Items", true);
  241. addElement(lbl);
  242. GUILabel* lbl2 = new GUILabel(82, 2, 100, COLOR_GRAY, "by MrARM", true);
  243. addElement(lbl2);
  244. pageText = new GUILabel(56, getScreenHeight()-16, 200, COLOR_GRAY, "Page 0/0", true);
  245. addElement(pageText);
  246. GUIButton* prevBtn = new GUIButton(1, 2, getScreenHeight()-22, 50, 20, "Previous");
  247. prevBtn->setClick(changePage);
  248. addElement(prevBtn);
  249. GUIButton* nextBtn = new GUIButton(2, getScreenWidth()-2-50-2-20, getScreenHeight()-22, 50, 20, "Next");
  250. nextBtn->setClick(changePage);
  251. addElement(nextBtn);
  252. GUIButton* aboutBtn = new GUIButton(2, getScreenWidth()-2-20, getScreenHeight()-22, 20, 20, "?");
  253. aboutBtn->setClick(showAbout);
  254. addElement(aboutBtn);
  255. }
  256.  
  257. bool checkItem(int id) {
  258. if(id == 95) return true;
  259. if(id < 255) {
  260. std::string tileName = MCPE_Item_getDescriptionId(MCPE_Item_items[id]);
  261. std::string missingStart = "tile.Missing block ID: ";
  262. if(tileName.compare(0, missingStart.length(), missingStart) == 0){
  263. return false;
  264. }
  265. return true;
  266. }
  267. return true;
  268. }
  269.  
  270. int getItemCount() {
  271. int items = 0;
  272. for(int i=0;i<512;i++) {
  273. if(MCPE_Item_items[i] != NULL && checkItem(i)) {
  274. items++;
  275. }
  276. }
  277. return items;
  278. }
  279.  
  280. int getRows() {
  281. int top = 14;
  282. int bottom = getScreenHeight()-24;
  283. int height = bottom-top;
  284. return floor(((float) height) / 17);
  285. }
  286.  
  287. int getItemsInRow() {
  288. return (getScreenWidth()-8)/17;
  289. }
  290.  
  291. int getItemsPerPage() {
  292. return getItemsInRow()*getRows();
  293. }
  294.  
  295. void render() {
  296. GUIScreen::render();
  297.  
  298. if(MCPE_Textures_instance != NULL) {
  299. int items = getItemCount();
  300. int perPage = getItemsPerPage();
  301. int newPages = ceil(((float)items)/perPage);
  302. if(pages != newPages) {
  303. page = 0;
  304. pages = newPages;
  305. std::stringstream newText;
  306. newText << "Page " << (page+1) << "/" << pages;
  307. pageText->setMsg(newText.str());
  308. }
  309.  
  310. int start = page*perPage;
  311.  
  312. int id = 0;
  313. int x = 2;
  314. int y = 14;
  315. for(int i=0;i<512;i++) {
  316. if(MCPE_Item_items[i] != NULL && checkItem(i)) {
  317. if(id >= start) {
  318. MCPE_ItemInstance* item = createItemInstance(i, 1, 0);
  319. MCPE_ItemRenderer_renderGuiItem(MCPE_Textures_instance, item, 0, x, y, 1, 1, 1);
  320. x += 17;
  321. if(x >= getScreenWidth()-4-17) {
  322. x = 2;
  323. y += 17;
  324. if(y >= getScreenHeight()-24-17) {
  325. break;
  326. }
  327. }
  328. }
  329. id++;
  330. }
  331. }
  332. }
  333. }
  334.  
  335. bool touchStarted(int id, short x, short y) {
  336. if(x >= 2 && x <= getScreenWidth()-2 && y >= 14 && y <= getScreenHeight()-24) {
  337. int rows = getRows();
  338. for(int xi=0;xi<getItemsInRow();xi++) {
  339. for(int yi=0;yi<rows;yi++) {
  340. if(xi*17 <= x-2 && (xi+1)*17 > x-2 &&
  341. yi*17 <= y-14 && (yi+1)*17 > y-14) {
  342. int start = page*getItemsPerPage();
  343. int iid = 0;
  344. int findId = start+yi*getItemsInRow()+xi;
  345. for(int i=0;i<512;i++) {
  346. if(MCPE_Item_items[i] != NULL && checkItem(i)) {
  347. if(iid == findId) {
  348. //MCPE_Inventory_add(MCPE_Inventory_instance, createItemInstance(i, 1, 0));
  349. setScreen(new AddItemScreen(i));
  350. return true;
  351. }
  352. iid++;
  353. }
  354. }
  355. }
  356. }
  357. }
  358. }
  359. return GUIScreen::touchStarted(id, x, y);
  360. }
  361. };
  362.  
  363. void showList(int btnId) {
  364. setScreen(new ListScreen);
  365. }
  366.  
  367. bool AddItemScreen::handleBackEvent() {
  368. setScreen(new ListScreen);
  369. return true;
  370. }
  371.  
  372. bool AboutScreen::handleBackEvent() {
  373. setScreen(new ListScreen);
  374. return true;
  375. }
  376.  
  377. static void (*MCPE_Inventory_Inventory)(MCPE_Inventory*, MCPE_Player*, bool);
  378. void inventoryHook(MCPE_Inventory* inventory, MCPE_Player* player, bool b) {
  379. MCPE_Inventory_instance = inventory;
  380. MCPE_Inventory_Inventory(inventory, player, b);
  381. }
  382.  
  383. JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) {
  384. jvm = vm;
  385. JNIEnv* env;
  386. int envStatus = jvm->GetEnv((void**) &env, JNI_VERSION_1_6);
  387. if (envStatus == JNI_EDETACHED) {
  388. jvm->AttachCurrentThread(&env, NULL);
  389. }
  390. jclass mainc = env->FindClass("com/mojang/minecraftpe/MainActivity");
  391. mainClass = (jclass) env->NewGlobalRef(mainc);
  392.  
  393. MCPE_Item_items = (MCPE_Item**) dlsym(RTLD_DEFAULT, "_ZN4Item5itemsE");
  394.  
  395. MCPE_Inventory_add = (void (*)(MCPE_Inventory*, MCPE_ItemInstance*)) dlsym(RTLD_DEFAULT, "_ZN9Inventory3addEP12ItemInstance");
  396. MCPE_Item_getDescriptionId = (std::string (*)(MCPE_Item*)) dlsym(RTLD_DEFAULT, "_ZNK8TileItem16getDescriptionIdEv");
  397.  
  398. setupGUI();
  399.  
  400. GUIBaseScreen* screen = getGUIScreen();
  401. GUIButton* btn = new GUIButton(0, 0, 12, 20, 20, "M");
  402. btn->setClick(showList);
  403. screen->addElement(btn);
  404.  
  405. void* inv = dlsym(RTLD_DEFAULT, "_ZN9InventoryC1EP6Playerb");
  406. mcpelauncher_hook(inv, (void*) &inventoryHook, (void**) &MCPE_Inventory_Inventory);
  407.  
  408. return JNI_VERSION_1_2;
  409. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement