Solved 2nd step: My 1st plugin: help!

Discussion in 'Spigot Plugin Development' started by NamerPRO, Jun 19, 2018.

?

Hope it's not the same mistake again :)

  1. Same one

    2 vote(s)
    50.0%
  2. Not the same one

    2 vote(s)
    50.0%
  1. Well, now I have a problem with sounds. Can somebody help me here?
    Problem line:
    Code (Java):
    music.playSound(music.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 1, 1);
    Also why isn't ChatColor.<color> working in console?
    -_-_-_-_-_-_-_
    Code (Java):
    package ru.namerpro;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Sound;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;

    public class mainListener extends JavaPlugin implements Listener{
     
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            getLogger().info(ChatColor.GREEN + "Plugin Enabled!");
        }
     
        public void onDisable() {
            getLogger().info(ChatColor.RED + "Plugin Disabled!");
        }
     
        @EventHandler
        public void onJoin(PlayerJoinEvent event) {
            Player music = event.getPlayer();
            event.setJoinMessage(ChatColor.AQUA + event.getPlayer().getName() + ChatColor.GREEN + " joined the server");
            music.sendMessage(ChatColor.GRAY + "Plugin version: " + ChatColor.RED + "1.0.0");
            music.playSound(music.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 1, 1);
        }
     
        @EventHandler
        public void onLeave(PlayerQuitEvent event) {
            event.setQuitMessage(ChatColor.AQUA + event.getPlayer().getName() + ChatColor.RED + " left the server");
        }
     
    }
    -_-_-_-_-_-_-_
     
    #1 NamerPRO, Jun 19, 2018
    Last edited: Jun 19, 2018
  2. You can send colors in console if you use Bukkit.getConsoleSender().sendMessage

    As for the other issue, what exactly is the problem? Is there a stack trace you can share?
     
    • Agree Agree x 1
  3. For colors do what @Trophonix just said. change
    Code (Text):
             getLogger().info(ChatColor.GREEN + "Plugin Enabled!");
    to
    Code (Text):
    Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "enabled");
    2nd problem (really just is a preference) Personally I would not set music to be a player as that would just confuse me so much. I would rather set it to p or player, but it is just a preference.
    Code (Text):
             Player music = event.getPlayer();
    (NOT A PROBLEM HERE JUST A PREFERENCE)
    Now, for the music you must do a BukkitRunnable (pretty much delays a task to be run) on player join event when you do stuff like playSound or add items to inventory
    Code (Text):
           @EventHandler
            public void onJoin(PlayerJoinEvent event) {
                Player music = event.getPlayer();
                event.setJoinMessage(ChatColor.AQUA + event.getPlayer().getName() + ChatColor.GREEN + " joined the server");
                music.sendMessage(ChatColor.GRAY + "Plugin version: " + ChatColor.RED + "1.0.0");
                new BukkitRunnable() {
                   
                    @Override
                    public void run() {
                        music.playSound(music.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 1, 1);
                       
                    }
                }.runTaskLater(main, 20L);
            }
    }
    For more information on runnables:
    https://forums.bukkit.org/threads/need-a-simple-example-of-scheduler-programming.93719/
    https://forums.bukkit.org/threads/scheduled-delayed-tasks.98590/
    https://forums.bukkit.org/threads/repeating-tasks.134389/
    https://forums.bukkit.org/threads/schedule-sync-repeating-task.223391/
    https://forums.bukkit.org/threads/schedule-indefinite-repeating-task.101723/
    https://forums.bukkit.org/threads/schedule-repeating-task-exit.60424/
     
    • Agree Agree x 1
  4. I am newbie and I don't understand what is @Override, new BukkitRunnable() {, }.runTaskLater(main, 20L);
    Please, help me. I found the example here too, is it correct? They say it must work.

    The code:
    Code (Java):
    // сюда пишем вашу папку
    package ru.Stebbilust.test;

    import org.bukkit.Bukkit;
    import org.bukkit.Sound;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin implements Listener { // Теперь мы следим за событиями

    public void onEnable() {
    Bukkit.getServer().getPluginManager().registerEvents(this, this); // регестрируем эвенты в этом классе
    getLogger().info("Hello!!!");
    }

    public void onDisable() {
    getLogger().info("Bye!!!");
    }

    @EventHandler
    // Пишем перед каждым эвентом
    public void onPlayerJoin(PlayerJoinEvent e) { // Когда заходит игрок то происходит...
    Player p = e.getPlayer(); // Передаем переменной p игрока, который зашел
    p.sendMessage(ChatColor.BLUE+"Привет"); // Пишем игроку сообщение "Привет" синим цветом
    p.playSound(p.getLocation(), Sound.BLOCK_ANVIL_FALL, 1, 1); // Проигрываем игроку звук
    }
    }
    Link to this page: https://ru-minecraft.ru/instrukcii-...2-sozdanie-plagina-spigot-chast-2-eventy.html (code is in spoiler)
     
  5. The .runTaskLater run the code that you put inside of the public void run after 20L in your case (1 second(20L == 1 sec)). This is Bukkit built in method. Please before attemping to write a java plugin learn the basics of Java as with the right knowlage you can solve your problems without any help: you can follow some courses online or use some books:
    https://drive.google.com/open?id=19XXeNOnwxJqmqtJKA4EDiZZYxB-6mjAS