Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Too Many Items mod by @MCMrARM
- Do *NOT* redistribute source code or biniares, do *NOT* make software based on it.
- */
- #include <jni.h>
- #include "mcpelauncher.h"
- #include "UILib.cpp"
- #include "colors.h"
- #include "elements/GUILabel.cpp"
- #include "elements/GUIButton.cpp"
- #include "elements/GUIToggleButton.cpp"
- #include "elements/GUIHeader.cpp"
- #include "elements/GUIItem.cpp"
- #include <android/log.h>
- #include <dlfcn.h>
- #include <string>
- #include <sstream>
- #include <stdlib.h>
- #include <cmath>
- typedef void* MCPE_Inventory;
- typedef void* MCPE_Player;
- MCPE_ItemInstance* createItemInstance(int id, int count, int damage) {
- MCPE_ItemInstance* item = new MCPE_ItemInstance();
- item->damage = damage;
- item->count = count;
- MCPE_ItemInstance_setId(item, id);
- return item;
- }
- MCPE_Inventory* MCPE_Inventory_instance;
- static void (*MCPE_Inventory_add)(MCPE_Inventory*, MCPE_ItemInstance*);
- MCPE_Item** MCPE_Item_items;
- JavaVM* jvm;
- int page;
- int pages;
- GUILabel* pageText;
- void changePage(int btn) {
- if(btn == 1) {
- if(page <= 0) return;
- page--;
- }else if(btn == 2) {
- if(page+1 >= pages) return;
- page++;
- }
- std::stringstream newText;
- newText << "Page " << (page+1) << "/" << pages;
- pageText->setMsg(newText.str());
- }
- void fireBackEvent(int anything) {
- if(currentScreen != NULL && currentScreenCustom) {
- GUIScreen* screen = (GUIScreen*) currentScreen;
- screen->handleBackEvent();
- }
- }
- int currentId = 0;
- GUILabel* lblItemDmgVal = NULL;
- GUILabel* check = NULL;
- int currentDmg = 0;
- void updateDmg(int id) {
- int dmg = id-2;
- std::stringstream newText;
- newText << dmg;
- lblItemDmgVal->setMsg(newText.str());
- check->setX(12+dmg*17);
- currentDmg = dmg;
- }
- GUILabel* lblItemCountVal = NULL;
- int currentCount = 1;
- void countBtn(int id) {
- if(id == 25) {
- // add
- currentCount++;
- if(currentCount > 1000) currentCount = 1000;
- }else if(id == 26) {
- // rem
- currentCount--;
- if(currentCount < 1) currentCount = 1;
- }else if(id == 27) {
- // 1
- currentCount = 1;
- }else if(id == 28) {
- // 64
- currentCount = 64;
- }
- std::stringstream newText;
- newText << currentCount;
- lblItemCountVal->setMsg(newText.str());
- }
- void addItem(int anything) {
- fireBackEvent(anything);
- MCPE_Inventory_add(MCPE_Inventory_instance, createItemInstance(currentId, currentCount, currentDmg));
- }
- class AddItemScreen: public GUIScreen {
- int itemId;
- public:
- AddItemScreen(int itemId) : GUIScreen(BACKGROUND_GAME_DARK) {
- this->itemId = itemId;
- currentId = itemId;
- GUIHeader* header = new GUIHeader(0, 0, 0, getScreenWidth(), 26, "Add item");
- addElement(header);
- GUIButton* back = new GUIButton(1, 4, 4, 38, 18, "Back");
- back->setClick(fireBackEvent);
- addElement(back);
- GUILabel* lblItemID = new GUILabel(4, 30, 35, COLOR_WHITE, "Item ID:", true);
- addElement(lblItemID);
- std::stringstream sid;
- sid << itemId;
- GUILabel* lblItemIDVal = new GUILabel(42, 30, 50, COLOR_GREEN, sid.str(), true);
- addElement(lblItemIDVal);
- sid.str("");
- sid.clear();
- GUILabel* lblItemDmg = new GUILabel(4, 45, 70, COLOR_WHITE, "Item damage:", true);
- addElement(lblItemDmg);
- currentDmg = 0;
- lblItemDmgVal = new GUILabel(68, 45, 50, COLOR_GREEN, "0", true);
- addElement(lblItemDmgVal);
- for(int ix=0;ix<(getScreenWidth()-8)/17;ix++) {
- GUIItem* itemGui = new GUIItem(2+ix, 4+ix*17, 55, itemId, ix);
- itemGui->setClick(updateDmg);
- addElement(itemGui);
- }
- check = new GUILabel(12, 63, 20, COLOR_GREEN, "\u2713", true);
- addElement(check);
- currentCount = 1;
- GUILabel* lblItemCount = new GUILabel(4, 78, 30, COLOR_WHITE, "Count:", true);
- addElement(lblItemCount);
- lblItemCountVal = new GUILabel(36, 78, 50, COLOR_GREEN, "1", true);
- addElement(lblItemCountVal);
- GUIButton* addCount = new GUIButton(25, 60, 73, 18, 18, "+");
- addCount->setClick(countBtn);
- addElement(addCount);
- GUIButton* remCount = new GUIButton(26, 80, 73, 18, 18, "-");
- remCount->setClick(countBtn);
- addElement(remCount);
- GUIButton* count1 = new GUIButton(27, 105, 73, 18, 18, "1");
- count1->setClick(countBtn);
- addElement(count1);
- GUIButton* count64 = new GUIButton(28, 125, 73, 24, 18, "64");
- count64->setClick(countBtn);
- addElement(count64);
- GUIButton* addBtn = new GUIButton(30, getScreenWidth()/4, getScreenHeight()-40, getScreenWidth()/4*2, 30, "Add");
- addBtn->setClick(addItem);
- addElement(addBtn);
- }
- bool handleBackEvent();
- };
- jclass mainClass;
- void aboutBtn(int id) {
- JNIEnv* env;
- int envStatus = jvm->GetEnv((void**) &env, JNI_VERSION_1_6);
- if (envStatus == JNI_EDETACHED) {
- jvm->AttachCurrentThread(&env, NULL);
- }
- const char* url = "https://twitter.com/MCMrARM";
- if(id == 4){
- url = "http://www.minecraftforum.net/forums/minecraft-pocket-edition/mcpe-mods-tools/1987484-too-many-items-mod";
- }
- jclass uriClass = env->FindClass("android/net/Uri");
- jmethodID uriParseID = env->GetStaticMethodID(uriClass, "parse", "(Ljava/lang/String;)Landroid/net/Uri;");
- jobject uri = env->CallStaticObjectMethod(uriClass, uriParseID, env->NewStringUTF(url));
- jclass intentClass = env->FindClass("android/content/Intent");
- jmethodID intentConstructor = env->GetMethodID(intentClass, "<init>", "(Ljava/lang/String;Landroid/net/Uri;)V");
- jobject intent = env->NewObject(intentClass, intentConstructor, env->NewStringUTF("android.intent.action.VIEW"), uri);
- jfieldID currentMainField = env->GetStaticFieldID(mainClass, "currentMainActivity", "Ljava/lang/ref/WeakReference;");
- jobject mainInstanceWeak = env->GetStaticObjectField(mainClass, currentMainField);
- jclass refClass = env->FindClass("java/lang/ref/Reference");
- jmethodID refGetID = env->GetMethodID(refClass, "get", "()Ljava/lang/Object;");
- jobject mainInstance = env->CallObjectMethod(mainInstanceWeak, refGetID);
- jmethodID startActivityID = env->GetMethodID(mainClass, "startActivity", "(Landroid/content/Intent;)V");
- env->CallVoidMethod(mainInstance, startActivityID, intent);
- if (envStatus == JNI_EDETACHED) {
- jvm->DetachCurrentThread();
- }
- }
- class AboutScreen: public GUIScreen {
- public:
- AboutScreen() : GUIScreen(BACKGROUND_GAME_DARK) {
- GUIHeader* header = new GUIHeader(0, 0, 0, getScreenWidth(), 26, "About");
- addElement(header);
- GUIButton* back = new GUIButton(1, 4, 4, 38, 18, "Back");
- back->setClick(fireBackEvent);
- addElement(back);
- GUILabel* lblAbout = new GUILabel(5, 30, getScreenWidth()-10, COLOR_GREEN, "Too Many Items", true);
- addElement(lblAbout);
- GUILabel* lblAbout2 = new GUILabel(5, 42, getScreenWidth()-10, COLOR_WHITE, "version 8", true);
- addElement(lblAbout2);
- GUILabel* lblAbout3 = new GUILabel(5, 54, getScreenWidth()-10, COLOR_WHITE, "made by MrARM", true);
- addElement(lblAbout3);
- GUIButton* twitter = new GUIButton(3, 5, getScreenHeight()-25, (getScreenWidth()-20)/2, 20, "Twitter");
- twitter->setClick(aboutBtn);
- addElement(twitter);
- GUIButton* mcforums = new GUIButton(4, getScreenWidth()/2+5, getScreenHeight()-25, (getScreenWidth()-20)/2, 20, "Minecraft Forums thread");
- mcforums->setClick(aboutBtn);
- addElement(mcforums);
- }
- bool handleBackEvent();
- };
- void showAbout(int id) {
- setScreen(new AboutScreen);
- }
- static std::string (*MCPE_Item_getDescriptionId)(MCPE_Item*);
- class ListScreen: public GUIScreen {
- public:
- ListScreen() : GUIScreen(BACKGROUND_GAME_DARK) {
- page = 0;
- pages = 0;
- }
- void init() {
- GUIScreen::init();
- GUILabel* lbl = new GUILabel(2, 2, 100, COLOR_GREEN, "Too Many Items", true);
- addElement(lbl);
- GUILabel* lbl2 = new GUILabel(82, 2, 100, COLOR_GRAY, "by MrARM", true);
- addElement(lbl2);
- pageText = new GUILabel(56, getScreenHeight()-16, 200, COLOR_GRAY, "Page 0/0", true);
- addElement(pageText);
- GUIButton* prevBtn = new GUIButton(1, 2, getScreenHeight()-22, 50, 20, "Previous");
- prevBtn->setClick(changePage);
- addElement(prevBtn);
- GUIButton* nextBtn = new GUIButton(2, getScreenWidth()-2-50-2-20, getScreenHeight()-22, 50, 20, "Next");
- nextBtn->setClick(changePage);
- addElement(nextBtn);
- GUIButton* aboutBtn = new GUIButton(2, getScreenWidth()-2-20, getScreenHeight()-22, 20, 20, "?");
- aboutBtn->setClick(showAbout);
- addElement(aboutBtn);
- }
- bool checkItem(int id) {
- if(id == 95) return true;
- if(id < 255) {
- std::string tileName = MCPE_Item_getDescriptionId(MCPE_Item_items[id]);
- std::string missingStart = "tile.Missing block ID: ";
- if(tileName.compare(0, missingStart.length(), missingStart) == 0){
- return false;
- }
- return true;
- }
- return true;
- }
- int getItemCount() {
- int items = 0;
- for(int i=0;i<512;i++) {
- if(MCPE_Item_items[i] != NULL && checkItem(i)) {
- items++;
- }
- }
- return items;
- }
- int getRows() {
- int top = 14;
- int bottom = getScreenHeight()-24;
- int height = bottom-top;
- return floor(((float) height) / 17);
- }
- int getItemsInRow() {
- return (getScreenWidth()-8)/17;
- }
- int getItemsPerPage() {
- return getItemsInRow()*getRows();
- }
- void render() {
- GUIScreen::render();
- if(MCPE_Textures_instance != NULL) {
- int items = getItemCount();
- int perPage = getItemsPerPage();
- int newPages = ceil(((float)items)/perPage);
- if(pages != newPages) {
- page = 0;
- pages = newPages;
- std::stringstream newText;
- newText << "Page " << (page+1) << "/" << pages;
- pageText->setMsg(newText.str());
- }
- int start = page*perPage;
- int id = 0;
- int x = 2;
- int y = 14;
- for(int i=0;i<512;i++) {
- if(MCPE_Item_items[i] != NULL && checkItem(i)) {
- if(id >= start) {
- MCPE_ItemInstance* item = createItemInstance(i, 1, 0);
- MCPE_ItemRenderer_renderGuiItem(MCPE_Textures_instance, item, 0, x, y, 1, 1, 1);
- x += 17;
- if(x >= getScreenWidth()-4-17) {
- x = 2;
- y += 17;
- if(y >= getScreenHeight()-24-17) {
- break;
- }
- }
- }
- id++;
- }
- }
- }
- }
- bool touchStarted(int id, short x, short y) {
- if(x >= 2 && x <= getScreenWidth()-2 && y >= 14 && y <= getScreenHeight()-24) {
- int rows = getRows();
- for(int xi=0;xi<getItemsInRow();xi++) {
- for(int yi=0;yi<rows;yi++) {
- if(xi*17 <= x-2 && (xi+1)*17 > x-2 &&
- yi*17 <= y-14 && (yi+1)*17 > y-14) {
- int start = page*getItemsPerPage();
- int iid = 0;
- int findId = start+yi*getItemsInRow()+xi;
- for(int i=0;i<512;i++) {
- if(MCPE_Item_items[i] != NULL && checkItem(i)) {
- if(iid == findId) {
- //MCPE_Inventory_add(MCPE_Inventory_instance, createItemInstance(i, 1, 0));
- setScreen(new AddItemScreen(i));
- return true;
- }
- iid++;
- }
- }
- }
- }
- }
- }
- return GUIScreen::touchStarted(id, x, y);
- }
- };
- void showList(int btnId) {
- setScreen(new ListScreen);
- }
- bool AddItemScreen::handleBackEvent() {
- setScreen(new ListScreen);
- return true;
- }
- bool AboutScreen::handleBackEvent() {
- setScreen(new ListScreen);
- return true;
- }
- static void (*MCPE_Inventory_Inventory)(MCPE_Inventory*, MCPE_Player*, bool);
- void inventoryHook(MCPE_Inventory* inventory, MCPE_Player* player, bool b) {
- MCPE_Inventory_instance = inventory;
- MCPE_Inventory_Inventory(inventory, player, b);
- }
- JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) {
- jvm = vm;
- JNIEnv* env;
- int envStatus = jvm->GetEnv((void**) &env, JNI_VERSION_1_6);
- if (envStatus == JNI_EDETACHED) {
- jvm->AttachCurrentThread(&env, NULL);
- }
- jclass mainc = env->FindClass("com/mojang/minecraftpe/MainActivity");
- mainClass = (jclass) env->NewGlobalRef(mainc);
- MCPE_Item_items = (MCPE_Item**) dlsym(RTLD_DEFAULT, "_ZN4Item5itemsE");
- MCPE_Inventory_add = (void (*)(MCPE_Inventory*, MCPE_ItemInstance*)) dlsym(RTLD_DEFAULT, "_ZN9Inventory3addEP12ItemInstance");
- MCPE_Item_getDescriptionId = (std::string (*)(MCPE_Item*)) dlsym(RTLD_DEFAULT, "_ZNK8TileItem16getDescriptionIdEv");
- setupGUI();
- GUIBaseScreen* screen = getGUIScreen();
- GUIButton* btn = new GUIButton(0, 0, 12, 20, 20, "M");
- btn->setClick(showList);
- screen->addElement(btn);
- void* inv = dlsym(RTLD_DEFAULT, "_ZN9InventoryC1EP6Playerb");
- mcpelauncher_hook(inv, (void*) &inventoryHook, (void**) &MCPE_Inventory_Inventory);
- return JNI_VERSION_1_2;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement