Clean up code, replace switch and try-with-resource where possible.

This commit is contained in:
Đạt Nhân Trương
2022-08-07 21:51:10 +07:00
parent f983b4dccf
commit 0ef3e69638
22 changed files with 609 additions and 589 deletions

View File

@@ -61,7 +61,7 @@ public class FaceCommand extends Command {
}
Character victim = c.getChannelServer().getPlayerStorage().getCharacterByName(params[0]);
if (victim == null) {
if (victim != null) {
victim.setFace(itemId);
victim.updateSingleStat(Stat.FACE, itemId);
victim.equipChanged();

View File

@@ -538,7 +538,8 @@ public class InventoryManipulator {
itemChanged = true;
}
if (dst == -6) { // unequip the overall
switch (dst) {
case -6: // unequip the overall
Item top = eqpdInv.getItem((short) -5);
if (top != null && ItemConstants.isOverall(top.getItemId())) {
if (eqpInv.isFull()) {
@@ -548,7 +549,8 @@ public class InventoryManipulator {
}
unequip(c, (byte) -5, eqpInv.getNextFreeSlot());
}
} else if (dst == -5) {
break;
case -5:
final Item bottom = eqpdInv.getItem((short) -6);
if (bottom != null && ItemConstants.isOverall(source.getItemId())) {
if (eqpInv.isFull()) {
@@ -558,7 +560,8 @@ public class InventoryManipulator {
}
unequip(c, (byte) -6, eqpInv.getNextFreeSlot());
}
} else if (dst == -10) {// check if weapon is two-handed
break;
case -10: // check if weapon is two-handed
Item weapon = eqpdInv.getItem((short) -11);
if (weapon != null && ii.isTwoHanded(weapon.getItemId())) {
if (eqpInv.isFull()) {
@@ -568,7 +571,8 @@ public class InventoryManipulator {
}
unequip(c, (byte) -11, eqpInv.getNextFreeSlot());
}
} else if (dst == -11) {
break;
case -11:
Item shield = eqpdInv.getItem((short) -10);
if (shield != null && ii.isTwoHanded(source.getItemId())) {
if (eqpInv.isFull()) {
@@ -578,11 +582,12 @@ public class InventoryManipulator {
}
unequip(c, (byte) -10, eqpInv.getNextFreeSlot());
}
}
if (dst == -18) {
break;
case -18:
if (chr.getMount() != null) {
chr.getMount().setItemId(source.getItemId());
}
break;
}
//1112413, 1112414, 1112405 (Lilin's Ring)

View File

@@ -60,7 +60,8 @@ public class StorageProcessor {
if (c.tryacquireClient()) {
try {
if (mode == 4) { // take out
switch (mode) {
case 4: { // take out
byte type = p.readByte();
byte slot = p.readByte();
if (slot < 0 || slot > storage.getSlots()) { // removal starts at zero
@@ -104,14 +105,17 @@ public class StorageProcessor {
c.sendPacket(PacketCreator.getStorageError((byte) 0x0A));
}
}
} else if (mode == 5) { // store
break;
}
case 5: { // store
short slot = p.readShort();
int itemId = p.readInt();
short quantity = p.readShort();
InventoryType invType = ItemConstants.getInventoryType(itemId);
Inventory inv = chr.getInventory(invType);
if (slot < 1 || slot > inv.getSlotLimit()) { //player inv starts at one
AutobanFactory.PACKET_EDIT.alert(c.getPlayer(), c.getPlayer().getName() + " tried to packet edit with storage.");
if (slot < 1 || slot > inv.getSlotLimit()) { // player inv starts at one
AutobanFactory.PACKET_EDIT.alert(c.getPlayer(),
c.getPlayer().getName() + " tried to packet edit with storage.");
log.warn("Chr {} tried to store item at slot {}", c.getPlayer().getName(), slot);
c.disconnect(true, false);
return;
@@ -124,17 +128,17 @@ public class StorageProcessor {
c.sendPacket(PacketCreator.getStorageError((byte) 0x11));
return;
}
int storeFee = storage.getStoreFee();
if (chr.getMeso() < storeFee) {
c.sendPacket(PacketCreator.getStorageError((byte) 0x0B));
} else {
Item item;
inv.lockInventory(); // thanks imbee for pointing a dupe within storage
inv.lockInventory(); // thanks imbee for pointing a dupe within storage
try {
item = inv.getItem(slot);
if (item != null && item.getItemId() == itemId && (item.getQuantity() >= quantity || ItemConstants.isRechargeable(itemId))) {
if (item != null && item.getItemId() == itemId
&& (item.getQuantity() >= quantity || ItemConstants.isRechargeable(itemId))) {
if (ItemId.isWeddingRing(itemId) || ItemId.isWeddingToken(itemId)) {
c.sendPacket(PacketCreator.enableActions());
return;
@@ -150,7 +154,7 @@ public class StorageProcessor {
return;
}
item = item.copy(); // thanks Robin Schulz & BHB88 for noticing a inventory glitch when storing items
item = item.copy(); // thanks Robin Schulz & BHB88 for noticing a inventory glitch when storing items
} finally {
inv.unlockInventory();
}
@@ -160,19 +164,22 @@ public class StorageProcessor {
KarmaManipulator.toggleKarmaFlagToUntradeable(item);
item.setQuantity(quantity);
storage.store(item); // inside a critical section, "!(storage.isFull())" is still in effect...
storage.store(item); // inside a critical section, "!(storage.isFull())" is still in effect...
chr.setUsedStorage();
String itemName = ii.getName(item.getItemId());
log.debug("Chr {} stored {}x {} ({})", c.getPlayer().getName(), item.getQuantity(), itemName, item.getItemId());
storage.sendStored(c, ItemConstants.getInventoryType(itemId));
}
} else if (mode == 6) { // arrange items
break;
}
case 6: // arrange items
if (YamlConfig.config.server.USE_STORAGE_ITEM_SORT) {
storage.arrangeItems(c);
}
c.sendPacket(PacketCreator.enableActions());
} else if (mode == 7) { // meso
break;
case 7: { // meso
int meso = p.readInt();
int storageMesos = storage.getMeso();
int playerMesos = chr.getMeso();
@@ -199,8 +206,11 @@ public class StorageProcessor {
c.sendPacket(PacketCreator.enableActions());
return;
}
} else if (mode == 8) {// close... unless the player decides to enter cash shop!
break;
}
case 8: // close... unless the player decides to enter cash shop!
storage.close();
break;
}
} finally {
c.releaseClient();

View File

@@ -418,7 +418,8 @@ public class AssignAPProcessor {
}
int newVal = 0;
if (type.equals(Stat.STR)) {
switch (type) {
case STR:
newVal = statUpdate[0] + gain;
if (newVal > YamlConfig.config.server.MAX_AP) {
statGain[0] += (gain - (newVal - YamlConfig.config.server.MAX_AP));
@@ -427,7 +428,8 @@ public class AssignAPProcessor {
statGain[0] += gain;
statUpdate[0] = newVal;
}
} else if (type.equals(Stat.INT)) {
break;
case INT:
newVal = statUpdate[3] + gain;
if (newVal > YamlConfig.config.server.MAX_AP) {
statGain[3] += (gain - (newVal - YamlConfig.config.server.MAX_AP));
@@ -436,7 +438,8 @@ public class AssignAPProcessor {
statGain[3] += gain;
statUpdate[3] = newVal;
}
} else if (type.equals(Stat.LUK)) {
break;
case LUK:
newVal = statUpdate[2] + gain;
if (newVal > YamlConfig.config.server.MAX_AP) {
statGain[2] += (gain - (newVal - YamlConfig.config.server.MAX_AP));
@@ -445,7 +448,8 @@ public class AssignAPProcessor {
statGain[2] += gain;
statUpdate[2] = newVal;
}
} else if (type.equals(Stat.DEX)) {
break;
case DEX:
newVal = statUpdate[1] + gain;
if (newVal > YamlConfig.config.server.MAX_AP) {
statGain[1] += (gain - (newVal - YamlConfig.config.server.MAX_AP));
@@ -454,6 +458,7 @@ public class AssignAPProcessor {
statGain[1] += gain;
statUpdate[1] = newVal;
}
break;
}
if (newVal > YamlConfig.config.server.MAX_AP) {