My code is not working.

Discussion in 'Plugin Development' started by OwenZane247, Apr 11, 2020.

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

    OwenZane247

    I am a noob at plugin development. I am trying to make a plugin that allows you to give all players an effect while in a certain world. this is my code. It is not doing what it is intended to do. PLS HELP!

    Main.java
    Code:
    package me.LANDOFOZ.SecondPlugin;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import net.md_5.bungee.api.ChatColor;
    import net.minecraft.server.v1_14_R1.ChatModifier;
    
    @SuppressWarnings("unused")
    public class Main extends JavaPlugin {
        @Override
        public void onEnable() {
            getLogger().info("&1Enabled!");
            new PlayerListener(this);
            this.saveDefaultConfig();
        }
        @Override
        public void onDisable() {
           
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (label.equalsIgnoreCase("allefect")) {
                if (!sender.hasPermission("alleffect.reload")) {
                    sender.sendMessage(ChatColor.RED + "You can't run this!");
                    return true;
                }
                if (args.length == 0) {
                    sender.sendMessage(ChatColor.DARK_RED + "Usage: /alleffect reload");
                    return true;
                }
                if (args.length > 0) {
                    if (args[0].equalsIgnoreCase("reload")) {
                        sender.sendMessage(ChatColor.translateAlternateColorCodes('&', this.getConfig().getString("reload.message")));
                        this.reloadConfig();
                    }
                }
            }
            return false;
        }
       
    }
    
    PlayerListener.java
    Code:
    package me.LANDOFOZ.SecondPlugin;
    
    import java.util.StringTokenizer;
    
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.help.HelpTopicComparator.TopicNameComparator;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    @SuppressWarnings("unused")
    public class PlayerListener implements Listener{
           
            Main plugin;
    
            public PlayerListener(Main plugin) {
                plugin.getServer().getPluginManager().registerEvents(this, plugin);
               
            }
           
           
    
            @EventHandler
            public void onJoin(PlayerJoinEvent event) {
                Player player = event.getPlayer();
                for (String world : plugin.getConfig().getStringList("worlds")) {
                if (event.getPlayer().getLocation().getWorld().getName().equals(world)) {
                    if (player.hasPermission(plugin.getConfig().getString("effect.Permission"))) {
                player.addPotionEffect(new PotionEffect(PotionEffectType.getByName(plugin.getConfig().getString("effect.type")), Integer.MAX_VALUE, plugin.getConfig().getInt("effect.Amp"), true, false));
                } else {
                player.removePotionEffect(PotionEffectType.getByName(plugin.getConfig().getString("effect.type")));   
                }
                }
                }
            }
           
    }
    
    config.yml
    Code:
    reload:
      message: "&l&9Reloading, Please Wait..."
    
    worlds:
      - "world"
      - "sky_Spawn"
      - "bed_Spawn"
     
    effect:
      type: "SPEED"
      Amp: 3
      Permission: "alleffects.speed"
     
    
    plugin.yml
    Code:
    name: AllSpeed
    main: me.LANDOFOZ.SecondPlugin.Main
    version: 1.0
    commands:
      alleffect:
        description: Use this for plugin needs!
       
    
    
    Note: I just found one error. i still don't know if that is what is causing this

    It seems like it's not reading data from the config. I tested the command in my mc server and it reloaded the config but did not give me a reload message.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Apr 11, 2020
  2. Offline

    Wick

    The variable plugin in your listener is null, you never defined it, not sure if that's the only problem though. Are you getting errors in your console?
     
    Strahan likes this.
  3. Offline

    Strahan

    Yea, that'd be your problem. A few thoughts on the rest:
    1. Don't suppress things, address what the compiler is telling you
    2. Sending enable messages is pointless; Spigot already does that
    3. Having empty functions is even more pointless
    4. Your code only checks on login. You should be checking on PlayerChangedWorldEvent as well
    5. Don't assume external data is valid. Check it before using it. For all you know, someone fat fingered the config file when editing it and a path is not valid. If the plugin attempts to operate against bad data, it will crash.
    6. Player exposes getWorld() directly, you don't need to getLocation() first.
    7. Be careful that your world effects don't use potion effects a player can obtain. Since you are removing effects if the permission is not present, if you at some point configure an effect that a player could use (like Speed II for example) it will strip it from them even if they had it legit.
    8. You also may want to make the effects structured to a world/global state because what if someone wants speed in one world and night vision for another? You also should make it support multiple effects not just one. I'd have it broken down by world with an optional global level to apply to all worlds.
     
  4. Offline

    OwenZane247

Thread Status:
Not open for further replies.

Share This Page