1
0
mirror of https://github.com/Rogiel/l2jserver2 synced 2025-12-05 23:22:47 +00:00

Implements item destroy

This commit is contained in:
2011-12-16 14:59:12 -02:00
parent e7832e1406
commit ccbc29d330
5 changed files with 165 additions and 5 deletions

View File

@@ -29,25 +29,26 @@ import com.l2jserver.game.net.Lineage2Client;
import com.l2jserver.game.net.packet.ClientPacket;
import com.l2jserver.game.net.packet.client.CM_ACTION_USE;
import com.l2jserver.game.net.packet.client.CM_ADMIN_COMMAND;
import com.l2jserver.game.net.packet.client.CM_CHAR_ATTACK;
import com.l2jserver.game.net.packet.client.CM_AUTH_LOGIN;
import com.l2jserver.game.net.packet.client.CM_BYPASS;
import com.l2jserver.game.net.packet.client.CM_CHAR_ACTION;
import com.l2jserver.game.net.packet.client.CM_CHAR_APPEARING;
import com.l2jserver.game.net.packet.client.CM_CHAR_ATTACK;
import com.l2jserver.game.net.packet.client.CM_CHAR_CHAT;
import com.l2jserver.game.net.packet.client.CM_CHAR_CREATE;
import com.l2jserver.game.net.packet.client.CM_CHAR_MOVE;
import com.l2jserver.game.net.packet.client.CM_CHAR_OPEN_MAP;
import com.l2jserver.game.net.packet.client.CM_CHAR_POSITION;
import com.l2jserver.game.net.packet.client.CM_CHAR_REQ_INVENTORY;
import com.l2jserver.game.net.packet.client.CM_CHAR_SELECT;
import com.l2jserver.game.net.packet.client.CM_CHAR_CHAT;
import com.l2jserver.game.net.packet.client.CM_ITEM_DROP;
import com.l2jserver.game.net.packet.client.CM_ENTER_WORLD;
import com.l2jserver.game.net.packet.client.CM_EXT_REQ_ALL_FORTRESS_INFO;
import com.l2jserver.game.net.packet.client.CM_EXT_REQ_KEY_MAPPING;
import com.l2jserver.game.net.packet.client.CM_EXT_REQ_MANOR_LIST;
import com.l2jserver.game.net.packet.client.CM_GG_KEY;
import com.l2jserver.game.net.packet.client.CM_GOTO_LOBBY;
import com.l2jserver.game.net.packet.client.CM_ITEM_DESTROY;
import com.l2jserver.game.net.packet.client.CM_ITEM_DROP;
import com.l2jserver.game.net.packet.client.CM_LOGOUT;
import com.l2jserver.game.net.packet.client.CM_PROTOCOL_VERSION;
import com.l2jserver.game.net.packet.client.CM_REQUEST_CHAR_TEMPLATE;
@@ -190,6 +191,8 @@ public class Lineage2PacketReader extends OneToOneDecoder {
return CM_CHAR_ATTACK.class;
case CM_ITEM_DROP.OPCODE:
return CM_ITEM_DROP.class;
case CM_ITEM_DESTROY.OPCODE:
return CM_ITEM_DESTROY.class;
default:
logger.warn("Unknown packet for 0x{}", Integer.toHexString(opcode));
break;

View File

@@ -0,0 +1,100 @@
/*
* This file is part of l2jserver2 <l2jserver2.com>.
*
* l2jserver2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* l2jserver2 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with l2jserver2. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jserver.game.net.packet.client;
import org.jboss.netty.buffer.ChannelBuffer;
import com.google.inject.Inject;
import com.l2jserver.game.net.Lineage2Client;
import com.l2jserver.game.net.SystemMessage;
import com.l2jserver.game.net.packet.AbstractClientPacket;
import com.l2jserver.model.id.object.ItemID;
import com.l2jserver.model.id.object.provider.ItemIDProvider;
import com.l2jserver.model.world.Item;
import com.l2jserver.service.game.item.ItemService;
import com.l2jserver.service.game.item.NotEnoughItemsServiceException;
/**
* This packet drops items on the ground.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class CM_ITEM_DESTROY extends AbstractClientPacket {
/**
* The packet OPCODE
*/
public static final int OPCODE = 0x60;
/**
* The {@link ItemService}
*/
private final ItemService itemService;
private final ItemIDProvider itemIdProvider;
/**
* The item ID
*/
private int objectId;
/**
* The number of items to be dropped
*/
private long count;
/**
* @param itemService
* the item service
* @param itemIdProvider
* the item id provider
*/
@Inject
public CM_ITEM_DESTROY(ItemService itemService,
ItemIDProvider itemIdProvider) {
this.itemService = itemService;
this.itemIdProvider = itemIdProvider;
}
@Override
public void read(Lineage2Client conn, ChannelBuffer buffer) {
objectId = buffer.readInt();
count = buffer.readLong();
}
@Override
public void process(final Lineage2Client conn) {
final ItemID id = itemIdProvider.resolveID(objectId);
final Item item = id.getObject();
if (item == null) {
conn.sendActionFailed();
return;
}
if (!conn.getCharacterID().equals(item.getOwnerID())) {
conn.sendActionFailed();
return;
}
try {
if (itemService.destroy(item, count)) {
conn.removeInventoryItems(item);
} else {
conn.updateInventoryItems(item);
}
} catch (NotEnoughItemsServiceException e) {
conn.sendSystemMessage(SystemMessage.CANNOT_DISCARD_THIS_ITEM);
}
}
}

View File

@@ -149,6 +149,13 @@ public class Item extends PositionableObject {
return ownerID;
}
/**
* @return the owner
*/
public L2Character getOwner() {
return ownerID.getObject();
}
/**
* @param ownerID
* the ownerID to set

View File

@@ -79,6 +79,29 @@ public interface ItemService extends Service {
*/
Item stack(Item... items) throws NonStackableItemsServiceException;
/**
* Destroys the given item
*
* @param item
* the item to be destroyed
* @param count
* the amount of items to be destroyed
* @return <code>true</code> if the entire item was destroyed.
* <code>false</code> only if it was partially destroyed.
* @throws NotEnoughItemsServiceException
* if <code>count</code> is bigger than {@link Item#getCount()}.
*/
boolean destroy(Item item, long count)
throws NotEnoughItemsServiceException;
/**
* Destroys several items
*
* @param items
* the items to be destroyed
*/
void destroy(Item... items);
/**
* Picks up an dropped item and places it into another players inventory
*

View File

@@ -37,6 +37,7 @@ import com.l2jserver.service.AbstractService;
import com.l2jserver.service.AbstractService.Depends;
import com.l2jserver.service.ServiceStartException;
import com.l2jserver.service.ServiceStopException;
import com.l2jserver.service.core.threading.AsyncFuture;
import com.l2jserver.service.database.DatabaseService;
import com.l2jserver.service.game.spawn.AlreadySpawnedServiceException;
import com.l2jserver.service.game.spawn.NotSpawnedServiceException;
@@ -157,6 +158,33 @@ public class ItemServiceImpl extends AbstractService implements ItemService {
return item;
}
@Override
public boolean destroy(Item item, long count)
throws NotEnoughItemsServiceException {
synchronized (item) {
Item destroyItem = split(item, count);
itemDao.delete(destroyItem);
if (destroyItem.getOwnerID() != null)
destroyItem.getOwner().getInventory().remove(destroyItem);
if (!destroyItem.equals(item))
itemDao.save(item);
return destroyItem.equals(item);
}
}
@Override
public void destroy(Item... items) {
// requests the delete and starts removing items from inventory model
AsyncFuture<Integer> async = itemDao.deleteObjectsAsync(items);
for (final Item item : items) {
if (item.getOwnerID() != null) {
item.getOwner().getInventory().remove(item);
}
}
// now wait until database operation finishes
async.awaitUninterruptibly();
}
@Override
public Item pickUp(Item item, L2Character character)
throws ItemNotOnGroundServiceException, NotSpawnedServiceException {
@@ -178,8 +206,7 @@ public class ItemServiceImpl extends AbstractService implements ItemService {
item = stack(stackItems);
Item[] deleteItems = ArrayUtils.copyArrayExcept(
Item[].class, stackItems, item);
character.getInventory().remove(deleteItems);
itemDao.deleteObjects(deleteItems);
destroy(deleteItems);
character.getInventory().add(item);
} catch (NonStackableItemsServiceException e) {
character.getInventory().add(item);