Guest User

Untitled

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