Jump to content

Recommended Posts

Posted (edited)

This code show up a window with a list of ur target's inventory  , and removes an item or more .

code is not mine i found it on net i think credits go to Zealar , i just readapted it to 374 aCis rev .
anyway it may come usefull for wiping bot's inv or ... w/e 

code tested and workin .

 

package net.sf.l2j.gameserver.handler.admincommandhandlers;

import java.util.Set;

import net.sf.l2j.commons.lang.StringUtil;

import net.sf.l2j.gameserver.handler.IAdminCommandHandler;
import net.sf.l2j.gameserver.model.actor.instance.Player;
import net.sf.l2j.gameserver.model.item.instance.ItemInstance;
import net.sf.l2j.gameserver.network.serverpackets.NpcHtmlMessage;


/**
 * This class handles following admin commands:
 * <ul>
 * <li>show_ivetory</li>
 * <li>delete_item</li>
 * </ul>
 * @author Zealar
 */

public class AdminInventory implements IAdminCommandHandler
{
	private static final String[] ADMIN_COMMANDS =
	{
		"admin_show_inventory",
		"admin_delete_item"
	};
	
	@Override
	public boolean useAdminCommand(String command, Player activeChar)
	{
		if ((activeChar.getTarget() == null))
		{
			activeChar.sendMessage("Select a target");
			return false;
		}
		
		if (!(activeChar.getTarget() instanceof Player))
		{
			activeChar.sendMessage("Target need to be player");
			return false;
		}
		
		Player player = activeChar.getTarget().getActingPlayer();
		
		if (command.startsWith(ADMIN_COMMANDS[0]))
		{
			if (command.length() > ADMIN_COMMANDS[0].length())
			{
				String com = command.substring(ADMIN_COMMANDS[0].length() + 1);
				if (StringUtil.isDigit(com))
				{
					showItemsPage(activeChar, Integer.parseInt(com));
				}
			}
			
			else
			{
				showItemsPage(activeChar, 0);
			}
		}
		else if (command.contains(ADMIN_COMMANDS[1]))
		{
			String val = command.substring(ADMIN_COMMANDS[1].length() + 1);
			
			player.destroyItem("GM Destroy", Integer.parseInt(val), player.getInventory().getItemByObjectId(Integer.parseInt(val)).getCount(), null, true);
			showItemsPage(activeChar, 0);
		}
		
		return true;
	}
	
	private static void showItemsPage(Player activeChar, int page)
	{
		final Player target = activeChar.getTarget().getActingPlayer();
		

		final Set<ItemInstance> items = target.getInventory().getItems();
		
		int maxItemsPerPage = 16;
		int maxPages = items.size() / maxItemsPerPage;
		if (items.size() > (maxItemsPerPage * maxPages))
		{
			maxPages++;
		}
	
		if (page > maxPages)
		{
			page = maxPages;
		}
		
		int itemsStart = maxItemsPerPage * page;
		int itemsEnd = items.size();
		if ((itemsEnd - itemsStart) > maxItemsPerPage)
		{
			itemsEnd = itemsStart + maxItemsPerPage;
		}
		
	final NpcHtmlMessage adminReply = new NpcHtmlMessage(0);
		adminReply.setFile("data/html/admin/inventory.htm");
		adminReply.replace("%PLAYER_NAME%", target.getName());
		
		StringBuilder sbPages = new StringBuilder();
		for (int x = 0; x < maxPages; x++)
		{
			int pagenr = x + 1;
			sbPages.append("<td><button value=\"" + String.valueOf(pagenr) + "\" action=\"bypass -h admin_show_inventory " + String.valueOf(x) + "\" width=14 height=14 back=\"sek.cbui67\" fore=\"sek.cbui67\"></td>");
		}
		
		adminReply.replace("%PAGES%", sbPages.toString());
		
		StringBuilder sbItems = new StringBuilder();
		
		for (ItemInstance item: items)
		{
			sbItems.append("<tr><td><button action=\"bypass -h admin_delete_item " + String.valueOf(item.getObjectId()) + "\" width=16 height=16 back=\"L2UI.bbs_delete\" fore=\"L2UI.bbs_delete\">" + "</td>");
			sbItems.append("<td width=60>" + item.getName() + "</td></tr><br>");
		}
		
		adminReply.replace("%ITEMS%", sbItems.toString());
		
		activeChar.sendPacket(adminReply);
	}
	
	@Override
	public String[] getAdminCommandList()
	{
		return ADMIN_COMMANDS;
	}
} 

register the handler

 

 

and  create inventory.htm into  gameserver/data/html/admin 


<html><body><title>Inventory : %PLAYER_NAME%</title>
<center>
<table width=270>
 <tr>
 %PAGES%
 </tr>
</table>

___________________________________
</center>
<br>
<table width=270>
 %ITEMS%
</table>
</body>
</html>

 

Edited by Kishin
  • Upvote 2
Posted

isDigit(String text)  is already part of StringUtil.

 

You got already ingame, a way to see inventories using alt+g > Inventory (redundant with show_inventory).

 

I introduced a way to easy cut pages (see //knownlist for exemple - but it's used on any page system normally).

 

It's optional, but there is no way to remove a quantity for a stackable.

 

For wiping all items, you should use ItemContainer#destroyAllItems(String process, Player actor, WorldObject reference) and introduces a new admincommand (delete_all_items, I guess) rather than clicking on every single item from inventory (up to 120 items for dwarves, 80 for regular ppls).

Posted (edited)
2 hours ago, Tryskell said:

isDigit(String text)  is already part of StringUtil.

 

You got already ingame, a way to see inventories using alt+g > Inventory (redundant with show_inventory).

 

I introduced a way to easy cut pages (see //knownlist for exemple - but it's used on any page system normally).

 

It's optional, but there is no way to remove a quantity for a stackable.

 

For wiping all items, you should use ItemContainer#destroyAllItems(String process, Player actor, WorldObject reference) and introduces a new admincommand (delete_all_items, I guess) rather than clicking on every single item from inventory (up to 120 items for dwarves, 80 for regular ppls).

didnt knew about the isDigit in string , i updated post , as for others its just handfull on pvp server to wipe a specific farm item or more from inv ,  can be edited to support iconstable .


wipAAkw.png

Edited by Kishin
  • 4 years later...
  • 8 months later...
Posted (edited)
On 12/29/2023 at 4:34 PM, MoodFreak said:

I tested this code, I think it have the problem with pages, it do not transfer the items to next pages and it gives the error cause of too long html.

well i didnt used to had that issue u reffering , u can always lower the page value that would show on each page . keep in mind it was taken  from h5 that it can handle more html length than interlude .

 

package handlers.admincommandhandlers;

import org.l2jmobius.gameserver.handler.IAdminCommandHandler;
import org.l2jmobius.gameserver.model.WorldObject;
import org.l2jmobius.gameserver.model.actor.Player;
import org.l2jmobius.gameserver.model.item.instance.Item;
import org.l2jmobius.gameserver.network.SystemMessageId;
import org.l2jmobius.gameserver.network.serverpackets.NpcHtmlMessage;
import org.l2jmobius.gameserver.util.Util;

public class AdminInventory implements IAdminCommandHandler {

    private static final String[] ADMIN_COMMANDS =
            {
                    "admin_showinv",
                    "admin_delete_item"
            };

    @Override
    public boolean useAdminCommand(String command, Player activeChar)
    {

        WorldObject target = activeChar.getTarget();

        if (target == null || !target.isPlayer())  {
            activeChar.sendPacket(SystemMessageId.INVALID_TARGET);
            return false;
        }

        if (command.startsWith(ADMIN_COMMANDS[0]))
        {
            if (command.length() > ADMIN_COMMANDS[0].length())
            {
                String com = command.substring(ADMIN_COMMANDS[0].length() + 1);
                if (Util.isDigit(com))
                {
                    showItemsPage(activeChar, Integer.parseInt(com));
                }
            }
            else
            {
                showItemsPage(activeChar, 0);
            }
        }

        int count = 1;
        if (command.contains(ADMIN_COMMANDS[1])) {
            String[] parts = command.split(" ");
            if (parts.length == 3) {
                try {
                    count = Integer.parseInt(parts[2]);
                } catch (NumberFormatException e) {
                    activeChar.sendMessage("Invalid quantity format.");
                    return false;
                }
            }

            if (count == 0) {
                activeChar.sendMessage("Quantity must be 1 or above, or left blank for default.");
                showItemsPage(activeChar, 0);
                return false;
            }

            String val = parts[1];
            target.getActingPlayer().destroyItem("GM Destroy", Integer.parseInt(val), count, null, true);
            showItemsPage(activeChar, 0);
        }
        return true;
    }

    private void showItemsPage(Player activeChar, int page)
    {
        final WorldObject target = activeChar.getTarget();
        final Player player = target.getActingPlayer();

        final Item[] items = player.getInventory().getItems().toArray(new Item[0]);

        int maxItemsPerPage = 13;
        int maxPages = items.length / maxItemsPerPage;
        if (items.length > (maxItemsPerPage * maxPages))
        {
            maxPages++;
        }

        if (page > maxPages)
        {
            page = maxPages;
        }

        int itemsStart = maxItemsPerPage * page;
        int itemsEnd = items.length;
        if ((itemsEnd - itemsStart) > maxItemsPerPage)
        {
            itemsEnd = itemsStart + maxItemsPerPage;
        }

        final NpcHtmlMessage adminReply = new NpcHtmlMessage(0);
        adminReply.setFile(activeChar, "data/html/admin/inventory.htm");
        adminReply.replace("%PLAYER_NAME%", activeChar.getName());

        StringBuilder sbPages = new StringBuilder();
        for (int x = 0; x < maxPages; x++)
        {
            int pagenr = x + 1;
            sbPages.append("<td><button value=\"" + pagenr + "\" action=\"bypass -h admin_showinv " + x + "\" width=20 height=20 back=\"L2UI_ct1.button_df\" fore=\"L2UI_ct1.button_df\"></td>");
        }

        adminReply.replace("%PAGES%", sbPages.toString());

        StringBuilder sbItems = getStringBuilder(itemsStart, itemsEnd, items);

        adminReply.replace("%ITEMS%", sbItems.toString());

        activeChar.sendPacket(adminReply);
    }

    private static StringBuilder getStringBuilder(int itemsStart, int itemsEnd, Item[] items) {
        StringBuilder sbItems = new StringBuilder();

        sbItems.append("<table width=270>");
        sbItems.append("<tr>");
        sbItems.append("<td width=24><b> </b></td>");
        sbItems.append("<td width=120><b> </b></td>");
        sbItems.append("<td width=90><b>Count</b></td>");
        sbItems.append("<td width=65><b>Quantity</b></td>");
        sbItems.append("</tr>");
        for (int i = itemsStart; i < itemsEnd; i++) {
            Item item = items[i];
            sbItems.append("<tr>");
            sbItems.append("<td><img src=").append(item.getTemplate().getIcon()).append(" width=24 height=24></td>");
            sbItems.append("<td>").append(item.getName()).append("</td>");
            sbItems.append("<td>").append(item.getCount()).append("</td>");
            sbItems.append("<td>");
            sbItems.append("<edit var=\"itemCount_").append(i).append("\" width=40>");
            sbItems.append("</td>");
            sbItems.append("<td><button action=\"bypass -h admin_delete_item ").append(item.getObjectId()).append(" $itemCount_").append(i).append("\" width=16 height=16 back=\"L2UI_ct1.Button_DF_Delete\" fore=\"L2UI_ct1.Button_DF_Delete\"></button></td>");
            sbItems.append("</tr>");
        }
        sbItems.append("</table>");
        return sbItems;
    }

    @Override
    public String[] getAdminCommandList()
    {
        return ADMIN_COMMANDS;
    }
}


Since i did jumped to mobius here the mobius one with quantity field aswell . 
feel free to adapt it back to acis or wherever 

 

<html><body><title>Inventory : %PLAYER_NAME%</title>
<center>
<table width=270>
    <tr>
    %PAGES%
    </tr>
</table>
</center>
<br>

    %ITEMS%

</body>
</html>


spacer.png

Edited by Acacia

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...