1. Hello I am really new at Java and I am working on a dumb plugin. I made commands and something with onplayerjoins from videos but I don't know how to make it so the command executes the onjoin event.

    The code:

    the AnimatedStand.java
    Code:
    package dev.debuggings.stand;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.entity.ArmorStand;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scheduler.BukkitRunnable;
    import dev.debuggings.stand.commands.Commands;
    
    public class AnimatedStand extends JavaPlugin implements Listener {
        
        @Override
        public void onEnable() {
            new Commands(this);
            getServer().getPluginManager().registerEvents(this, this);
        }
        
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            Player player = e.getPlayer();
            Location loc = player.getLocation();
            
            ArmorStand stand = loc.getWorld().spawn(loc, ArmorStand.class);
            stand.setVisible(false);
            stand.setGravity(false);
            stand.setArms(true);
            stand.setHelmet(new ItemStack(Material.SKULL_ITEM));
            int animate = Bukkit.getScheduler().scheduleAsyncRepeatingTask(this, new Animate(stand), 0, 1);
            
            new BukkitRunnable() {
                
                @Override
                public void run() {
                    Bukkit.getScheduler().cancelTask(animate);
                    stand.remove();
                }
            }.runTaskLater(this, 100);
                    
        }
    }
    
    The Animate.java
    Code:
    package dev.debuggings.stand;
    
    import org.bukkit.entity.ArmorStand;
    import org.bukkit.scheduler.BukkitRunnable;
    import org.bukkit.util.EulerAngle;
    
    public class Animate extends BukkitRunnable {
        
        ArmorStand s;
        
        public Animate(ArmorStand stand) {
            s = stand;
        }
    
        @Override
        public void run() {
            EulerAngle oldRot = s.getHeadPose();
            EulerAngle newRot = oldRot.add(0, 0.2f, 0);
            s.setHeadPose(newRot);
        }
        
    }
    
    The Commands.Java inside commands package
    Code:
    package dev.debuggings.stand.commands;
    
    import dev.debuggings.stand.AnimatedStand;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class Commands implements CommandExecutor {
        
        @SuppressWarnings("unused")
        private AnimatedStand plugin;
        
        public Commands(AnimatedStand plugin) {
            this.plugin = plugin;
            plugin.getCommand("overflux").setExecutor(this);
        }
        
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (!(sender instanceof Player)) {
                sender.sendMessage("Only players may execute this command!");
                return true;
            }
        
            Player p = (Player) sender;
        
            if (p.hasPermission("overflux.place")) {
                p.sendMessage("Placed Overflux");
                return true;
            } else {
                p.sendMessage("You do not have permission to run this command!");
            }
            return false;
        }
    
    }
    
    Again I want to make it so when you type in /overflux it does the onPlayerJoin event in the animatedstand.java
     
    #1
  2. I think you are confusing 2 things

    1) The command:

    A command is always typed by the player, and executes code based on that. if a player uses the /overflux command, in your example, the onCommand() function in that commandclass will be called.

    2) The onPlayerJoin

    This is an event, and this specific event (PlayerJoinEvent) is called every time that a player joins the server. It will only be called on that time


    If you want to have the same piece of code execute on the command, AND when a player joins, you'll have to create a new function with that code. Then you call that function both in your onCommand function in your command class, and in the onPlayerJoin function.
     
    #2
    • Useful Useful x 1
  3. I only want to use the command and with a cooldown. How can I achieve that?

    So like instead of the onPlayerJoin i want onCommand
     
    #3
  4. In your command class, you have this function

    Code:
    @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (!(sender instanceof Player)) {
                sender.sendMessage("Only players may execute this command!");
                return true;
            }
        
            Player p = (Player) sender;
        
            if (p.hasPermission("overflux.place")) {
                p.sendMessage("Placed Overflux");
                return true;
            } else {
                p.sendMessage("You do not have permission to run this command!");
            }
            return false;
        }
    everything in that function is executed when a player uses the command

    To do what you have in the onPlayerJoin in the onCommand instead, simply put the code in onCommand, and remove the onPlayerJoin (as it is no longer necessary then)

    If you want to make it a cooldown, theres a few things you could do
    The most common one is to get the time of when the command has been executed, save it somewhere in a variable, and the next time the command is run, check if enough time has passed for it to be valid.
     
    #4
    • Useful Useful x 1

Share This Page