⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mapleinventorymanipulator.java.svn-base

📁 冒险岛私服Java版服务端(Odinms)源代码。学习JAVA开发的朋友
💻 SVN-BASE
📖 第 1 页 / 共 2 页
字号:
/*
	This file is part of the OdinMS Maple Story Server
    Copyright (C) 2008 Patrick Huy <patrick.huy@frz.cc> 
                       Matthias Butz <matze@odinms.de>
                       Jan Christian Meyer <vimes@odinms.de>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License version 3
    as published by the Free Software Foundation. You may not use, modify
    or distribute this program under any other version of the
    GNU Affero General Public License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
 * MapleInventoryManipulator.java
 * 
 * Created on 27. November 2007, 16:19
 * 
 * To change this template, choose Tools | Template Manager and open the template in the editor.
 */

package net.sf.odinms.server;

import java.awt.Point;
import java.util.Iterator;
import java.util.List;

import net.sf.odinms.client.Equip;
import net.sf.odinms.client.IItem;
import net.sf.odinms.client.InventoryException;
import net.sf.odinms.client.Item;
import net.sf.odinms.client.MapleClient;
import net.sf.odinms.client.MapleInventoryType;
import net.sf.odinms.tools.MaplePacketCreator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * @author Matze
 */
public class MapleInventoryManipulator {
	private static Logger log = LoggerFactory.getLogger(MapleInventoryManipulator.class);

	/** Creates a new instance of MapleInventoryManipulator */
	private MapleInventoryManipulator() {
	}

	public static boolean addById(MapleClient c, int itemId, short quantity, String logInfo) {
		MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
		MapleInventoryType type = ii.getInventoryType(itemId);
		if (!type.equals(MapleInventoryType.EQUIP)) {
			short slotMax = ii.getSlotMax(itemId);
			List<IItem> existing = c.getPlayer().getInventory(type).listById(itemId);
			if (!ii.isThrowingStar(itemId)) {
				if (existing.size() > 0) { // first update all existing slots to slotMax
					Iterator<IItem> i = existing.iterator();
					while (quantity > 0) {
						if (i.hasNext()) {
							Item eItem = (Item) i.next();
							short oldQ = eItem.getQuantity();
							if (oldQ < slotMax) {
								short newQ = (short) Math.min(oldQ + quantity, slotMax);
								quantity -= (newQ - oldQ);
								eItem.setQuantity(newQ);
								StringBuilder logMsg = new StringBuilder("Added ");
								logMsg.append(newQ - oldQ);
								logMsg.append(" items to stack, new quantity is ");
								logMsg.append(newQ);
								logMsg.append(" (");
								logMsg.append(logInfo);
								logMsg.append(" )");
								eItem.log(logMsg.toString(),false);
								c.getSession().write(MaplePacketCreator.updateInventorySlot(type, eItem));
							}
						} else
							break;
					}
				}
			}
			// add new slots if there is still something left
			while (quantity > 0 || ii.isThrowingStar(itemId)) {
				short newQ = (short) Math.min(quantity, slotMax);
				quantity -= newQ;
				Item nItem = new Item(itemId, (byte) 0, newQ);
				StringBuilder logMsg = new StringBuilder("Created while adding by id. Quantity ");
				logMsg.append(newQ);
				logMsg.append(" (");
				logMsg.append(logInfo);
				logMsg.append(" )");
				nItem.log(logMsg.toString(),false);
				byte newSlot = c.getPlayer().getInventory(type).addItem(nItem);
				if (newSlot == -1) {
					c.getSession().write(MaplePacketCreator.getInventoryFull());
					c.getSession().write(MaplePacketCreator.getShowInventoryFull());
					return false;
				}
				c.getSession().write(MaplePacketCreator.addInventorySlot(type, nItem));
				if (ii.isThrowingStar(itemId) && quantity == 0) break;
			}
		} else {
			if (quantity == 1) {
				IItem nEquip = ii.getEquipById(itemId);
				StringBuilder logMsg = new StringBuilder("Created while adding by id. (");
				logMsg.append(logInfo);
				logMsg.append(" )");
				nEquip.log(logMsg.toString(),false);

				byte newSlot = c.getPlayer().getInventory(type).addItem(nEquip);
				if (newSlot == -1) {
					c.getSession().write(MaplePacketCreator.getInventoryFull());
					c.getSession().write(MaplePacketCreator.getShowInventoryFull());
					return false;
				}
				c.getSession().write(MaplePacketCreator.addInventorySlot(type, nEquip));
			} else {
				throw new InventoryException("Trying to create equip with non-one quantity");
			}
		}
		return true;
	}

	public static boolean addFromDrop(MapleClient c, IItem item, String logInfo) {
		MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
		MapleInventoryType type = ii.getInventoryType(item.getItemId());

		short quantity = item.getQuantity();
		if (!type.equals(MapleInventoryType.EQUIP)) {
			short slotMax = ii.getSlotMax(item.getItemId());
			List<IItem> existing = c.getPlayer().getInventory(type).listById(item.getItemId());
			if (!ii.isThrowingStar(item.getItemId())) {
				if (existing.size() > 0) { // first update all existing slots to slotMax
					Iterator<IItem> i = existing.iterator();
					while (quantity > 0) {
						if (i.hasNext()) {
							Item eItem = (Item) i.next();
							short oldQ = eItem.getQuantity();
							if (oldQ < slotMax) {
								short newQ = (short) Math.min(oldQ + quantity, slotMax);
								quantity -= (newQ - oldQ);
								eItem.setQuantity(newQ);
								StringBuilder logMsg = new StringBuilder("Added ");
								logMsg.append(newQ - oldQ);
								logMsg.append(" items to stack, new quantity is ");
								logMsg.append(newQ);
								logMsg.append(" (");
								logMsg.append(logInfo);
								logMsg.append(" )");
								eItem.log(logMsg.toString(),false);
								c.getSession().write(MaplePacketCreator.updateInventorySlot(type, eItem, true));
							}
						} else
							break;
					}
				}
			}
			// add new slots if there is still something left
			while (quantity > 0 || ii.isThrowingStar(item.getItemId())) {
				short newQ = (short) Math.min(quantity, slotMax);
				quantity -= newQ;
				Item nItem = new Item(item.getItemId(), (byte) 0, newQ);
				StringBuilder logMsg = new StringBuilder("Created while adding from drop. Quantity ");
				logMsg.append(newQ);
				logMsg.append(" (");
				logMsg.append(logInfo);
				logMsg.append(" )");
				nItem.log(logMsg.toString(),false);
				byte newSlot = c.getPlayer().getInventory(type).addItem(nItem);
				if (newSlot == -1) {
					c.getSession().write(MaplePacketCreator.getInventoryFull());
					c.getSession().write(MaplePacketCreator.getShowInventoryFull());
					return false;
				}
				c.getSession().write(MaplePacketCreator.addInventorySlot(type, nItem, true));
				if (ii.isThrowingStar(item.getItemId()) && quantity == 0) break;
			}
		} else {
			if (quantity == 1) {
				byte newSlot = c.getPlayer().getInventory(type).addItem(item);
				StringBuilder logMsg = new StringBuilder("Adding from drop. (");
				logMsg.append(logInfo);
				logMsg.append(" )");
				item.log(logMsg.toString(),false);

				if (newSlot == -1) {
					c.getSession().write(MaplePacketCreator.getInventoryFull());
					c.getSession().write(MaplePacketCreator.getShowInventoryFull());
					return false;
				}
				c.getSession().write(MaplePacketCreator.addInventorySlot(type, item, true));
			} else {
				throw new RuntimeException("Trying to create equip with non-one quantity");
			}
		}
		c.getSession().write(MaplePacketCreator.getShowItemGain(item.getItemId(), item.getQuantity()));
		return true;
	}

	public static void removeFromSlot(MapleClient c, MapleInventoryType type, byte slot, short quantity,
										boolean fromDrop) {
		removeFromSlot(c, type, slot, quantity, fromDrop, false);
	}

	public static void removeFromSlot(MapleClient c, MapleInventoryType type, byte slot, short quantity,
										boolean fromDrop, boolean consume) {
		IItem item = c.getPlayer().getInventory(type).getItem(slot);
		boolean allowZero = consume && MapleItemInformationProvider.getInstance().isThrowingStar(item.getItemId());
		c.getPlayer().getInventory(type).removeItem(slot, quantity, allowZero);
		if (item.getQuantity() == 0 && !allowZero) {
			c.getSession().write(MaplePacketCreator.clearInventoryItem(type, item.getPosition(), fromDrop));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -