two questions - need help

Discussion in 'Plugin Development' started by RebzO1, Mar 10, 2014.

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

    RebzO1

    First question
    I have this but i get an unhandled exception when i type it and im not sure why.
    What did i do wrong?

    Code:java
    1. // start reload
    2. if (CommandLabel.equalsIgnoreCase("ccreload")){
    3. Plugin plugin = p.getServer().getPluginManager().getPlugin("cctools");
    4. p.getServer().getPluginManager().disablePlugin(plugin);
    5. p.getServer().getPluginManager().enablePlugin(plugin);
    6. sender.sendMessage(ChatColor.AQUA + "CC-Tools Has been reloaded!");
    7. return true;
    8. }
    9. // end reload


    Second question
    I have this & i want to check if there are golden apples in a players inventory and still have them drop i have checked tutorials read through forums for a fix but can't get it working.
    How would i achieve this?

    Code:java
    1. @EventHandler
    2. public void onPlayerDeath(PlayerDeathEvent e) {
    3. Player p = e.getEntity();
    4. e.getDrops().clear();
    5. ItemStack skull = new ItemStack(Material.SKULL_ITEM);
    6. skull.setDurability((short) 3);
    7. SkullMeta meta = (SkullMeta) skull.getItemMeta();
    8. meta.setDisplayName(ChatColor.AQUA + p.getName());
    9. meta.setOwner(p.getName());
    10. skull.setItemMeta(meta);
    11. p.getWorld().dropItem(p.getLocation(), skull);
    12. Bukkit.broadcastMessage(ChatColor.GOLD + "TAKEDOWN!!");
    13. if (e.getEntity() instanceof Player){
    14. for (Player online : getServer().getOnlinePlayers()) {
    15. online.playSound(online.getLocation(), Sound.WOLF_HOWL, 1, 2);
    16. }
    17. }
    18. }


    Any help with either or both of these issues would be great.

    Regards
    RebzO1

    The error i get for the reload is as follows.
    Show Spoiler

    [09:48:45 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'ccr
    load' in plugin CC-Tools v1.1.8
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[cr
    ftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:1
    5) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServ
    r.java:683) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.PlayerConnection.handleCommand(PlayerCo
    nection.java:952) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.jav
    :814) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.PacketPlayInChat.a(PacketPlayInChat.jav
    :28) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.PacketPlayInChat.handle(PacketPlayInCha
    .java:47) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:14
    ) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [cra
    tbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:
    55) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:
    50) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:
    45) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.jav
    :457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:
    17) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    Caused by: java.lang.NullPointerException
    at org.bukkit.plugin.SimplePluginManager.disablePlugin(SimplePluginMana
    er.java:406) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at me.rebz.cctools.Main.onCommand(Main.java:46) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[cr
    ftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    ... 13 more


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  2. Offline

    StealerSlain

    46 line of Main.java?
     
  3. Offline

    RebzO1

    StealerSlain
    not sure what you mean?

    Code:java
    1. /**
    2. * CardinalCraft Tools By RebzO1
    3. */
    4. package me.rebz.cctools;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.GameMode;
    9. import org.bukkit.Material;
    10. import org.bukkit.command.Command;
    11. import org.bukkit.command.CommandSender;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.event.HandlerList;
    14. import org.bukkit.event.Listener;
    15. import org.bukkit.inventory.Inventory;
    16. import org.bukkit.inventory.ItemStack;
    17. import org.bukkit.plugin.java.JavaPlugin;
    18. import org.bukkit.potion.PotionEffect;
    19. import org.bukkit.potion.PotionEffectType;
    20.  
    21. import java.util.logging.Logger;
    22.  
    23.  
    24. public class Main extends JavaPlugin implements Listener{
    25. public static Bukkit plugin;
    26. public final Logger logger = Logger.getLogger("Minecraft");
    27.  
    28.  
    29. public void onEnable() {
    30. getConfig().options().copyDefaults(true);
    31. saveConfig();
    32. logger.info("CC-Tools Enabled!");
    33. }
    34. public void onDisable() {
    35. HandlerList.unregisterAll();
    36. logger.info("CC-Tools Disabled!");
    37. }
    38.  
    39. public boolean onCommand(CommandSender sender, Command cmd, String CommandLabel, String[] args){
    40. Player p = (Player) sender;
    41.  
    42. // start reload
    43. if (CommandLabel.equalsIgnoreCase("ccreload")){
    44. Plugin plugin = p.getServer().getPluginManager().getPlugin("cctools");
    45. p.getServer().getPluginManager().disablePlugin(plugin);
    46. p.getServer().getPluginManager().enablePlugin(plugin);
    47. sender.sendMessage(ChatColor.AQUA + "CC-Tools Has been reloaded!");
    48. return true;
    49. }
    50. // end reload


    i just don't know what the issue with it is i see it in the error log

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  4. Offline

    Aperx

    Pretty sure once you disable your plugin, it can't be re-enabled by your plugin again, since you've just turned it off...
    But why do you need to reload the actual plugin anyway?
    You can just reload the config singularly, or you can have the server execute the actual "reload" command and reload the whole server and thus in the process, reloading your plugin.
     
  5. Offline

    RebzO1

    Aperx
    it has a config file and i would like to be able to reload after changing config file
    and since i have never needed it before im unsure how to do it, i have tried looking around but can't find what i need
     
  6. Offline

    Aperx

    If you just want to reload the config
    Config.reload()
     
  7. Offline

    RebzO1

    Aperx
    where would i put that?
     
  8. RebzO1
    PHP:
    if (CommandLabel.equalsIgnoreCase("ccreload")){
        
    reloadConfig();
        
    sender.sendMessage(ChatColor.AQUA "CC-Tools Has been reloaded!");
        return 
    true;
    }
    I would also recommend looking into changing CommandLabel to cmd.getName() - just in case you want to use aliases at some point.
     
    RebzO1 likes this.
  9. Offline

    RebzO1

  10. RebzO1 Another point I would make, which I missed until just now, is that you should probably be checking whether the sender has permission to use the reload command, otherwise anybody will be able to use it, which probably isn't what you're going for.

    As for your second question, which I must also have missed, when do you want to check for the golden apples and what exactly do you want to happen if they do or don't have them? Or is it just that all you want to drop are the golden apples and the player's head (which I see you're adding in).
     
    RebzO1 likes this.
  11. Offline

    RebzO1

    AdamQpzm
    i still get the same error using that.
    could my issue be somewhere else?

    Code:java
    1. /**
    2. * CardinalCraft Tools By RebzO1
    3. */
    4. package me.rebz.cctools;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.GameMode;
    9. import org.bukkit.Material;
    10. import org.bukkit.command.Command;
    11. import org.bukkit.command.CommandSender;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.event.HandlerList;
    14. import org.bukkit.event.Listener;
    15. import org.bukkit.inventory.Inventory;
    16. import org.bukkit.inventory.ItemStack;
    17. import org.bukkit.plugin.java.JavaPlugin;
    18. import org.bukkit.potion.PotionEffect;
    19. import org.bukkit.potion.PotionEffectType;
    20.  
    21. import java.util.logging.Logger;
    22.  
    23.  
    24. public class Main extends JavaPlugin implements Listener{
    25. public static Bukkit plugin;
    26. public final Logger logger = Logger.getLogger("Minecraft");
    27.  
    28. public void onEnable() {
    29. getConfig().options().copyDefaults(true);
    30. saveConfig();
    31. logger.info("CC-Tools Enabled!");
    32. }
    33. public void onDisable() {
    34. HandlerList.unregisterAll();
    35. logger.info("CC-Tools Disabled!");
    36. }
    37.  
    38. public boolean onCommand(CommandSender sender, Command cmd, String CommandLabel, String[] args){
    39. Player p = (Player) sender;
    40.  
    41. // start reload
    42. if (CommandLabel.equalsIgnoreCase("ccreload")){
    43. .getConfig().reloadConfig();
    44. sender.sendMessage(ChatColor.AQUA + "CC-Tools Has been reloaded!");
    45. return true;
    46. }
    47. // end reload


    error =
    Show Spoiler
    [15:50:37 WARN]: Unexpected exception while parsing console command "ccreload"
    org.bukkit.command.CommandException: Unhandled exception executing command 'ccre
    load' in plugin CC-Tools v1.1.8
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[cra
    ftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:17
    5) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServe
    r.java:683) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchServerCommand(Craf
    tServer.java:670) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.DedicatedServer.aw(DedicatedServer.java:
    286) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:2
    51) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:5
    45) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java
    :457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:6
    17) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_7_R1.command.
    ColouredConsoleSender cannot be cast to org.bukkit.entity.Player
    at me.rebz.cctools.Main.onCommand(Main.java:39) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[cra
    ftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    ... 8 more


    AdamQpzm

    ok in order lol
    1. i only get the error if i type ccreload in config so ty now fixed that and everything works
    2. the other question: yes i want players to drop their head and any golden apples they have n clear the rest

    == edit ==
    the reload config just went in my writtenbooks plugin too OMG thats going to save so much time and stop server restarts TY
    =======

    You have been a great help so far so i thank you!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  12. Offline

    Aqua

    In the command you cast sender to Player, without checking if the sender is an actual player, thus giving you the error:
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_7_R1.command.
    ColouredConsoleSender cannot be cast to org.bukkit.entity.Player
    Add this to the top of onCommand:
    if (!(sender instanceof Player)) {
    sender.sendMessage(ChatColor.RED + "This command can only be executed as player!");
    return false;
    }

    OR:
    remove the Player p = (Player) sender;
     
  13. RebzO1

    1. As above - I probably should've noticed your Player p = (Player) sender; right away :p

    2. You could always make it so that it will only remove the items that aren't golden apples. Instead of event.getDrops().clear():

    PHP:
    for(int i event.getDrops().size()-1> -1i--) {
        if(
    event.getDrops().get(i).getType() != Material.GOLDEN_APPLE) {
            
    event.getDrops().remove(i);
        }
    }
     
  14. Offline

    RebzO1

    AdamQpzm
    replaced mine and i get this when i try to validate
    The type of the expression must be an array type but it resolved to List<ItemStack>

    and it is this line that is flagged -- e.getDrops()[i ]
     
  15. RebzO1 Yeah sorry, that was my bad. I, for some reason, used the way you get values from arrays - see the post I had already edited. The line should be:
    PHP:
    if(e.getDrops().get(i).getType() != Material.GOLDEN_APPLE) {
     
    RebzO1 likes this.
  16. Offline

    RebzO1

    AdamQpzm likes this.
Thread Status:
Not open for further replies.

Share This Page