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);
}
Aggregations