Plugin Error

Discussion in 'Plugin Development' started by jakemaster2003, Jan 7, 2016.

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

    jakemaster2003

    I made a plugin but if i run this on my test server wich runs Java 8 it works but on my host server that runs java 7 it dont work and i have added both libary's here is my code it is dutch in the lores and the names p.s it works on Java 7 but the command and the event not.

    My Core.java

    Code:
    package me.XUniversePVP.McFantasy;
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Core extends JavaPlugin{
      
      
        @Override
        public void onEnable(){
            getLogger().info(">> McFantasy v" + getDescription().getVersion() + " is Enabled <<");
            Listeners();
            getCommand("tdtool").setExecutor(new Commands());
        }
      
        @Override
        public void onDisable(){
            getLogger().info(">> McFantasy is Disabled <<");
        }
      
        private void Listeners(){
            getServer().getPluginManager().registerEvents(new Gamemode(this), this);
            getServer().getPluginManager().registerEvents(new AbiltyUse(this), this);
        }
    
    }
    
    My Gamemode.java

    Code:
    package me.XUniversePVP.McFantasy;
    
    import org.bukkit.GameMode;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    
    public class Gamemode implements Listener{
      
      
        public Gamemode(Core Plugin) {
        }
          
          
            @EventHandler
            public void onPlayerJoin(PlayerJoinEvent event){
                event.getPlayer().setGameMode(GameMode.ADVENTURE);
        }
    
    }
    
    Commands.java

    Code:
    package me.XUniversePVP.McFantasy;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class Commands implements CommandExecutor{
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(sender instanceof Player);
            Player p = (Player) sender;
            if (cmd.getName().equalsIgnoreCase("tdtool")){
                if (p.hasPermission("mcfantasy.tdtool")){
                    ItemStack i = new ItemStack(Material.DIAMOND_SWORD);
                    ItemMeta meta = i.getItemMeta();
                    List<String> lore = new ArrayList<String>();
                    lore.add(ChatColor.GRAY + "Dit is het " + ChatColor.DARK_AQUA + "Technische" + ChatColor.DARK_GRAY + "-" + ChatColor.DARK_AQUA + "Dienst" + ChatColor.DARK_BLUE + " Zwaard");
                    meta.setLore(lore);
                    meta.setDisplayName(ChatColor.DARK_AQUA + "Technische" + ChatColor.DARK_GRAY + "-" + ChatColor.DARK_AQUA + "Dienst" + ChatColor.DARK_BLUE + " Zwaard");
                    i.setItemMeta(meta);
                    p.getInventory().addItem(i);
                    return true;
                    }
            }
            return false;
        }
    
    }
    My AbiltyUse.java

    Code:
    package me.XUniversePVP.McFantasy;
    
    import java.util.Set;
    
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    
    public class AbiltyUse implements Listener{
      
      
        public AbiltyUse(Core Plugin){
          
        }
      
        @EventHandler
        public void onUse(PlayerInteractEvent event){
            Player p = event.getPlayer();
            if(event.getItem().getItemMeta().getDisplayName().toLowerCase().contains("technische-dienst zwaard")){
                p.getWorld().strikeLightning(p.getTargetBlock((Set<Material>)null, 25).getLocation());
            }
        }
    }
    It works on Java 8 but the events and commands not on Java 7 what is the problem


    Update The Event AbiltyUse dont work completely
     
  2. Offline

    Gorbit99

    Keep your package names lower-cased
    Try containsIgnoreCase instead of toLowerCase().contains(). it wont help your problem, but it'll make the code readable
    Put your plugins name inside the permissionname to avoid compatibility problems
    You don't need to log your plugin, bukkit does that for your
    You made a 1-line-if on the 20th line of Commands.java:
    You check if sender is a player, and if it is, you cast it to p, the only problem is, that you continue the method, even if it isn't, so if you get to line 30, and for example the command executor was the console, you'll get a NullException

    Finally, I would suggest updating to java 8, so the compatibility reason disappears
    (BTW: I believe, your problem is, that you compile the jar with java 8, and java 8 is barely compatible with java 7)
     
  3. Offline

    JoaoBM

    @jakemaster2003 Dont you need to check if theres an item, if the item has ItemMeta and if it has a Display name?
     
  4. Offline

    Gorbit99

    @JoaoBM an item HAS itemmeta and it has A DISPLAY NAME too, if there's an item there, but you're right, a null check is useful

    You don't need to do Set<Material> null, imagine null as a "unisex" thing. If something is not a primitive (byte, short, int, long, float, double, string) then it can be null, so targetBlock can work with targetBlock(null, 25) too
     
    JoaoBM likes this.
  5. Offline

    Xerox262

    No, not all items have item meta, nor does every item have a display name, getDisplayName() will return null if a custom name was not specifically set.
     
  6. Offline

    mine-care

    @Gorbit99 having read what @Xerox262 said, @JoaoBM is right.
    That is also why if you get an item -say diamond pickaxe- without a custom name and you invoke getDisplayName() it will not return "Diamond Pickaxe" but null...
     
    JoaoBM likes this.
  7. Offline

    mcdorli

    Compile with java 8, or update to it. Java 8 files can't run on java 7 machines.
     
  8. Offline

    jakemaster2003

    This is a good explenation and the plugin name is McFantasy ad and i add jre7 libary i had the libary of 8 i have them both now but if i remove 7 the plugin is not gonna display on my console for the rest my problem is fixed the event of lightning works now
     
  9. Offline

    JoaoBM

    @jakemaster2003 Don't forget to mark the thread as solved if you found a solution to the problem :)
     
  10. Offline

    jakemaster2003

    wel its not completely solved i have stil 2 errors still it wont work for java 7 and i have fixed lightningstrike but if its not come true with what i have asked to do the plugin i get a Error here the log and the code


    Log:

    http://pastebin.com/A6N2CVhS



    Code:
    Code:
    package me.XUniversePVP.McFantasy;
    
    import java.util.Set;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    
    public class AbiltyUse implements Listener{
      
      
        public AbiltyUse(Core Plugin){
          
        }
      
        @EventHandler
        public void onUse(PlayerInteractEvent event){
            Player p = event.getPlayer();
            if(event.getItem().equals(Material.DIAMOND_SWORD))
            if(event.getAction()!= Action.LEFT_CLICK_AIR)
            if(p.getInventory().getItemInHand().getItemMeta().getDisplayName().contains(ChatColor.DARK_AQUA + "Technische" + ChatColor.DARK_GRAY + "-" + ChatColor.DARK_AQUA + "Dienst" + ChatColor.DARK_BLUE + " Zwaard")){
              
            }
              
                if (event.getPlayer().hasPermission("mcfantasy.tdtool")){
                  
                }
                p.getWorld().strikeLightning(p.getTargetBlock((Set<Material>)null, 100).getLocation());
        }
    }
    

    Update My server has updated to Java 8 that problem is fixed i was so stupid to not click on save before i restart XD but the lightning strike dont work got any ideas
     
    Last edited: Jan 8, 2016
  11. Offline

    Zombie_Striker

    Caused by: java.lang.NullPointerException
    at me.XUniversePVP.McFantasy.AbiltyUse.onUse(AbiltyUse.java:24) ~[?:?]

    24. if(event.getAction()!= Action.LEFT_CLICK_AIR)

    Something on this line is null. I really can't see what it could be ,so try the following:
    • Check if the event is null
    • Check if the action is null
    ['Edit]
    Because you did not encapsulate properly, all of these if statements do nothing. You have the first two if statements only checking the lines after them (if you want if statements to control multiple lines, you need brackets), and then the final if statement has nothing inside the encapsulation.

    Then you also have this line that does nothing whatsoever as well.

    I believe your problem is that you do not know how to encapsulate properly. Please refer to the followinmg link:

    http://docs.oracle.com/javase/tutorial/java/nutsandbolts/expressions.html

    //And this if you are not currently using a tutorial:
    http://docs.oracle.com/javase/tutorial/
     
Thread Status:
Not open for further replies.

Share This Page