Need help, first plugin =D

Discussion in 'Plugin Help/Development/Requests' started by isaiahDAruler, Jan 22, 2015.

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

    isaiahDAruler

    Hello im Isaiah, to start i want anyone reading this to know that i am not expirianced in Java i actually have recently started (learning bit by bit off of tutorials and such).
    Currently i have made my test plugin and i want it to 1 display info about itself (have already figured this out without much difficulty)
    And 2 for it to display death messages saying: who died and by who
    I do not however want it to do anything special if not by another player instead just to say: Died by worldly causes...
    As you will see below i have put the command thing for info and under that in its own boolean i have the code for deathmessages that appears to not read when plugin is in server (spigot 1.8 JSYK).

    Code:
    package me.main;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    public class Main extends JavaPlugin {
    
        @Override
        public void onEnable() {
        }
       
        @Override
        public void onDisable() {
        }
    
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (cmd.getName().equalsIgnoreCase("Isaiahsplugin") && sender instanceof Player) {
            Player player = (Player) sender;
            player.sendMessage("IsaiahsPlugin Version 1.0 is running do /help IsaiahsPlugin for commands, " + player.getName() + "!");
            return true;
    }
        return false;
    }
    private boolean onCommand(final PlayerDeathEvent event) {
        event.getEntity();
        event.getEntity().getKiller();
        if (event.getEntity().getKiller() != null) {
            Bukkit.getServer().broadcastMessage(event.getEntity() + "Died by worldly causes...");
        }
        if (event.getEntity().getKiller() != instanceof Player){
            Bukkit.getServer().broadcastMessage(event.getEntity() + "has been killed by" + event.getEntity().getKiller());
    }
        return false;
    }   
    }
    
    Again new here so i am looking for any suggestions onn how to clean this up and get that second part starting at line 28 to work (to reiterate it doesnt appear to acknowledge that its there in the server.
     
  2. Offline

    Cheesepro

    @isaiahDAruler

    • Change line 10 to
      Code:
      public class Main extends JavaPlugin implements Listener{
      since you have a player death listener at the bottom.

    • Add this line in to your onEnable()
      Code:
      Bukkit.getServer().getPluginManager().registerEvents(this, this);

    • Add @EventHandler right before line 28

    • Don't use onCommand on line 28... since it is a listener not a command xD. and don't use private boolean xD. Use:
      Code:
      public void onPlayerDeath(final PlayerDeathevent event)

    I strongly suggest you to look over more tutorials on Java and Bukkit plugins... :D
     
    isaiahDAruler likes this.
  3. @isaiahDAruler
    1. You are returning false when your command is being executed. Change "false" to "true".
    2. A PlayerDeathEvent is a void, not a boolean. Change "boolean" to "void".
    3. In your PlayerDeathEvent, the part before the parenthesis (onCommand) is the name of the constructor. You should change "onCommand" to "onPlayerDeath".
    4. Why is your event final? Remove that.
    5. You need to implement Listener into your class. At the top, where it says "extends JavaPlugin", to the right of that put, "implements Listener" then import Listener into your class.
    6. Above your PlayerDeathEvent put "@EventHandler" and import that into your class.
    7. You aren't registering your events. To register your events, do this under your onEnable method:
    Code:
    PluginManager plManager = Bukkit.getServer().getPluginManager();
    plManager.registerEvents(this, this);
    Hope I helped you fix your problems. Make sure to tag me with @CodePlaysMinecraft if you have any questions, or want to reply to me. :)
     
    isaiahDAruler likes this.
  4. Offline

    isaiahDAruler

    Okay thank you both for the replies im gonna try changing that stuff and get right back to ya :D

    EDIT:
    Okay i did the changes and it ran the section (thank you) however i get the following:

    Code:
    [21:57:24 ERROR]: Could not pass event PlayerDeathEvent to IsaiahsPlugin v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:305) ~[spigot.jar:git-Spigot-b1e6da1-1092acb]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[spigot.jar:git-Spigot-b1e6da1-1092acb]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:502) [spigot.jar:git-Spigot-b1e6da1-1092acb]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:487) [spigot.jar:git-Spigot-b1e6da1-1092acb]
            at org.bukkit.craftbukkit.v1_8_R1.event.CraftEventFactory.callPlayerDeat
    hEvent(CraftEventFactory.java:381) [spigot.jar:git-Spigot-b1e6da1-1092acb]
            at net.minecraft.server.v1_8_R1.EntityPlayer.die(EntityPlayer.java:407)
    [spigot.jar:git-Spigot-b1e6da1-1092acb]
            at net.minecraft.server.v1_8_R1.EntityLiving.damageEntity(EntityLiving.j
    ava:778) [spigot.jar:git-Spigot-b1e6da1-1092acb]
            at net.minecraft.server.v1_8_R1.EntityHuman.damageEntity(EntityHuman.jav
    a:801) [spigot.jar:git-Spigot-b1e6da1-1092acb]
            at net.minecraft.server.v1_8_R1.EntityPlayer.damageEntity(EntityPlayer.j
    ava:486) [spigot.jar:git-Spigot-b1e6da1-1092acb]
            at net.minecraft.server.v1_8_R1.EntityArrow.s_(EntityArrow.java:244) [sp
    igot.jar:git-Spigot-b1e6da1-1092acb]
            at net.minecraft.server.v1_8_R1.World.entityJoinedWorld(World.java:1590)
    [spigot.jar:git-Spigot-b1e6da1-1092acb]
            at net.minecraft.server.v1_8_R1.World.g(World.java:1565) [spigot.jar:git
    -Spigot-b1e6da1-1092acb]
            at net.minecraft.server.v1_8_R1.World.tickEntities(World.java:1412) [spi
    got.jar:git-Spigot-b1e6da1-1092acb]
            at net.minecraft.server.v1_8_R1.WorldServer.tickEntities(WorldServer.jav
    a:576) [spigot.jar:git-Spigot-b1e6da1-1092acb]
            at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:7
    57) [spigot.jar:git-Spigot-b1e6da1-1092acb]
            at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:3
    16) [spigot.jar:git-Spigot-b1e6da1-1092acb]
            at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:6
    23) [spigot.jar:git-Spigot-b1e6da1-1092acb]
            at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java
    :526) [spigot.jar:git-Spigot-b1e6da1-1092acb]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_25]
    Caused by: java.lang.Error: Unresolved compilation problems:
            Syntax error on token "instanceof", delete this token
            Player cannot be resolved to a variable
    
            at me.main.Main.onPlayerDeath(Main.java:38) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0
    _25]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0
    _25]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .8.0_25]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_25]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:301) ~[spigot.jar:git-Spigot-b1e6da1-1092acb]
            ... 18 more
     
    Last edited: Jan 22, 2015
  5. Offline

    Infuzion

    You may want to read about the Bukkit Event API and the Plugin Tutorial.

    That said, you should put a @EventHandler annotation in front of your event handler method.
    You need to add
    Code:
    implements Listener
    to your class and import Listener (from Bukkit) into your class. The event handline function should be public, not private. You shouldn't name it
    as that becomes confusing when trying to differentiate between the onCommand method for executing commands and this one.
    The event handling method shouldn't return a boolean. The PlayerDeathEvent doesn't have to be final.

    What is the point of:
    and:
    ?

    You should also read about conditional operators.
    If #getKiller is NOT equal to null, wouldn't that mean a player killed them?

    Also be sure to register your events handlers.
    Code:
    PluginManager plManager = Bukkit.getServer().getPluginManager();
    plManager.registerEvents(this, this);
    Add that into your onEnable method.

    You can remove your onDisable method since nothing is inside it.
     
    isaiahDAruler likes this.
  6. Offline

    isaiahDAruler

    Sorry i didnt awnser this, from what i read the event.getentity() as i understood it was to get the name of the person who died. while the event.getentity().getkiller() would get me the killer of the dead. If you have a way to rewrite that id be glad to use it because what i have doesnt work :c


    Btw guys thanks for the links i have them bookmarked and will be looking over those sources :D
     
  7. Offline

    sirrus86

    Those methods will return the player object for the player who died and the player (if any) who killed them. The reason for @Infuzion 's concern is you aren't storing or using them, so they aren't accomplishing anything by being there.
     
    isaiahDAruler likes this.
  8. Offline

    isaiahDAruler

    Oh okay, yeah that makes sense now how might I go about doing that if you don't mind expanding on that? :p
     
  9. Offline

    sirrus86

    You can store player objects by assigning them to fields:
    Code:
    Player player = event.getEntity();
    Player killer = player.getKiller();
    This is basic Java though, you may wanna check out some tutorials and get a good foundation before tackling plugins. We all start somewhere.
     
  10. Offline

    isaiahDAruler

    @sirrus86
    ohhhh yeah I didn't realize I could use that there, done that before you don't even know how dum I feel atm okay thanks I'll edit and post back results when I get a chance till then thanks ya'll
     
  11. Offline

    mrCookieSlime

    Moved to Alternatives Section.
     
  12. Offline

    isaiahDAruler

    Hello again all! Just one more thing i must ask (about this plugin/section)
    I have it working great and i've actually been adding other stuff but about the part i posted yesterday, once i got it working we get this when (Top) i die by anything besides other player and (Bottom) player dies to player.
    My question is : How might i go about removing the Craftplayer{name=...} part? My guess would be to have it do what i wanted yesterday then before i have it broadcast i take the Player and Killer "objects" and run a second task to get their true name then broadcast those?

    [15:27:38] [Client thread/INFO]: [CHAT] CraftPlayer{name=isaiahDAruler}Died by worldly causes...
    [15:30:43] [Client thread/INFO]: [CHAT] CraftPlayer{name=kirby171212}has been killed byCraftPlayer{name=isaiahDAruler}

    EDIT:
    Nevermind all i should have tried that before posting i got it, thanks for the help with this part! :D
     
    Last edited: Jan 23, 2015
Thread Status:
Not open for further replies.

Share This Page