Referencing an interface from another jar

Discussion in 'Plugin Development' started by Rellac_, Aug 31, 2020.

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

    Rellac_

    Having a bit of trouble trying to integrate an interface callback between two plugins so they can communicate

    I have two plugins: "Skills" and "CustomMining"

    I want Skills to reference CustomMining, but not the other way around

    I've created my interface within CustomMining as such:

    Code:
    public interface IMining {
        public abstract void OnMinedOre(ItemStack item);
    }
    I've created a class to contain this interface as such:

    Code:
    public class Mining implements IMining {
       public void OnMinedOre(ItemStack item) {
         System.out.println("OnMinedOre: CustomMining");
       }
    }
    
    elsewhere in the project, I then call this as such:

    Code:
                    Mining mining = new Mining();
                    mining.OnMinedCustomOre(item);
    If I then add an override within this plugin, it all works just fine as I would expect.

    However, within the Skills plugin, I reference this a such:

    Code:
    import online.altum.CustomMining.IMining;
    public class Mining implements IMining {
        @Override
        public void OnMinedOre(ItemStack item) {
            System.out.println("OnMinedOre: Skills");
        }
    }
    
    This print never gets called, but I'm not too sure what I've done wrong here. I'm struggling to find much reference to this when I try to google java implementations and would appreciate a kick in the correct direction, thanks
     
  2. Online

    timtower Administrator Administrator Moderator

    @Rellac_ How are you telling the CustomMining plugin to use the Mining object from Skills then?
     
  3. Offline

    Rellac_

    @timtower I'm hoping that CustomMining never has a reference to Skills at all

    Should I be using something other than an interface for Skills to know when CustomMining has completed its expected action?
     
  4. Online

    timtower Administrator Administrator Moderator

    @Rellac_ What are you trying to accomplish?
     
  5. Offline

    Rellac_

    @timtower I'd like for CustomMining to send a message whenever a user sucessfully mines with the custom mechanics that I've implemented, which can be captured by any plugin that hooks into it to perform some other action
     
  6. Online

    timtower Administrator Administrator Moderator

    @Rellac_ Then you should turn it into an event.
     
  7. Offline

    Rellac_

    Thanks a lot @timtower, this looks like what I need. I've been trying to implement this but I still seem to be running into issues if you wouldn't mind further assistance:

    Within CustomMining, I have the following class to send my message:
    Code:
    public class EventSender extends Event implements Listener {
        private static final HandlerList handlers = new HandlerList();
        public EventSender(ItemStack item) {
            OnMinedCustomOre(item);
        }
        public void OnMinedCustomOre(ItemStack item) {
            System.out.println("OnMinedCustomOre: CustomMining");
        }
        public HandlerList getHandlers() {
            return handlers;
        }
    }
    I send the event when I need it as such:
    Code:
                    EventSender eventSender = new EventSender(item);
                    Bukkit.getServer().getPluginManager().callEvent(eventSender);
    Within Skills, I then try to catch this message as such:
    Code:
    public class Mining extends EventSender {
        public Mining(ItemStack input) {
            super(input);
            OnMinedCustomOre(input);
        }
    
        public void OnMinedCustomOre(ItemStack item) {
            System.out.println("Mined Custom Ore: Skills");
        }
    }
    I think the issue is in how I register the event here. It doesn't make too much sense to me, as it seems to take an input on the default constructor, but that should be irrelevant at this end when I register?
    Code:
    getServer().getPluginManager().registerEvents(new Mining(null), this);
    I have the same issue as before, where I'm only getting the print in CustomMining, but not Skills

    Struggling a lot with this, it's very different from the events that I'm used to using. I appreciate your patience
     
Thread Status:
Not open for further replies.

Share This Page