Accessing code from another package

Discussion in 'Plugin Development' started by floory565, Jun 4, 2016.

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

    floory565

    Hi,

    I'm new to plugin development, so forgive me for asking what might be really basic.

    I've defined a player from an event in one of several packages in my plugin:
    Code:
    Player passenger = event.getPlayer();
    and I want to refer to "passenger" in another package of the plugin.

    How would I go about doing this?

    Thanks in advance!
     
  2. Online

    timtower Administrator Administrator Moderator

    @floory565 That has nothing to do with packages.
    You want to access a variable that is defined in a method.
    You are trying to do something that isn't possible the way that you explained it.
    Could you please tell what you are trying to do?
     
  3. Offline

    floory565

    I may have meant "Class" sorry, not entirely sure.
     
  4. Online

    timtower Administrator Administrator Moderator

    @floory565 What is the goal? What do you need that specific player for?
     
  5. You can access a class's code in multiple ways. One of the most common ways is to pass an instance of the class variable:
    Code:
    ClassName variableName = new ClassName();
    From there you can use all of the public variables and methods.
     
  6. Offline

    floory565

    How would I make the variable from the event public?
     
  7. Offline

    Zombie_Striker

    @floory565
    You can't. What you can do is make a field public, and set that field equal to the value from the event.
     
  8. Offline

    floory565

    What I've done is this:

    In the class with the event (let's call it ClassWithEvent), I've added
    Code:
    private Player passenger;
    and in the event, after I've defined the Player as passenger, I've added
    Code:
    this.passenger = passenger;
    and lastly, I've added the following to the same class
    Code:
    public Player getPlayer() {
          return passenger;
      }
    Then, in the other class that I want to utilise "passenger", I've added
    Code:
    Player passenger = ClassWithEvent.getPlayer();
    should that be alright?
     
    Last edited: Jun 4, 2016
  9. Online

    timtower Administrator Administrator Moderator

    @floory565 That is a very bad way of doing it.
    Just call the method with the player as argument instead.

    It is very unclear what you are wanting to do.
     
  10. Offline

    Xerox262

    @floory565 Explain why you need the player and we can answer your question, the reason you shouldn't store the player in a public variable is because when the event runs again the player will be overwritten, so if you're other block of code doesn't execute in that time it won't work like you want.

    There's an easier way to do it that I can think of right now, Although I'm assuming you want the player for 1 of 2 reasons, to check if they trigger two events or to do some code after a specific event.
     
  11. Offline

    floory565

    The "ClassWithEvent" class implements an event handler for when a player "catches" a custom entity with a fishing rod. The "OtherClass" class handles several things in regards to the "passenger" riding the custom entity.
    For this reason I need to reference "passenger" from "ClassWithEvent" in "OtherClass".
     
  12. Offline

    Xerox262

    @floory565 You want the code to run right after the event is handled? Call the method from your other class in the event with the passenger as a parameter.
     
  13. Offline

    floory565

    I do
    How would I go about doing that sorry? Like I originally stated, I'm new to plugin development (and Java in general) :p
     
  14. Offline

    Xerox262

    @floory565 See the method header in your other class? Looks something like

    public returnType methodName() {

    inside those parenthesies () insert Player passenger, then you can refer to it in the method body.
    Should look like this after

    public returnType methodName(Player passenger) {

    Then in your event you can call it like so

    otherClass.methodName(passenger);
     
  15. Offline

    MadMaxCookie

    @floory565 hey buddy remember me :p

    try doing this my bad
    Code:
    public class OtherClassName {
      Player player;
      public OtherClassName(Player player) {
              return this.player = player;
      }
      public void sendMessageToPlayer() {
        player.sendMessage("Hi this is from the other class");
     }
    then you can do this one at the other class
    Code:
    public static OtherClassName getPlayer(Player player) {
         return new OtherClassName(player);
    }
    
    //then do
    OtherClass.getPlayer(passenger).sendMessageToPlayer();
    this is just a psudo code.
     
  16. Online

    timtower Administrator Administrator Moderator

    @MadMaxCookie That static method is overkill as it just calls the constructor.
    Having a reference is a better idea.
     
  17. Offline

    MadMaxCookie

    @timtower Thanks for the info I abused static too much :p, I based to what he said "Accessing from OtherClass".
     
  18. Offline

    Xerox262

    @MadMaxCookie Why does your constructor have a return? :confused:
    And why would you choose to create an instance for each player when you could just as easily pass a player, if it was doing something more than sending a message it might be a wise choice but @floory565 hasn't specified what he's trying to do.
     
  19. Offline

    MadMaxCookie

    @Xerox262 eh ? did I said that was exactly what he needs to do ?
     
  20. Offline

    floory565

    @MadMaxCookie Oh, it's you... :p
    @Xerox262 @timtower Thanks to all of you, I'll have a read through what you've all suggested tomorrow, need some sleep now ;)
     
Thread Status:
Not open for further replies.

Share This Page