[guide] skript code to java code

Discussion in 'Programming' started by omgitsjayden, Feb 12, 2020.

?

bad poll

  1. yes (correct answer)

    33.3%
  2. no (probably wrong answer idk)

    66.7%
Thread Status:
Not open for further replies.
  1. 1. loop all players :: gets all online players

    Java would look like this:

    Code (Text):
    for (Player <text> : Bukkit.getOnlinePlayers()) {

    }

    2. loop-player. the looped player

    Java's loop-player is the variable set at <text>

    So if it said Player p... then you replace loop-player with "p".

    3. Sender / Player

    In skript, sender refers to the instance running the command. and player refers to the player executing the command.

    There are 2 main types of instances: player and console

    In Java, "sender" is the same, however "player" refers to an entity type.

    To set a player variable. Type this:

    Code (Text):
    Player player = (Player) sender
    So now you can say:

    Code (Text):
    player.sendMessage("Variables are cool, but strings are better.");
    With this, you can replace "player" with "sender", as you can send message to player and console instances.
    You can also do:

    Code (Text):
    sender.setHealth(0.0F);
    Note that I used "sender" instead of player. That's because it gets the object that sent the command, not the entity instance.

    The sender/player bit would have to be marked as a Damageable like this:

    Code (Text):

    if (sender instanceof Player) {
         Damageable player = (Damageable) sender
         player.setHealth(0.0F);
    }
     
    Important: health is a float, not an int, nor a long. It is a FLOAT. You must have a decimal place and end the float with an F

    4. Documentation.

    Skriptdocs: SkUnity Github
    Javadocs: Oracle
    Bukkitdocs: SpigotMC

    Conclusion: don't use Skript, ever.[/code]
     
    #1 omgitsjayden, Feb 12, 2020
    Last edited: Feb 12, 2020
    • Like Like x 1
  2. Well, you asked for it. This is incorrect, sender refers to the object that executed the command (assuming you are in an onCommand and named your first parameter like that). This can be the console a command block or an entity. In this case, the actual type is unknown and you can't set the health of, for example, a command block or the console (or some entities), hence this will not work. You will first have to cast this to a Damgeable before you can set the health (although you should probably check if the object is actually a Damageable before doing this).
     
  3. yep it seems wrong even just looking at what i said
     
  4. Player implements Damageable - if the cast from sender to Player fails, the code won't continue, which means that sender can then inherit Player methods
     
Thread Status:
Not open for further replies.