Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit

Permalink
🍣 instances management and message broadcast
Browse files Browse the repository at this point in the history
  • Loading branch information
Belikhun committed May 21, 2022
1 parent 90e7bf6 commit 55876c2
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,60 @@
package com.github.belikhun.Countdown.Commands;

import java.util.ArrayList;
import java.util.Arrays;

import com.github.belikhun.Countdown.CountInstance;
import com.github.belikhun.Countdown.Countdown;
import com.github.belikhun.Countdown.CountInstance.CountdownCallback;

import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.boss.BossBar;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;

public class CountdownCommand implements CommandExecutor {
protected class Instance {
public int id;
public String title;
public CountInstance count;

public Instance(String title) {
this.title = title;
}

public void start(int seconds) {
count = new CountInstance(seconds, new CountdownCallback() {

@Override
public void begin(BossBar bar) {

}

@Override
public void update(BossBar bar, double remain) {
String message = String.format("&r&7#%d &a%s&r sau %s", id, title, CountInstance.readableTime(remain));
bar.setTitle(Countdown.colorize(message));
}

@Override
public void complete(BossBar bar) {
String message = String.format("&r&7#%d &a%s&r đã bắt đầu!", id, title);
bar.setTitle(Countdown.colorize(message));
}
});
}

public void stop() {
count.stop(String.format("Đã hủy bỏ &d%s", title));
Bukkit.broadcastMessage(Countdown.colorize(
String.format("Sự kiện &7(#%d) &d%s &fđã bị hủy!", id, title)));
}
}

public static ArrayList<Instance> instances = new ArrayList<>();
public CountdownCommand() { }

@Override
Expand All @@ -22,39 +63,72 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
return false;

if (args.length < 1) {
sender.sendMessage(Countdown.colorize("&fSử dụng &a/countdown &b<length> &7[message]"));
sender.sendMessage(Countdown.colorize("&fSử dụng &a/countdown &7<start|stop|stopall>"));
return true;
}

String title = "Đếm Ngược Kết Thúc";
int length = Integer.parseInt(args[0]);
switch (args[0]) {
case "start":
if (args.length < 2) {
sender.sendMessage(Countdown.colorize("&fSử dụng &a/countdown start &7<length> [message]"));
return true;
}

String title = "Sự Kiện Kết Thúc";
int length = Countdown.parseTime(args[1]);

if (args.length >= 2)
title = StringUtils.join(Arrays.copyOfRange(args, 1, args.length), ' ');
if (length < 0) {
sender.sendMessage(Countdown.colorize("&cThời gian không phải là một số hợp lệ: " + args[1]));
return true;
}

if (args.length >= 3)
title = StringUtils.join(Arrays.copyOfRange(args, 2, args.length), ' ');

countdown(title, length);
break;

countdown(title, length);
case "stop":
if (args.length < 2) {
sender.sendMessage(Countdown.colorize("&fSử dụng &a/countdown stop &7<id>"));
return true;
}

int id = Integer.parseInt(args[1]);

for (Instance instance : instances) {
if (instance.id != id)
continue;

instance.stop();
return true;
}

sender.sendMessage(Countdown.colorize("&cKhông tìm thấy countdown với ID " + id));
return true;

case "stopall":
stopAll();

break;

default:
sender.sendMessage(Countdown.colorize("&cHành động " + args[0] + " không tồn tại! &fSử dụng &a/countdown &7<start|stop|stopall>"));
return true;
}

return true;
}

public void countdown(String title, int seconds) {
new CountInstance(seconds, new CountdownCallback() {
Instance instance = new Instance(title);
instances.add(instance);
instance.id = instances.indexOf(instance);
instance.start(seconds);
}

@Override
public void begin(BossBar bar) {

}

@Override
public void update(BossBar bar, double remain) {
String message = String.format("&r&a%s&r sau %s", title, CountInstance.readableTime(remain));
bar.setTitle(Countdown.colorize(message));
}

@Override
public void complete(BossBar bar) {
String message = String.format("&r&a%s&r đã bắt đầu!", title);
bar.setTitle(Countdown.colorize(message));
}
});
public static void stopAll() {
for (Instance instance : instances)
instance.stop();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
return true;
}

instance.stop("&a&lTắt Máy Chủ Đã Được Hủy!");
instance.stop("&a&lĐã Hủy Tắt Máy Chủ!");
instance.bar.setColor(BarColor.GREEN);
Bukkit.broadcastMessage("Đã hủy tắt máy chủ.");
instance = null;
Expand Down Expand Up @@ -79,11 +79,11 @@ public void begin(BossBar bar) {

@Override
public void update(BossBar bar, double remain) {
ChatColor textColor = ChatColor.of("#ffabc0");
ChatColor textColor = ChatColor.of("#ff3b62");

String message = (reason != null)
? String.format("&r%s&l⚠⚠⚠ Tắt Máy Chủ ⚠⚠⚠&r sau %s &7(%s)", textColor, remain, reason)
: String.format("&r%s&l⚠⚠⚠ Tắt Máy Chủ ⚠⚠⚠&r sau %s", textColor, CountInstance.readableTime(remain));
? String.format("&r%s&l⚠⚠⚠ TẮT MÁY CHỦ ⚠⚠⚠&r sau %s &7(%s)", textColor, remain, reason)
: String.format("&r%s&l⚠⚠⚠ TẮT MÁY CHỦ ⚠⚠⚠&r sau %s", textColor, CountInstance.readableTime(remain));

bar.setTitle(Countdown.colorize(message));
}
Expand Down
15 changes: 11 additions & 4 deletions lib/src/main/java/com/github/belikhun/Countdown/CountInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
import net.md_5.bungee.api.ChatColor;

public class CountInstance {
public static ChatColor timeColor = ChatColor.of("#abcfff");
public static ChatColor timeColor[] = new ChatColor[] {
ChatColor.of("#ff9466"),
ChatColor.of("#4efcbd"),
ChatColor.of("#66bdff")
};

public static ArrayList<CountInstance> instances = new ArrayList<>();

public BossBar bar;
Expand Down Expand Up @@ -113,9 +118,11 @@ public void run() {
}

public static String readableTime(double seconds) {
return (seconds > 120d)
? String.format("%s%.1fm&r", timeColor, (seconds / 60d))
: String.format("%s%.3fs&r", timeColor, seconds);
return (seconds / 3600d > 1)
? String.format("%s%.1fh&r", timeColor[0], (seconds / 3600d))
: ((seconds > 120d)
? String.format("%s%.1fm&r", timeColor[1], (seconds / 60d))
: String.format("%s%.3fs&r", timeColor[2], seconds));
}

public static void handlePlayerJoin(Player player) {
Expand Down
20 changes: 20 additions & 0 deletions lib/src/main/java/com/github/belikhun/Countdown/Countdown.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.io.InputStream;
import java.util.logging.Logger;

import javax.annotation.Nullable;

import com.github.belikhun.Countdown.Commands.CountdownCommand;
import com.github.belikhun.Countdown.Commands.ShutdownCommand;
import com.mojang.brigadier.tree.LiteralCommandNode;
Expand Down Expand Up @@ -55,6 +57,8 @@ public void onEnable() {
public void onDisable() {
if (ShutdownCommand.instance != null)
ShutdownCommand.instance.cancel();

CountdownCommand.stopAll();
}

public void loadCommodore(Command command) {
Expand All @@ -78,4 +82,20 @@ public void loadCommodore(Command command) {
public static String colorize(String string) {
return ChatColor.translateAlternateColorCodes('&', string);
}

public static int parseTime(String input) {
try {
if (input.endsWith("d")) {
return Integer.parseInt(input.replace("d", "")) * 86400;
} else if (input.endsWith("h")) {
return Integer.parseInt(input.replace("h", "")) * 3600;
} else if (input.endsWith("m")) {
return Integer.parseInt(input.replace("m", "")) * 60;
} else {
return Integer.parseInt(input);
}
} catch(NumberFormatException e) {
return -1;
}
}
}
12 changes: 10 additions & 2 deletions lib/src/main/resources/commodores/countdown.commodore
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
countdown {
length brigadier:integer {
message brigadier:string greedy_phrase;
start {
length brigadier:integer {
message brigadier:string greedy_phrase;
}
}

stop {
id brigadier:integer;
}

stopall;
}
4 changes: 2 additions & 2 deletions lib/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ api-version: "1.18"

commands:
countdown:
description: "Bắt đầu đếm ngược"
usage: /countdown <length> [message]
description: "Điều khiển đếm ngược"
usage: /countdown <start|stop|stopall>
permission: countdown.countdown
permission-message: Bạn không có quyền countdown.countdown
aliases:
Expand Down

0 comments on commit 55876c2

Please sign in to comment.