Totally confused on my plugin "STTLCraft"

Discussion in 'Plugin Development' started by STTL, Nov 5, 2013.

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

    STTL

    I'm working on a plugin for 1.6.4 called STTLCraft (ATM, i'm still trying to manage events and stuff :p)
    and I'm really stuck. I followed the wiki's tutorial but don't really understand a few things, like if you put methods in a seperate class etc.
    I'm getting lots of errors and, being extremely new to bukkit, have no idea how to manage them. Can you please tell me what these mean?

    Line 4-STTLCraft.class (main): "The import org cannot be resolved"
    Context:
    Code:
    import org.bukkit.plugin.java.JavaPlugin;
    Line 16-STTLCraft.class (main):"The method getLogger() is undefined for the type STTLCraft"
    Context:
    Code:
    public void onDisable(){
                getLogger().info("STTLCraft's onDisable function has been activated!")
        }
    There is a lot of errors within my onEnable() method :(

    Code:
        public void onEnable(){
                getLogger().info("STTLCraft's onEnable Function has been activated!");
                for (Player player : this.getServer().getOnlinePlayers()) {
                playerList.put(player, playerData(player));
                getCommand("sttl").setExecutor(new STTLCommandExecutor (this));
        }
        
    P.S I attached an archive of my work in my post because it gave me errors while trying to export to a jar file :( Here is a link to it.
    Also, please do not compile this and put it on your server, it will crash.
    If you are also having trouble with your first plugin, please post in this thread :)
     

    Attached Files:

  2. Offline

    _Filip

    1. Why would you import something if you're already inheriting everything from it with extends?

    2. The for loop at line 10 doesn't end.

    3. The class command executor is completely unrequired, since you have the onCommand method in the main class.
     
  3. Offline

    maxben34

    STTL

    It is obvious that you have no idea what you are doing.
    You don't know java. You barely know bukkit.

    This is a problem when it comes to programming using the bukkit api. If you want to be successful at what you're doing, you need to know Java.

    I'll just say that you don't need an onDisable() method in your code.
    You don't need that command executer in your onEnable() as long as you called your commands correctly in your code.
    You never even ended the method for your onEnable... you only ended the forloop (this shows me how little you know about java).

    The only way your import wasn't resolved is either that you didn't extend JavaPlugin... or you don't even have the craftbukkit jar in your build path.

    I don't mean to sound like a douche by telling you these things... i'm trying to help you by showing you that you're going about making plugins the wrong way.

    What I suggest you to do is learn java from www.thenewboston.org by watching his basic java coding tutorials.

    Then learn the bukkit api from www.youtube.com/pogostick29dev.

    Code:java
    1. @EventHandler
    2.  
    3. public void onCodingLearn(JavaLearnEvent e){
    4.  
    5. if(e.getPlayer().knowsJava()){
    6.  
    7. e.getPlayer().sendMessage(ChatColor.GREEN + "It's not that hard to code plugins!");
    8.  
    9. }else{
    10.  
    11. e.getPlayer().sendMessage(ChatColor.RED + "You should learn Java First!");
    12.  
    13. }
    14.  
    15. }
    16.  
     
  4. Offline

    _Filip

    maxben34
    Wtf, don't be so rude, at least help the guy, telling him "It is obvious that you have no idea what you are doing.
    You don't know java. You barely know bukkit." is really helping him.

    You also made a thread on how to set multiple blocks at once...
     
  5. Offline

    maxben34

    I want you to look at my post. I want you to fully read it. I want you to see that I'm really helping him. At least i'm helping him more than somebody who is telling him that he doesn't have to import JavaPlugin (which you actually do need to import when you extend it...).
     
  6. Offline

    _Filip

    maxben34
    Wrong once again, extending org.bukkit.plugin.java.JavaPlugin works just as well. Don't try getting smart with me even though you aren't.
     
  7. Offline

    maxben34

    Im not here to flame, but you edited your post with the things that i told the guy to fix. I would imagine that he extended JavaPlugin and then imprted java plugin, which js what most people do. Thirdly, i dont think its a problem that i made a thread about setting multiple blocks at once.

    If you read it, i was trying to do a world edity sort of thing, without having to call each settype individually. Sorry that i cant ask for help as well as help others. Im sure that if i cared enough, i could go through your threads and acknowledge the fact that nobody is perfect when it comes to programming.
     
  8. Offline

    Cirno

    I suggest you start reading a bit of this:
    http://docs.oracle.com/javase/tutorial/

    You don't necessarily have to read all of it; I would recommend you just get familiar with the Java syntax. Most of the other stuff you can learn by googling (Hopefully, you won't copy and paste the code/ask to be spoonfed).

    For the multiple classes part,
    http://forums.bukkit.org/threads/tutorial-using-multiple-classes.179833

    rant (open)
    You should use "e.getPlayer().knowsJava()" instead of "e.getPlayer().knowsJava", unless you like public fields. Besides, the Player class doesn't have a knowsJava field or method to begin with, so you'll have to use an external method to tell if they know Java or not. Your formatting is a little weird too; there's a newline every for every non-empty line. For the whole knowsJava part, I suggest you use metadata, preferably a FixedMetadataValue, so that it sticks during reloads. Maybe a HashMap<String, Boolean> if you're not used to metadata. This is all assuming that JavaLearnEvent extends PlayerEvent, and is called by a plugin. Also assuming that it's possible for knowsJava/knowsJava() to be/return true or false; a boolean that is permanently true or false is pointless. I will say that I'm not the best Java person and know everything about Java, but please at least provide proper joke code.
     
    maxben34 likes this.
  9. Offline

    _Filip

    maxben34
    Actually, no, I edited it before you posted.
     
  10. Offline

    mattrick

    Why would you extend an import?

    This may be shorter:
    Code:
    public void PlginMainClass extends org.bukkit.plugin.java.JavaPlugin{
    }
    But This looks much neater and is what most if not all coders do:
    Code:
    import org.bukkit.plugin.java.JavaPlugin
    public void PluginMainClass extends JavaPlugin{
    }
    
     
    maxben34 likes this.
  11. Offline

    maxben34

    Is that why it says that i posted at 8:14 and you edited at 8:16?

    Funny that you lied and think once again you can outsmart me...

    I dont want to fight, we are both here to help the OP so lets stick on tht topic...
     
    mattrick16 likes this.
  12. Offline

    _Filip

    mattrick16
    That is what I do too, but I was just trying to prove a point to maxben.

    maxben34
    You know, you can edit your post without refreshing the page. I didn't even know that someone else posted when I edited it.

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

    maxben34

    No way of proving that. You know, you shouldnt be double posting.
     
  14. Offline

    mattrick

    Anyway to stay on topic, I recommend Java Coding for Dummies, thenewboston's tutorials or if you want to try your hand at programming without learning much, you an try GameMaker. I honestly got my start with GameMaker, then literally got straight into Bukkit coding. It wasn't too hard, because I had been developing in GameMaker for a good 2 or 3 years. Anyway good luck with learning how to code.
     
  15. Offline

    JPG2000

    swampshark19 FYI maxben34 wasn't being rude. He was being helpful, but very direct. Maby it came off rude, but the bottom line was to help him improve.
     
    maxben34 and xTrollxDudex like this.
  16. Offline

    STTL

    thank you everyone, I'll try to learn Java before trying my hand at any of this.
     
  17. Offline

    STTL

    I'm back with a new plugin (I wanted to start clean) i just called it MyPlugin because I didn't think I would go through with it. It's giving an error when I run the /information command. It says "An internal error occured while using this command" in the chat.
    In the server console- here is what it says...


    Code:
    18:16:05 [INFO] STTL issued server command: /information
     
    18:16:05 [SEVERE] null
     
    org.bukkit.command.CommandException: Unhandled exception executing command 'info
     
    rmation' in plugin MyPlugin v0.0.1
     
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
     
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:19
     
    2)
     
    at org.bukkit.craftbukkit.v1_6_R3.CraftServer.dispatchCommand(CraftServe
     
    r.java:528)
     
    at net.minecraft.server.v1_6_R3.PlayerConnection.handleCommand(PlayerCon
     
    nection.java:968)
     
    at net.minecraft.server.v1_6_R3.PlayerConnection.chat(PlayerConnection.j
     
    ava:886)
     
    at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java
     
    :837)
     
    at net.minecraft.server.v1_6_R3.Packet3Chat.handle(SourceFile:49)
     
    at net.minecraft.server.v1_6_R3.NetworkManager.b(NetworkManager.java:296
     
    )
     
    at net.minecraft.server.v1_6_R3.PlayerConnection.e(PlayerConnection.java
     
    :116)
     
    at net.minecraft.server.v1_6_R3.ServerConnection.b(SourceFile:37)
     
    at net.minecraft.server.v1_6_R3.DedicatedServerConnection.b(SourceFile:3
     
    0)
     
    at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:5
     
    92)
     
    at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:2
     
    27)
     
    at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:4
     
    88)
     
    at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java
     
    :421)
     
    at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:5
     
    83)
     
    Caused by: java.lang.Error: Unresolved compilation problems:
     
    player cannot be resolved
     
    player cannot be resolved
     
    player cannot be resolved
     
    at com.gmail.sttlmodder.MyPlugin.MyPlugin.onCommand(MyPlugin.java:15)
     
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
     
    ... 15 more

    Here is an archive of my plugin MyPlugin.zip
    MyPlugin.java (Main Class)


    Code:
    package com.gmail.sttlmodder.MyPlugin;
     
    import org.bukkit.command.Command;
     
    import org.bukkit.command.CommandSender;
     
    import org.bukkit.event.inventory.*;
     
    import org.bukkit.util.permissions.*;
     
    import org.bukkit.plugin.*;
     
    import org.bukkit.event.*;
     
    import org.bukkit.event.player.*;
     
    import org.bukkit.entity.*;
     
    import org.bukkit.plugin.java.JavaPlugin;
     
    public final class MyPlugin extends JavaPlugin {
     
    @Override
     
    public void onEnable(){
     
    getLogger().info("My Plugin's onEnable Function has been called!");
     
    }
     
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
     
    if((cmd.getName().equalsIgnoreCase("information")) && (player.hasPermission("sttl.myplugin.info"))) {
     
    player.sendMessage("Welcome to STTL's Server! Here are the rules...\n1. Be kind to your fellow players\n2. Do not abuse/spam commands\n3. Do not argue with the mighty STTL or he will use /kill on you! :p\nI hope you enjoy our server, have a nice day!");
     
    return true;
     
    }else{
     
    player.sendMessage("You don't have the permission- sttl.myplugin.info");
     
    return false;
     
    }}}
    plugin.yml


    Code:
    package com.gmail.sttlmodder.MyPlugin;
     
    import org.bukkit.command.Command;
     
    import org.bukkit.command.CommandSender;
     
    import org.bukkit.event.inventory.*;
     
    import org.bukkit.util.permissions.*;
     
    import org.bukkit.plugin.*;
     
    import org.bukkit.event.*;
     
    import org.bukkit.event.player.*;
     
    import org.bukkit.entity.*;
     
    import org.bukkit.plugin.java.JavaPlugin;
     
    public final class MyPlugin extends JavaPlugin {
     
    @Override
     
    public void onEnable(){
     
    getLogger().info("My Plugin's onEnable Function has been called!");
     
    }
     
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
     
    if((cmd.getName().equalsIgnoreCase("information")) && (player.hasPermission("sttl.myplugin.info"))) {
     
    player.sendMessage("Welcome to STTL's Server! Here are the rules...\n1. Be kind to your fellow players\n2. Do not abuse/spam commands\n3. Do not argue with the mighty STTL or he will use /kill on you! :p\nI hope you enjoy our server, have a nice day!");
     
    return true;
     
    }else{
     
    player.sendMessage("You don't have the permission- sttl.myplugin.info");
     
    return false;
     
    }}}
    Sorry if my mistakes are very obvious, I am very new to Java and don't want to give up on programming :)
     

    Attached Files:

  18. Offline

    xTrollxDudex

    STTL
    Please start on the basics, or get a proper IDE... You need to declare a player field
     
    STTL likes this.
  19. Offline

    STTL

    xTrollxDudex
    Do you declare player field with this?
    Code:
        private CommandSender player;
    (above "@Override")
     
  20. Offline

    xTrollxDudex

    STTL
    No. You cast player to sender.
    PHP:
    Player player = (Playersender;
    Just make sure to do this before hand:
    PHP:
    if(!sender instanceof Player) {
        
    sender.sendMessage("You are not a player!");
        return 
    true;
    }
     
  21. Offline

    afistofirony

    STTL You have the right idea about what a field is (a class-wide variable, basically), but you need to use a variable here, not a field (looking at you, xTrollxDudex! >:o).

    Basically, what he's saying is that you need to make the variable player before you can actually use it. In any case, this seems like a command that should be executable by anyone, so we don't need the player variable:
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (label.equalsIgnoreCase("information")) {
            if (sender.hasPermission("myplugin.info")) { // By convention, you should use <plugin>.<permission>, not <author>.<plugin>.<permission>
                sender.sendMessage("Welcome to STTL's Server! Here are the rules..." +
                                   "\n1. Be kind to your fellow players" +
                                   "\n2. Do not abuse/spam commands" +
                                   "\n3. Do not argue with the mighty STTL or he will use /kill on you! :P" +
                                   "\nI hope you enjoy our server, have a nice day!");
            } else {
                sender.sendMessage("You don't have the permission myplugin.info.");
            }
     
            return true;
        }
     
        return false;
    }
    If you do need to restrict access to players only, you can do so by casting:
    Code:
    if (sender instanceof Player) {
        Player player = (Player) sender;
        // do stuff
    }
    Also, I know this isn't really important right now, but you should get into the habit of writing nicely formatted code so it's easier for both you and others to pick up on. :p

    EDIT: Fixed my own code. :oops:
     
  22. Offline

    xTrollxDudex

    Fields are a generally used term
     
  23. Offline

    STTL

    afistofirony
    I used sttl.MyPlugin.info because it is very likely that you are going to have another plugin called "myplugin" installed. :)
    Thanks for the help everybody :D

    I ran into a problem when trying to add another command to my main class.
    The "sender" and the "label" (when saying ignore case) are both undefined, but in my first command (information) sender and label are both recognized. What is causing this?
    Am I supposed to have 2 onCommand methods with different names?
    Or do I need a seperate CommandExecutor class because I have more than 1 command?
     
  24. Offline

    STTL

    Hello everybody, I'm confused on how you can have multiple effects with a command.
    I know that this code doesn't work, I just was just wondering how get this effect :)
    Code:
    package com.gmail.sttlmodder.STTLLogin;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    //import org.bukkit.event.player.PlayerLoginEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public final class STTLLogin extends JavaPlugin {
        @Override
        public void onEnable() {
            getLogger().info("STTLLOGIN HAS BEEN ACTIVATED!");
        }
     
        public void onDisable() {
            getLogger().info("STTLLOGIN HAS BEEN DISABLED!");
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (label.equalsIgnoreCase("LoginMSG") && (sender.hasPermission("sttllogin.info") && (args.length < 1))) {
                    sender.sendMessage(ChatColor.GREEN + "This server is running STTLLoginMSG 0.1.1A" +
                                                        "\nThis plugin was made by STTL." +
                                                        "\nCheck out it's page at http://dev.bukkit.org/bukkit-plugins/STTLLoginMSG");
    // This page does not exist yet
                    }
                if (label.equalsIgnoreCase("Diamond") && (sender.hasPermission("sttllogin.diamond") && (args.length < 1))) {
                    Player player = event.getPlayer();
    // event is not recognized, do i have to use org.bukkit.event.player.getPlayer()?
                    PlayerInventory Pinventory = player.getInventory();
                    ItemStack Diamonds = new ItemStack(Material.DIAMOND, 5);
                    //ItemStack Leather = new ItemStack(Material.LEATHER, 5);
                    ItemStack DHelm = new ItemStack(Material.DIAMOND_HELMET, 1);
                    ItemStack DChest = new ItemStack(Material.DIAMOND_CHESTPLATE, 1);
                    ItemStack DLeggings = new ItemStack(Material.DIAMOND_LEGGINGS, 1);
                    ItemStack DBoots = new ItemStack(Material.DIAMOND_BOOTS, 1);
                    ItemStack DSword = new ItemStack(Material.DIAMOND_SWORD, 1);
    if (Pinventory.contains(Diamonds) || (Pinventory.contains(Leather) || (Pinventory.contains(DHelm) || (Pinventory.contains(DChest) || (Pinventory.contains(DLeggings) || (Pinventory.contains(DBoots) || (Pinventory.contains(DSword)))))))) {
    // if the inventory specified contains these itemstacks, do the following
                            player.sendMessage(ChatColor.RED + "You already have diamond armor!");
    }else{
    // if the inventory does NOT contain these itemstacks, add them to the inventory and give the player message "Here is your diamond armor!"
                            //Pinventory.addItem(Leather);
                            Pinventory.addItem(Diamonds);
                            Pinventory.addItem(DHelm);
                            Pinventory.addItem(DChest);
                            Pinventory.addItem(DLeggings);
                            Pinventory.addItem(DBoots);
                            Pinventory.addItem(DSword);
                                player.sendMessage("Here is your diamond armor!");
                    }
    return false;}}
     
Thread Status:
Not open for further replies.

Share This Page