Rename and clean up MapleStorageInventory

This commit is contained in:
P0nk
2021-09-09 22:53:16 +02:00
parent e9c8a82b47
commit 701d735202
2 changed files with 202 additions and 172 deletions

View File

@@ -274,7 +274,7 @@ public class Storage {
public void arrangeItems(Client c) {
lock.lock();
try {
MapleStorageInventory msi = new MapleStorageInventory(c, items);
StorageInventory msi = new StorageInventory(c, items);
msi.mergeItems();
items = msi.sortItems();

View File

@@ -28,161 +28,19 @@ import constants.inventory.ItemConstants;
import java.util.*;
/**
*
* @author RonanLana
*/
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);
}
}
}
public class MapleStorageInventory {
private Client c;
public class StorageInventory {
private final Client c;
private Map<Short, Item> inventory = new LinkedHashMap<>();
private byte slotLimit;
public MapleStorageInventory(Client c, List<Item> toSort) {
private final byte slotLimit;
public StorageInventory(Client c, List<Item> toSort) {
this.inventory = new LinkedHashMap<>();
this.slotLimit = (byte)toSort.size();
this.slotLimit = (byte) toSort.size();
this.c = c;
for(Item item : toSort) {
for (Item item : toSort) {
this.addItem(item);
}
}
@@ -190,7 +48,7 @@ public class MapleStorageInventory {
private byte getSlotLimit() {
return slotLimit;
}
private Collection<Item> list() {
return Collections.unmodifiableCollection(inventory.values());
}
@@ -209,11 +67,11 @@ public class MapleStorageInventory {
int type = item.getItemId() / 1000000;
return type == 1 || type == 5;
}
private static boolean isSameOwner(Item source, Item target) {
return source.getOwner().equals(target.getOwner());
}
private void move(short sSlot, short dSlot, short slotMax) {
Item source = inventory.get(sSlot);
Item target = inventory.get(dSlot);
@@ -239,15 +97,15 @@ public class MapleStorageInventory {
swap(target, source);
}
}
private void moveItem(short src, short dst) {
if (src < 0 || dst < 0) {
return;
}
if(dst > this.getSlotLimit()) {
if (dst > this.getSlotLimit()) {
return;
}
Item source = this.getItem(src);
if (source == null) {
return;
@@ -273,7 +131,7 @@ public class MapleStorageInventory {
private void addSlot(short slot, Item item) {
inventory.put(slot, item);
}
private void removeSlot(short slot) {
inventory.remove(slot);
}
@@ -286,7 +144,7 @@ public class MapleStorageInventory {
if (isFull()) {
return -1;
}
for (short i = 1; i <= slotLimit; i++) {
if (!inventory.containsKey(i)) {
return i;
@@ -299,21 +157,29 @@ public class MapleStorageInventory {
ItemInformationProvider ii = ItemInformationProvider.getInstance();
Item srcItem, dstItem;
for(short dst = 1; dst <= this.getSlotLimit(); dst++) {
for (short dst = 1; dst <= this.getSlotLimit(); 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);
if(srcItem == null) continue;
if (srcItem == null) {
continue;
}
if(dstItem.getItemId() != srcItem.getItemId()) continue;
if(dstItem.getQuantity() == ii.getSlotMax(c, this.getItem(dst).getItemId())) break;
if (dstItem.getItemId() != srcItem.getItemId()) {
continue;
}
if (dstItem.getQuantity() == ii.getSlotMax(c, this.getItem(dst).getItemId())) {
break;
}
moveItem(src, dst);
}
}
boolean sorted = false;
while (!sorted) {
@@ -337,26 +203,190 @@ public class MapleStorageInventory {
}
}
}
public List<Item> sortItems() {
ArrayList<Item> itemarray = new ArrayList<>();
for (short i = 1; i <= this.getSlotLimit(); i++) {
Item item = this.getItem(i);
if (item != null) {
itemarray.add(item.copy());
itemarray.add(item.copy());
}
}
for (Item item : itemarray) {
this.removeSlot(item.getPosition());
}
int invTypeCriteria = 1;
int sortCriteria = (YamlConfig.config.server.USE_ITEM_SORT_BY_NAME == true) ? 2 : 0;
PairedQuicksort pq = new PairedQuicksort(itemarray, sortCriteria, invTypeCriteria);
inventory.clear();
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);
}
}
}
}