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