mirror of
https://github.com/Melledy/LunarCore.git
synced 2025-12-13 13:54:37 +01:00
Prevent int overflows when adding/removing items
This commit is contained in:
@@ -24,6 +24,7 @@ import emu.lunarcore.game.player.BasePlayerManager;
|
||||
import emu.lunarcore.game.player.Player;
|
||||
import emu.lunarcore.server.packet.send.PacketPlayerSyncScNotify;
|
||||
import emu.lunarcore.server.packet.send.PacketScenePlaneEventScNotify;
|
||||
import emu.lunarcore.util.Utils;
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
||||
|
||||
@@ -233,8 +234,9 @@ public class Inventory extends BasePlayerManager {
|
||||
item.save();
|
||||
return item;
|
||||
} else {
|
||||
// Add count
|
||||
existingItem.setCount(Math.min(existingItem.getCount() + item.getCount(), item.getExcel().getPileLimit()));
|
||||
// Add count to item
|
||||
int amount = Utils.safeAdd(existingItem.getCount(), item.getCount(), item.getExcel().getPileLimit(), 0);
|
||||
existingItem.setCount(amount);
|
||||
existingItem.save();
|
||||
return existingItem;
|
||||
}
|
||||
@@ -381,7 +383,7 @@ public class Inventory extends BasePlayerManager {
|
||||
if (item.getExcel() == null || item.getExcel().isEquippable()) {
|
||||
item.setCount(0);
|
||||
} else {
|
||||
item.setCount(item.getCount() - count);
|
||||
item.setCount(Utils.safeSubtract(item.getCount(), count));
|
||||
}
|
||||
|
||||
if (item.getCount() <= 0) {
|
||||
|
||||
@@ -63,6 +63,7 @@ import emu.lunarcore.server.packet.BasePacket;
|
||||
import emu.lunarcore.server.packet.CmdId;
|
||||
import emu.lunarcore.server.packet.send.*;
|
||||
import emu.lunarcore.util.Position;
|
||||
import emu.lunarcore.util.Utils;
|
||||
import it.unimi.dsi.fastutil.ints.Int2IntMap;
|
||||
import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap;
|
||||
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
|
||||
@@ -362,22 +363,22 @@ public class Player {
|
||||
}
|
||||
|
||||
public void addSCoin(int amount) {
|
||||
this.scoin += amount;
|
||||
this.scoin = Utils.safeAdd(this.scoin, amount);
|
||||
this.sendPacket(new PacketPlayerSyncScNotify(this));
|
||||
}
|
||||
|
||||
public void addHCoin(int amount) {
|
||||
this.hcoin += amount;
|
||||
this.hcoin = Utils.safeAdd(this.hcoin, amount);
|
||||
this.sendPacket(new PacketPlayerSyncScNotify(this));
|
||||
}
|
||||
|
||||
public void addMCoin(int amount) {
|
||||
this.mcoin += amount;
|
||||
this.mcoin = Utils.safeAdd(this.mcoin, amount);
|
||||
this.sendPacket(new PacketPlayerSyncScNotify(this));
|
||||
}
|
||||
|
||||
public void addTalentPoints(int amount) {
|
||||
this.talentPoints += amount;
|
||||
this.talentPoints = Utils.safeAdd(this.talentPoints, amount);
|
||||
this.sendPacket(new PacketSyncRogueVirtualItemInfoScNotify(this));
|
||||
}
|
||||
|
||||
@@ -440,7 +441,7 @@ public class Player {
|
||||
}
|
||||
|
||||
public void addStamina(int amount) {
|
||||
this.stamina += amount;
|
||||
this.stamina = Utils.safeAdd(this.stamina, amount);
|
||||
this.sendPacket(new PacketStaminaInfoScNotify(this));
|
||||
}
|
||||
|
||||
|
||||
@@ -121,6 +121,44 @@ public class Utils {
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add 2 integers without overflowing
|
||||
*/
|
||||
public static int safeAdd(int a, int b) {
|
||||
return safeAdd(a, b, Integer.MAX_VALUE, Integer.MIN_VALUE);
|
||||
}
|
||||
|
||||
public static int safeAdd(int a, int b, long max, long min) {
|
||||
long sum = (long) a + (long) b;
|
||||
|
||||
if (sum > max) {
|
||||
return (int) max;
|
||||
} else if (sum < min) {
|
||||
return (int) min;
|
||||
}
|
||||
|
||||
return (int) sum;
|
||||
}
|
||||
|
||||
/**
|
||||
* Subtract 2 integers without overflowing
|
||||
*/
|
||||
public static int safeSubtract(int a, int b) {
|
||||
return safeSubtract(a, b, Integer.MAX_VALUE, Integer.MIN_VALUE);
|
||||
}
|
||||
|
||||
public static int safeSubtract(int a, int b, long max, long min) {
|
||||
long sum = (long) a - (long) b;
|
||||
|
||||
if (sum > max) {
|
||||
return (int) max;
|
||||
} else if (sum < min) {
|
||||
return (int) min;
|
||||
}
|
||||
|
||||
return (int) sum;
|
||||
}
|
||||
|
||||
public static double generateRandomDouble() {
|
||||
return ThreadLocalRandom.current().nextDouble();
|
||||
|
||||
Reference in New Issue
Block a user