Sign in to follow this  
Eject

Random number get no new value

Recommended Posts

Hello friends of wurm

 

First thanks to Xype to help me with the code :)

 

Players should get a random welcome message if they log in, but it seems the variable wont change the value, i hope you can help me to fix this issue :)

Players get always the case 3 message.

 

Here is the code:

public class AnnounceMod implements WurmServerMod, Initable, PreInitable, Configurable, PlayerLoginListener {
    boolean announcePlayers = true;
    int announceMaxPower = 0;
    String announceMessage;
    String randomMessage;


    public AnnounceMod() {
    }

    public void configure(Properties properties) {
        this.announcePlayers = Boolean.valueOf(properties.getProperty("announcePlayers", String.valueOf(this.announcePlayers))).booleanValue();
        this.announceMaxPower = Integer.valueOf(properties.getProperty("announceMaxPower", String.valueOf(this.announceMaxPower))).intValue();
        this.announceMessage = String.valueOf(properties.getProperty("announceMessage", String.valueOf(this.announceMessage))).toString();
        Logger.getLogger(AnnounceMod.class.getName()).log(Level.INFO, "announcePlayers: " + this.announcePlayers);
        Logger.getLogger(AnnounceMod.class.getName()).log(Level.INFO, "announceMaxPower: " + this.announceMaxPower);
    }

    public void preInit() {
    }

    public void init() {
    }

    public void onPlayerLogin(Player player) {
        int myRandom = 1 + (int)(Math.random() * ((3 - 1) + 1));
        switch (myRandom) {
            case 1: randomMessage = " plumpst ins Spiel";
            case 2: randomMessage = " fiel vom Himmel";
            case 3: randomMessage = " kracht auf den Server";
        }

        if(this.announcePlayers && player.getPower() < 1) {
            MessageServer.broadCastSafe("Spieler " + player.getName() + randomMessage, (byte) 1);
            //MessageServer.broadCastSafe("" + announceMessage + "", (byte) 1);
            player.getCommunicator().sendNormalServerMessage(  "" + announceMessage + "", (byte) 1);
        }
        if(this.announcePlayers && player.getPower() > 0) {
            MessageServer.broadCastSafe("GM " + player.getName() + " hat deinen Server betreten.", (byte) 1);
            //MessageServer.broadCastSafe("" + announceMessage + "", (byte) 1);
        }

    }
}

Thank you in advance

Eject

Edited by Eject

Share this post


Link to post
Share on other sites

ooops thank you =) i dont know why i forget this statement but now i got just messages from case 1

 

any idea?

 

Eject

Share this post


Link to post
Share on other sites

nvm, now a get different messages

i should make more than 3 messages so its more mixed =)

 

Eject

Thanks again

 

Now i can create a lot of funny welcome messages =)

Edited by Eject

Share this post


Link to post
Share on other sites

ehm, I do not know this Java magic, I prefer C# or Delphi Pascal, but

is not:

    int myRandom = 1 + (int)(Math.random() * ((3 - 1) + 1));

exactly the same as:

    int myRandom = 1 + (int)(Math.random() * 2);

well, not that it is important :D

 

usually random return real value between 0 and 1 as default parametr is 1

I expect, it should work using non default parametr of Math.random as well

    int myRandom = 1 + (int)Math.random(2);

 

I'm not sure if casting (int) round it down or mathematically ... in case round down, chance for result 3 is really low and you should look for some round() function

probably something like

    int myRandom = 1 + Math.round(Math.random(2));

 

but as I said, I'm not expert in this Java magic ...

Edited by Zakerak
  • Like 1

Share this post


Link to post
Share on other sites

Just use Server.rand.nextInt(3) - gives a random number from 0 1 2, no need for casting or all the other weirdness

  • Like 1

Share this post


Link to post
Share on other sites

That was discussed a bit in IRC bdew, Eject is learning java though so thought showing how to do it in base java without letting the server code do it for you might be nice. Plus personally I have found the servers random to be not so random for whatever reason.

Share this post


Link to post
Share on other sites

It's just an instance of a java.lang.Random... the same thing also used by Math.random.

 

You can make your own instance with new Random() and use that instead of Server.rand - it will be the same thing...

  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this