Rainbow Sheep

Discussion in 'Plugin Development' started by ThatCoffeeBean, Oct 30, 2014.

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

    ThatCoffeeBean

    Hey guys. Im wondering, what is the easiest way to spawn randomly colored sheep at the players location?
    Ive looked many places and can't find how seep coloring works.

    Any Ideas
     
  2. Offline

    OffLuffy

    World#spawnEntity(Location, EntityType) to spawn the sheep, which will return an Entity. Cast it to a Sheep and use the Sheep#setColor(DyeColor) method on it.
     
  3. Offline

    ThatCoffeeBean

    How do i make the coloring random?
    OffLuffy
     
  4. Offline

    SleepyDog

    This will spawn a black sheep at 'loaction' with name 'name'
     
  5. Offline

    OffLuffy

    Not tested, but I would likely have a method to fetch a random int, and get a value from DyeColor.values() using that int.

    i.e.
    Code:java
    1. DyeColor randColor = DyeColor.values()[ rand(0, DyeColors.values().length-1) ];
    2. public int rand(int min, int max) { return min + (int)(Math.random() * ((max - min) + 1)); }


    setColor(DyeColor), not setDyeColor(DyeColor)
    Sheep extends Colorable

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  6. Offline

    ThatCoffeeBean

    Guys - Can i ask you to review my code in a minuet?
    OffLuffy
     
  7. Offline

    SleepyDog

    I pulled this from an old forums post. Never messed with sheep before.
     
  8. Offline

    OffLuffy

    Posting your attempt is usually preferable, to help us understand what you've managed so far. I'd say yes, although this forum community is generally against 'spoon feeding' so we likely won't do too much of the work for you XP

    I haven't either, lmao. Just looked up the JavaDocs for most of it.
     
  9. Offline

    ThatCoffeeBean

    Basically, I need a different colored sheep to spawn at the players location every 1/2 second.
    OffLuffy
    SleepyDog


    package XCraft;

    import org.bukkit.Bukkit;
    import org.bukkit.DyeColor;
    import org.bukkit.Material;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Sheep;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.scheduler.BukkitScheduler;

    public class NyanSheep {

    DyeColor randColor = DyeColor.values()[ rand(0, DyeColor.values().length-1) ];
    public int rand(int min, int max) { return min + (int)(Math.random() * ((max - min) + 1)); }

    @EventHandler
    public void onPlayerInteract(final PlayerInteractEvent evt) {
    if (evt.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
    if (evt.getClickedBlock().getType() == Material.BOOKSHELF) {

    @SuppressWarnings("unused")

    BukkitScheduler scheduler = Bukkit.getServer().getScheduler(); {
    ((BukkitScheduler) new NyanSheep()).scheduleSyncDelayedTask((Plugin) this, new Runnable() {
    public void run() {
    LivingEntity entity = (LivingEntity) Bukkit.getWorld("world").spawnEntity(evt.getPlayer().getLocation(), EntityType.SHEEP);
    entity.setCustomName("name");

    Sheep sheep = (Sheep) Bukkit.getWorld("world").spawnEntity(evt.getPlayer().getLocation(), EntityType.SHEEP);
    sheep.setCustomName("name");
    sheep.setColor(DyeColor.BLACK);
    }
    }, 20L);
    }
    }

    }
    }
    }
     
  10. Offline

    OffLuffy

    If you wrap that in
    Code:
    [syntax=java] and [/syntax]
    it's a bit easier to read XD

    Also, 20L is one second in ticks. 10L is half a second.
     
  11. Offline

    SleepyDog

    So i know what i am looking at.

    Code:java
    1. package XCraft;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.DyeColor;
    5. import org.bukkit.Material;
    6. import org.bukkit.entity.EntityType;
    7. import org.bukkit.entity.LivingEntity;
    8. import org.bukkit.entity.Sheep;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.block.Action;
    11. import org.bukkit.event.player.PlayerInteractEvent;
    12. import org.bukkit.plugin.Plugin;
    13. import org.bukkit.scheduler.BukkitScheduler;
    14.  
    15. public class NyanSheep {
    16.  
    17. DyeColor randColor = DyeColor.values()[ rand(0, DyeColor.values().length-1) ];
    18. public int rand(int min, int max) { return min + (int)(Math.random() * ((max - min) + 1)); }
    19.  
    20. @EventHandler
    21. public void onPlayerInteract(final PlayerInteractEvent evt) {
    22. if (evt.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
    23. if (evt.getClickedBlock().getType() == Material.BOOKSHELF) {
    24.  
    25. @SuppressWarnings("unused")
    26.  
    27. BukkitScheduler scheduler = Bukkit.getServer().getScheduler(); {
    28. ((BukkitScheduler) new NyanSheep()).scheduleSyncDelayedTask((Plugin) this, new Runnable() {
    29. public void run() {
    30. LivingEntity entity = (LivingEntity) Bukkit.getWorld("world").spawnEntity(evt.getPlayer().getLocation(), EntityType.SHEEP);
    31. entity.setCustomName("name");
    32.  
    33. Sheep sheep = (Sheep) Bukkit.getWorld("world").spawnEntity(evt.getPlayer().getLocation(), EntityType.SHEEP);
    34. sheep.setCustomName("name");
    35. sheep.setColor(DyeColor.BLACK);
    36. }
    37. }, 20L);
    38. }
    39. }
    40.  
    41. }
    42. }
    43. }
    44.  
     
  12. Offline

    ThatCoffeeBean

    So will i work?
     
  13. Offline

    SleepyDog

    You dont need to set the name of the sheep if you don't want to.
    If you want to look good, there is a name you can set the sheep to and it will change color every 0.5 seconds.
     
  14. Offline

    OffLuffy

    Why are you initializing a scheduler variable, then trying to get one by initializing the NyanSheep class?

    You probably want to do scheduler.scheduleSyncDelayedTask()

    You also seem to be just copying and pasting a lot of code without understanding it. Much of it is for example and not necessary.
     
  15. Offline

    ThatCoffeeBean

  16. Offline

    CBaer4848

    ^^^ that works lawl
     
  17. Offline

    ThatCoffeeBean

    Also, will they be random colored everytime they spawn?
     
  18. Offline

    SleepyDog

    Ignore everything, just spawn a sheep with the name 'jeb_' and everything will look awesome.
     
    bennie3211 likes this.
  19. Offline

    CBaer4848

    Maybe try setting the sheeps name to "jeb_" in the custom name. It might tie in with Vanilla :p
     
  20. Offline

    ThatCoffeeBean

    and finally, how can i cancel the task after 8 seconds and kill all the sheep?
     
  21. Offline

    OffLuffy

    Not likely. The sheep's color doesn't technically change, it's a client-side effect, but the sheep's color is technically always the same. The client just sort of ignores the color and uses the smooth color transition. If the sheep is sheared, it won't be random.
     
  22. Offline

    ThatCoffeeBean

    thats fine read my last comment.
     
  23. Offline

    OffLuffy

    Use the scheduler as you have been and use the remove() method for the Sheep. Note that when accessing variables outside a scheduler block from within it, the variable needs to be declared final.
     
  24. Offline

    SleepyDog

    ^ What he said
     
  25. Offline

    FabeGabeMC

    Hawktasard likes this.
Thread Status:
Not open for further replies.

Share This Page