Unsure how to fix script issue
-
Hello, my goal is to detect if an item is a fish within a certain server then deposit it automatically into whatever the current open container is. Fish detection works fine, the only issue with my code is moving it to the container (for example an open single chest). Currently what happens is the fish is attempted to be moved into the chest but it continuously gets sent back and just doesn't work.
/// api_version=2 var ENABLED = false; var DEBUG = false; function Log(message) { if (DEBUG) Chat.print(message); } var script = registerScript({ name: "AutoFishSave", version: "1.0.0", authors: [""] }); script.registerModule({ name: "AutoFishSave", category: "Misc", description: "Automatically deposit fish into your open storage." }, function (module) { module.on("enable", function() { ENABLED = true; }); module.on("disable", function() { ENABLED = false; }); module.on("update", function() { if (ENABLED) { Log("Initial execution.") var Inventory = mc.thePlayer.inventory.mainInventory; Log("Inventory found, length is " + Inventory.length + ".") for (var Idx = 0; Idx < Inventory.length; Idx++) { Log("Iterating through inventory, index is " + Idx + ".") var Item = Inventory[Idx]; if (Item == null) continue; Log("Item found.") var ToolTipData = Item.getTooltip(mc.thePlayer, true); if (ToolTipData == null) continue; Log("Tooltip data found, length is " + ToolTipData.length + ".") var IsFish = false; // for debugging set to true; for (var SubIdx = 0; SubIdx < ToolTipData.length; SubIdx++) { var TTLine = ToolTipData[SubIdx]; if (TTLine.contains("fish") && !TTLine.contains("rod")) { IsFish = true; break; } } Log("IsFish is " + IsFish + ".") if (!IsFish) continue; var CurrentOpenContainer = mc.thePlayer.openContainer; if (CurrentOpenContainer == null || CurrentOpenContainer.windowId == 0) continue; Log("Suitable container found.") mc.playerController.windowClick( CurrentOpenContainer.windowId, Idx, 0, 2, mc.thePlayer ); Log("Moved fish to storage.") } } }); });
-
Well i do like that you tried to debug. There are a couple vars that are not needed, e.g. ENABLED because onUpdate will only be called when the module is enabled - you don't need to worry about that.
I just recoded your script to get it working. I don't know if this is the most memory efficient way to do it tho. If you want to add some Items you will have to do that on your own (via import ...).
This script won't allow to take fish out of a storage when enabled, so you could optimize it.
To understand what is going on you could take a look at this script since what you want is basically a reverse chest stealer
///api_version=2 (script = registerScript({ name: "DepositFish", version: "1.0", authors: ["some fisherman"] })); var GuiChest = Java.type("net.minecraft.client.gui.inventory.GuiChest"), ItemFish = Java.type("net.minecraft.item.ItemFishFood") script.registerModule({ name: "DepositFish", category: "Misc", description: "Stores fish lol" }, function (module) { module.on("update", function() { if (mc.currentScreen instanceof GuiChest) { var inventoryContainer = Java.from(mc.thePlayer.inventoryContainer.getInventory()) var openContainer = Java.from(mc.thePlayer.openContainer.getInventory()) var fishSlots = inventoryContainer.filter(function (stack) stack && stack.getItem() instanceof ItemFish/*|| you can add more items here, you need to import them*/) fishSlots.length && fishSlots.forEach(function (stack) mc.playerController.windowClick(mc.thePlayer.openContainer.windowId, openContainer.indexOf(stack), 0, 1, mc.thePlayer)); } }); });
PS: Unique way of detecting a fish! :))
-
@FaaatPotato Actually it is too cool to not be used. So I included your way of doing it and you can whitelist items via text setting
///api_version=2 (script = registerScript({ name: "DepositFish", version: "1.0", authors: ["some fisherman"] })); var GuiChest = Java.type("net.minecraft.client.gui.inventory.GuiChest"), ItemFish = Java.type("net.minecraft.item.ItemFishFood") script.registerModule({ name: "DepositFish", category: "Misc", description: "Stores fish lol", settings: { modeValue: mode = Setting.list({ name: "Mode", values: ["FaaatPotato", "ToolTip"], default: "FaaatPotato" }), itemsValue: items = Setting.text({ name: "ItemsToStore", default: "fish", isSupported: function() { return mode.get() == "ToolTip" } }) } }, function (module) { module.on("update", function() { if (mc.currentScreen instanceof GuiChest) { var inventoryContainer = Java.from(mc.thePlayer.inventoryContainer.getInventory()) var openContainer = Java.from(mc.thePlayer.openContainer.getInventory()) if (mode.get() == "FaaatPotato") { var fishSlots = inventoryContainer.filter(function (stack) stack && stack.getItem() instanceof ItemFish/*|| you can add more items here, you need to import them*/) fishSlots.length && fishSlots.forEach(function (stack) mc.playerController.windowClick(mc.thePlayer.openContainer.windowId, openContainer.indexOf(stack), 0, 1, mc.thePlayer)); } if (mode.get() == "ToolTip") { var itemsToStore = items.get().toLowerCase().split(",") var itemSlots = inventoryContainer.filter(function (stack) stack && itemsToStore.some(function (itemName) Java.from(stack.getTooltip(mc.thePlayer, true)).toString().toLowerCase().contains(itemName))) itemSlots.length && itemSlots.forEach(function (stack) mc.playerController.windowClick(mc.thePlayer.openContainer.windowId, openContainer.indexOf(stack), 0, 1, mc.thePlayer)) } } }); });
just make sure to type the whole item name in the text field so no similar items will be put in the storage
e.g.
.depositfish itemstostore diamond_sword,fishing_rod,fishhowever this could lead to issues if names contain for example fish as in fishing_rod. Thats why you checked for != contains("rod"). But that should be solvable for you.