好吧,所以我有这个问题,你可以得到比你实际支付的更多的钱。这有点难以解释,当你增加数量时,首先这是我的代码:
public void setAmount(Player player, int button) {
int amount = (Integer) player.getTemporaryAttribute("geAmount");
int id = (Integer) player.getTemporaryAttribute("geItem");
int price = (Integer) player.getTemporaryAttribute("price");
long totalPrice = price * amount;
switch(button) {
case 157:
if(player.getRights() > 1){
player.sm(""+totalPrice+"");
}
if(amount > 1) {
amount--;
} else {
amount = 1;
}
break;
case 159:
if(player.getRights() > 1){
player.sm(""+totalPrice+"");
}
if(amount < Integer.MAX_VALUE && totalPrice <= Integer.MAX_VALUE && totalPrice >= 0) {
amount++;
}
break;
case 162:
if(player.getRights() > 1){
player.sm(""+totalPrice+"");
}
if(player.getTemporaryAttribute("buying") == Boolean.TRUE && totalPrice <= Integer.MAX_VALUE && totalPrice >= 0) {
amount += 1;
} else {
amount = 1;
}
break;
case 164:
if(player.getRights() > 1){
player.sm(""+totalPrice+"");
}
if(player.getTemporaryAttribute("buying") == Boolean.TRUE && totalPrice <= Integer.MAX_VALUE && totalPrice >= 0) {
amount += 10;
} else {
amount = 10;
}
break;
case 166:
if(player.getRights() > 1){
player.sm(""+totalPrice+"");
}
if(player.getTemporaryAttribute("buying") == Boolean.TRUE && totalPrice <= Integer.MAX_VALUE && totalPrice >= 0) {
amount += 100;
} else {
amount = 100;
}
break;
case 168:
if(player.getRights() > 1){
player.sm(""+totalPrice+"");
}
if(player.getTemporaryAttribute("buying") == Boolean.TRUE && totalPrice <= Integer.MAX_VALUE && totalPrice >= 0) {
amount += 1000;
} else {
amount = player.getInventory().getContainer().getNumberOff(id);
}
break;
case 171:
case 173:
if(player.getRights() > 1){
player.sm(""+totalPrice+"");
}
player.getActionSender().sendConfig(1111, (Integer) player.getTemporaryAttribute("price"));
break;
}
if(amount == 0){
amount = 1;
}
if(amount >= Integer.MAX_VALUE || amount < 0) {
amount = Integer.MAX_VALUE;
}
player.setTemporaryAttribute("geAmount", amount);
player.getActionSender().sendConfig(1109, id);
player.getActionSender().sendConfig(1110, amount);
}
总价格一直在溢出,但我不确定如何让它停止。有人建议使用这样的东西:
int last = 0;
int current = 0;
while(last < current) //loop terminates on overflow
last = current++;
但我不知道如何在这段代码中使用它。有人可以帮我吗?