Search in sources :

Example 1 with NearestPlayer

use of com.jaoafa.MyMaid2.Lib.NearestPlayer in project MyMaid2 by jaoafa.

the class Event_CommandBlockVariable method onCommandBlockCall.

@EventHandler
public void onCommandBlockCall(ServerCommandEvent event) {
    if (!(event.getSender() instanceof BlockCommandSender))
        return;
    BlockCommandSender sender = (BlockCommandSender) event.getSender();
    if (sender.getBlock() == null || !(sender.getBlock().getState() instanceof CommandBlock))
        return;
    CommandBlock cmdb = (CommandBlock) sender.getBlock().getState();
    String command = cmdb.getCommand();
    // 最初に$が入ってたら変数入りコマンド
    if (!command.startsWith("$"))
        return;
    // $を消す
    command = StringUtils.stripStart(command, "$");
    // /を消す
    command = StringUtils.stripStart(command, "/");
    // 「@p」のみ置き換える動作をする
    if (sender instanceof Player) {
        Player player = (Player) sender;
        NearestPlayer npr = new NearestPlayer(player.getLocation());
        if (npr.getStatus()) {
            command = command.replaceAll("@" + "p" + "", npr.getPlayer().getName());
        }
    } else if (sender instanceof BlockCommandSender) {
        NearestPlayer npr = new NearestPlayer(cmdb.getBlock().getLocation());
        if (npr.getStatus()) {
            command = command.replaceAll("@" + "p" + "", npr.getPlayer().getName());
        }
    }
    // ----- 事前定義(予約済み変数) ----- //
    SimpleDateFormat sdf_Year = new SimpleDateFormat("yyyy");
    command = command.replaceAll("\\$" + "DateTime_Year" + "\\$", sdf_Year.format(new Date()));
    SimpleDateFormat sdf_Month = new SimpleDateFormat("MM");
    command = command.replaceAll("\\$" + "DateTime_Month" + "\\$", sdf_Month.format(new Date()));
    SimpleDateFormat sdf_Day = new SimpleDateFormat("dd");
    command = command.replaceAll("\\$" + "DateTime_Day" + "\\$", sdf_Day.format(new Date()));
    SimpleDateFormat sdf_Hour = new SimpleDateFormat("HH");
    command = command.replaceAll("\\$" + "DateTime_Hour" + "\\$", sdf_Hour.format(new Date()));
    SimpleDateFormat sdf_Minute = new SimpleDateFormat("mm");
    command = command.replaceAll("\\$" + "DateTime_Minute" + "\\$", sdf_Minute.format(new Date()));
    SimpleDateFormat sdf_Second = new SimpleDateFormat("ss");
    command = command.replaceAll("\\$" + "DateTime_Second" + "\\$", sdf_Second.format(new Date()));
    command = command.replaceAll("\\$" + "PlayerCount" + "\\$", String.valueOf(Bukkit.getServer().getOnlinePlayers().size()));
    for (Player p : Bukkit.getOnlinePlayers()) {
        if (!command.contains("$" + "Damager_" + p.getName() + "$")) {
            continue;
        }
        EntityDamageEvent ede = p.getLastDamageCause();
        if (ede == null) {
            continue;
        }
        Entity e = ede.getEntity();
        if (e == null) {
            continue;
        }
        String name = e.getName();
        command = command.replaceAll("\\$" + "Damager_" + p.getName() + "\\$", name);
    }
    ScoreboardManager sbm = Bukkit.getScoreboardManager();
    Scoreboard sb = sbm.getMainScoreboard();
    for (Objective obj : sb.getObjectives()) {
        String regex = "\\$Score_" + obj.getName() + "_(.+?)\\$";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(command);
        while (m.find()) {
            Score i = obj.getScore(m.group(1));
            if (i == null) {
                continue;
            }
            command = command.replaceAll("\\$" + "Score_" + obj.getName() + "_" + m.group(1) + "\\$", "" + i.getScore());
        }
    }
    // ----- 事前定義(予約済み変数) ----- //
    Map<String, String> map = MyMaidVariable.listALL();
    for (Map.Entry<String, String> e : map.entrySet()) {
        command = command.replaceAll("\\$" + e.getKey() + "\\$", e.getValue());
    }
    Bukkit.dispatchCommand(sender, command);
}
Also used : Entity(org.bukkit.entity.Entity) Pattern(java.util.regex.Pattern) Player(org.bukkit.entity.Player) NearestPlayer(com.jaoafa.MyMaid2.Lib.NearestPlayer) NearestPlayer(com.jaoafa.MyMaid2.Lib.NearestPlayer) Matcher(java.util.regex.Matcher) CommandBlock(org.bukkit.block.CommandBlock) Date(java.util.Date) Objective(org.bukkit.scoreboard.Objective) Score(org.bukkit.scoreboard.Score) Scoreboard(org.bukkit.scoreboard.Scoreboard) ScoreboardManager(org.bukkit.scoreboard.ScoreboardManager) SimpleDateFormat(java.text.SimpleDateFormat) Map(java.util.Map) BlockCommandSender(org.bukkit.command.BlockCommandSender) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent) EventHandler(org.bukkit.event.EventHandler)

Aggregations

NearestPlayer (com.jaoafa.MyMaid2.Lib.NearestPlayer)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 Map (java.util.Map)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 CommandBlock (org.bukkit.block.CommandBlock)1 BlockCommandSender (org.bukkit.command.BlockCommandSender)1 Entity (org.bukkit.entity.Entity)1 Player (org.bukkit.entity.Player)1 EventHandler (org.bukkit.event.EventHandler)1 EntityDamageEvent (org.bukkit.event.entity.EntityDamageEvent)1 Objective (org.bukkit.scoreboard.Objective)1 Score (org.bukkit.scoreboard.Score)1 Scoreboard (org.bukkit.scoreboard.Scoreboard)1 ScoreboardManager (org.bukkit.scoreboard.ScoreboardManager)1