Creating a player toggle.

Discussion in 'Plugin Development' started by u_mad_bros, Jul 6, 2017.

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

    u_mad_bros

    I am creating a player toggle using Metadata. For some reason, once one player sets off a trigger that removes the metadata from themselves, it breaks the plugin. Entirely. I have an event that gets when a player leaves a certain range, and when they trigger this and have the metadata, it removes the metadata from the player and teleports them somewhere. Though after one player triggers this, no other players can. Anybody know what's wrong?
     
  2. Offline

    A5H73Y

    Incredibly hard to fix a problem if you don't give us code to work with.
     
  3. Offline

    u_mad_bros

    Code:
    
    package me.umadbros.skyblockfly.commands;
    
    import java.util.ArrayList;
    import java.util.UUID;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.metadata.FixedMetadataValue;
    import org.bukkit.metadata.MetadataValue;
    import org.bukkit.plugin.Plugin;
    
    import me.umadbros.skyblockfly.Main;
    
    import com.wasteofplastic.askyblock.ASkyBlock;
    import com.wasteofplastic.askyblock.ASkyBlockAPI;
    public class IslandFlyCommand implements CommandExecutor {
    
        private Main main;
        public static Player pl;
        public FileConfiguration config;
        public static ASkyBlockAPI d = ASkyBlockAPI.getInstance();
       
        public IslandFlyCommand(Main main) {
            config = main.getConfig();
            this.main = main;
            return;
        }
        public ASkyBlock getASkyBlock() {
            Plugin p = Bukkit.getServer().getPluginManager().getPlugin("ASkyBlock");
            if (p instanceof ASkyBlock) return (ASkyBlock) p;
            else return null;
        }
    
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "You must be a player to do this!");
                return true;
            } else {
               
                pl = (Player) sender;
                UUID playeruuid = (UUID) pl.getUniqueId();
                if (!(pl.hasPermission("islandfly.use"))) {
                    pl.sendMessage(ChatColor.RED + "You do not have permission to use this command!");
                    return true;
                   
                } else {
                   
                if (!(pl.getWorld().getName().equals(config.getString("allow-ifly-world")))) {
                    pl.sendMessage(ChatColor.translateAlternateColorCodes('&', config.getString("try-enable-outside-world")));
                    return true;
                   
                } else {
                    UUID loc1 = d.getIslandAt(pl.getLocation()).getOwner();
                    UUID loc2 = d.getIslandOwnedBy(playeruuid).getOwner();
                    if (cmd.getName().equalsIgnoreCase("ifly") && !(pl.hasMetadata("IFLY")) && loc1 == loc2) {
                        pl.setMetadata("IFLY", new FixedMetadataValue(main, "IFLY"));
                        pl.sendMessage(ChatColor.translateAlternateColorCodes('&',config.getString("Fly-Enabled-Message")));
                        pl.setAllowFlight(true);
                        return true;
                    }
                    if (cmd.getName().equalsIgnoreCase("ifly") && pl.hasMetadata("IFLY")) {
                        pl.sendMessage(ChatColor.translateAlternateColorCodes('&',config.getString("Fly-Disabled-Message")));
                        pl.removeMetadata("IFLY", main);
                        pl.setAllowFlight(false);
                        return true;
                    }
                    if (cmd.getName().equalsIgnoreCase("ifly") && !(pl.hasMetadata("IFLY"))) {
                        pl.sendMessage(ChatColor.translateAlternateColorCodes('&',config.getString("try-enable-outside-island")));
                        return true;
                    }
                }
            }
            return false;
        }
    }
    }
    
    Code:
    package me.umadbros.skyblockfly.events;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.metadata.FixedMetadataValue;
    
    import com.wasteofplastic.askyblock.events.IslandExitEvent;
    
    import me.umadbros.skyblockfly.Main;
    import me.umadbros.skyblockfly.commands.IslandFlyCommand;
    
    public class IslandLeaveWithFly implements Listener {
        private FileConfiguration config;
        private Main main;
       
        public IslandLeaveWithFly(Main main) {
            this.config = main.getConfig();
            this.main = main;
        }
        @EventHandler
        public void IslandLeave(IslandExitEvent e) {
            if (IslandFlyCommand.pl.hasMetadata("IFLY")) {
                IslandFlyCommand.pl.removeMetadata("IFLY", main);
                IslandFlyCommand.pl.setAllowFlight(false);
                IslandFlyCommand.pl.sendMessage(ChatColor.translateAlternateColorCodes('&', config.getString("island-exit-message")));
                if (IslandFlyCommand.d.getIslandOwnedBy(IslandFlyCommand.pl.getUniqueId()).getSpawnPoint() != null) {
                    IslandFlyCommand.pl.teleport(IslandFlyCommand.d.getIslandOwnedBy(IslandFlyCommand.pl.getUniqueId()).getSpawnPoint());
                } else {
                    Location worldspawn = Bukkit.getWorld(config.getString("teleport-world")).getSpawnLocation();
                    IslandFlyCommand.pl.sendMessage(ChatColor.translateAlternateColorCodes('&', config.getString("Kicked-To-World-Message")));
                    IslandFlyCommand.pl.teleport(worldspawn);
                }
               
                return;
            }
        }
    
    }
    
    
    Help..?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 6, 2017
Thread Status:
Not open for further replies.

Share This Page