骰子指令 原始碼公開
骰子的隨機數產生方式與原始碼(公開以驗證結果公正)
Random random = new Random();
public Roll() {
super("Roll");
Bukkit.getPluginCommand("roll").setExecutor(this);
random.setSeed(System.currentTimeMillis());
}
public void onDisable() {
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;
ItemStack item = player.getInventory().getItemInMainHand();
if (item.getType() != Material.LAPIS_LAZULI) {
player.sendActionBar(Component.text("請先手持青金石 才能使用").color(NamedTextColor.DARK_RED));
return true;
}
int min = 1;
int max = 6;
int repeat = 1;
// 數值判斷
if (args.length != 0) {
if (args.length >= 2) {
try {
min = Integer.parseInt(args[0]);
max = Integer.parseInt(args[1]);
} catch (NumberFormatException e) {
player.sendActionBar(Component.text("最小值與最大值 需輸入整數數字才行")
.color(NamedTextColor.DARK_PURPLE));
return true;
}
if (args.length == 3) {
try {
repeat = Integer.parseInt(args[2]);
} catch (Exception e) {
player.sendActionBar(Component.text("次數需輸入整數數字(1-10)")
.color(NamedTextColor.DARK_PURPLE));
return true;
}
}
} else {
player.sendActionBar(Component.text("若要指定數值 請輸入 ")
.color(NamedTextColor.DARK_PURPLE).append(Component
.text("/roll <最小值> <最大值> [重複次數]").color(NamedTextColor.RED)));
return true;
}
}
if (min >= max) {
player.sendActionBar(Component.text("最大值需大於最小值").color(NamedTextColor.DARK_PURPLE));
return true;
}
if (min < 1 || max < 1) {
player.sendActionBar(
Component.text("最大值與最小值需>=1").color(NamedTextColor.DARK_PURPLE));
return true;
}
if (repeat < 1 || repeat > 10) {
player.sendActionBar(
Component.text("次數只能在1-10次").color(NamedTextColor.DARK_PURPLE));
return true;
}
if (item.getAmount() < repeat) {
player.sendActionBar(Component.text("青金石不足 需要 ").color(NamedTextColor.DARK_RED)
.append(Component.text(repeat + "個").color(NamedTextColor.YELLOW))
.append(Component.text(" 才能使用").color(NamedTextColor.DARK_RED)));
return true;
}
// 程序開始
item.setAmount(item.getAmount() - repeat);
List<Player> playerList = new ArrayList<Player>();
String playerListText = "";
for (Player other : Bukkit.getOnlinePlayers()) {
if (other.getWorld() == player.getWorld()) {
if (other.getLocation().distance(player.getLocation()) <= 20
&& other.getWorld() == player.getWorld()) {
playerList.add(other);
playerListText += other.getName() + " ";
}
}
}
Component numberText = Component.empty();
long sum = 0;
for (int i = 0; i < repeat; i++) {
Integer number = getRandom(min, max);
numberText = numberText.append(
Component.text(number.toString() + " ").color(NamedTextColor.YELLOW));
sum += number;
}
if (repeat > 1) {
numberText = numberText.append(Component.text("(" + String.valueOf(sum) + ")")
.color(NamedTextColor.LIGHT_PURPLE));
}
Component component = Component.empty()
.append(Component.text(player.getName()).color(NamedTextColor.GREEN))
.append(Component.text(" 骰出了 ").color(NamedTextColor.GOLD)).append(numberText);
Component hoverText = Component.empty()
.append(Component.text("數值 : ").color(NamedTextColor.AQUA)).append(numberText)
.append(Component.text("\n範圍 : ").color(NamedTextColor.GOLD))
.append(Component.text(min + "-" + max).color(NamedTextColor.RED))
.append(Component.text("\n對象 : ").color(NamedTextColor.GREEN))
.append(Component.text(playerListText).color(NamedTextColor.GRAY))
.append(Component.text("\n時間 : "
+ new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SS").format(new Date()))
.color(NamedTextColor.DARK_GRAY));
component = component.hoverEvent(hoverText);
for (Player p : playerList) {
p.sendMessage(component);
}
} else {
sender.sendActionBar(Component.text("只有玩家可使用此功能").color(NamedTextColor.RED));
}
return true;
}
int getRandom(int min, int max) {
return random.nextInt(max - min + 1) + min;
}