Jump to content

Search the Community

Showing results for tags 'playerlistener'.



More search options

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


Forums

  • RuneLocus
    • News & Announcements
    • Information Booth
    • Website Support
    • Introductions
    • Chat
    • Forum Games
  • RuneScape Development
    • RSPS General
    • RuneScape Private Server (RS2)
    • RuneScape Private Server (503+)
    • RuneScape Private Server (EoC 742+)
  • Other
    • Digital Art
    • Gaming
    • Webdevelopment
    • Computers
  • Marketplace
    • RuneScape Market
    • RSPS Market
    • Others Market
    • Freelance Middleman Services
  • Super Secret Club's Topics

Find results in...

Find results that contain...


Date Created

  • Start

    End


Last Updated

  • Start

    End


Filter by number of...

Joined

  • Start

    End


Group


Found 1 result

  1. Hiya there. I figured I would release a system which I've recently added to my own source, converted to Hyperion. This should work for all versions. [B]Description[/B] The PlayerListener system I shall be explaining here, is a simple implementation of the system I've added to my own source. It allows a more stable, convenient and better performing way to perform certain actions when certain events occur. This tutorial will not add any specific features, but will allow an easier access to actually add them. For example, I currently have the bank deposit box, the trading system, and a start of personal shops. And they are all using this system to secure their actions in a more convenient way than currently seen in the private server scene. In the future, I plan on creating a lot more features with the use of this system. The concept of it is quite simple, really. It's just a far better performing way to e.g. revert a trade when a player gets disconnected. Instead of having one or more methods which check if the player is trading when it got disconnected and perform actions if so, this will just allow you to actually perform these actions while the player is in a trade. When the player is no longer in a trade, you simply remove the assigned PlayerListener implementation and it will no longer perform the desired actions within. Because this system is quite extensible, I will not be giving you everything of it. I will show you how to implement it, and give you an example to demonstrate how it works. So if you're not up to learning some basic Java or are just aiming to cram as much as tutorials as possible into your source, this thread (or the entire Hyperion source for that matter) is not meant for you. [B]Before we get started, I highly suggest you make a full backup of your source. As you should always do when following tutorials.[/B] Now, let's start shall we? :) [HR][/HR] [COLOR="#000080"][B]Step 1. [I]Creating the interface[/I][/COLOR] Preferable package;[/B] [I]org.hyperion.rs2.model.listener[/I] [INDENT]This package probably doesn't exist yet in your source. If you're planning on extending this system to more classes than mentioned in this tutorial, I recommend you to create this new package for later use as well.[/INDENT] [B]Preferable class name;[/B] [I]AbstractPlayerListener[/I] [INDENT]The reason I've chosen this name is because you might want to use a name like PlayerListener for collective purposes of all other extending interfaces.[/INDENT] So, create a new class in the aforementioned package named AbstractPlayerListener.java. Here's the content; [CODE]package org.hyperion.rs2.model.util.listener; import org.hyperion.rs2.model.Player; /** * A tag interface which represents a listener for events that occur at and * around a player. * * @author Mike (The Eternity) * */ public interface AbstractPlayerListener { /** * Listens to general actions of a {@link Player} such as logging out. * * @author Mike (The Eternity) * */ public interface GeneralPlayerListener extends AbstractPlayerListener { /** * Called when the specified {@link Player} has logged out. * * @param player The player. */ public void loggedOut(Player player); } } [/CODE] [B]Explanation of the code:[/B] This code is quite simple (as I said in the description, the entire system is actually quite simple). [LIST] [*]The AbstractPlayerListener is a 'tag interface'. [I]What is a 'tag interface'?[/I] A 'tag interface' is a simple interface which is implemented pure for representation of multiple classes collectively, it doesn't have any methods to override. [/LIST] Moving on to the inner class. [I]What is an inner class?[/I] An inner class is a class or interface type which is declared within another. A good example is actually the code above. As you can see, GeneralPlayerListener is an interface which is actually declared within the AbstractPlayerListener type. More information about (anonymous) inner/nested classes can be found here: [url]http://en.wikipedia.org/wiki/Inner_class[/url] [LIST] [*]The GeneralPlayerListener is an interface type which extends the AbstractPlayerListener. It's implementing classes are categorized as listeners for general events around the player such as logging out, hence it adds the method 'loggedOut(Player)' in our case. [/LIST] So we've added our first basic PlayerListener. Now it's time to make it work, don't you think? ;) [COLOR="#000080"][B]Step 2. [I]Implementing the system[/I][/B][/COLOR] [U]To start off, we're going into the org.hyperion.rs2.model.Player class.[/U] Let's begin with adding the imports, naturally at the top of the class: [CODE]import org.hyperion.rs2.model.util.listener.AbstractPlayerListener; import org.hyperion.rs2.model.util.listener.AbstractPlayerListener.GeneralPlayerListener;[/CODE] Now, at the rest of the declarations (somewhere around the inventory, bank and equipment fields will do), add the following: [CODE] /** * A list of {@link AbstractPlayerListener}s. */ private final List<AbstractPlayerListener> listeners = new CopyOnWriteArrayList<AbstractPlayerListener>();[/CODE] [B]Explanation of the code:[/B] This is nothing special although people that are not that familiar with Java's arsenal of collections, might run into the 'CopyOnWriteArrayList' part. [LIST] [*][I]So what is the CopyOnWriteArrayList and why do we use it here?[/I] The CopyOnWriteArrayList is a special type of List that Java provides us to 'rule out' [URL=http://download.oracle.com/javase/7/docs/api/java/util/ConcurrentModificationException.html]ConcurrentModificationExceptions[/URL]. So basically, it allows you to perform read and write operations at the same time, because it copies the contents when a write operation is requested. This way, the read operation can proceed undisturbed. The main reason we use it here is to allow listeners to actually be able to remove itself or other listeners from the list while performing their desirable actions. [URL="http://download.oracle.com/javase/7/docs/api/java/util/concurrent/CopyOnWriteArrayList.html"]Click here for the CopyOnWriteArrayList API.[/URL] [/LIST] Moving on.. Between your 'getters and setters' add the following piece of code: [CODE] /** * Adds an {@link AbstractPlayerListener} to this {@link Player}. * * @param listener The listener to add. */ public void addListener(AbstractPlayerListener listener) { listeners.add(listener); } /** * Removes an {@link AbstractPlayerListener} from this {@link Player}. * * @param listener The listener to remove. */ public void removeListener(AbstractPlayerListener listener) { listeners.remove(listener); } /** * Removes all listeners currently active for this {@link Player}. */ public void removeAllListeners() { listeners.clear(); } /** * Gets all {@link AbstractPlayerListener}s for this {@link Player}. * * @return The list of listeners for this player. */ public List<AbstractPlayerListener> getListeners() { return listeners; }[/CODE] [B]Explanation of the code:[/B] These methods add, remove, remove all and get the entire list of listeners of a player respectively. This basically allows you to manage the listeners conveniently, for example [I]player.addListener(new HelloWorldPlayerListener());[/I] [HR][/HR] So, what's next? Nothing! This was actually everything there is to it. You can now start using the system! But as promised, I will give you a simple example to work with. [COLOR="#000080"][B]Step 3. [I]Working with the system[/I][/B][/COLOR] [U]For this example, we're opening the org.hyperion.rs2.net.ActionSender class.[/U] Within the ActionSender class, you will find the method 'sendLogin()'. This method basically sends all packets that must be sent to the client when a player logs in. By default, it also adds some utilities to the player's inventory and equipment (which is also part of a listener system, by the way). So let's pick a good spot for our first implementation of the system. I've actually chosen to almost add it at the very top of the method, just below [I]player.setActive(true);[/I] At your chosen spot, we'll add the following piece of code: [CODE]player.addListener(new GeneralPlayerListener() { @Override public void loggedOut(Player player) { System.out.println("Have you heard!? " + player.getName() + " has logged out!"); } });[/CODE] [B]Explanation of the code:[/B] This will add a simple GeneralPlayerListener to the player, which prints out a sentence when a player has logged out. Let's not forget to add the import! [CODE]import org.hyperion.rs2.model.listener.AbstractPlayerListener.GeneralPlayerListener;[/CODE] And that's all for the ActionSender class. To make our example actually functional, we will go into the [I]org.hyperion.rs2.model.World[/I] class. In this class, look for the 'unregister(Player)' method. That method is responsible for performing the necessary actions when a player is being disconnected from the server. At the very top of that method, add the following piece of code: [CODE] for (AbstractPlayerListener l : player.getListeners()) { if (l instanceof GeneralPlayerListener) { ((GeneralPlayerListener) l).loggedOut(player); } }[/CODE] [B]Explanation of the code:[/B] This piece of code will loop through all currently active listeners of the player, and whichever ones are declared as GeneralPlayerListener (hence have the required 'loggedOut(Player)' method) will be notified that the player is being unregistered. Naturally, we add the imports: [CODE]import org.hyperion.rs2.model.util.listener.AbstractPlayerListener; import org.hyperion.rs2.model.util.listener.AbstractPlayerListener.GeneralPlayerListener;[/CODE] [B]And that's all there is to it![/B] If you've followed this tutorial properly, you should be able to compile & run without any errors. If you do happen to face any issues, do not hesitate asking/informing me about them. Congratulations, you have successfully implemented the PlayerListener system! I recommend you play around with it for a while before considering building actual content on it. First, get the hang of it. Then, make use of it. ;) Well that was my tutorial, I hope you liked it! All constructive criticism is appreciated, and questions/comments are of course always welcome. Good luck and have fun! [B] - Mike[/B]
×