Solved Problems with bar API

Discussion in 'Plugin Development' started by DominicGamesHD, Jun 5, 2016.

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

    DominicGamesHD

    Hello, I am making a plugin that gives perks to admins to allow them to give speed to all players in one command etc. So I followed this guide and it didn't work(for the bar API) Click Here.

    My code for it:
    SpeedAll Class:

    Code:
    package me.gothdom.cfe.commands;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.craftbukkit.v1_8_R3.CraftWorld;
    import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    import net.minecraft.server.v1_8_R3.EntityEnderDragon;
    import net.minecraft.server.v1_8_R3.Packet;
    import net.minecraft.server.v1_8_R3.PacketPlayOutSpawnEntityLiving;
    import net.minecraft.server.v1_8_R3.WorldServer;
    
    public class SpeedAll implements CommandExecutor, Listener {
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            for (Player players : Bukkit.getOnlinePlayers())
                if (cmd.getName().equalsIgnoreCase("speedall")) {
                    Location loc = players.getLocation();
                    WorldServer world = ((CraftWorld) players.getLocation().getWorld()).getHandle();
                    EntityEnderDragon dragon = new EntityEnderDragon(world);
                    dragon.setLocation(loc.getX() - 30, loc.getY() - 100, loc.getZ(), 0, 0);
                    dragon.setCustomName(ChatColor.GOLD + "Test");
                    dragon.setInvisible(true);
                    Packet<?> packet = new PacketPlayOutSpawnEntityLiving(dragon);
                    ((CraftPlayer) players).getHandle().playerConnection.sendPacket(packet);
                    sender.sendMessage(ChatColor.RED + "[" + ChatColor.YELLOW + "CFE" + ChatColor.RED + "]" + " "
                            + ChatColor.AQUA + "Given all players Speed!");
                    players.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 10000, 1));
                    players.sendMessage(ChatColor.RED + "[" + ChatColor.YELLOW + "CFE" + ChatColor.RED + "]" + " "
                            + ChatColor.GREEN + sender.getName() + " " + ChatColor.AQUA + "Has given you speed!");
                }
    
            return true;
        }
    }
    Main Class:
    Code:
    package me.gothdom.cfe.main;
    
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import me.gothdom.cfe.commands.HealAll;
    import me.gothdom.cfe.commands.SpeedAll;
    import me.gothdom.cfe.info.Help;
    
    public class CustomFactionsExtra extends JavaPlugin implements Listener {
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);
            getCommand("speedall").setExecutor(new SpeedAll());
            getCommand("cfe").setExecutor(new Help());
            getCommand("healall").setExecutor(new HealAll());
        }
    }
    
    Please HELP!!!!!!!!
     
  2. Offline

    Xerox262

    @DominicGamesHD In order for us to know the problem we would need to see the stacktrace generated when you try the above code.
     
  3. Offline

    DominicGamesHD

    Stacktrace? xD (New to bukkit and java again but just tell me where I can find that) @Xerox262
     
  4. Offline

    Xerox262

  5. Offline

    DominicGamesHD

    Thought so xD @Xerox262

    @Xerox262 There is no error but it just doesn't work?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  6. @DominicGamesHD
    I think i know where is the problem
    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
                if (label.equalsIgnoreCase("speedall")) {
                   for(Player player : Bukkit.getOnlinePlayers()) {
                    Location loc = player.getLocation();
                    WorldServer world = ((CraftWorld) player.getLocation().getWorld()).getHandle();
                    EntityEnderDragon dragon = new EntityEnderDragon(world);
                    dragon.setLocation(loc.getX() - 30, loc.getY() - 100, loc.getZ(), 0, 0);
                    dragon.setCustomName(ChatColor.GOLD + "Test");
                    dragon.setInvisible(true);
                    Packet<?> packet = new PacketPlayOutSpawnEntityLiving(dragon);
                    ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);
                    player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 10000, 1));
                    player.sendMessage(ChatColor.RED + "[" + ChatColor.YELLOW + "CFE" + ChatColor.RED + "]" + " "
                            + ChatColor.GREEN + sender.getName() + " " + ChatColor.AQUA + "Has given you speed!");
                  }
                                  sender.sendMessage(ChatColor.RED + "[" + ChatColor.YELLOW + "CFE" + ChatColor.RED + "]" + " "
                            + ChatColor.AQUA + "Given all players Speed!");
                }
            return true;
        }
    Try putting this onCommand.
     
    Last edited: Jun 5, 2016
  7. Offline

    DominicGamesHD

  8. Offline

    Xerox262

    @Minecraft1o1 Using CommandExceutors means you don't have to check the command, it'll never reach the executor if it's not the correct command.

    P.S. You shouldn't give away code, it could be wrong or filled with mistakes.
    P.S.S If you're going to give away code anyway you should at least say what you changed so they know what fixed their problem

    @DominicGamesHD Are you sure the method is executing?
     
  9. @Xerox262
    What are you talking about?
    The problem was that the EnderDragon would be spawned and seen only by one player becuase the for method was on the wrong place.
    I think the code is correct, and if it isn't @DominicGamesHD will Reply.
     
  10. Offline

    Xerox262

    @Minecraft1o1 The only thing you changed was you moved the if up a line, that wouldn't change anything, if it's true it'll execute anyway, cmd.getName() won't return true for one person and false for others. but yes, I didn't realise his original post had an if checking the command name.

    @DominicGamesHD You don't need the if checking the command name.
     
  11. Offline

    DominicGamesHD

    Still not working :(
     
  12. Offline

    Xerox262

    @DominicGamesHD Are you sure it's not working? Is the text displaying? If it is then it's working and the odds are your problem is that the packet is only sending for a tick instead of how long it's supposed to stay.
     
  13. Offline

    DominicGamesHD

    Its fine now I have just gone with doing titles :p
     
  14. Offline

    Zombie_Striker

    If your problem has been solved, mark this thread as solved.
     
Thread Status:
Not open for further replies.

Share This Page