Simple Newb Questions

Discussion in 'Plugin Development' started by RangerNuk, Oct 4, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    RangerNuk

    A few simple questions...

    1. How do I delay events, such as, the player is lit on fire, then 10 seconds later, they're health is set at 10. How would this be done?

    2. How do I make a percentage? Like there's a fifty percent chance that you can get poisoned, and a fifty percent chance that you get speed.

    3. Is there an easier way than writing the same code line 100 times to spawn a bunch of mobs.

    3.5. Is there a way to make the amount of mobs spawned determined by an argument and if so how do you do this?

    example- /<command> [player] [the number here determines how many mobs spawn]
     
  2. Offline

    ZeusAllMighty11

    1) http://wiki.bukkit.org/Scheduler_Programming

    2) Sorry I don't know, can't answer this

    3) Yes. Depends what you are doing I guess. If you are doing like "if mob type == creeper , do stuff", then you can use a switch statment. Example:


    if(player.getName().equalsignoreCase("bob"){
    // do stuff
    }if(player.getName().equalsignoreCase("Jeb_"){
    // do stuff
    }if(player.getName().equalsignoreCase("Notch"){
    // do stuff
    }



    ^ Bad way.

    How I do it:

    switch(player.getName()){

    case "bob" : System.out.println("It's bob!");
    case "Notch" : System.out.println("It's Notch!");
    case "Jeb_" : System.out.println("It's Jeb_!");
    }

    Not really a good example but close enough, just look up switch on youtube, bucky did a tut on it :)

    3.5) Get the user's argument, and make sure it is an int. If it's not an int, don't execute code (errors will surely come up)

    so like
    if(args.length == 2){
    if(args[0].equalsIgnoreCase("creeper"){

    World.spawnCreature(MobType.CREEPER, args[1]); // or however you spawn creatures. never done this sorry

    }
    }

    i'm probably wrong, let me look it up . Also, try using search button
     
  3. Offline

    MrFigg

  4. Offline

    RangerNuk

    Thanks both of u. but for 3 and 3.5 theres some issues.

    3. I want to spawn a bunch of mobs at once, like this maybe,
    target.getWorld().spawnCreature(target.getLocation(), CreatureType.ZOMBIE, 20);

    3.5. I want the argument to determine the NUMBER OF MOBS SPAWNED, not the mob spawning, THE NUMBER.

    Any more help would be nice.
     
  5. Offline

    CorrieKay

    1) as the poster above said, check out the scheduler
    2) use a random. You can set the seed yourself (do this if you need to use it more than once) but typically, just do this
    Random rand = new Random();
    int r = rand.nextInt(99); //the 99 parameter is the ceiling of the random (which it starts at 0) so this gives us a total of 100 numbers. Then if(r>=49) do something else do something else. 50/50 chance.

    3) for loops.
    int n = 100;
    for(int i = 1; i<n;i++){
    //spawn mob
    }
    will net you 100 mobs spawned in the same location.

    3.5) Yes.

    same code as above, except this:
    int n;
    try{
    n = Integer.parseInt(args[1]);
    } catch(NumberFormatException e){
    //number invalid/not an integer, return here or youre gonna have problems. ALTERNATIVELY, you can default to a number if you want, with this
    n = (your default number);
    }
    //for loop here
     
  6. Offline

    MrFigg

    Code:
    for(int i = 0; i < NUMBEROFMOBSSPAWNED; i++) {
        // Spawn mob here
    }
     
  7. Offline

    RangerNuk

    Heres some code, theres an issue, idk why, it says MobType can't be resolved to a variable.

    if(cmd.getName().equalsIgnoreCase("zomboid")){
    Player s = (Player)sender;
    Player target = s.getServer().getPlayer(args [0]);
    for(int i = 0; i < 20; i++) {
    World.spawnCreature(MobType.CREEPER, args[1]);
    }
    }

    P.S- how do you do the whole code bracket thing?

    It just does weird stuff for me.

    When i put the code brackets, its all weird. This is the same code as above.

    Code:
    [/SIZE][/FONT][/SIZE][/FONT]
    [SIZE=11px][FONT=Monaco](cmd.getName().equalsIgnoreCase([COLOR=#3933ff]"zomboid")){[/FONT][/SIZE]
    [SIZE=11px][FONT=Monaco]Player s = (Player)sender;[/FONT][/SIZE]
    [SIZE=11px][FONT=Monaco]Player target = s.getServer().getPlayer(args [0]);[/FONT][/SIZE]
    [SIZE=11px][FONT=Monaco]int i = 0; i < 20; i++) {[/FONT][/SIZE]
    [SIZE=11px][FONT=Monaco]World.spawnCreatureMobType.CREEPER, args[1]);[/FONT][/SIZE]
    [SIZE=11px][FONT=Monaco]}[/FONT][/SIZE]
    [SIZE=11px][FONT=Monaco]}[/FONT][/SIZE]
     
    [SIZE=11px][FONT=Monaco]
    [/code][/SIZE][/FONT]
     
  8. Offline

    CorrieKay

    spawnCreature is deprecated. Use spawnEntity instead, and use EntityType.CREEPER

    Theres no such thing as MobType :p
     
  9. Offline

    MrFigg

  10. Offline

    RangerNuk

    Now it says CREEPER cant be resolved to a field or isn't a field.

    I have imported it like this.

    import org.bukkit.entity.Creeper;
     
  11. Offline

    CorrieKay

  12. Offline

    RangerNuk

    Here is my code- it doesnt work!! When i do /plague rangernuk cold

    it returns false every time!!!

    if(cmd.getName().equalsIgnoreCase("plague")) {
    if(args.length > 1){
    sender.sendMessage("Too many arguments");
    returnfalse;
    }
    if(args.length < 1){
    sender.sendMessage("You didn't specify a plague...");
    returnfalse;
    }
    Player s = (Player)sender;
    Player target = s.getServer().getPlayer(args[0]);
    if (target == null) {
    sender.sendMessage(" is not online");
    returnfalse;
    }
    if(args.length == 2){
    if(args[0].equalsIgnoreCase("cold")){
    target.addPotionEffect(new PotionEffect(PotionEffectType.WEAKNESS, 10000, 1));
    target.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 10000, 1));
    target.addPotionEffect(new PotionEffect(PotionEffectType.SLOW_DIGGING, 10000, 1));

    }

    }
     
  13. Offline

    Drkmaster83

    It's obvious. You're checking for a second argument when you only have 1 argument.
    Also, I don't think the target == null method works. It throws a NPE, I believe.

    Correct Code:
    Code:
    if(cmd.getName().equalsIgnoreCase("plague")) {
    if(!(sender instanceof Player)){
    if(args.length > 1){
     
    sender.sendMessage("Too many arguments...");
     
    return false;
     
    }
     
    if(args.length < 1){
     
    sender.sendMessage("You didn't specify a plague...");
    // I don't think this part is correct either, it looks wrong.
     
    return false;
     
    }
    }
     
    if(sender instanceof Player){
    Player s = (Player) sender;
     
    Player target = s.getServer().getPlayer(args[0]); //args[1]?
    String targetName = target.getDisplayName();
     
    if (target == null) {
     
    s.sendMessage(targetName + " is not online!");
     
    return false;
     
    }
     
    if(args.length == 1){
     
    if(args[0].equalsIgnoreCase("cold")){
     
    s.sendMessage("You didn't specify a player!");
     
    }
    if(args.length == 2)
    {
    if(args[1] == targetName)
    {
    target.addPotionEffect(new PotionEffect(PotionEffectType.WEAKNESS, 10000, 1));
     
    target.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 10000, 1));
     
    target.addPotionEffect(new PotionEffect(PotionEffectType.SLOW_DIGGING, 10000, 1));
    }
    else
    {
    s.sendMessage("Player does not exist!");
    }
    }
    }
     
  14. Offline

    RangerNuk

    The if(target == null) method works for me all the time,
    ps, ur code failed
     
  15. Offline

    brettleaver

    To spawn a bunch of mobs, just use a simple for loop. If you don't know what this, I reccomend learning java a little more before getting into bukkit plugins :)
     
  16. Offline

    CorrieKay

    first of all, if you have two arguments, this would tell you there are too many arguments. thats why its returning false.

    Also,
    Player s = (Player)sender;
    Player target = s.getServer().getPlayer(args[0]);

    No need for this. just do
    Player target = Bukkit.getPlayer(args[0]);
    If you do it the previous way, automatically casting the sender into a player, if the console sends the command, it will throw an exception instead of handling it and exiting properly.

    finally, if you only DO give it one argument, this is what your code is doing.
    is it greater than one? no
    is it less than one? no.
    Is it equal to two? no. It cant possibly be anything but one at this point.
    return false.
     
  17. Offline

    RangerNuk

    I'm new to bukkit, not 2 java. I really figured out everything I know or got it on forums. At this point I've only tried minecraft mods and I've had no reason to use for loops at this point. I am still pretty much a been to java, even though I've made a successful plugin (2 actually). I don't think that matters, If you could show me the for loop, that would be very helpful.

    Thanks

    Here is the revised code- it still doesn't work-


    if(cmd.getName().equalsIgnoreCase("plague")) {
    if(args.length > 2){
    sender.sendMessage("Too many arguments");
    returnfalse;
    }
    if(args.length < 2){
    sender.sendMessage("You didn't specify a plague...");
    returnfalse;
    }
    Player target = Bukkit.getPlayer(args[0]);
    if (target == null) {
    sender.sendMessage(" is not online");
    returnfalse;
    }
    if(args.length == 2){
    if(args[0].equalsIgnoreCase("cold")){
    target.addPotionEffect(new PotionEffect(PotionEffectType.WEAKNESS, 10000, 1));
    target.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 10000, 1));
    target.addPotionEffect(new PotionEffect(PotionEffectType.SLOW_DIGGING, 10000, 1));

    }
    }
    //Add all other plague types here




    returntrue;

    }

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  18. Offline

    Woobie

    Not sure if this makes any difference, but
    Code:
    returnfalse;

    There should be space between "return" and "false"
     
  19. Offline

    RangerNuk

    there is, it just got changed in the copy/paste
     
  20. Offline

    Woobie

    Okay, well not sure about this either, but
    Code:
    if(args.length < 2){
    Shouldnt there be
    Code:
    else
    in front of the "if"

    So for example:
    Code:
    else if(args.length < 2){
    Code:
    else if(args.length == 2){
     
  21. Offline

    CorrieKay

    No, not necessarily, since he handles his returns correctly.

    RangerNuk your final if statement, change it to args[1] not args[0]. unless youre only ever allowed to target a player with a plague that shares his/her own name :p
     
  22. Offline

    brettleaver


    The for loop would be like this:
    If you are not new to java, then you should know what a for loop is, and how to make one, but here it is:

    Code:
     
    for(int x = 10; x < 20; x = x+1){
            //spawn mob code goes here
    }
     
     
    
     
  23. Offline

    RangerNuk

    That's what I'm using, I just didn't know what it was called.
     
  24. Offline

    Drkmaster83

    So, solved or not? :p
     
  25. Offline

    RangerNuk

Thread Status:
Not open for further replies.

Share This Page