Rename and clean up MapleStorageInventory
This commit is contained in:
@@ -274,7 +274,7 @@ public class Storage {
|
|||||||
public void arrangeItems(Client c) {
|
public void arrangeItems(Client c) {
|
||||||
lock.lock();
|
lock.lock();
|
||||||
try {
|
try {
|
||||||
MapleStorageInventory msi = new MapleStorageInventory(c, items);
|
StorageInventory msi = new StorageInventory(c, items);
|
||||||
msi.mergeItems();
|
msi.mergeItems();
|
||||||
items = msi.sortItems();
|
items = msi.sortItems();
|
||||||
|
|
||||||
|
|||||||
@@ -28,161 +28,19 @@ import constants.inventory.ItemConstants;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @author RonanLana
|
* @author RonanLana
|
||||||
*/
|
*/
|
||||||
|
public class StorageInventory {
|
||||||
class PairedQuicksort {
|
private final Client c;
|
||||||
private int i = 0;
|
|
||||||
private int j = 0;
|
|
||||||
private final ArrayList<Integer> intersect;
|
|
||||||
ItemInformationProvider ii = ItemInformationProvider.getInstance();
|
|
||||||
|
|
||||||
private void PartitionByItemId(int Esq, int Dir, ArrayList<Item> A) {
|
|
||||||
Item x, w;
|
|
||||||
|
|
||||||
i = Esq;
|
|
||||||
j = Dir;
|
|
||||||
|
|
||||||
x = A.get((i + j) / 2);
|
|
||||||
do {
|
|
||||||
while (x.getItemId() > A.get(i).getItemId()) i++;
|
|
||||||
while (x.getItemId() < A.get(j).getItemId()) j--;
|
|
||||||
|
|
||||||
if (i <= j) {
|
|
||||||
w = A.get(i);
|
|
||||||
A.set(i, A.get(j));
|
|
||||||
A.set(j, w);
|
|
||||||
|
|
||||||
i++;
|
|
||||||
j--;
|
|
||||||
}
|
|
||||||
} while (i <= j);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void PartitionByName(int Esq, int Dir, ArrayList<Item> A) {
|
|
||||||
Item x, w;
|
|
||||||
|
|
||||||
i = Esq;
|
|
||||||
j = Dir;
|
|
||||||
|
|
||||||
x = A.get((i + j) / 2);
|
|
||||||
do {
|
|
||||||
while (ii.getName(x.getItemId()).compareTo(ii.getName(A.get(i).getItemId())) > 0) i++;
|
|
||||||
while (ii.getName(x.getItemId()).compareTo(ii.getName(A.get(j).getItemId())) < 0) j--;
|
|
||||||
|
|
||||||
if (i <= j) {
|
|
||||||
w = A.get(i);
|
|
||||||
A.set(i, A.get(j));
|
|
||||||
A.set(j, w);
|
|
||||||
|
|
||||||
i++;
|
|
||||||
j--;
|
|
||||||
}
|
|
||||||
} while (i <= j);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void PartitionByQuantity(int Esq, int Dir, ArrayList<Item> A) {
|
|
||||||
Item x, w;
|
|
||||||
|
|
||||||
i = Esq;
|
|
||||||
j = Dir;
|
|
||||||
|
|
||||||
x = A.get((i + j) / 2);
|
|
||||||
do {
|
|
||||||
while (x.getQuantity() > A.get(i).getQuantity()) i++;
|
|
||||||
while (x.getQuantity() < A.get(j).getQuantity()) j--;
|
|
||||||
|
|
||||||
if (i <= j) {
|
|
||||||
w = A.get(i);
|
|
||||||
A.set(i, A.get(j));
|
|
||||||
A.set(j, w);
|
|
||||||
|
|
||||||
i++;
|
|
||||||
j--;
|
|
||||||
}
|
|
||||||
} while (i <= j);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void PartitionByLevel(int Esq, int Dir, ArrayList<Item> A) {
|
|
||||||
Equip x, w, eqpI, eqpJ;
|
|
||||||
|
|
||||||
i = Esq;
|
|
||||||
j = Dir;
|
|
||||||
|
|
||||||
x = (Equip)(A.get((i + j) / 2));
|
|
||||||
|
|
||||||
do {
|
|
||||||
eqpI = (Equip)A.get(i);
|
|
||||||
eqpJ = (Equip)A.get(j);
|
|
||||||
|
|
||||||
while (x.getLevel() > eqpI.getLevel()) i++;
|
|
||||||
while (x.getLevel() < eqpJ.getLevel()) j--;
|
|
||||||
|
|
||||||
if (i <= j) {
|
|
||||||
w = (Equip)A.get(i);
|
|
||||||
A.set(i, A.get(j));
|
|
||||||
A.set(j, w);
|
|
||||||
|
|
||||||
i++;
|
|
||||||
j--;
|
|
||||||
}
|
|
||||||
} while (i <= j);
|
|
||||||
}
|
|
||||||
|
|
||||||
void MapleQuicksort(int Esq, int Dir, ArrayList<Item> A, int sort) {
|
|
||||||
switch(sort) {
|
|
||||||
case 3:
|
|
||||||
PartitionByLevel(Esq, Dir, A);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 2:
|
|
||||||
PartitionByName(Esq, Dir, A);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 1:
|
|
||||||
PartitionByQuantity(Esq, Dir, A);
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
PartitionByItemId(Esq, Dir, A);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if (Esq < j) MapleQuicksort(Esq, j, A, sort);
|
|
||||||
if (i < Dir) MapleQuicksort(i, Dir, A, sort);
|
|
||||||
}
|
|
||||||
|
|
||||||
public PairedQuicksort(ArrayList<Item> A, int primarySort, int secondarySort) {
|
|
||||||
intersect = new ArrayList<>();
|
|
||||||
|
|
||||||
if(A.size() > 0) MapleQuicksort(0, A.size() - 1, A, primarySort);
|
|
||||||
|
|
||||||
intersect.add(0);
|
|
||||||
for(int ind = 1; ind < A.size(); ind++) {
|
|
||||||
if(A.get(ind - 1).getItemId() != A.get(ind).getItemId()) {
|
|
||||||
intersect.add(ind);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
intersect.add(A.size());
|
|
||||||
|
|
||||||
for(int ind = 0; ind < intersect.size() - 1; ind++) {
|
|
||||||
if(intersect.get(ind + 1) > intersect.get(ind)) MapleQuicksort(intersect.get(ind), intersect.get(ind + 1) - 1, A, secondarySort);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class MapleStorageInventory {
|
|
||||||
private Client c;
|
|
||||||
private Map<Short, Item> inventory = new LinkedHashMap<>();
|
private Map<Short, Item> inventory = new LinkedHashMap<>();
|
||||||
private byte slotLimit;
|
private final byte slotLimit;
|
||||||
|
|
||||||
public MapleStorageInventory(Client c, List<Item> toSort) {
|
public StorageInventory(Client c, List<Item> toSort) {
|
||||||
this.inventory = new LinkedHashMap<>();
|
this.inventory = new LinkedHashMap<>();
|
||||||
this.slotLimit = (byte)toSort.size();
|
this.slotLimit = (byte) toSort.size();
|
||||||
this.c = c;
|
this.c = c;
|
||||||
|
|
||||||
for(Item item : toSort) {
|
for (Item item : toSort) {
|
||||||
this.addItem(item);
|
this.addItem(item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -190,7 +48,7 @@ public class MapleStorageInventory {
|
|||||||
private byte getSlotLimit() {
|
private byte getSlotLimit() {
|
||||||
return slotLimit;
|
return slotLimit;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Collection<Item> list() {
|
private Collection<Item> list() {
|
||||||
return Collections.unmodifiableCollection(inventory.values());
|
return Collections.unmodifiableCollection(inventory.values());
|
||||||
}
|
}
|
||||||
@@ -209,11 +67,11 @@ public class MapleStorageInventory {
|
|||||||
int type = item.getItemId() / 1000000;
|
int type = item.getItemId() / 1000000;
|
||||||
return type == 1 || type == 5;
|
return type == 1 || type == 5;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isSameOwner(Item source, Item target) {
|
private static boolean isSameOwner(Item source, Item target) {
|
||||||
return source.getOwner().equals(target.getOwner());
|
return source.getOwner().equals(target.getOwner());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void move(short sSlot, short dSlot, short slotMax) {
|
private void move(short sSlot, short dSlot, short slotMax) {
|
||||||
Item source = inventory.get(sSlot);
|
Item source = inventory.get(sSlot);
|
||||||
Item target = inventory.get(dSlot);
|
Item target = inventory.get(dSlot);
|
||||||
@@ -239,15 +97,15 @@ public class MapleStorageInventory {
|
|||||||
swap(target, source);
|
swap(target, source);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void moveItem(short src, short dst) {
|
private void moveItem(short src, short dst) {
|
||||||
if (src < 0 || dst < 0) {
|
if (src < 0 || dst < 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(dst > this.getSlotLimit()) {
|
if (dst > this.getSlotLimit()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Item source = this.getItem(src);
|
Item source = this.getItem(src);
|
||||||
if (source == null) {
|
if (source == null) {
|
||||||
return;
|
return;
|
||||||
@@ -273,7 +131,7 @@ public class MapleStorageInventory {
|
|||||||
private void addSlot(short slot, Item item) {
|
private void addSlot(short slot, Item item) {
|
||||||
inventory.put(slot, item);
|
inventory.put(slot, item);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void removeSlot(short slot) {
|
private void removeSlot(short slot) {
|
||||||
inventory.remove(slot);
|
inventory.remove(slot);
|
||||||
}
|
}
|
||||||
@@ -286,7 +144,7 @@ public class MapleStorageInventory {
|
|||||||
if (isFull()) {
|
if (isFull()) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (short i = 1; i <= slotLimit; i++) {
|
for (short i = 1; i <= slotLimit; i++) {
|
||||||
if (!inventory.containsKey(i)) {
|
if (!inventory.containsKey(i)) {
|
||||||
return i;
|
return i;
|
||||||
@@ -299,21 +157,29 @@ public class MapleStorageInventory {
|
|||||||
ItemInformationProvider ii = ItemInformationProvider.getInstance();
|
ItemInformationProvider ii = ItemInformationProvider.getInstance();
|
||||||
Item srcItem, dstItem;
|
Item srcItem, dstItem;
|
||||||
|
|
||||||
for(short dst = 1; dst <= this.getSlotLimit(); dst++) {
|
for (short dst = 1; dst <= this.getSlotLimit(); dst++) {
|
||||||
dstItem = this.getItem(dst);
|
dstItem = this.getItem(dst);
|
||||||
if(dstItem == null) continue;
|
if (dstItem == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
for(short src = (short)(dst + 1); src <= this.getSlotLimit(); src++) {
|
for (short src = (short) (dst + 1); src <= this.getSlotLimit(); src++) {
|
||||||
srcItem = this.getItem(src);
|
srcItem = this.getItem(src);
|
||||||
if(srcItem == null) continue;
|
if (srcItem == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if(dstItem.getItemId() != srcItem.getItemId()) continue;
|
if (dstItem.getItemId() != srcItem.getItemId()) {
|
||||||
if(dstItem.getQuantity() == ii.getSlotMax(c, this.getItem(dst).getItemId())) break;
|
continue;
|
||||||
|
}
|
||||||
|
if (dstItem.getQuantity() == ii.getSlotMax(c, this.getItem(dst).getItemId())) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
moveItem(src, dst);
|
moveItem(src, dst);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean sorted = false;
|
boolean sorted = false;
|
||||||
|
|
||||||
while (!sorted) {
|
while (!sorted) {
|
||||||
@@ -337,26 +203,190 @@ public class MapleStorageInventory {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Item> sortItems() {
|
public List<Item> sortItems() {
|
||||||
ArrayList<Item> itemarray = new ArrayList<>();
|
ArrayList<Item> itemarray = new ArrayList<>();
|
||||||
|
|
||||||
for (short i = 1; i <= this.getSlotLimit(); i++) {
|
for (short i = 1; i <= this.getSlotLimit(); i++) {
|
||||||
Item item = this.getItem(i);
|
Item item = this.getItem(i);
|
||||||
if (item != null) {
|
if (item != null) {
|
||||||
itemarray.add(item.copy());
|
itemarray.add(item.copy());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (Item item : itemarray) {
|
for (Item item : itemarray) {
|
||||||
this.removeSlot(item.getPosition());
|
this.removeSlot(item.getPosition());
|
||||||
}
|
}
|
||||||
|
|
||||||
int invTypeCriteria = 1;
|
int invTypeCriteria = 1;
|
||||||
int sortCriteria = (YamlConfig.config.server.USE_ITEM_SORT_BY_NAME == true) ? 2 : 0;
|
int sortCriteria = (YamlConfig.config.server.USE_ITEM_SORT_BY_NAME == true) ? 2 : 0;
|
||||||
PairedQuicksort pq = new PairedQuicksort(itemarray, sortCriteria, invTypeCriteria);
|
PairedQuicksort pq = new PairedQuicksort(itemarray, sortCriteria, invTypeCriteria);
|
||||||
|
|
||||||
inventory.clear();
|
inventory.clear();
|
||||||
return itemarray;
|
return itemarray;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class PairedQuicksort {
|
||||||
|
private int i = 0;
|
||||||
|
private int j = 0;
|
||||||
|
private final ArrayList<Integer> intersect;
|
||||||
|
ItemInformationProvider ii = ItemInformationProvider.getInstance();
|
||||||
|
|
||||||
|
private void PartitionByItemId(int Esq, int Dir, ArrayList<Item> A) {
|
||||||
|
Item x, w;
|
||||||
|
|
||||||
|
i = Esq;
|
||||||
|
j = Dir;
|
||||||
|
|
||||||
|
x = A.get((i + j) / 2);
|
||||||
|
do {
|
||||||
|
while (x.getItemId() > A.get(i).getItemId()) {
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
while (x.getItemId() < A.get(j).getItemId()) {
|
||||||
|
j--;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i <= j) {
|
||||||
|
w = A.get(i);
|
||||||
|
A.set(i, A.get(j));
|
||||||
|
A.set(j, w);
|
||||||
|
|
||||||
|
i++;
|
||||||
|
j--;
|
||||||
|
}
|
||||||
|
} while (i <= j);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void PartitionByName(int Esq, int Dir, ArrayList<Item> A) {
|
||||||
|
Item x, w;
|
||||||
|
|
||||||
|
i = Esq;
|
||||||
|
j = Dir;
|
||||||
|
|
||||||
|
x = A.get((i + j) / 2);
|
||||||
|
do {
|
||||||
|
while (ii.getName(x.getItemId()).compareTo(ii.getName(A.get(i).getItemId())) > 0) {
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
while (ii.getName(x.getItemId()).compareTo(ii.getName(A.get(j).getItemId())) < 0) {
|
||||||
|
j--;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i <= j) {
|
||||||
|
w = A.get(i);
|
||||||
|
A.set(i, A.get(j));
|
||||||
|
A.set(j, w);
|
||||||
|
|
||||||
|
i++;
|
||||||
|
j--;
|
||||||
|
}
|
||||||
|
} while (i <= j);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void PartitionByQuantity(int Esq, int Dir, ArrayList<Item> A) {
|
||||||
|
Item x, w;
|
||||||
|
|
||||||
|
i = Esq;
|
||||||
|
j = Dir;
|
||||||
|
|
||||||
|
x = A.get((i + j) / 2);
|
||||||
|
do {
|
||||||
|
while (x.getQuantity() > A.get(i).getQuantity()) {
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
while (x.getQuantity() < A.get(j).getQuantity()) {
|
||||||
|
j--;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i <= j) {
|
||||||
|
w = A.get(i);
|
||||||
|
A.set(i, A.get(j));
|
||||||
|
A.set(j, w);
|
||||||
|
|
||||||
|
i++;
|
||||||
|
j--;
|
||||||
|
}
|
||||||
|
} while (i <= j);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void PartitionByLevel(int Esq, int Dir, ArrayList<Item> A) {
|
||||||
|
Equip x, w, eqpI, eqpJ;
|
||||||
|
|
||||||
|
i = Esq;
|
||||||
|
j = Dir;
|
||||||
|
|
||||||
|
x = (Equip) (A.get((i + j) / 2));
|
||||||
|
|
||||||
|
do {
|
||||||
|
eqpI = (Equip) A.get(i);
|
||||||
|
eqpJ = (Equip) A.get(j);
|
||||||
|
|
||||||
|
while (x.getLevel() > eqpI.getLevel()) {
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
while (x.getLevel() < eqpJ.getLevel()) {
|
||||||
|
j--;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i <= j) {
|
||||||
|
w = (Equip) A.get(i);
|
||||||
|
A.set(i, A.get(j));
|
||||||
|
A.set(j, w);
|
||||||
|
|
||||||
|
i++;
|
||||||
|
j--;
|
||||||
|
}
|
||||||
|
} while (i <= j);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MapleQuicksort(int Esq, int Dir, ArrayList<Item> A, int sort) {
|
||||||
|
switch (sort) {
|
||||||
|
case 3:
|
||||||
|
PartitionByLevel(Esq, Dir, A);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 2:
|
||||||
|
PartitionByName(Esq, Dir, A);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 1:
|
||||||
|
PartitionByQuantity(Esq, Dir, A);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
PartitionByItemId(Esq, Dir, A);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (Esq < j) {
|
||||||
|
MapleQuicksort(Esq, j, A, sort);
|
||||||
|
}
|
||||||
|
if (i < Dir) {
|
||||||
|
MapleQuicksort(i, Dir, A, sort);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public PairedQuicksort(ArrayList<Item> A, int primarySort, int secondarySort) {
|
||||||
|
intersect = new ArrayList<>();
|
||||||
|
|
||||||
|
if (A.size() > 0) {
|
||||||
|
MapleQuicksort(0, A.size() - 1, A, primarySort);
|
||||||
|
}
|
||||||
|
|
||||||
|
intersect.add(0);
|
||||||
|
for (int ind = 1; ind < A.size(); ind++) {
|
||||||
|
if (A.get(ind - 1).getItemId() != A.get(ind).getItemId()) {
|
||||||
|
intersect.add(ind);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
intersect.add(A.size());
|
||||||
|
|
||||||
|
for (int ind = 0; ind < intersect.size() - 1; ind++) {
|
||||||
|
if (intersect.get(ind + 1) > intersect.get(ind)) {
|
||||||
|
MapleQuicksort(intersect.get(ind), intersect.get(ind + 1) - 1, A, secondarySort);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user