Sueta
JI

Jibe

1.0.0

Events for Redis

·Plugin

Sending events through Redis has never been easier!

Repository

<repositories  >    
    <repository  >    
      <id  >    jibe   </id  >    
      <url  >    https://dl.bintray.com/simpleauthority/jibe   </url  >    
    </repository  >    
  </repositories  >

Dependency

<dependencies  >    
    <dependency  >    
      <groupId  >    com.algorithmjunkie   </groupId  >    
      <artifactId  >    jibe   </artifactId  >    
      <version  >    1.0.0   </version  >    
    </dependency  >    
  </dependencies  >

Example :
Create a new plugin and instantiate Jibe.

public   class  TestPlugin  extends  JavaPlugin  {  
     private  Jibe jibe  ;  

    @Override 
     public   void  onEnable  (   )   {  
        jibe  =   new  Jibe  (  
                 "MY_CHANNEL"  , 
                 new  RedisAuthAdapter  (  
                         "localhost"  , 
                         6379  
                 )  
         )   ;  

 //        Alternatively, use a password:  
 //        jibe = new Jibe(  
 //                "MY_CHANNEL",  
 //                new SecureRedisAuthAdapter(  
 //                        "localhost",  
 //                        "password",  
 //                        6379  
 //                )  
 //        );  
     }  
 }

The channel you chose is the channel on which messages will be dispatched. All your clients must use the same channel to send and receive messages to each other.

Assume a JibeMessage like so:

// A JibeMessage that has a sender and a list of affected players.  
 public   class  TestJibeMessage  extends  Jibe.  JibeMessage   {  
     private   final  UUID sender  ;  
     private   final  List  <  UUID  >  players  ;  

     public  TestJibeMessage  (  UUID sender, List  <  UUID  >  players  )   {  
         this  .  sender   =  sender  ;  
         this  .  players   =  players  ;  
     }  

     public  UUID getSender  (   )   {  
         return  sender  ;  
     }  

     public  List  <  UUID  >  getPlayers  (   )   {  
         return  players  ;  
     }  
 }

As an example, here is dispatch a "TestJibeMessage" in reaction to an AsyncPlayerChatEvent :

public   class  TestListener  implements  Listener  {  
     private  Jibe jibe  ;  
  
     public  TestListener  (  Jibe jibe  )   {  
         this  .  jibe   =  jibe  ;  
     }  
  
    @EventHandler 
     public   void  onChat  (  AsyncPlayerChatEvent event  )   {  
        jibe.  post   (   new  TestJibeMessage  (  
                event.  getPlayer   (   )  .  getUniqueId   (   )  , 
                event.  getRecipients   (   )  .  stream   (   )  
                        .  map   (  Player  ::  getUniqueId  )  
                        .  collect   (  Collectors.  toList   (   )   )  
         )   )   ;  
     }  
 }

Now, to catch the "TestJibeMessage" on any Jibe instance for the same channel, just create a listener class:

public   class  TestJibeListener  {  
    @Subscribe 
     public   void  onTestJibe  (  TestJibeMessage message  )   {  
          System   .  out  .  println   (    String   .  format   (  
                 "Test Jibe received for %s"  , 
                message.  getSender   (   )  .  toString   (   )  
         )   )   ;  

        message.  getPlayers   (   )  .  forEach   (  player  ->  
                  System   .  out  .  println   (    String   .  format   (  
                         ">>>> Received by %s"  , 
                        player.  toString   (   )  
                 )   )  
         )   ;  
     }  
 }

Then register it with Jibe's Event Bus:

@Override 
 public   void  onEnable  (   )   {  
  jibe.  getEventBus   (   )  .  register   (   new  TestJibeListener  (   )   )   ;  
 }

Similar projects

Plugin →