Package com.github.zathrus_writer.commandsex.helpers.scripting

Examples of com.github.zathrus_writer.commandsex.helpers.scripting.ScriptEnvironment


   * @return
   */
  @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
  public void replaceChat(AsyncPlayerChatEvent e) {
    try {
      ScriptEnvironment env = new ScriptEnvironment(); {
        env.setCommandSender(e.getPlayer());
        env.setServer(e.getPlayer().getServer());
      }
      ArrayList<ReplacementPair> preparedEffects = new ArrayList<ReplacementPair>(); //holds all effects until all replacements done

      for (ReplacementPair rp : pairs) {
        StringBuffer sb = new StringBuffer();
        Matcher m = rp.getRegex().matcher(e.getMessage());
        if (!m.find()) continue;
        env.setMatcher(m);

        if (rp.playerWillVanish()) { //the player will vanish as a result of this, special handling
          int cutlen = CommandsEX.getConf().getInt("replacements.cutoff.length", 1);
          String cuttext = CommandsEX.getConf().getString("replacements.cutoff.indicator", "--*");
 
          String rep = m.group().substring(0, cutlen).concat(cuttext);
          m.appendReplacement(sb, rep);
          e.setMessage(sb.toString());
          //e.setCancelled(true);
          //e.getPlayer().chat(sb.toString()); //chat first
 
          rp.executeEffects(env); //then execute the replacement
          return;
        }

        //loop through with find/replace
        do { //use do while, due to the find() invocation above
          // test if it is all upper, and replace with all upper (if we have this set up in the regex itself - in config file)
          if (rp.getSameOutputCase() && allUpper && m.group().toUpperCase().equals(m.group())) {
            m.appendReplacement(sb, rp.executeString(env).toUpperCase());
          } else {
            m.appendReplacement(sb, rp.executeString(env));
          }
        } while (m.find());
        m.appendTail(sb);

        if (!preparedEffects.contains(rp)) {
          preparedEffects.add(rp);
        }
        e.setMessage(sb.toString());
      }
     
      //after all replacements are in: execute the effects
      if (!preparedEffects.isEmpty()) {
        //e.setCancelled(true);
        //e.getPlayer().chat(sb.toString()); //chat first
 
        env.setMatcher(null);
        for (ReplacementPair rp : preparedEffects){
          rp.executeEffects(env);
        }
      }
    } catch (Exception ex){
View Full Code Here


   * @return
   */
  @EventHandler(priority = EventPriority.LOWEST)
  public static void replaceCommand(PlayerCommandPreprocessEvent e) {
    try {
      ScriptEnvironment env = new ScriptEnvironment(); {
        env.setCommandSender(e.getPlayer());
        env.setServer(e.getPlayer().getServer());
      }

      for (ReplacementPair rp : pairs) {
        Matcher m = rp.getRegex().matcher(e.getMessage().substring(1));
        if (m.matches()){
          env.setMatcher(m);
          rp.executeEffects(env);
          e.setCancelled(true);
          return;
        }
      }
View Full Code Here

   * @return
   */
  @EventHandler(priority = EventPriority.LOWEST)
  public void replaceCommand(ServerCommandEvent e) {
    try {
      ScriptEnvironment env = new ScriptEnvironment(); {
        env.setCommandSender(e.getSender());
        env.setServer(e.getSender().getServer());
      }
     
      for (ReplacementPair rp : pairs) {
        Matcher m = rp.getRegex().matcher(e.getCommand());
        if (m.matches()){
          env.setMatcher(m);
          rp.executeEffects(env);
          e.setCommand("cex null"); //does nothing, prints nothing
          return;
        }
      }
View Full Code Here

TOP

Related Classes of com.github.zathrus_writer.commandsex.helpers.scripting.ScriptEnvironment

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.