1.16.5 Basic java plugin help

Discussion in 'Spigot Plugin Development' started by DenDennisCool, Jul 8, 2021.

  1. Hey, I'm new to plugins and I wanted to send a string from Main.java to Events.java in a different package.
    I will put the code in for reference, but it's probably better if someone told me how I can do this or how to look up stuff like this.
    What I want is to broadcast the message "You moved!", but is it possible to instead of just creating the string in Events.java, you can make it in Main.java and send it to Events.java somehow?
    Main.java
    Code (Java):

    package me.dennis.listeners;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;

    import me.dennis.listeners.join.Events;

    public class Main extends JavaPlugin implements CommandExecutor {
        String string = "You moved!";
        @Override
        public void onEnable() {

            saveDefaultConfig();
           
            new Events(this);

            String string = "You moved!";

        }

       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
           
            if (sender instanceof Player) {
                  Player p = (Player) sender;
                if (cmd.getName().equalsIgnoreCase("hey")) {
              String string = "Hey";
             
                }
                    return true;
        } else {return true;}
    }
    }


     
    Events.java
    Code (Java):

    package me.dennis.listeners.join;

    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;

    import me.dennis.listeners.Main;
    import me.dennis.listeners.utils.Utils;

    public class Events implements Listener{
       
       
        private static Main plugin;
       
        public Events(Main plugin) {
            this.plugin = plugin;
            Bukkit.getPluginManager().registerEvents(this, plugin);
        }

    @EventHandler
    public void onMove(PlayerMoveEvent e) {
        Bukkit.broadcastMessage(string);
    }

    }
     
     
    • Agree Agree x 2
  2. Create a private final String in your Events-class, and modify the constructor to take your Plugin and the given String as parameters.

    In your constructor, you set the value of the private final String you declared to the value passed as a parameter.

    Modify the Main-class to pass the String you want to use.

    However, it's probably better to use either a config-file, or a Resource bundle for this kind of stuff.
     
  3. Other than everything said above, you might want to learn more about Java before coding plugins.
     
    • Agree Agree x 2