Custom events question

Discussion in 'Plugin Development' started by amitlin14, Dec 18, 2012.

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

    amitlin14

    Hello, i am currently working on a dueling plugin and i want to do something when the duel is over, now, i assume the best way to do that is to listen to an event that the duel is over, but honestly, i have no idea how to do that, ive looked at the wiki and some tutorials online, but i still dont quite understand how to perform such tasks, like, what does everything do in the custon event? is the constructor the code that runs when the event is performed? how do i know when it is performed?

    Now before you start replying with links to the wiki, please explain firstly HOW to use the custom event.

    Thank you.
     
  2. Offline

    gomeow

    I don't think you need to make an event for this. Just listen to the player death event. If it has multiple players, then check if the amount of people in the duel is or is less than 1
     
  3. Offline

    SirTyler

    As goemow said you don't need a custom event for this. As long as you are storing somewhere who is in these duels (I assume you are) then you just need to check if one of them dies then say the duel is over.

    But since you do not understand how events and custom events work I will explain to better you (I will use whats on the wiki here to explain). So we make our custom event, extend the bukkit Event class, and add in the code they provided for the event. We don't need anything else here cause our event won't do anything but give the message we set in its constructor (the constructor is used when we fire the event to assin information to it). Now lets say we have a command that when we use we want to fire the event, so when the command is issued we call the following code:
    Code:java
    1. CustomEvent event = new CustomEvent("Sample Message");
    2. Bukkit.getServer().getPluginManager().callEvent(event);

    That makes a new object of our event which we then send to the method callEvent in the server's plugin manager. So now that we call the event we just need to have a EventListener somewhere that works like any other event listener, the only difference is that instead of listening for PlayerDeathEvent or something like that we listen for CustomEvent (or whatever the event class is).
     
    suckycomedian likes this.
  4. Offline

    suckycomedian

    That's awesome! I didn't even know you could make custom events lol. learn something new every day.

    If you don't want to create a custom event for it, what I would do is have all the players in the duel added to an ArrayList and when a player dies, check to see if they're on that list. If they are, remove them and then check the length of the list. If the length == 1 then the duel is over and you can do what ever else.

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

    SirTyler

    That would limit you to only 1 duel at a time. I would make an object to contain all the players in a duel and then have a List of that (basically a list inside a list, LISTCEPTION); or you can have a list of all players in any duel and then use metadata to specify on them WHICH duel they are on.
     
  6. Offline

    amitlin14

    That looks like a better way then hashmaps(which im currently using), but how the hell do you make something like that?!
     
  7. Offline

    EnvisionRed

    Something along the lines of making a "Duel" object which contains all the players in the duel and all mechanics involved, and making a list of duel objects in the main class.
     
  8. Offline

    suckycomedian

    Could you give an example please?

    I've been trying for a few hours now and can't figure it out lol... I was trying to name the object the location of a button clicked.toString() and then add all the players to that and and the object to a list... but i couldn't get it to work.. i don't even know if that's how to go about it.
     
  9. Offline

    gomeow

    In that case, you need to learn more java first. Making objects is one of the most basic things to do in a object oriented programming language...
     
  10. Offline

    suckycomedian

    I start class in a couple weeks lol. I've made dozens of plugins, just never made an object before. In the tutorials I watched they didn't go over that and the things I've been reading I can't get to work.
     
  11. Offline

    gomeow

    Ok, I will tell you.

    First, make a new class file:
    It would look like this:

    Code:java
    1. public class Duel extends MainClass {
    2. //Variables of the class
    3. ArrayList<String> duelers = new ArrayList<String>();
    4. //Constructor
    5. public Duel(ArrayList<String> list) {
    6. for(String str : list) {
    7. duelers.add(str);
    8. }
    9. }
    10. //Methods
    11. public void addDueler(String s) {
    12. duelers.add(s);
    13. }
    14. }

    That is a basic class. All it does is add people to a list.

    To use it:
    Code:java
    1. Duel d = new Duel(arrayList);
    2. d.addDueler(player.getName());

    Of course, you would need to add a lot more to it than that, but that is a basic.

    P.S. Every class is an object. Even your plugins main class.

    I would suggest watching thenewboston's java tutorials. They are very helpful
     
  12. Offline

    suckycomedian

    gomeow
    Thanks :D I watched the first few and then just started looking at other people's code to learn from that cause it was more interesting lol. Umm.. could you do this with a hashmap? I just wrote this and am playing around with it on the server. It works except that it doesn't remove me from the hashmap when I die. It does let two different games go though.
    Code:
    @EventHandler
    public void push(final PlayerInteractEvent event){
    if(event.getAction() == Action.RIGHT_CLICK_BLOCK){
    if(event.getClickedBlock().getType() == Material.STONE_BUTTON){
    if(plugin.loc.contains(event.getClickedBlock().getLocation())){
    final String player = event.getPlayer().getName();
    final Object loc = event.getClickedBlock().getLocation().toString();
    if(!(plugin.duels.containsKey(loc) && plugin.duels.containsValue(player))){
    plugin.waitlist.add(player);
    if(plugin.waitlist.contains(player)){
    event.getPlayer().sendMessage("[Button Roulette] Please wait 5 seconds.");
    }
    }
    if(!(plugin.duels.containsKey(loc) && plugin.duels.containsValue(player))){
    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    @Override
    public void run() {
    plugin.duels.put(loc, player);
    plugin.waitlist.remove(player);
    event.getPlayer().sendMessage(ChatColor.GREEN + "[Button Roulette] You may now begin!");
    event.getPlayer().sendMessage("" + plugin.duels);
    }
    } , 20L * 5);
    }
    if(plugin.duels.containsKey(loc) && plugin.duels.containsValue(player)){
    int chance = (int)(Math.random() * 6) + 1;
    if (chance == 1){
    event.getPlayer().damage(100);
    }
    }
    }
    }
    }
    }
     
    @EventHandler
    public void death(PlayerDeathEvent event){
    if(plugin.duels.containsValue(event.getEntity().getName())){
    plugin.duels.remove(event.getEntity().getName());
    event.setDeathMessage(ChatColor.DARK_RED + event.getEntity().getName() + " lost a game of roulette!");
    }
    }
    }
     
  13. Offline

    gomeow

    I wouldn't do it that way

    P.S. the tags are [syntax=java]code here[/syntax]
     
  14. Offline

    suckycomedian

    is it just cause it's easier the other way or because it's not good to do it this way?

    Ah. I used to use syntax=java but then I took a break from this for a few months and forgot what to put lol. Thanks for helping with objects ^^
     
  15. Offline

    gomeow

    While it is possible to use hashmaps that are say a string and a list of players names, I think it could be better done using objects
     
  16. Offline

    amitlin14

    Well, i went with a whole other way then object, i an arraylist to store those who are currently dueling, and i used metadata to nerrow it down to the 2 people that are dueling.

    But now i have a new problem, envolving timers. I want to make it so that from the moment the invite is sent, if in 10 seconds the invited one doesnt decline or accept the invite, the invite is deleted. now, i know how to basiclly do it, but when i try to write it i feel like my brain is going to blow, im sure that im missing something and im making it alot more complicated then it should be.

    Also, 1 more thing, i want to make it so that fromt he moment the last hit was planted, a timer of a minute is created, so that the players would duel, then just stop halfway and not do anything, i want to make it so that if they stop, if a minute has passed the duel is stopped, but unlike the previous problem, this i have no clue how to do.

    Please help :>
     
  17. Offline

    fireblast709

    First problem: At the moment of the invite, run a delayed task. Inside the task, check if the duel is accepted (this part depends on how you store the current duels).

    Second problem: Create a timestamp field somewhere, and set this each time one player hits the other. Also, when the duel is started, schedule a repeating task which compares the current time with the timestamp. If this is more than x seconds, the duel should end
     
    suckycomedian likes this.
Thread Status:
Not open for further replies.

Share This Page