Guest User

Untitled

a guest
Dec 6th, 2018
100
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package ru.delusive.oc;
  2.  
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.Collection;
  6.  
  7. import org.slf4j.Logger;
  8. import org.spongepowered.api.config.DefaultConfig;
  9. import org.spongepowered.api.entity.living.player.Player;
  10. import org.spongepowered.api.event.Listener;
  11. import org.spongepowered.api.event.Order;
  12. import org.spongepowered.api.event.game.state.GameStartedServerEvent;
  13. import org.spongepowered.api.event.message.MessageChannelEvent;
  14. import org.spongepowered.api.plugin.Plugin;
  15. import org.spongepowered.api.text.Text;
  16. import org.spongepowered.api.text.channel.MessageChannel;
  17. import com.google.inject.Inject;
  18.  
  19. import ninja.leaping.configurate.ConfigurationOptions;
  20. import ninja.leaping.configurate.commented.CommentedConfigurationNode;
  21. import ninja.leaping.configurate.loader.ConfigurationLoader;
  22.  
  23. @Plugin(id = "oldschoolchat", name = "OldschoolChat", version = "1.0")
  24. public class MainClass {
  25.     @Inject
  26.     Logger log;
  27.     @Inject
  28.     @DefaultConfig(sharedRoot = false)
  29.     public ConfigurationLoader <CommentedConfigurationNode> localLoader;
  30.     //config
  31.     boolean isRangedChatEnabled;
  32.     double messageRange;
  33.     String globalSymbol;
  34.     String rangedSymbol;
  35.    
  36.     @Listener(order=Order.POST)
  37.     public void onChat(MessageChannelEvent.Chat e) {
  38.         if(!isRangedChatEnabled) {
  39.             return;
  40.         }
  41.         if(e.getCause().first(Player.class).isPresent()) {
  42.             Player p = e.getCause().first(Player.class).get();
  43.             Text.Builder b = Text.builder();
  44.             if(e.getRawMessage().toPlain().charAt(0) == '!') {
  45.                 b.append(Text.of(globalSymbol));
  46.                 b.append(e.getMessage().getChildren().get(0)); //Prefix + username
  47.                 b.append(Text.of(e.getRawMessage().toPlain().substring(1).replaceAll("&", "§")));
  48.                 e.setMessage(b.build());
  49.                 return;
  50.             }
  51.             b.append(Text.of(rangedSymbol));
  52.             b.append(e.getMessage());
  53.             MessageChannel.fixed(getNearbyPlayers(p)).send(b.build());
  54.             e.setCancelled(true);
  55.             e.setMessageCancelled(true);
  56.            
  57.         }
  58.     }
  59.    
  60.     public Collection<Player> getNearbyPlayers(Player p){
  61.         Collection <Player> list = new ArrayList<>();
  62.         for(Player currPlayer : p.getWorld().getPlayers()) {
  63.             if(p.getPosition().distanceSquared(currPlayer.getPosition()) <= messageRange*messageRange) {
  64.                 list.add(currPlayer);
  65.             }
  66.         }
  67.         return list;
  68.     }
  69.    
  70.     @Listener
  71.     public void onEnable(GameStartedServerEvent e) throws IOException {
  72.         CommentedConfigurationNode conf = localLoader.load(ConfigurationOptions.defaults().setShouldCopyDefaults(true));
  73.         this.isRangedChatEnabled = conf.getNode("config", "isRangedChatEnabled").getBoolean(true);
  74.         conf.getNode("config", "isRangedChatEnabled").setComment("Should it work?");
  75.         this.messageRange = conf.getNode("config", "messageRange").getDouble(100.0);
  76.         conf.getNode("config", "messageRange").setComment("Local messages range");
  77.         this.globalSymbol = conf.getNode("config", "globalSymbol").getString("[G]");
  78.         conf.getNode("config", "globalSymbol").setComment("Prefix of Global message ( [G][Admin]Delusive: Hello everybody! )");
  79.         this.rangedSymbol = conf.getNode("config", "rangedSymbol").getString("[L]");
  80.         conf.getNode("config", "rangedSymbol").setComment("Prefix of Local message ( [L][Admin]Delusive: Only players near can see that message! )");
  81.         localLoader.save(conf);
  82.     }
  83. }
RAW Paste Data