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

Implements item drop mode configuration

This commit is contained in:
2011-12-29 01:16:50 -02:00
parent 394a2853e2
commit c5911ea884
8 changed files with 144 additions and 22 deletions

View File

@@ -21,12 +21,20 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* An abstract service implementing basic life-cycle methods. * An abstract service implementing basic life-cycle methods.
* *
* @author <a href="http://www.rogiel.com">Rogiel</a> * @author <a href="http://www.rogiel.com">Rogiel</a>
*/ */
public abstract class AbstractService implements Service { public abstract class AbstractService implements Service {
/**
* The service logger
*/
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
/** /**
* Running state of a service * Running state of a service
*/ */

View File

@@ -30,8 +30,6 @@ public class ArrayUtils {
/** /**
* Copy an entire array except objects in <code>except</code> array. * Copy an entire array except objects in <code>except</code> array.
* *
* @param type
* the array type
* @param array * @param array
* the source array * the source array
* @param except * @param except
@@ -39,8 +37,8 @@ public class ArrayUtils {
* @return the copied array * @return the copied array
*/ */
@SafeVarargs @SafeVarargs
public final static <T> T[] copyArrayExcept(Class<T[]> type, T[] array, @SuppressWarnings("unchecked")
T... except) { public final static <T> T[] copyArrayExcept(T[] array, T... except) {
final List<T> values = CollectionFactory.newList(); final List<T> values = CollectionFactory.newList();
for (final T item : array) { for (final T item : array) {
if (Arrays.binarySearch(except, item, new Comparator<T>() { if (Arrays.binarySearch(except, item, new Comparator<T>() {
@@ -52,6 +50,6 @@ public class ArrayUtils {
values.add(item); values.add(item);
} }
} }
return Arrays.copyOf(values.toArray(), values.size(), type); return (T[]) Arrays.copyOf(values.toArray(), values.size(), array.getClass());
} }
} }

View File

@@ -26,6 +26,7 @@ import com.l2jserver.util.transformer.impl.BooleanTransformer;
import com.l2jserver.util.transformer.impl.ByteTransformer; import com.l2jserver.util.transformer.impl.ByteTransformer;
import com.l2jserver.util.transformer.impl.ClassTransformer; import com.l2jserver.util.transformer.impl.ClassTransformer;
import com.l2jserver.util.transformer.impl.DoubleTransformer; import com.l2jserver.util.transformer.impl.DoubleTransformer;
import com.l2jserver.util.transformer.impl.EnumTransformer;
import com.l2jserver.util.transformer.impl.FileTransformer; import com.l2jserver.util.transformer.impl.FileTransformer;
import com.l2jserver.util.transformer.impl.FloatTransformer; import com.l2jserver.util.transformer.impl.FloatTransformer;
import com.l2jserver.util.transformer.impl.InetSocketAddressTransformer; import com.l2jserver.util.transformer.impl.InetSocketAddressTransformer;
@@ -77,6 +78,8 @@ public class TransformerFactory {
return URLTransformer.SHARED_INSTANCE; return URLTransformer.SHARED_INSTANCE;
} else if (type == Path.class) { } else if (type == Path.class) {
return PathTransformer.SHARED_INSTANCE; return PathTransformer.SHARED_INSTANCE;
} else if(type.isEnum()) {
return EnumTransformer.SHARED_INSTANCE;
} }
return null; return null;
} }

View File

@@ -29,7 +29,7 @@ import org.junit.Test;
*/ */
public class ArrayUtilsTest extends ArrayUtils { public class ArrayUtilsTest extends ArrayUtils {
/** /**
* Test for {@link ArrayUtils#copyArrayExcept(Class, Object[], Object...)} * Test for {@link ArrayUtils#copyArrayExcept(Object[], Object...)}
*/ */
@Test @Test
public void testCopyArrayExcept() { public void testCopyArrayExcept() {
@@ -38,7 +38,7 @@ public class ArrayUtilsTest extends ArrayUtils {
final TestClass objC = new TestClass("c"); final TestClass objC = new TestClass("c");
TestClass[] arr = new TestClass[] { objA, objB, objC }; TestClass[] arr = new TestClass[] { objA, objB, objC };
TestClass[] selected = copyArrayExcept(TestClass[].class, arr, objB); TestClass[] selected = copyArrayExcept(arr, objB);
System.out.println(Arrays.toString(selected)); System.out.println(Arrays.toString(selected));
Assert.assertTrue(Arrays.equals(new TestClass[] { objA, objC }, Assert.assertTrue(Arrays.equals(new TestClass[] { objA, objC },

View File

@@ -96,7 +96,13 @@
<service interface="com.l2jserver.service.game.npc.NPCService" <service interface="com.l2jserver.service.game.npc.NPCService"
implementation="com.l2jserver.service.game.npc.NPCServiceImpl" /> implementation="com.l2jserver.service.game.npc.NPCServiceImpl" />
<service interface="com.l2jserver.service.game.item.ItemService" <service interface="com.l2jserver.service.game.item.ItemService"
implementation="com.l2jserver.service.game.item.ItemServiceImpl" /> implementation="com.l2jserver.service.game.item.ItemServiceImpl">
<!-- Whether drops are persisted in the database. Valid modes are: -->
<!-- ALL - All types of drops are stored into the database -->
<!-- CHARACTER_ONLY - Only items dropped by characters are stored in the database -->
<!-- NONE - None of the dropped items are saved into the database -->
<drop persistent="ALL" />
</service>
<service interface="com.l2jserver.service.game.world.WorldService" <service interface="com.l2jserver.service.game.world.WorldService"
implementation="com.l2jserver.service.game.world.WorldServiceImpl" /> implementation="com.l2jserver.service.game.world.WorldServiceImpl" />
<service interface="com.l2jserver.service.game.world.event.WorldEventDispatcher" <service interface="com.l2jserver.service.game.world.event.WorldEventDispatcher"

View File

@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!-- This file should be renamed as "services.xml" and configured according <!-- This file should be copied as "services.xml" and configured according
to your needs. Since "services.xml" has been added to ".gitignore", this to your needs. Since "services.xml" has been added to ".gitignore", this
means that your password won't be sent to the repository when you make a means that your password won't be sent to the repository when you make a
commit. --> commit. -->
@@ -64,11 +64,16 @@
implementation="com.l2jserver.service.core.Log4JLoggingService"> implementation="com.l2jserver.service.core.Log4JLoggingService">
<logger name="" level="ERROR" /> <logger name="" level="ERROR" />
<logger name="com.l2jserver" level="INFO" /> <logger name="com.l2jserver" level="INFO" />
<logger name="com.l2jserver.service.game.template.XMLTemplateService" level="INFO" /> <logger name="com.l2jserver.service.game.template.XMLTemplateService"
level="INFO" />
<logger name="com.l2jserver.service.cache" level="INFO" /> <logger name="com.l2jserver.service.cache" level="INFO" />
<logger name="com.l2jserver.service.database.sql.AbstractSQLDatabaseService" level="INFO" /> <logger
<logger name="com.l2jserver.service.game.world.CachedWorldIDService" level="INFO" /> name="com.l2jserver.service.database.sql.AbstractSQLDatabaseService"
<logger name="com.l2jserver.model.id.object.allocator.BitSetIDAllocator" level="INFO" /> level="INFO" />
<logger name="com.l2jserver.service.game.world.CachedWorldIDService"
level="INFO" />
<logger name="com.l2jserver.model.id.object.allocator.BitSetIDAllocator"
level="INFO" />
</service> </service>
<!-- ###################################################################### --> <!-- ###################################################################### -->
@@ -112,7 +117,14 @@
<service interface="com.l2jserver.service.game.npc.NPCService" <service interface="com.l2jserver.service.game.npc.NPCService"
implementation="com.l2jserver.service.game.npc.NPCServiceImpl" /> implementation="com.l2jserver.service.game.npc.NPCServiceImpl" />
<service interface="com.l2jserver.service.game.item.ItemService" <service interface="com.l2jserver.service.game.item.ItemService"
implementation="com.l2jserver.service.game.item.ItemServiceImpl" /> implementation="com.l2jserver.service.game.item.ItemServiceImpl">
<!-- Whether drops are persisted in the database. Valid modes are: -->
<!-- ALL - All types of drops are stored into the database -->
<!-- CHARACTER_ONLY - Only items dropped by characters are stored in the
database -->
<!-- NONE - None of the dropped items are saved into the database -->
<drop persistent="ALL" />
</service>
<service interface="com.l2jserver.service.game.world.WorldService" <service interface="com.l2jserver.service.game.world.WorldService"
implementation="com.l2jserver.service.game.world.WorldServiceImpl" /> implementation="com.l2jserver.service.game.world.WorldServiceImpl" />
<service interface="com.l2jserver.service.game.world.event.WorldEventDispatcher" <service interface="com.l2jserver.service.game.world.event.WorldEventDispatcher"

View File

@@ -0,0 +1,66 @@
/*
* 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.service.game.item;
import com.l2jserver.service.ServiceConfiguration;
import com.l2jserver.service.configuration.XMLConfigurationService.ConfigurationXPath;
/**
* Defines configurations for {@link ItemService} implementations
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public interface ItemServiceConfiguration extends ServiceConfiguration {
/**
* Gets the item drop mode
*
* @return the drop mode
*/
@ConfigurationPropertyGetter(defaultValue = "ALL")
@ConfigurationXPath("drop/@persistent")
ItemServiceDropMode getItemDropMode();
/**
* Sets the item drop mode
*
* @param mode
* the drop mode
*/
@ConfigurationPropertySetter
@ConfigurationXPath("drop/@persistent")
void setItemDropMode(ItemServiceDropMode mode);
/**
* The drop modes available
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public enum ItemServiceDropMode {
/**
* All types of drops are stored into the database.
*/
ALL,
/**
* Only items dropped by characters are stored in the database.
*/
CHARACTER_ONLY,
/**
* None of the dropped items are saved into the database.
*/
NONE;
}
}

View File

@@ -33,7 +33,7 @@ import com.l2jserver.model.world.L2Character;
import com.l2jserver.model.world.character.CharacterInventory.ItemLocation; import com.l2jserver.model.world.character.CharacterInventory.ItemLocation;
import com.l2jserver.model.world.item.ItemDropEvent; import com.l2jserver.model.world.item.ItemDropEvent;
import com.l2jserver.model.world.item.ItemPickEvent; import com.l2jserver.model.world.item.ItemPickEvent;
import com.l2jserver.service.AbstractService; import com.l2jserver.service.AbstractConfigurableService;
import com.l2jserver.service.AbstractService.Depends; import com.l2jserver.service.AbstractService.Depends;
import com.l2jserver.service.ServiceStartException; import com.l2jserver.service.ServiceStartException;
import com.l2jserver.service.ServiceStopException; import com.l2jserver.service.ServiceStopException;
@@ -52,7 +52,9 @@ import com.l2jserver.util.geometry.Point3D;
* @author <a href="http://www.rogiel.com">Rogiel</a> * @author <a href="http://www.rogiel.com">Rogiel</a>
*/ */
@Depends({ SpawnService.class, DatabaseService.class }) @Depends({ SpawnService.class, DatabaseService.class })
public class ItemServiceImpl extends AbstractService implements ItemService { public class ItemServiceImpl extends
AbstractConfigurableService<ItemServiceConfiguration> implements
ItemService {
/** /**
* The item DAO * The item DAO
*/ */
@@ -88,6 +90,7 @@ public class ItemServiceImpl extends AbstractService implements ItemService {
@Inject @Inject
private ItemServiceImpl(ItemDAO itemDao, SpawnService spawnService, private ItemServiceImpl(ItemDAO itemDao, SpawnService spawnService,
WorldEventDispatcher eventDispatcher, ItemIDProvider itemIdProvider) { WorldEventDispatcher eventDispatcher, ItemIDProvider itemIdProvider) {
super(ItemServiceConfiguration.class);
this.itemDao = itemDao; this.itemDao = itemDao;
this.spawnService = spawnService; this.spawnService = spawnService;
this.eventDispatcher = eventDispatcher; this.eventDispatcher = eventDispatcher;
@@ -96,6 +99,7 @@ public class ItemServiceImpl extends AbstractService implements ItemService {
@Override @Override
protected void doStart() throws ServiceStartException { protected void doStart() throws ServiceStartException {
logger.info("ItemService drop mode is {}", config.getItemDropMode());
items = itemDao.selectDroppedItems(); items = itemDao.selectDroppedItems();
try { try {
for (final Item item : items) { for (final Item item : items) {
@@ -204,8 +208,8 @@ public class ItemServiceImpl extends AbstractService implements ItemService {
stackItems[items.length] = item; stackItems[items.length] = item;
try { try {
item = stack(stackItems); item = stack(stackItems);
Item[] deleteItems = ArrayUtils.copyArrayExcept( Item[] deleteItems = ArrayUtils.copyArrayExcept(stackItems,
Item[].class, stackItems, item); item);
destroy(deleteItems); destroy(deleteItems);
character.getInventory().add(item); character.getInventory().add(item);
} catch (NonStackableItemsServiceException e) { } catch (NonStackableItemsServiceException e) {
@@ -218,6 +222,11 @@ public class ItemServiceImpl extends AbstractService implements ItemService {
character.getInventory().add(item); character.getInventory().add(item);
this.items.remove(item); this.items.remove(item);
// removes transient state
if (item.equals(originalItem)
&& item.getObjectDesire() == ObjectDesire.TRANSIENT)
item.setObjectDesire(ObjectDesire.UPDATE);
itemDao.saveObjectsAsync(item); itemDao.saveObjectsAsync(item);
if (!item.equals(originalItem)) { if (!item.equals(originalItem)) {
itemDao.saveObjectsAsync(originalItem); itemDao.saveObjectsAsync(originalItem);
@@ -254,10 +263,30 @@ public class ItemServiceImpl extends AbstractService implements ItemService {
} }
} }
boolean persist = true;
switch (config.getItemDropMode()) {
case ALL:
persist = true;
break;
case CHARACTER_ONLY:
persist = (actor instanceof L2Character);
break;
case NONE:
persist = false;
break;
}
if (persist) {
itemDao.saveObjectsAsync(item); itemDao.saveObjectsAsync(item);
if (!item.equals(sourceItem)) { if (!item.equals(sourceItem)) {
itemDao.saveObjectsAsync(sourceItem); itemDao.saveObjectsAsync(sourceItem);
} }
} else {
if (item.equals(sourceItem)) {
itemDao.delete(item);
item.setObjectDesire(ObjectDesire.TRANSIENT);
}
}
items.add(item); items.add(item);
return item; return item;