|
So i writen this was awhile ago like 6 month ago, i think i did base it of old codes of rune-servers but i cant remember who or if i actually did, the only reason im releasing it is because it never took me long, and when i searched it up on rune-server to save me time i couldnt find one that had it all perfect. anyways here it is, add it however you want to an object, to an npc but this is the main part of the code.
Platinum Tokens part:
Coin part:Code:case 13204: if (!player.getItems().playerHasItem(13204)) { player.sendMessage("You need coins in your inventory to do this."); return; } final int tokenAmount = player.getItems().getItemAmount(13204); if (player.getItems().getItemAmount(13204) + (player.getItems().getItemAmount(13307)/1000) == MAX_VALUE) { player.sendMessage("You cant do this as you will exceed the max value in tokens."); return; } if (player.getItems().playerHasItem(13307, MAX_VALUE)) { player.sendMessage("You cant do this as you will exceed the max value"); return; } if (tokenAmount >= MAX_TOKEN_VALUE && player.getItems().playerHasItem(13307, MAX_VALUE)) { player.sendMessage("You cant do this as you will exceed the max value"); return; } if (tokenAmount > MAX_TOKEN_VALUE) { if (player.getItems().getItemAmount(13204) >= MAX_VALUE) { player.sendMessage("You cant do this as you will exceed the max value"); return; } int remaining = player.getItems().getItemAmount(13307); player.getItems().deleteItem(13204, MAX_TOKEN_VALUE); //3000k- 2147k = 853k player.getItems().addItem(13307, MAX_VALUE); player.getItems().addItem(13204, remaining / 1000); return; } player.getItems().deleteItem(13204, 1 * tokenAmount); player.getItems().addItem(13307, tokenAmount * 1000); break; } break;
If you want to improve / make it cleaner or add something to it or see a fault in it that i have somehow missed, feel free to, i did not really use the code much i just needed it at the time of coding my game.Code:case 995: if (!player.getItems().playerHasItem(995)) { player.sendMessage("You need coins in your inventory to do this."); return; } int coinAmount = player.getItems().getItemAmount(995); final int platinumTokenValue = 1000; final int platinumTokenAmount = coinAmount / platinumTokenValue; if (player.getItems().getItemAmount(13204) >= MAX_VALUE) { player.sendMessage("You already have the maximum tokens possible."); return; } if (platinumTokenAmount > 0) { player.getItems().deleteItem(995, platinumTokenAmount * platinumTokenValue); player.getItems().addItem(13204, coinAmount / 1000); } else { player.sendMessage("You dont have enough coins to convert."); } break;
edit: it was a guy called corey i believe that i looked at a comment and found a bit of his code, saved me a good 3 minutes but was good code.
so i guess credits to him for a bit of the code : https://www.rune-server.ee/members/corey/
Last edited by Aaron W; 10-16-2020 at 01:30 PM.
Hes being sarcastic as usual.
Couple of things here.
Move this up above your checks, and use it instead of calling getItemAmount multiple times. Also fix the naming.Code:int remainding = player.getItems().getItemAmount(13307);
Swap this around.
Fix all of your calls to check inventory amount with your tokenAmount and remaining values.Code:final int tokenAmount = player.getItems().getItemAmount(13204); if (tokenAmount <= 0) { player.sendMessage("You need coins in your inventory to do this."); return; }
You're gonna have to explain what MAX_TOKEN_VALUE and MAX_VALUE are, because this code is hard to read. You shouldn't be deleting all of the item, then adding back more. There needs to be math to calculate how much space you have left, and only delete whats needed.
Tested on my Elvarg source.
Nearly fully functioning conversion, with proper checks. (Instead of not allowing the player to convert any coins to tokens if it would exceeds the max value, the player should exchange up-to the maximum amount)
Code://Handle coins <-> platinum tokens. if (itemId == 995 && (object.getDefinition().getName().toLowerCase().equals("bank booth") || object.getDefinition().getName().toLowerCase().equals("bank chest"))) { final int COIN_SIZE = player.getInventory().getAmount(995); if (COIN_SIZE < 1000) { player.getPacketSender().sendMessage("You must have atleast 1,000 coins to exchange for Platinum tokens."); return; } final int PLATINUM_TOKEN_SIZE = player.getInventory().getAmount(13204); System.out.println((long)(PLATINUM_TOKEN_SIZE + (COIN_SIZE / 1000))); if ((long)(PLATINUM_TOKEN_SIZE + (COIN_SIZE / 1000)) < 0) { player.getPacketSender().sendMessage("You can't do this as you will exceed the maximum value."); return; } final int REMAINDER = COIN_SIZE % 1000; if (REMAINDER != 0 && PLATINUM_TOKEN_SIZE == 0 && player.getInventory().getFreeSlots() == 0) { player.getPacketSender().sendMessage("You don't have enough space in your inventory."); return; } player.getInventory().delete(new Item(995, COIN_SIZE - REMAINDER)); player.getInventory().add(new Item(13204, COIN_SIZE / 1000)); player.getPacketSender().sendMessage("You exchange " + Misc.format(COIN_SIZE) + " Coins for " + Misc.format(player.getInventory().getAmount(13204)) + " Platinum tokens."); return; } if (itemId == 13204 && (object.getDefinition().getName().toLowerCase().equals("bank booth") || object.getDefinition().getName().toLowerCase().equals("bank chest"))) { final int COIN_SIZE = player.getInventory().getAmount(995); final int PLATINUM_TOKEN_SIZE = player.getInventory().getAmount(13204); if ((PLATINUM_TOKEN_SIZE * 1000) < 0 || ((PLATINUM_TOKEN_SIZE * 1000) + COIN_SIZE) < 0) { player.getPacketSender().sendMessage("You can't do this as you will exceed the maximum value."); return; } player.getInventory().delete(new Item(13204, PLATINUM_TOKEN_SIZE)); player.getInventory().add(new Item(995, PLATINUM_TOKEN_SIZE * 1000)); player.getPacketSender().sendMessage("You exchange " + Misc.format(PLATINUM_TOKEN_SIZE) + " Platinum tokens for " + Misc.format(player.getInventory().getAmount(995)) + " Coins."); return; }
Last edited by VoidBox; 10-01-2020 at 10:39 AM. Reason: Forgot a return value and fixed overflow.
I prefer caps on final variables, but perhaps this is bad practice. I will change my conventions.
I am only using Integer.MAX_VALUE as a check for surpassing the maximum amount and am never incrementing past the maximum value. Why do you think this would cause an overflow out of curiosity?
Thank you for the explanation. Can't believe I hadn't even considered the coins/tokens exceeding max(big oof). I checked this and it does not result in an overflow error, but does not give the user the converted item. (Elvarg might have an overflow check?)
I fixed this and added a return I had forgot.
« Previous Thread | Next Thread » |
Thread Information |
Users Browsing this ThreadThere are currently 1 users browsing this thread. (0 members and 1 guests) |