Your sending the message inside of a for loop you need declare an variable outside of the loop add each players value to the variable and the print that variable outside of the loop
|
Hello everyone, If this is the wrong section please let me know.
I am having difficulty making a Total wealth of my server, for example, if I have a commmand thats :erverwealth, I want it to send a message saying the total wealth of the server. I do have a getWealth method, but when I put the array in my command it sends the message in game but for every playerSave that I have, which looks like this:
This is the code that's producing it:
Instead of it printing multiple lines of each individual characters wealth, how do I instead multiply it to where it will only print 1 line with all characters wealth combined. I've googled and researched a lot of different methods using sum of elements, etc. Just can't quite figure it out.Code:if (keyword.equals("totalw")) { //Kinda works no total for (Hplayer player : PlayerSave.listOfPlayersAll) { getActionSender().sendMessage("There is currently @[email protected]" + player.getWealthAll() + " @[email protected] of gold in the server");
Thanks.
PS: still learning arrays, so any help would be appreciated.
Your sending the message inside of a for loop you need declare an variable outside of the loop add each players value to the variable and the print that variable outside of the loop
To clarify your goal is to return a number that is the sum of every players wealth combined right?
yes that is correct, I have a mess of spaghetti that I've tried and seem to not be successful, with a return of 0 instead, Im understanding the size of a players bank is probably 0 because it doesnt know where its pulling bank sizes from and multiplying players, which would return 0.
Here's what I have tried:
Even so it still loops all players, so I get 400+ sendMessage in the chat box of 0Code://int sum = 0; /*int size = bank.size(); int size2 = PlayerSave.listOfPlayersAll.size(); int myArray[] = new int [size]; int myArray2[] = new int [size2];*/ /*int myArray[] = new int[bank.size()]; int myArray2[] = new int[PlayerSave.listOfPlayersAll.size()]; int lengthOfArray = myArray.length; int lengthofArray2 = myArray2.length; int[] total = new int[lengthOfArray * lengthofArray2];*/
Hopefully this will help as long as your player.getWealthAll(); is returning the correct value you want this should workCode:private void foobar() { long value = 0; //long value as combined wealth would exceed the max value of an integer //loop anything contained within the loop with execute per object in the loop for (Hplayer player : PlayerSave.listOfPlayersAll) { value = value + player.getWealthAll(); } //Sending the message after the loop will only print the message once instead of for every player getActionSender().sendMessage("There is currently @[email protected]" + value + " @[email protected] of gold in the server"); }
Alternatively you could use lambda functionality but stick to some basic stuff for now
« Help with kronos | Getting RSPSI to work with Valius cache please help » |
Thread Information |
Users Browsing this ThreadThere are currently 1 users browsing this thread. (0 members and 1 guests) |