Ripping my hair out. To much, to soon.

Discussion in 'Plugin Development' started by spoonikle, Feb 19, 2011.

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

    spoonikle

    ok, so the bukkit documentation, although extensive means hog wash to me. i cant make heads or tails of of the most basic things. it took me 3 hours of pondering to figure out how to add and item to a players inventory.

    whats worse is the current plug-in i hope to develop is proving to be more complicated than its idea seemed. after fingering through the source code of 4-5 plug-ins (finding plug-ins with available code is hard) they all use things that my plug-in doesn't even care about. I am lost, my head is exploding, and I am done looking at source code for the day.

    my plug-in, was supposed to do a lot of things.

    1. when a player casts a fishing line, clock how long till they reel the line back in in milliseconds or seconds and store that into a value called fishingTime.

    2. when a player drops a fish set a fishDropped value to true, for 1 min, once the time is up set it to false (this is to prevent exploiting of the next function)

    3. when a fish enters a players inventory set caughtFish to true.

    4. if fishingTime is >= 10 seconds && caughtFish == true && dropFish == false add 3 fish to the players inventory, else if fishingTime >= 20 seconds and caughtFish == False && dropFish == false add 1 fish to player inventory.

    after writing this all out, my head starting spinning. How the flipping hell am i gonna do this! i just started cramming Java 2 weeks ago i am green. i still can't grasp variable declaration, so how am i supposed to do this!?

    I am asking for help, lines of code, messages of encouragement, what ever. Please look over my source code, its not much, and probably is doing more harm than good, i just need help.

    http://dl.dropbox.com/u/19143385/BigCatch.rar
     
  2. Offline

    Edward Hand

    Well, you seem to have a clear idea in your head of what you want to do, which is a good start.

    Trying to find code examples is definitely the way to go I think. You don't necessarily need the source code. I may get shot down for this but a bit of 'ethical decompiling' can be very helpful.

    http://java.decompiler.free.fr/?q=jdgui#downloads

    Run any plugin through this program and it will spit the source code out. Remember ETHICAL. Don't steal other peoples code without their permission! Just read and learn.

    Nobody is going to write your whole plugin for you. If you have any specific questions as to how to implement specific things, ask away.

    Don't give up!
     
  3. Offline

    retsrif

    You don't seem to have imported the Bukkit libraries? Or is it because its just a copy of the file and requires me to add it? Your main file seems to be OK. Btw, you also don't have a plugin.yml? You need that.
     
  4. Offline

    fullwall

    As Edward said, examples are the best way to go. Try looking at a basic plugin with small functionality, just to get a feel for the basic setup of a plugin. Look at my sig - I'm going to put in a basic plugin template.
     
  5. Offline

    spoonikle

    the files are just a copy of my eclipse work space, and i am also surprised that this base code is even remotely correct.

    I am not in the bizz of stealing code, and i don't think there is anything more than snip-its out there that may help me any way. I would like to try this, as long as the makers don't object, I will PM them fist.


    ok, here is a general question. i initialize my listener variables in the main code, i have a class that uses variables within the listeners to determine an outcome. should i initialize the method in the main plug-in declarations onEnable() method? can someone explain to me how bukkit dose what it does, from the time an event that a plug-in wants occurs?
     
  6. Offline

    NathanWolf

    I'm putting together a "bootstrap" sample plugin... you can see/fork/steal it from here:

    https://github.com/NathanWolf/BukkitPlugins/tree/master/SamplePlugin

    git link: git://github.com/NathanWolf/BukkitPlugins.git

    Or, just check out the source code, right in your browser, courtesy of github!

    It's pretty simple, but it does some cool things that can really get you started quickly

    I'm also writing a "getting started" guide that will get you going with Eclipse, Maven, and git (if you want a nice "cloud storage" solution for your open source code). This isn't done yet, but it's mostly going to be pieced together from PM's I've written to various people, so all the info is there- and the sample project is done, and ready.

    SamplePlugin is a fully-functioning nickname plugin (more or less), in a couple of dozen lines of code.

    I'm hoping this helps get people up and going with plugin dev all that much quicker!
     
  7. Offline

    spoonikle

  8. Offline

    NathanWolf

    Yessirree I can!

    Dinnerbone did me/us the kind favor of getting rid of that giant cumbersome constructor that all plugins had to implement.

    So, for now, you can implement either the old one (which will nag in the server log, calling out the dev by name!) or get rid of it and just use the default constructor- which is really a whole lot cleaner and simpler anyway, as you can see by that change :)

    I was pretty psyched about this, especially right as I was releasing sample code.

    By the way, I halted my "tutorial" plans- this one here is great. I'm hoping to add some screenshots and make it a little more "walkthrough-y" if Cogito is cool with it, but otherwise I couldn't have done much better than that.

    The bukkit team is also either putting together a sample plugin, or has one already. I would consider my sample an example on how to use Persistence (and maybe Gameplay, later) rather than a general "start here"- though my personal recommendation would be, that you start there :)
     
  9. Offline

    spoonikle

    persistence? how exactly is that handled lol, what does it mean... well i know the Websters definition, but what does it mean to bukkit?

    i am updating my source code in just a few min. everything seems to be coming together, also can you explain this line of code -

    Code:
    private APlugin plugin;
    
            public aPluginPart(APlugin instance) {
                plugin = instance;
            }
    i want to know what it does and when it is needed, mainly because i am annoyed by the variable plugin not being used warning... its driving me crazy!!!!

    edit: new source code is at the same DropBox link, reposting it here http://dl.dropbox.com/u/19143385/BigCatch.rar
     
  10. Offline

    darknesschaos

    it makes it so you can reference the main code of your plugin, so you can easily have common functions, or have access to variables held in your main java file.
     
  11. Offline

    spoonikle

    omg, I'm so dumb.
    so that means i can reference variables that i have declared in my main class, in any class that i place that statement? well, now i want to rearrange my code a bit. remove most of the statements because they are not needed, and move the catchDelay clock and the planed Do While loop to a separate class to clean things up a little.

    so in order to use a variable from the instance of a plugin all i have to do is use-

    plugin.varaiable.method();

    correct?
     
  12. Offline

    fullwall

    That is a constructor - it is used when you create a new instance of a class, for instance aPluginPart app = new aPluginPart(plugin). Basically, you create a new instance of the class and pass the plugin variable to it in the constructor, as you couldn't do that otherwise since it's a private variable. It's called instantiation, if Google can explain it better than I can :p.
    --- merged: Feb 20, 2011 6:39 AM ---
    And yes, that's correct.
     
  13. Offline

    spoonikle

    sweet, while you guys are around, can you tell me if this will work? or fix it?
    Code:
    public void onPLayerPickUpItem(PlayerPickupItemEvent event) {
            Player player = event.getPlayer();
            fishingplayer = event.getPlayer();
            if (event.getItem().toString() == "RAW_FISH") {
                caughtFish = true;
                player.sendMessage("this is a fish"); //Purely for test purposes
    ignore the variable fishingPlayer, thats just my attempt at making a public player variable so i can use it for the addFish3 and addFish methods.

    further updated the source code, now all variables that need to reset get reset. note about the dropedFish variable, if a player drops more than one fish, i have no idea how the plug-in would handle it, i think im gona see a bug, where when a player drops a fish and they drop another 58 seconds after, the clock dosnt start over again so the dropedFish variable will still be switched to false after 2 more seconds. This would allow for a very infective exploit.
     
  14. Offline

    fullwall

    Hmmm... That would work, but you must remember that strings can't be =='d. Here, I'll put in an example of fixing your code, and my own example (which I think is more appropriate).
    Code:
    if (event.getItem().toString().equals("RAW_FISH") {
    //etc.
    }
    
    My example - better to rely on Material types IMO.
    
    if (event.getItem().getType() == Material.RAW_FISH) {
    //etc
    }
    --- merged: Feb 20, 2011 7:10 AM ---
    Oh, and you have a capital L in onPlayerPickUpItem. I'm not sure if that is correct, as I'm not at my developing environment, but make sure to check that your method overrides the superclass method.
     
  15. Offline

    spoonikle

    ok will do that, now i have an issue involving a hash table

    static HashMap<Player, Long> fishingTime = new HashMap<Player, Long>();

    i need to get the long value from this hash map, and check to see if it is >= and <= another long.

    thank you for catching that, i would have been pondering away forever to find what was wrong with my plug-in.
     
  16. Offline

    darknesschaos

    to compare strings I like to use:
    string.comparetoignorecase("comparison string")==0

    but you don't want to turn things into strings when there isn't any need to, so fullwall is correct on the code you should use.
    --- merged: Feb 20, 2011 7:21 AM ---
    for that, as long as you have the key it is easy.
    use fishingTime.get(player) to get the long associated with that player. check to make sure that key exists first though (I am not terribly sure if you get an error if you don't though.)
     
  17. Offline

    fullwall

    Code:
    long newlong = fishingTime.get(yourPlayer)
    if(newlong >= anotherlong) {
    //dostuff
    }
    else {
    //newlong is <= anotherlong
    }
    
    You should try to use doubles though.
    --- merged: Feb 20, 2011 7:22 AM ---
    Yep, you might try if(fishingTime.contains(yourplayer))

    But I think it returns a null reference to the long if yourplayer isn't in the map.
    --- merged: Feb 20, 2011 7:25 AM ---
    NVM about using doubles.
     
  18. Offline

    darknesschaos

    yeah, long is best for milis
     
  19. Offline

    fullwall

    Indeed :p.
     
  20. Offline

    spoonikle

    humm.... so i have to make a variable. well this is ok, because the method that uses this only initializes when there are variables in the hash map. the method will be called in a do while loop for when a value is true set in the method that makes the hash maps.

    i cant get (event.getItem().getType() == Material.RAW_FISH) to work... for now I am using the string method.
     
  21. Offline

    fullwall

    Well, you don't *have* to make a new variable, it's just cleaner to (and it's a primitive, so not too much memory usage). Why doesn't material work?

    if(fishingTime.get(yourPlayer) >= anotherlong)
     
  22. Offline

    spoonikle

    it trys to cast it as an event for some reason, it wants me to make it look like this -
    if ((Event)event.getItem().getType() == Material.RAW_FISH)
    and then when it does this, it tells me event is not compatable with material. this is very weird.

    when i go to use code assist on event., getItem() is not on the list of methods.

    edit: updated source http://dl.dropbox.com/u/19143385/BigCatch.rar
     
  23. Offline

    darknesschaos

    you should give us the entire event code, we will be able to help a bit more if you do. (the problem is you are casting material to event)
    --- merged: Feb 20, 2011 7:40 AM ---
    you need to do:

    if(event.getItem().getId() == Material.Raw_Fish){
    //do stuffs
    }
    --- merged: Feb 20, 2011 7:41 AM ---
    use git hub, it is quite nice for sharing code.
     
  24. Offline

    spoonikle

    Code:
    package com.hotmail.spoonikle.bigcatch;
    
    import java.util.Timer;
    import java.util.TimerTask;
    import org.bukkit.entity.Player;
    import org.bukkit.event.player.PlayerListener;
    import org.bukkit.event.player.PlayerPickupItemEvent;
    
    public class CatchFish extends PlayerListener {
    
        private boolean caughtFish = false;
        private Player fishingplayer;
    
        public Player getFishingplayer() {
            return fishingplayer;
        }
    
        public void setCaughtFish(boolean caughtFish) {
            this.caughtFish = caughtFish;
        }
    
        public void onPlayerPickUpItem(PlayerPickupItemEvent event) {
            Player player = event.getPlayer();
            fishingplayer = event.getPlayer();
            if ( event.getItem().toString().equals("RAW_FISH")) {
                caughtFish = true;
                player.sendMessage("this is a fish"); //Purely for test purposes
                new CatchReset(1); //resets caughtFish value after 1 second.
    
            }
        }
    
        public boolean isCaughtFish() {
            return caughtFish;
        }
        public class CatchReset {
            Timer timer;
    
            public CatchReset(int seconds) {
                timer = new Timer();
                timer.schedule(new RemindTask(), seconds * 1000);
            }
            //Declaration of the timer and its method.
            class RemindTask extends TimerTask {
                public void run() {
                    setCaughtFish(false);
                }
            }
        }
    }
    for those scared of a dropbox link.

    edit: so ID is a material.... well thats a misleading method name.
     
  25. Offline

    darknesschaos

    what event are you using?
    too slow, I will need to check into that event. brb
    --- merged: Feb 20, 2011 7:45 AM ---
    out of curiosity, are you using eclipse?
     
  26. Offline

    spoonikle

    yes, because i fell in love with it. easier than beans, more powerful than Jedit.
     
  27. Offline

    fullwall

    No, event.getItem.getId() is not a method AFAIK. It is getItem().getTypeId() that returns the id, and getItem().getType() that returns the material, IIRC.
    --- merged: Feb 20, 2011 7:47 AM ---
    Like eclipse too :).
    --- merged: Feb 20, 2011 7:49 AM ---
    I think the problem is that you should cast the event.getItem() to an ItemStack object, like so - ItemStack is = (ItemStack) event.getItem();
     
  28. Offline

    darknesschaos

    getItem() should work fine
    --- merged: Feb 20, 2011 7:50 AM ---
    he should not need to cast it to an itemstack.
     
  29. Offline

    spoonikle

    i also browse in chrome, hate on my own OS in Vista, rock out with Zune, and veiw my media library with PS3 media server. ok enough with more useless info, back to my code.

    well, eclipse says it wont work, but then again it could very well be wrong. if so, i wonder why.
     
  30. Offline

    darknesschaos

    as stated by fullwall

    if(event.getItem().getId() ==Material.Raw_Fish)

    should work just fine.
     
Thread Status:
Not open for further replies.

Share This Page