Rename and clean up MapleStorage
This commit is contained in:
@@ -166,7 +166,7 @@ public class Character extends AbstractCharacterObject {
|
||||
private PlayerShop playerShop = null;
|
||||
private Shop shop = null;
|
||||
private SkinColor skinColor = SkinColor.NORMAL;
|
||||
private MapleStorage storage = null;
|
||||
private Storage storage = null;
|
||||
private MapleTrade trade = null;
|
||||
private MonsterBook monsterbook;
|
||||
private CashShop cashshop;
|
||||
@@ -5863,7 +5863,7 @@ public class Character extends AbstractCharacterObject {
|
||||
}
|
||||
}
|
||||
|
||||
public MapleStorage getStorage() {
|
||||
public Storage getStorage() {
|
||||
return storage;
|
||||
}
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ import config.YamlConfig;
|
||||
import constants.inventory.ItemConstants;
|
||||
import net.packet.InPacket;
|
||||
import server.ItemInformationProvider;
|
||||
import server.MapleStorage;
|
||||
import server.Storage;
|
||||
import tools.FilePrinter;
|
||||
import tools.PacketCreator;
|
||||
|
||||
@@ -47,7 +47,7 @@ public class StorageProcessor {
|
||||
public static void storageAction(InPacket p, Client c) {
|
||||
ItemInformationProvider ii = ItemInformationProvider.getInstance();
|
||||
Character chr = c.getPlayer();
|
||||
MapleStorage storage = chr.getStorage();
|
||||
Storage storage = chr.getStorage();
|
||||
byte mode = p.readByte();
|
||||
|
||||
if (chr.getLevel() < 15){
|
||||
|
||||
@@ -53,7 +53,7 @@ import net.server.services.ServicesManager;
|
||||
import net.server.services.type.WorldServices;
|
||||
import net.server.task.*;
|
||||
import scripting.event.EventInstanceManager;
|
||||
import server.MapleStorage;
|
||||
import server.Storage;
|
||||
import server.TimerManager;
|
||||
import server.maps.*;
|
||||
import tools.DatabaseConnection;
|
||||
@@ -96,7 +96,7 @@ public class World {
|
||||
private MonitoredWriteLock chnWLock = MonitoredWriteLockFactory.createLock(chnLock);
|
||||
|
||||
private Map<Integer, SortedMap<Integer, Character>> accountChars = new HashMap<>();
|
||||
private Map<Integer, MapleStorage> accountStorages = new HashMap<>();
|
||||
private Map<Integer, Storage> accountStorages = new HashMap<>();
|
||||
private MonitoredReentrantLock accountCharsLock = MonitoredReentrantLockFactory.createLock(MonitoredLockType.WORLD_CHARS, true);
|
||||
|
||||
private Set<Integer> queuedGuilds = new HashSet<>();
|
||||
@@ -435,7 +435,7 @@ public class World {
|
||||
}
|
||||
|
||||
private void registerAccountStorage(Integer accountId) {
|
||||
MapleStorage storage = MapleStorage.loadOrCreateFromDB(accountId, this.id);
|
||||
Storage storage = Storage.loadOrCreateFromDB(accountId, this.id);
|
||||
accountCharsLock.lock();
|
||||
try {
|
||||
accountStorages.put(accountId, storage);
|
||||
@@ -453,7 +453,7 @@ public class World {
|
||||
}
|
||||
}
|
||||
|
||||
public MapleStorage getAccountStorage(Integer accountId) {
|
||||
public Storage getAccountStorage(Integer accountId) {
|
||||
return accountStorages.get(accountId);
|
||||
}
|
||||
|
||||
|
||||
@@ -43,28 +43,27 @@ import java.util.*;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Matze
|
||||
*/
|
||||
public class MapleStorage {
|
||||
private static Map<Integer, Integer> trunkGetCache = new HashMap<>();
|
||||
private static Map<Integer, Integer> trunkPutCache = new HashMap<>();
|
||||
|
||||
private int id;
|
||||
public class Storage {
|
||||
private static final Map<Integer, Integer> trunkGetCache = new HashMap<>();
|
||||
private static final Map<Integer, Integer> trunkPutCache = new HashMap<>();
|
||||
|
||||
private final int id;
|
||||
private int currentNpcid;
|
||||
private int meso;
|
||||
private byte slots;
|
||||
private Map<InventoryType, List<Item>> typeItems = new HashMap<>();
|
||||
private final Map<InventoryType, List<Item>> typeItems = new HashMap<>();
|
||||
private List<Item> items = new LinkedList<>();
|
||||
private Lock lock = MonitoredReentrantLockFactory.createLock(MonitoredLockType.STORAGE, true);
|
||||
private final Lock lock = MonitoredReentrantLockFactory.createLock(MonitoredLockType.STORAGE, true);
|
||||
|
||||
private MapleStorage(int id, byte slots, int meso) {
|
||||
private Storage(int id, byte slots, int meso) {
|
||||
this.id = id;
|
||||
this.slots = slots;
|
||||
this.meso = meso;
|
||||
}
|
||||
|
||||
private static MapleStorage create(int id, int world) throws SQLException {
|
||||
private static Storage create(int id, int world) throws SQLException {
|
||||
try (Connection con = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = con.prepareStatement("INSERT INTO storages (accountid, world, slots, meso) VALUES (?, ?, 4, 0)")) {
|
||||
ps.setInt(1, id);
|
||||
@@ -75,8 +74,8 @@ public class MapleStorage {
|
||||
return loadOrCreateFromDB(id, world);
|
||||
}
|
||||
|
||||
public static MapleStorage loadOrCreateFromDB(int id, int world) {
|
||||
MapleStorage ret;
|
||||
public static Storage loadOrCreateFromDB(int id, int world) {
|
||||
Storage ret;
|
||||
try (Connection con = DatabaseConnection.getConnection();
|
||||
PreparedStatement ps = con.prepareStatement("SELECT storageid, slots, meso FROM storages WHERE accountid = ? AND world = ?")) {
|
||||
ps.setInt(1, id);
|
||||
@@ -84,7 +83,7 @@ public class MapleStorage {
|
||||
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
ret = new MapleStorage(rs.getInt("storageid"), (byte) rs.getInt("slots"), rs.getInt("meso"));
|
||||
ret = new Storage(rs.getInt("storageid"), (byte) rs.getInt("slots"), rs.getInt("meso"));
|
||||
for (Pair<Item, InventoryType> item : ItemFactory.STORAGE.loadItems(ret.id, false)) {
|
||||
ret.items.add(item.getLeft());
|
||||
}
|
||||
@@ -108,7 +107,7 @@ public class MapleStorage {
|
||||
slots += this.slots;
|
||||
return slots <= 48;
|
||||
}
|
||||
|
||||
|
||||
public boolean gainSlots(int slots) {
|
||||
lock.lock();
|
||||
try {
|
||||
@@ -123,7 +122,7 @@ public class MapleStorage {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void saveToDB(Connection con) {
|
||||
try {
|
||||
try (PreparedStatement ps = con.prepareStatement("UPDATE storages SET slots = ?, meso = ? WHERE storageid = ?")) {
|
||||
@@ -158,10 +157,10 @@ public class MapleStorage {
|
||||
lock.lock();
|
||||
try {
|
||||
boolean ret = items.remove(item);
|
||||
|
||||
|
||||
InventoryType type = item.getInventoryType();
|
||||
typeItems.put(type, new ArrayList<>(filterItems(type)));
|
||||
|
||||
|
||||
return ret;
|
||||
} finally {
|
||||
lock.unlock();
|
||||
@@ -174,12 +173,12 @@ public class MapleStorage {
|
||||
if (isFull()) { // thanks Optimist for noticing unrestricted amount of insertions here
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
items.add(item);
|
||||
|
||||
|
||||
InventoryType type = item.getInventoryType();
|
||||
typeItems.put(type, new ArrayList<>(filterItems(type)));
|
||||
|
||||
|
||||
return true;
|
||||
} finally {
|
||||
lock.unlock();
|
||||
@@ -194,11 +193,11 @@ public class MapleStorage {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private List<Item> filterItems(InventoryType type) {
|
||||
List<Item> storageItems = getItems();
|
||||
List<Item> ret = new LinkedList<>();
|
||||
|
||||
|
||||
for (Item item : storageItems) {
|
||||
if (item.getInventoryType() == type) {
|
||||
ret.add(item);
|
||||
@@ -206,7 +205,7 @@ public class MapleStorage {
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
public byte getSlot(InventoryType type, byte slot) {
|
||||
lock.lock();
|
||||
try {
|
||||
@@ -223,14 +222,14 @@ public class MapleStorage {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void sendStorage(Client c, int npcId) {
|
||||
if (c.getPlayer().getLevel() < 15){
|
||||
if (c.getPlayer().getLevel() < 15) {
|
||||
c.getPlayer().dropMessage(1, "You may only use the storage once you have reached level 15.");
|
||||
c.sendPacket(PacketCreator.enableActions());
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
lock.lock();
|
||||
try {
|
||||
items.sort((o1, o2) -> {
|
||||
@@ -241,12 +240,12 @@ public class MapleStorage {
|
||||
}
|
||||
return 1;
|
||||
});
|
||||
|
||||
|
||||
List<Item> storageItems = getItems();
|
||||
for (InventoryType type : InventoryType.values()) {
|
||||
typeItems.put(type, new ArrayList<>(storageItems));
|
||||
}
|
||||
|
||||
|
||||
currentNpcid = npcId;
|
||||
c.sendPacket(PacketCreator.getStorage(npcId, slots, storageItems, meso));
|
||||
} finally {
|
||||
@@ -271,18 +270,18 @@ public class MapleStorage {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void arrangeItems(Client c) {
|
||||
lock.lock();
|
||||
try {
|
||||
MapleStorageInventory msi = new MapleStorageInventory(c, items);
|
||||
msi.mergeItems();
|
||||
items = msi.sortItems();
|
||||
|
||||
|
||||
for (InventoryType type : InventoryType.values()) {
|
||||
typeItems.put(type, new ArrayList<>(items));
|
||||
}
|
||||
|
||||
|
||||
c.sendPacket(PacketCreator.arrangeStorage(slots, items));
|
||||
} finally {
|
||||
lock.unlock();
|
||||
@@ -303,40 +302,40 @@ public class MapleStorage {
|
||||
public void sendMeso(Client c) {
|
||||
c.sendPacket(PacketCreator.mesoStorage(slots, meso));
|
||||
}
|
||||
|
||||
|
||||
public int getStoreFee() { // thanks to GabrielSin
|
||||
int npcId = currentNpcid;
|
||||
Integer fee = trunkPutCache.get(npcId);
|
||||
if(fee == null) {
|
||||
if (fee == null) {
|
||||
fee = 100;
|
||||
|
||||
|
||||
DataProvider npc = DataProviderFactory.getDataProvider(WZFiles.NPC);
|
||||
Data npcData = npc.getData(npcId + ".img");
|
||||
if(npcData != null) {
|
||||
if (npcData != null) {
|
||||
fee = DataTool.getIntConvert("info/trunkPut", npcData, 100);
|
||||
}
|
||||
|
||||
|
||||
trunkPutCache.put(npcId, fee);
|
||||
}
|
||||
|
||||
|
||||
return fee;
|
||||
}
|
||||
|
||||
|
||||
public int getTakeOutFee() {
|
||||
int npcId = currentNpcid;
|
||||
Integer fee = trunkGetCache.get(npcId);
|
||||
if(fee == null) {
|
||||
if (fee == null) {
|
||||
fee = 0;
|
||||
|
||||
|
||||
DataProvider npc = DataProviderFactory.getDataProvider(WZFiles.NPC);
|
||||
Data npcData = npc.getData(npcId + ".img");
|
||||
if(npcData != null) {
|
||||
if (npcData != null) {
|
||||
fee = DataTool.getIntConvert("info/trunkGet", npcData, 0);
|
||||
}
|
||||
|
||||
|
||||
trunkGetCache.put(npcId, fee);
|
||||
}
|
||||
|
||||
|
||||
return fee;
|
||||
}
|
||||
|
||||
@@ -348,7 +347,7 @@ public class MapleStorage {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void close() {
|
||||
lock.lock();
|
||||
try {
|
||||
@@ -357,5 +356,5 @@ public class MapleStorage {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user