My Plugin doesnt work how it should

Discussion in 'Plugin Development' started by xjannik.361, Jun 27, 2021.

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

    xjannik.361

    I coded a Plugin which gives you the ability to have a few shorten commands.
    Like /gm 0-3
    or /w sun,rain,thunder

    And if i try execute /gm 1 it work's but don't gives the Player the Title which i wanna.
    It only says: "An internal error occured while attempting to perform this command."

    If i try execute /w sun, rain or thunder it doesn't do anything.

    Any Help?

    The first Code is the Class for Gm 1-3
    The Second is The Main Class
    And the third is the Weather Class

    Code:
    package de.jannik.functional.cmd;
    
    import de.jannik.functional.Functional;
    import org.bukkit.Bukkit;
    import org.bukkit.GameMode;
    import org.bukkit.Sound;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class cmd implements CommandExecutor {
    
        @Override
        public boolean onCommand(CommandSender s, Command cmd, String label, String[] args) {
            if (!(s instanceof Player)) {
                Functional.INSTANCE.log("You are not a Player!");
            }
            assert s instanceof Player;
            Player p = (Player) s;
            if (args.length == 1) {
                if (args[0].equalsIgnoreCase("1")) {
                    if (p.hasPermission("gm.1")) {
                        p.setGameMode(GameMode.CREATIVE);
                        p.sendTitle("§4§l[§6Info:§4§l]", "§cYour Gamemode is now §a§lCreative§c!", 3, 10, 3);
                        p.playSound(p.getLocation(), Sound.ENTITY_ENDER_DRAGON_GROWL, 3.0F, 2.0F);
                    } else {
                        p.sendTitle("§4§l[§6Info:§4§l]", "§cYou have no §a§lPermission§c!", 3, 10, 3);
                        p.playSound(p.getLocation(), Sound.ENTITY_VILLAGER_DEATH, 3.0F, 1.0F);
                    }
                } else if (args[0].equalsIgnoreCase("2")) {
                    if (p.hasPermission("gm.2")) {
                        p.setGameMode(GameMode.ADVENTURE);
                        p.sendTitle("§4§l[§6Info:§4§l]", "§cYour Gamemode is now §a§lAdventure§c!", 3, 10, 3);
                        p.playSound(p.getLocation(), Sound.ENTITY_ENDER_DRAGON_GROWL, 3.0F, 2.0F);
                    } else {
                        p.sendTitle("§4§l[§6Info:§4§l]", "§cYou have no §a§lPermission§c!", 3, 10, 3);
                        p.playSound(p.getLocation(), Sound.ENTITY_VILLAGER_DEATH, 3.0F, 1.0F);
                    }
                } else if (args[0].equalsIgnoreCase("3")) {
                    if (p.hasPermission("gm.3")) {
                        p.setGameMode(GameMode.SPECTATOR);
                        p.sendTitle("§4§l[§6Info:§4§l]", "§cYour Gamemode is now §a§lSpectator§c!", 3, 10, 3);
                        p.playSound(p.getLocation(), Sound.ENTITY_ENDER_DRAGON_GROWL, 3.0F, 2.0F);
                    } else {
                        p.sendTitle("§4§l[§6Info:§4§l]", "§cYou have no §a§lPermission§c!", 3, 10, 3);
                        p.playSound(p.getLocation(), Sound.ENTITY_VILLAGER_DEATH, 3.0F, 1.0F);
                    }
                } else if (args[0].equalsIgnoreCase("0")) {
                if (p.hasPermission("gm.0")) {
                    p.setGameMode(GameMode.SURVIVAL);
                    p.sendTitle("§4§l[§6Info:§4§l]", "§cYour Gamemode is now §a§lSurvival§c!", 3, 10, 3);
                } else {
                    p.sendTitle("§4§l[§6Info:§4§l]", "§cYou have no permission to execute this Command!", 3, 10, 3);
                }
                    p.playSound(p.getLocation(), Sound.ENTITY_ENDER_DRAGON_GROWL, 3.0F, 2.0F);
                } else {
                p.sendTitle("§4§l[§6Info:§4§l]", "That's not a valid number! Did you typed in correctly?", 3, 10, 3);
                p.playSound(p.getLocation(), Sound.ENTITY_ENDER_DRAGON_GROWL, 3.0F, 2.0F);
            }
        }   else if (args.length == 2) {
                try {
                    Player a = Bukkit.getPlayer(args[1]);
                    if (args[0].equalsIgnoreCase("1")) {
                        if (p.hasPermission("gm.1")) {
                            a.setGameMode(GameMode.CREATIVE);
                            a.sendTitle("§4§l[§6Info:§4§l]","§cYour Gamemode is now §a§lCreative§c!", 3, 10, 3);
                            a.playSound(a.getLocation(), Sound.ENTITY_ENDER_DRAGON_GROWL, 3.0F, 2.0F);
    
                        } else {
                            p.sendTitle("§4§l[§6Info:§4§l]", "§cYou have no permission to execute this Command!", 3, 10, 3);
                        }
                    } else if (args[0].equalsIgnoreCase("2")) {
                        if (p.hasPermission("gm.2")) {
                            a.setGameMode(GameMode.ADVENTURE);
                            a.sendTitle("§4§l[§6Info:§4§l]","§cYour Gamemode is now §a§lAdventure§c!", 3, 10, 3);
                            a.playSound(a.getLocation(), Sound.ENTITY_ENDER_DRAGON_GROWL, 3.0F, 2.0F);
                        } else {
                            p.sendTitle("§4§l[§6Info:§4§l]", "§cYou have no permission to execute this Command!", 3, 10, 3);
                        }
                    } else if (args[0].equalsIgnoreCase("3")) {
                        if (p.hasPermission("gm.3")) {
                            a.setGameMode(GameMode.SPECTATOR);
                            a.sendTitle("§4§l[§6Info:§4§l]","§cYour Gamemode is now §a§lSpectator§c!", 3, 10, 3);
                            a.playSound(a.getLocation(), Sound.ENTITY_ENDER_DRAGON_GROWL, 3.0F, 2.0F);
                        } else {
                            p.sendTitle("§4§l[§6Info:§4§l]", "§cYou have no permission to execute this Command!", 3, 10, 3);
                        }
                    } else if (args[0].equalsIgnoreCase("0")) {
                        if (p.hasPermission("gm.0")) {
                            a.setGameMode(GameMode.SURVIVAL);
                            a.sendTitle("§4§l[§6Info:§4§l]","§cYour Gamemode is now §a§lSurvival§c!", 3, 10, 3);
                            a.playSound(a.getLocation(), Sound.ENTITY_ENDER_DRAGON_GROWL, 3.0F, 2.0F);
                        } else {
                            p.sendTitle("§4§l[§6Info:§4§l]", "§cYou have no permission to execute this Command!", 3, 10, 3);
                        }
                    } else {
                        p.sendTitle("§4§l[§6Info:§4§l]", "§cThat's not a valid number! Did you typed in correctly?", 3, 10, 3);
                    }
                } catch (NullPointerException d) {
                    p.sendTitle("§4§l[§6Info:§4§l]", "§cThis Player is not online!", 3, 10, 3);
                    p.playSound(p.getLocation(), Sound.ENTITY_VILLAGER_DEATH, 3.0F, 1.0F);
                }
            } else {
                p.sendTitle("§4§l[§6Usage:§4§l]", "§c/gm 0-3 [Player]", 3, 10, 3);
                p.playSound(p.getLocation(), Sound.ENTITY_VILLAGER_DEATH, 3.0F, 1.0F);
            }
            return true;
        }
    
    }
    
    




    Code:
    package de.jannik.functional;
    
    import de.jannik.functional.cmd.Weather;
    import de.jannik.functional.cmd.cmd;
    import org.bukkit.Bukkit;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public final class Functional extends JavaPlugin {
    
        public static String PREFIX = "§4§l[§6Functional§4§l]";
        public static Functional INSTANCE;
    
        public Functional() {
            INSTANCE = this;
        }
        @Override
        public void onEnable() {
            // Plugin startup logic
            log(" Plugin loaded!");
            getCommand("gm").setExecutor(new cmd());
            getCommand("w").setExecutor(new Weather());
        }
    
        @Override
        public void onDisable() {
            // Plugin shutdown logic
            log("  Plugin unloaded!");
        }
    
        public void log(String text) {
            Bukkit.getConsoleSender().sendMessage(PREFIX + text);
        }
    
    }
    
    




    Code:
    package de.jannik.functional.cmd;
    
    import de.jannik.functional.Functional;
    import org.bukkit.Bukkit;
    import org.bukkit.Sound;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class Weather implements CommandExecutor {
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player n = Bukkit.getPlayer(Bukkit.getName());
            World w = Bukkit.getWorld("world");
            if (!(sender instanceof Player)) {
                Functional.INSTANCE.log("You are not a Player!");
            }
            if (args[0].equalsIgnoreCase("sun")) {
                {
                        if ("sun".equalsIgnoreCase(args[0])) {
                            if (n.hasPermission("w.sun")) {
                                w.hasStorm();
                                w.setStorm(false);
                                w.setThundering(false);
    
                                n.sendTitle("§4§l[§6Info:§4§l]", "§cWeather changed to §e§lSun§c!", 3, 10, 3);
                                     n.sendTitle("§4§l[§6Info:§4§l]", "§cYou have no §a§lPermission§c!", 3, 10, 3);
                                n.playSound(n.getLocation(), Sound.ENTITY_ENDER_DRAGON_GROWL, 3.0F, 2.0F);
                            } else {
                                n.playSound(n.getLocation(), Sound.ENTITY_VILLAGER_DEATH, 3.0F, 1.0F);
                            }
                        } else if ("rain".equalsIgnoreCase(args[0])) {
                            if (n.hasPermission("w.rain")) {
                                w.setStorm(true);
                                w.setThundering(false);
    
                                n.sendTitle("§4§l[§6Info:§4§l]", "§cWeather changed to §b§lRain§c!", 3, 10, 3);
                                n.playSound(n.getLocation(), Sound.ENTITY_ENDER_DRAGON_GROWL, 3.0F, 2.0F);
    
                            }   else {
                            n.sendTitle("§4§l[§6Info:§4§l]", "§cYou have no §a§lPermission§c!", 3, 10, 3);
                            n.playSound(n.getLocation(), Sound.ENTITY_VILLAGER_DEATH, 3.0F, 1.0F);
                            }
                        } else if ("thunder".equalsIgnoreCase(args[0])) {
                            w.setStorm(true);
                            w.setThundering(true);
    
                            n.sendTitle("§4§l[§6Info:§4§l]", "§cWeather changed to §1§lThunder§c!", 3, 10, 3);
                            n.playSound(n.getLocation(), Sound.ENTITY_ENDER_DRAGON_GROWL, 3.0F, 2.0F);
    
                    } else {
                        n.sendTitle("§4§l[§6Info:§4§l]", "§cYou have no §a§lPermission§c!", 3, 10, 3);
                        n.playSound(n.getLocation(), Sound.ENTITY_VILLAGER_DEATH, 3.0F, 1.0F);
                             }
    
                     }
                }
    
    
        return false;
        }
            }
    
    
    
    
     
  2. Offline

    Shqep

    This means an error has occurred, and most of the times, its stack trace is printed within the console. You should learn to read stack traces, because as the name suggests, it helps you trace back from the error to the problematic codes.

    Because you haven't shown us the stack trace, I'm assuming that you are using a different API version than the one running the server, so that the method .sendTitle() was not yet available.
    And about the weather command not running, are you sure you registered the command in plugin.yml?

    There are some problems that I think it might help you to point out, you can ignore this if you're fine with how you're doing.
    Possible Improvements (open)

    1. Class names
    Java's convention for class names is that each word is capitalized. For example, CommandExecutor. And the convention for package names is that it starts with a lower case character. For example, org.bukkit.entity.
    Wouldn't "GamemodeCommand" be a better class name for class "cmd"?

    2. Console handling
    [​IMG]

    3. if else if
    [​IMG]
    [​IMG]

    4. Possible NPE from Bukkit.getPlayer()
    You have to check first whether the player retrieved is null or not. Instead of surrounding the entire thing with try-catch, why not just a single if statement and returns if the player was null?
    [​IMG]

    5. Read the documentations
    [​IMG]

    6. Uh oh nested ifs failed
    [​IMG]

    7. Hard coded color sign
    [​IMG]
     
  3. Offline

    xjannik.361

    I've readed your Message.
    I registered it in the plugin.yml, i'm gonna look for the Right API.
    Thanks for the help.
     
Thread Status:
Not open for further replies.

Share This Page