parent
2d65f54213
commit
c472ca57ca
@ -1,30 +1,31 @@
|
||||
package com.ultikits.plugins.commands;
|
||||
|
||||
import com.ultikits.plugins.BasicFunctions;
|
||||
import com.ultikits.ultitools.abstracts.AbstractCommendExecutor;
|
||||
import com.ultikits.ultitools.annotations.command.CmdExecutor;
|
||||
import com.ultikits.ultitools.annotations.command.CmdMapping;
|
||||
import com.ultikits.ultitools.annotations.command.CmdSender;
|
||||
import com.ultikits.ultitools.annotations.command.CmdTarget;
|
||||
import com.ultikits.ultitools.abstracts.AbstractPlayerCommandExecutor;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import static com.ultikits.ultitools.utils.MessageUtils.info;
|
||||
import static com.ultikits.ultitools.utils.MessageUtils.warning;
|
||||
|
||||
@CmdTarget(CmdTarget.CmdTargetType.PLAYER)
|
||||
@CmdExecutor(alias = {"fly"}, manualRegister = true, permission = "ultikits.tools.command.fly", description = "飞行功能")
|
||||
public class FlyCommands extends AbstractCommendExecutor {
|
||||
public class FlyCommands extends AbstractPlayerCommandExecutor {
|
||||
|
||||
@CmdMapping(format = "")
|
||||
public void fly(@CmdSender Player player) {
|
||||
@Override
|
||||
protected boolean onPlayerCommand(Command command, String[] strings, Player player) {
|
||||
if (!player.hasPermission("ultikits.tools.command.fly")) {
|
||||
player.sendMessage(warning(BasicFunctions.getInstance().i18n("你没有权限使用此指令!")));
|
||||
return true;
|
||||
}
|
||||
player.setAllowFlight(!player.getAllowFlight());
|
||||
player.sendMessage(info(player.getAllowFlight() ?
|
||||
BasicFunctions.getInstance().i18n("已打开飞行") :
|
||||
BasicFunctions.getInstance().i18n("已关闭飞行")));
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handleHelp(CommandSender sender) {
|
||||
protected void sendHelpMessage(CommandSender sender) {
|
||||
sender.sendMessage(info(BasicFunctions.getInstance().i18n("用法:/fly")));
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,46 +1,78 @@
|
||||
package com.ultikits.plugins.commands;
|
||||
|
||||
import com.ultikits.plugins.BasicFunctions;
|
||||
import com.ultikits.ultitools.abstracts.AbstractCommendExecutor;
|
||||
import com.ultikits.ultitools.annotations.command.CmdExecutor;
|
||||
import com.ultikits.ultitools.annotations.command.CmdMapping;
|
||||
import com.ultikits.ultitools.annotations.command.CmdSender;
|
||||
import com.ultikits.ultitools.annotations.command.CmdTarget;
|
||||
import com.ultikits.ultitools.abstracts.AbstractTabExecutor;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@CmdTarget(CmdTarget.CmdTargetType.PLAYER)
|
||||
@CmdExecutor(alias = {"gm"}, manualRegister = true, permission = "ultikits.tools.command.gm", description = "游戏模式切换功能")
|
||||
public class GMChangeCommand extends AbstractCommendExecutor {
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@CmdMapping(format = "0", permission = "ultikits.tools.command.gm.0")
|
||||
public void changeGameMode0(@CmdSender Player player, String mode) {
|
||||
player.setGameMode(GameMode.SURVIVAL);
|
||||
player.sendMessage(ChatColor.YELLOW + String.format(BasicFunctions.getInstance().i18n("你的游戏模式已设置为%s"), BasicFunctions.getInstance().i18n("生存模式")));
|
||||
}
|
||||
|
||||
@CmdMapping(format = "1", permission = "ultikits.tools.command.gm.1")
|
||||
public void changeGameMode1(@CmdSender Player player, String mode) {
|
||||
player.setGameMode(GameMode.CREATIVE);
|
||||
player.sendMessage(ChatColor.YELLOW + String.format(BasicFunctions.getInstance().i18n("你的游戏模式已设置为%s"), BasicFunctions.getInstance().i18n("创造模式")));
|
||||
}
|
||||
import static com.ultikits.ultitools.utils.MessageUtils.warning;
|
||||
|
||||
@CmdMapping(format = "2", permission = "ultikits.tools.command.gm.2")
|
||||
public void changeGameMode2(@CmdSender Player player, String mode) {
|
||||
player.setGameMode(GameMode.ADVENTURE);
|
||||
player.sendMessage(ChatColor.YELLOW + String.format(BasicFunctions.getInstance().i18n("你的游戏模式已设置为%s"), BasicFunctions.getInstance().i18n("冒险模式")));
|
||||
public class GMChangeCommand extends AbstractTabExecutor {
|
||||
@Override
|
||||
protected boolean onPlayerCommand(Command command, String[] strings, Player player) {
|
||||
if (player.hasPermission("ultikits.tools.command.gm.all")
|
||||
|| player.hasPermission("ultikits.tools.command.gm.0")
|
||||
|| player.hasPermission("ultikits.tools.command.gm.1")
|
||||
|| player.hasPermission("ultikits.tools.command.gm.2")
|
||||
|| player.hasPermission("ultikits.tools.command.gm.3")) {
|
||||
switch (strings[0]) {
|
||||
case "0":
|
||||
if (player.hasPermission("ultikits.tools.command.gm.all")
|
||||
|| player.hasPermission("ultikits.tools.command.gm.0")) {
|
||||
player.setGameMode(GameMode.SURVIVAL);
|
||||
player.sendMessage(ChatColor.YELLOW + String.format(BasicFunctions.getInstance().i18n("你的游戏模式已设置为%s"), BasicFunctions.getInstance().i18n("生存模式")));
|
||||
return true;
|
||||
}
|
||||
player.sendMessage(warning(BasicFunctions.getInstance().i18n("你没有权限使用此指令!")));
|
||||
return true;
|
||||
case "1":
|
||||
if (player.hasPermission("ultikits.tools.command.gm.all")
|
||||
|| player.hasPermission("ultikits.tools.command.gm.1")) {
|
||||
player.setGameMode(GameMode.CREATIVE);
|
||||
player.sendMessage(ChatColor.YELLOW + String.format(BasicFunctions.getInstance().i18n("你的游戏模式已设置为%s"), BasicFunctions.getInstance().i18n("创造模式")));
|
||||
return true;
|
||||
}
|
||||
player.sendMessage(warning(BasicFunctions.getInstance().i18n("你没有权限使用此指令!")));
|
||||
return true;
|
||||
case "2":
|
||||
if (player.hasPermission("ultikits.tools.command.gm.all")
|
||||
|| player.hasPermission("ultikits.tools.command.gm.2")) {
|
||||
player.setGameMode(GameMode.ADVENTURE);
|
||||
player.sendMessage(ChatColor.YELLOW + String.format(BasicFunctions.getInstance().i18n("你的游戏模式已设置为%s"), BasicFunctions.getInstance().i18n("冒险模式")));
|
||||
return true;
|
||||
}
|
||||
player.sendMessage(warning(BasicFunctions.getInstance().i18n("你没有权限使用此指令!")));
|
||||
return true;
|
||||
case "3":
|
||||
if (player.hasPermission("ultikits.tools.command.gm.all")
|
||||
|| player.hasPermission("ultikits.tools.command.gm.3")) {
|
||||
player.setGameMode(GameMode.SPECTATOR);
|
||||
player.sendMessage(ChatColor.YELLOW + String.format(BasicFunctions.getInstance().i18n("你的游戏模式已设置为%s"), BasicFunctions.getInstance().i18n("旁观模式")));
|
||||
return true;
|
||||
}
|
||||
player.sendMessage(warning(BasicFunctions.getInstance().i18n("你没有权限使用此指令!")));
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
player.sendMessage(warning(BasicFunctions.getInstance().i18n("你没有权限使用此指令!")));
|
||||
return true;
|
||||
}
|
||||
|
||||
@CmdMapping(format = "3", permission = "ultikits.tools.command.gm.3")
|
||||
public void changeGameMode3(@CmdSender Player player, String mode) {
|
||||
player.setGameMode(GameMode.SPECTATOR);
|
||||
player.sendMessage(ChatColor.YELLOW + String.format(BasicFunctions.getInstance().i18n("你的游戏模式已设置为%s"), BasicFunctions.getInstance().i18n("旁观模式")));
|
||||
@Override
|
||||
protected List<String> onPlayerTabComplete(Command command, String[] strings, Player player) {
|
||||
return Arrays.asList("0", "1", "2", "3");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handleHelp(CommandSender sender) {
|
||||
protected void sendHelpMessage(CommandSender sender) {
|
||||
sender.sendMessage(ChatColor.YELLOW + "/gm <0/1/2/3>");
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,28 +1,30 @@
|
||||
package com.ultikits.plugins.commands;
|
||||
|
||||
import com.ultikits.plugins.BasicFunctions;
|
||||
import com.ultikits.ultitools.abstracts.AbstractCommendExecutor;
|
||||
import com.ultikits.ultitools.annotations.command.CmdExecutor;
|
||||
import com.ultikits.ultitools.annotations.command.CmdMapping;
|
||||
import com.ultikits.ultitools.annotations.command.CmdSender;
|
||||
import com.ultikits.ultitools.annotations.command.CmdTarget;
|
||||
import com.ultikits.ultitools.abstracts.AbstractPlayerCommandExecutor;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@CmdTarget(CmdTarget.CmdTargetType.PLAYER)
|
||||
@CmdExecutor(alias = {"heal", "h"}, manualRegister = true, permission = "ultikits.tools.command.heal", description = "指令治愈功能")
|
||||
public class HealCommand extends AbstractCommendExecutor {
|
||||
import static com.ultikits.ultitools.utils.MessageUtils.warning;
|
||||
|
||||
@CmdMapping(format = "")
|
||||
public void heal(@CmdSender Player player) {
|
||||
player.setHealth(player.getMaxHealth());
|
||||
player.setFoodLevel(20);
|
||||
player.sendMessage(ChatColor.YELLOW + BasicFunctions.getInstance().i18n("你已被治愈!"));
|
||||
public class HealCommand extends AbstractPlayerCommandExecutor {
|
||||
|
||||
@Override
|
||||
protected boolean onPlayerCommand(Command command, String[] strings, Player player) {
|
||||
if (player.hasPermission("ultikits.tools.command.heal")) {
|
||||
player.setHealth(player.getMaxHealth());
|
||||
player.setFoodLevel(20);
|
||||
player.sendMessage(ChatColor.YELLOW + BasicFunctions.getInstance().i18n("你已被治愈!"));
|
||||
return true;
|
||||
}
|
||||
player.sendMessage(warning(BasicFunctions.getInstance().i18n("你没有权限使用此指令!")));
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handleHelp(CommandSender sender) {
|
||||
sender.sendMessage(BasicFunctions.getInstance().i18n("§c/heal §7治愈自己"));
|
||||
protected void sendHelpMessage(CommandSender sender) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,69 +0,0 @@
|
||||
package com.ultikits.plugins.commands;
|
||||
|
||||
import com.ultikits.plugins.BasicFunctions;
|
||||
import com.ultikits.ultitools.UltiTools;
|
||||
import com.ultikits.ultitools.abstracts.AbstractCommendExecutor;
|
||||
import com.ultikits.ultitools.annotations.command.CmdExecutor;
|
||||
import com.ultikits.ultitools.annotations.command.CmdMapping;
|
||||
import com.ultikits.ultitools.annotations.command.CmdSender;
|
||||
import com.ultikits.ultitools.annotations.command.CmdTarget;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static com.ultikits.ultitools.utils.MessageUtils.info;
|
||||
|
||||
@CmdTarget(CmdTarget.CmdTargetType.PLAYER)
|
||||
@CmdExecutor(alias = {"hide"}, manualRegister = true, permission = "ultikits.tools.command.hide", description = "隐身功能")
|
||||
public class HideCommands extends AbstractCommendExecutor {
|
||||
private static final List<UUID> hidePlayers = new ArrayList<>();
|
||||
|
||||
public static void removeHidePlayer(UUID uuid) {
|
||||
hidePlayers.remove(uuid);
|
||||
}
|
||||
|
||||
@CmdMapping(format = "")
|
||||
public void hide(@CmdSender Player player) {
|
||||
if (hidePlayers.contains(player.getUniqueId())) {
|
||||
hidePlayers.remove(player.getUniqueId());
|
||||
new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
for (Player p : Bukkit.getOnlinePlayers()) {
|
||||
if (p.getUniqueId().equals(player.getUniqueId())) {
|
||||
continue;
|
||||
}
|
||||
p.showPlayer(UltiTools.getInstance(), player);
|
||||
}
|
||||
}
|
||||
}.runTaskAsynchronously(UltiTools.getInstance());
|
||||
player.sendMessage(info(BasicFunctions.getInstance().i18n("你已退出隐身")));
|
||||
} else {
|
||||
hidePlayers.add(player.getUniqueId());
|
||||
new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
for (Player p : Bukkit.getOnlinePlayers()) {
|
||||
if (p.getUniqueId().equals(player.getUniqueId())) {
|
||||
continue;
|
||||
}
|
||||
p.hidePlayer(UltiTools.getInstance(), player);
|
||||
}
|
||||
}
|
||||
}.runTaskAsynchronously(UltiTools.getInstance());
|
||||
player.sendMessage(info(BasicFunctions.getInstance().i18n("你已进入隐身")));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handleHelp(CommandSender sender) {
|
||||
sender.sendMessage(info(BasicFunctions.getInstance().i18n("/hide - 开启/关闭隐身")));
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,152 +0,0 @@
|
||||
package com.ultikits.plugins.commands;
|
||||
|
||||
import com.ultikits.plugins.BasicFunctions;
|
||||
import com.ultikits.ultitools.abstracts.AbstractCommendExecutor;
|
||||
import com.ultikits.ultitools.annotations.command.*;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static com.ultikits.ultitools.utils.MessageUtils.coloredMsg;
|
||||
|
||||
@CmdTarget(CmdTarget.CmdTargetType.PLAYER)
|
||||
@CmdExecutor(alias = {"lore"}, manualRegister = true, permission = "ultikits.tools.command.lore", description = "物品Lore编辑功能")
|
||||
public class LoreCommands extends AbstractCommendExecutor {
|
||||
|
||||
@CmdMapping(format = "add <lore...>")
|
||||
public void addLore(@CmdSender Player player, @CmdParam("lore...") String[] lore) {
|
||||
ItemStack itemStack = player.getInventory().getItemInMainHand();
|
||||
if (itemStack.getType() == Material.AIR) {
|
||||
player.sendMessage(ChatColor.RED + BasicFunctions.getInstance().i18n("请手持物品来修改Lore"));
|
||||
return;
|
||||
}
|
||||
ItemMeta itemMeta = Objects.requireNonNull(itemStack.getItemMeta());
|
||||
List<String> loreList = itemMeta.getLore();
|
||||
if (loreList == null) {
|
||||
loreList = new ArrayList<>();
|
||||
}
|
||||
String loreToAdd = String.join(" ", lore);
|
||||
loreList.add(coloredMsg(loreToAdd));
|
||||
itemMeta.setLore(loreList);
|
||||
itemStack.setItemMeta(itemMeta);
|
||||
player.getInventory().setItemInMainHand(itemStack);
|
||||
player.sendMessage(ChatColor.GREEN + BasicFunctions.getInstance().i18n("物品Lore已修改"));
|
||||
}
|
||||
|
||||
@CmdMapping(format = "delete <position>")
|
||||
public void deleteLore(@CmdSender Player player, @CmdParam("position") int position) {
|
||||
ItemStack itemStack = player.getInventory().getItemInMainHand();
|
||||
if (itemStack.getType() == Material.AIR) {
|
||||
player.sendMessage(ChatColor.RED + BasicFunctions.getInstance().i18n("请手持物品来修改Lore"));
|
||||
return;
|
||||
}
|
||||
ItemMeta itemMeta = Objects.requireNonNull(itemStack.getItemMeta());
|
||||
List<String> loreList = itemMeta.getLore();
|
||||
if (loreList == null || loreList.isEmpty()) {
|
||||
player.sendMessage(ChatColor.RED + BasicFunctions.getInstance().i18n("物品Lore为空"));
|
||||
return;
|
||||
}
|
||||
if (position > loreList.size() || position < 1) {
|
||||
player.sendMessage(ChatColor.RED + BasicFunctions.getInstance().i18n("Lore行数超出范围"));
|
||||
return;
|
||||
}
|
||||
loreList.remove(position - 1);
|
||||
itemMeta.setLore(loreList);
|
||||
itemStack.setItemMeta(itemMeta);
|
||||
player.getInventory().setItemInMainHand(itemStack);
|
||||
player.sendMessage(ChatColor.GREEN + BasicFunctions.getInstance().i18n("物品Lore已修改"));
|
||||
}
|
||||
|
||||
@CmdMapping(format = "edit <position> <lore...>")
|
||||
public void editLore(@CmdSender Player player, @CmdParam("position") int position, @CmdParam("lore...") String[] lore) {
|
||||
ItemStack itemStack = player.getInventory().getItemInMainHand();
|
||||
if (itemStack.getType() == Material.AIR) {
|
||||
player.sendMessage(ChatColor.RED + BasicFunctions.getInstance().i18n("请手持物品来修改Lore"));
|
||||
return;
|
||||
}
|
||||
ItemMeta itemMeta = Objects.requireNonNull(itemStack.getItemMeta());
|
||||
List<String> loreList = itemMeta.getLore();
|
||||
if (loreList == null || loreList.isEmpty()) {
|
||||
player.sendMessage(ChatColor.RED + BasicFunctions.getInstance().i18n("物品Lore为空"));
|
||||
return;
|
||||
}
|
||||
if (position > loreList.size() || position < 1) {
|
||||
player.sendMessage(ChatColor.RED + BasicFunctions.getInstance().i18n("Lore行数超出范围"));
|
||||
return;
|
||||
}
|
||||
String loreToEdit = String.join(" ", lore);
|
||||
if (position == 0) {
|
||||
itemMeta.setDisplayName(coloredMsg(loreToEdit));
|
||||
} else {
|
||||
loreList.set(position - 1, coloredMsg(loreToEdit));
|
||||
itemMeta.setLore(loreList);
|
||||
}
|
||||
itemStack.setItemMeta(itemMeta);
|
||||
player.getInventory().setItemInMainHand(itemStack);
|
||||
player.sendMessage(ChatColor.GREEN + BasicFunctions.getInstance().i18n("物品Lore已修改"));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<String> suggest(Player player, String[] strings) {
|
||||
switch (strings.length) {
|
||||
case 1:
|
||||
return Arrays.asList(
|
||||
"add",
|
||||
"delete",
|
||||
"edit"
|
||||
);
|
||||
case 2:
|
||||
case 3:
|
||||
if (strings[0].equals("add")) {
|
||||
return Collections.singletonList(BasicFunctions.getInstance().i18n("<内容>"));
|
||||
}
|
||||
ItemStack itemStack = player.getInventory().getItemInMainHand();
|
||||
if (itemStack.getType() == Material.AIR) {
|
||||
player.sendMessage(ChatColor.RED + BasicFunctions.getInstance().i18n("请手持物品来修改Lore"));
|
||||
return Collections.emptyList();
|
||||
}
|
||||
ItemMeta itemMeta = Objects.requireNonNull(itemStack.getItemMeta());
|
||||
List<String> lore = itemMeta.getLore();
|
||||
if (lore == null || lore.isEmpty()) {
|
||||
player.sendMessage(ChatColor.RED + BasicFunctions.getInstance().i18n("物品Lore为空"));
|
||||
return Collections.emptyList();
|
||||
}
|
||||
List<String> list = new ArrayList<>();
|
||||
for (int i = 1; i <= lore.size(); i++) {
|
||||
list.add(i + ". " + lore.get(i - 1));
|
||||
}
|
||||
|
||||
if ((strings[0].equals("edit") || strings[0].equals("delete")) && strings.length == 2) {
|
||||
return list;
|
||||
} else if (strings[0].equals("edit")) {
|
||||
int position;
|
||||
try {
|
||||
position = Integer.parseInt(strings[1]);
|
||||
} catch (NumberFormatException e) {
|
||||
return Collections.singletonList(BasicFunctions.getInstance().i18n("<- 请输入对应的lore行数"));
|
||||
}
|
||||
if (position - 1 < lore.size() && position - 1 >= 0) {
|
||||
return Collections.singletonList(lore.get(position - 1));
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handleHelp(CommandSender sender) {
|
||||
sender.sendMessage(ChatColor.RED + "lore add <内容>" + ChatColor.GRAY + " - " + BasicFunctions.getInstance().i18n("添加Lore"));
|
||||
sender.sendMessage(ChatColor.RED + "lore delete <行数>" + ChatColor.GRAY + " - " + BasicFunctions.getInstance().i18n("删除Lore"));
|
||||
sender.sendMessage(ChatColor.RED + "lore edit <行数> <内容>" + ChatColor.GRAY + " - " + BasicFunctions.getInstance().i18n("编辑Lore"));
|
||||
}
|
||||
}
|
||||
@ -1,38 +0,0 @@
|
||||
package com.ultikits.plugins.commands;
|
||||
|
||||
import com.ultikits.plugins.BasicFunctions;
|
||||
import com.ultikits.ultitools.abstracts.AbstractCommendExecutor;
|
||||
import com.ultikits.ultitools.annotations.command.CmdExecutor;
|
||||
import com.ultikits.ultitools.annotations.command.CmdMapping;
|
||||
import com.ultikits.ultitools.annotations.command.CmdSender;
|
||||
import com.ultikits.ultitools.annotations.command.CmdTarget;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import static com.ultikits.ultitools.utils.MessageUtils.error;
|
||||
import static com.ultikits.ultitools.utils.MessageUtils.info;
|
||||
|
||||
@CmdTarget(CmdTarget.CmdTargetType.PLAYER)
|
||||
@CmdExecutor(alias = {"setspawn"}, manualRegister = true, permission = "ultikits.tools.command.setspawn", description = "重生点功能")
|
||||
public class SetSpawnCommands extends AbstractCommendExecutor {
|
||||
|
||||
@CmdMapping(format = "")
|
||||
public void setSpawn(@CmdSender Player player) {
|
||||
Location location = player.getLocation();
|
||||
World world = location.getWorld();
|
||||
if (world == null) {
|
||||
player.sendMessage(error(BasicFunctions.getInstance().i18n("未找到世界!")));
|
||||
return;
|
||||
}
|
||||
world.setSpawnLocation(location);
|
||||
player.sendMessage(info(BasicFunctions.getInstance().i18n("已重设世界重生点!")));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handleHelp(CommandSender sender) {
|
||||
sender.sendMessage(ChatColor.RED + "setspawn" + ChatColor.GRAY + " - " + BasicFunctions.getInstance().i18n("设置世界重生点"));
|
||||
}
|
||||
}
|
||||
@ -1,37 +0,0 @@
|
||||
package com.ultikits.plugins.commands;
|
||||
|
||||
import com.ultikits.plugins.BasicFunctions;
|
||||
import com.ultikits.ultitools.abstracts.AbstractCommendExecutor;
|
||||
import com.ultikits.ultitools.annotations.command.CmdExecutor;
|
||||
import com.ultikits.ultitools.annotations.command.CmdMapping;
|
||||
import com.ultikits.ultitools.annotations.command.CmdSender;
|
||||
import com.ultikits.ultitools.annotations.command.CmdTarget;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import static com.ultikits.ultitools.utils.MessageUtils.error;
|
||||
|
||||
@CmdTarget(CmdTarget.CmdTargetType.PLAYER)
|
||||
@CmdExecutor(alias = {"spawn"}, manualRegister = true, permission = "ultikits.tools.command.spawn", description = "重生点功能")
|
||||
public class SpawnCommands extends AbstractCommendExecutor {
|
||||
|
||||
@CmdMapping(format = "")
|
||||
public void spawn(@CmdSender Player player) {
|
||||
if (player.getLocation().getWorld() == null) {
|
||||
player.sendMessage(error(BasicFunctions.getInstance().i18n("未找到世界!")));
|
||||
return;
|
||||
}
|
||||
if (player.getLocation().getWorld().getSpawnLocation().equals(new Location(player.getLocation().getWorld(), 0, 0, 0, 0, 0))) {
|
||||
player.sendMessage(error(BasicFunctions.getInstance().i18n("这个世界没有重生点!")));
|
||||
return;
|
||||
}
|
||||
player.teleport(player.getLocation().getWorld().getSpawnLocation());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handleHelp(CommandSender sender) {
|
||||
sender.sendMessage(ChatColor.RED + "spawn" + ChatColor.GRAY + " - " + BasicFunctions.getInstance().i18n("传送到世界重生点"));
|
||||
}
|
||||
}
|
||||
@ -1,96 +0,0 @@
|
||||
package com.ultikits.plugins.commands;
|
||||
|
||||
import com.ultikits.plugins.BasicFunctions;
|
||||
import com.ultikits.plugins.data.WarpData;
|
||||
import com.ultikits.plugins.guis.WarpGui;
|
||||
import com.ultikits.plugins.services.WarpService;
|
||||
import com.ultikits.ultitools.abstracts.AbstractCommendExecutor;
|
||||
import com.ultikits.ultitools.annotations.command.*;
|
||||
import com.ultikits.ultitools.services.TeleportService;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@CmdTarget(CmdTarget.CmdTargetType.PLAYER)
|
||||
@CmdExecutor(alias = {"warp"}, manualRegister = true, permission = "ultikits.tools.command.warp", description = "传送点功能")
|
||||
public class WarpCommands extends AbstractCommendExecutor {
|
||||
@Autowired
|
||||
private WarpService warpService;
|
||||
|
||||
@CmdMapping(format = "list")
|
||||
public void listWarps(@CmdSender Player player) {
|
||||
WarpGui warpGui = new WarpGui(player);
|
||||
warpGui.open();
|
||||
}
|
||||
|
||||
@CmdMapping(format = "tp <name>")
|
||||
public void tpWarp(@CmdSender Player player, @CmdParam("name") String name) {
|
||||
Location warpLocation = warpService.getWarpLocation(name);
|
||||
player.sendMessage(String.format(BasicFunctions.getInstance().i18n("§a已传送至传送点 %s"), name));
|
||||
TeleportService teleportService = BasicFunctions.getInstance().getContext().getBean(TeleportService.class);
|
||||
teleportService.delayTeleport(player, warpLocation, 3);
|
||||
}
|
||||
|
||||
@CmdMapping(format = "add <name>", requireOp = true)
|
||||
public void addWarp(@CmdSender Player player, @CmdParam("name") String name) {
|
||||
warpService.addWarp(name, player.getLocation());
|
||||
player.sendMessage(String.format(BasicFunctions.getInstance().i18n("§a已添加传送点 %s"), name));
|
||||
}
|
||||
|
||||
@CmdMapping(format = "remove <name>", requireOp = true)
|
||||
public void removeWarp(@CmdSender Player player, @CmdParam("name") String name) {
|
||||
warpService.removeWarp(name);
|
||||
player.sendMessage(String.format(BasicFunctions.getInstance().i18n("§a已删除传送点 %s"), name));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<String> suggest(Player player, String[] strings) {
|
||||
ArrayList<String> list = new ArrayList<>();
|
||||
switch (strings.length) {
|
||||
case 1:
|
||||
list.add("list");
|
||||
list.add("tp");
|
||||
if (player.isOp()) {
|
||||
list.add("add");
|
||||
list.add("remove");
|
||||
}
|
||||
return list;
|
||||
case 2:
|
||||
switch (strings[0]) {
|
||||
case "remove":
|
||||
if (player.isOp()) {
|
||||
List<WarpData> allWarps = warpService.getAllWarps();
|
||||
for (WarpData warpData : allWarps) {
|
||||
list.add(warpData.getName());
|
||||
}
|
||||
return list;
|
||||
}
|
||||
return list;
|
||||
case "tp":
|
||||
List<WarpData> allWarps = warpService.getAllWarps();
|
||||
for (WarpData warpData : allWarps) {
|
||||
list.add(warpData.getName());
|
||||
}
|
||||
return list;
|
||||
default:
|
||||
return list;
|
||||
}
|
||||
default:
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handleHelp(CommandSender sender) {
|
||||
sender.sendMessage(BasicFunctions.getInstance().i18n("§a/warp list §7- §e查看所有传送点"));
|
||||
sender.sendMessage(BasicFunctions.getInstance().i18n("§a/warp tp <name> §7- §e传送至传送点"));
|
||||
if (sender.isOp()) {
|
||||
sender.sendMessage(BasicFunctions.getInstance().i18n("§a/warp add <name> §7- §e添加传送点"));
|
||||
sender.sendMessage(BasicFunctions.getInstance().i18n("§a/warp remove <name> §7- §e删除传送点"));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,41 +0,0 @@
|
||||
package com.ultikits.plugins.config;
|
||||
|
||||
import com.ultikits.ultitools.abstracts.AbstractConfigEntity;
|
||||
import com.ultikits.ultitools.annotations.ConfigEntity;
|
||||
import com.ultikits.ultitools.annotations.ConfigEntry;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@ConfigEntity("config/death.yml")
|
||||
public class DeathPunishmentConfig extends AbstractConfigEntity {
|
||||
@ConfigEntry(path = "enable_item_drop", comment = "是否开启物品掉落")
|
||||
private boolean enableItemDrop = false;
|
||||
@ConfigEntry(path = "enable_money_drop", comment = "是否开启金币掉落")
|
||||
private boolean enableMoneyDrop = true;
|
||||
@ConfigEntry(path = "enable_punish_commands", comment = "是否开启死亡执行后台命令")
|
||||
private boolean enablePunishCommands = true;
|
||||
@ConfigEntry(path = "money_dropped_ondeath", comment = "死亡后掉落的金币数")
|
||||
private int moneyDropOnDeath = 100;
|
||||
@ConfigEntry(path = "item_dropped_ondeath", comment = "死亡后随机掉落的物品数")
|
||||
private int itemDropOnDeath = 3;
|
||||
@ConfigEntry(path = "punish_commands", comment = "死亡后后台执行的指令({PLAYER}指代玩家名占位符)")
|
||||
private List<String> punishCommands = Collections.emptyList();
|
||||
@ConfigEntry(path = "worlds_enabled_item_drop", comment = "开启死亡随机物品掉落的世界")
|
||||
private List<String> worldEnabledItemDrop = Arrays.asList("world", "world_nether", "world_the_end");
|
||||
@ConfigEntry(path = "worlds_enabled_money_drop", comment = "开启死亡后金币掉落的世界")
|
||||
private List<String> worldEnabledMoneyDrop = Arrays.asList("world", "world_nether", "world_the_end");
|
||||
@ConfigEntry(path = "worlds_enabled_punish_commands", comment = "开启死亡后执行后台指令的世界")
|
||||
private List<String> worldEnabledPunishCommands = Arrays.asList("world", "world_nether", "world_the_end");
|
||||
@ConfigEntry(path = "item_drop_whitelist", comment = "物品掉落白名单")
|
||||
private List<String> itemDropWhiteList = Collections.emptyList();
|
||||
|
||||
public DeathPunishmentConfig(String configFilePath) {
|
||||
super(configFilePath);
|
||||
}
|
||||
}
|
||||
@ -1,25 +0,0 @@
|
||||
package com.ultikits.plugins.config;
|
||||
|
||||
import com.ultikits.ultitools.abstracts.AbstractConfigEntity;
|
||||
import com.ultikits.ultitools.annotations.ConfigEntity;
|
||||
import com.ultikits.ultitools.annotations.ConfigEntry;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@ConfigEntity("config/title.yml")
|
||||
public class PlayerNameTagConfig extends AbstractConfigEntity {
|
||||
@ConfigEntry(path = "prefix", comment = "玩家名字前缀")
|
||||
private String prefix = "&e[&dLv.%player_level%&e]";
|
||||
@ConfigEntry(path = "suffix", comment = "玩家名字后缀")
|
||||
private String suffix = "&e[&a%player_health_rounded%&e/&a%player_max_health_rounded%&e]";
|
||||
@ConfigEntry(path = "updateInterval", comment = "更新间隔")
|
||||
private int updateInterval = 20;
|
||||
@ConfigEntry(path = "enableNameTagEdit", comment = "是否启用NameTagEdit支持")
|
||||
private boolean enableNameTagEdit = false;
|
||||
|
||||
public PlayerNameTagConfig(String configFilePath) {
|
||||
super(configFilePath);
|
||||
}
|
||||
}
|
||||
@ -1,23 +0,0 @@
|
||||
package com.ultikits.plugins.config;
|
||||
|
||||
import com.ultikits.ultitools.abstracts.AbstractConfigEntity;
|
||||
import com.ultikits.ultitools.annotations.ConfigEntity;
|
||||
import com.ultikits.ultitools.annotations.ConfigEntry;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@ConfigEntity("config/warp.yml")
|
||||
public class WarpConfig extends AbstractConfigEntity {
|
||||
@ConfigEntry(path = "enable_blue_map", comment = "是否启用BlueMap")
|
||||
private boolean enableBlueMap = false;
|
||||
@ConfigEntry(path = "blue_map_warp_max_distance", comment = "传送点在BlueMap中最远显示距离")
|
||||
private int maxDistance = 1000;
|
||||
@ConfigEntry(path = "blue_map_marker_set", comment = "传送点在BlueMap中的标记集名称")
|
||||
private String markerSet = "UltiTools Warps";
|
||||
|
||||
public WarpConfig(String configFilePath) {
|
||||
super(configFilePath);
|
||||
}
|
||||
}
|
||||
@ -1,23 +0,0 @@
|
||||
package com.ultikits.plugins.data;
|
||||
|
||||
import com.ultikits.ultitools.abstracts.AbstractDataEntity;
|
||||
import com.ultikits.ultitools.annotations.Column;
|
||||
import com.ultikits.ultitools.annotations.Table;
|
||||
import com.ultikits.ultitools.entities.common.WorldLocation;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import org.bukkit.Location;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@Data
|
||||
@Table("warp_table")
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
public class WarpData extends AbstractDataEntity {
|
||||
@Column("id")
|
||||
private Long id = new Date().getTime();
|
||||
@Column("name")
|
||||
private String name;
|
||||
@Column("location")
|
||||
private WorldLocation location;
|
||||
}
|
||||
@ -1,52 +0,0 @@
|
||||
package com.ultikits.plugins.guis;
|
||||
|
||||
import com.ultikits.plugins.BasicFunctions;
|
||||
import com.ultikits.plugins.data.WarpData;
|
||||
import com.ultikits.plugins.services.WarpService;
|
||||
import com.ultikits.ultitools.UltiTools;
|
||||
import com.ultikits.ultitools.abstracts.guis.PagingPage;
|
||||
import mc.obliviate.inventory.Icon;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import net.kyori.adventure.text.format.TextColor;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class WarpGui extends PagingPage {
|
||||
private final WarpService warpService = new WarpService();
|
||||
|
||||
public WarpGui(Player player) {
|
||||
super(
|
||||
player,
|
||||
"Warp-list",
|
||||
Component.text(BasicFunctions.getInstance().i18n("传送点列表"))
|
||||
.color(TextColor.color(0xFF00A6)),
|
||||
3
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Icon> setAllItems() {
|
||||
List<Icon> icons = new ArrayList<>();
|
||||
List<WarpData> allWarps = warpService.getAllWarps();
|
||||
for (WarpData warpData : allWarps) {
|
||||
Location location = WarpService.toLocation(warpData.getLocation());
|
||||
Icon icon = new Icon(UltiTools.getInstance().getVersionWrapper().getEndEye());
|
||||
TextComponent textComponent = Component.text(warpData.getName()).color(TextColor.color(0xFF00A6));
|
||||
icon.toComp().setName(textComponent);
|
||||
String world = String.format(ChatColor.YELLOW + BasicFunctions.getInstance().i18n("所在世界 %s"), location.getWorld().getName());
|
||||
String xyz = String.format(ChatColor.GRAY + "X: %.2f Y: %.2f Z: %.2f", location.getX(), location.getY(), location.getZ());
|
||||
icon.setLore(world, xyz);
|
||||
icon.onClick((e) -> {
|
||||
player.performCommand("warp tp " + warpData.getName());
|
||||
player.closeInventory();
|
||||
});
|
||||
icons.add(icon);
|
||||
}
|
||||
return icons;
|
||||
}
|
||||
}
|
||||
@ -1,45 +0,0 @@
|
||||
package com.ultikits.plugins.listeners;
|
||||
|
||||
import com.ultikits.plugins.tasks.DeathPunishmentTask;
|
||||
import com.ultikits.ultitools.annotations.EventListener;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.entity.EntityDamageEvent;
|
||||
import org.bukkit.event.entity.PlayerDeathEvent;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author qianmo, wisdomme
|
||||
*/
|
||||
@EventListener(manualRegister = true)
|
||||
public class DeathListener implements Listener {
|
||||
private static final List<Player> list = new ArrayList<>();
|
||||
@Autowired
|
||||
private DeathPunishmentTask deathPunishmentTask;
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerDeath(PlayerDeathEvent event) {
|
||||
Player player = event.getEntity();
|
||||
if (list.contains(player)) {
|
||||
list.remove(player);
|
||||
deathPunishmentTask.addPlayerToQueue(player);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onEntityDamage(EntityDamageEvent event) {
|
||||
if (!(event.getEntity() instanceof Player)) {
|
||||
return;
|
||||
}
|
||||
Player player = (Player) event.getEntity();
|
||||
|
||||
if (player.getHealth() > event.getFinalDamage()) {
|
||||
return;
|
||||
}
|
||||
list.add(player);
|
||||
}
|
||||
}
|
||||
@ -1,15 +0,0 @@
|
||||
package com.ultikits.plugins.listeners;
|
||||
|
||||
import com.ultikits.plugins.commands.HideCommands;
|
||||
import com.ultikits.ultitools.annotations.EventListener;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
|
||||
@EventListener(manualRegister = true)
|
||||
public class HideListener implements Listener {
|
||||
@EventHandler
|
||||
public void onPlayerQuit(PlayerQuitEvent event) {
|
||||
HideCommands.removeHidePlayer(event.getPlayer().getUniqueId());
|
||||
}
|
||||
}
|
||||
@ -1,32 +0,0 @@
|
||||
package com.ultikits.plugins.listeners;
|
||||
|
||||
import com.nametagedit.plugin.NametagEdit;
|
||||
import com.ultikits.plugins.BasicFunctions;
|
||||
import com.ultikits.plugins.config.PlayerNameTagConfig;
|
||||
import com.ultikits.plugins.utils.TitlesUtils;
|
||||
import com.ultikits.ultitools.annotations.EventListener;
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
|
||||
import static com.ultikits.ultitools.utils.MessageUtils.coloredMsg;
|
||||
|
||||
@EventListener(manualRegister = true)
|
||||
public class PlayerNameTagListener implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerJoin(PlayerJoinEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
PlayerNameTagConfig nameTagConfig = BasicFunctions.getInstance().getConfig(PlayerNameTagConfig.class);
|
||||
String prefix = coloredMsg(PlaceholderAPI.setPlaceholders(player, nameTagConfig.getPrefix()));
|
||||
String suffix = coloredMsg(PlaceholderAPI.setPlaceholders(player, nameTagConfig.getSuffix()));
|
||||
if (nameTagConfig.isEnableNameTagEdit()) {
|
||||
NametagEdit.getApi().updatePlayerNametag(player.getName(), prefix, suffix);
|
||||
} else {
|
||||
TitlesUtils.setPrefixSuffix(player, prefix, suffix);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,55 +0,0 @@
|
||||
package com.ultikits.plugins.services;
|
||||
|
||||
import com.ultikits.ultitools.UltiTools;
|
||||
import net.milkbowl.vault.economy.Economy;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author qianmo, wisdomme
|
||||
*/
|
||||
@Service
|
||||
public class DeathPunishService {
|
||||
public void takeMoney(Player player, int money) {
|
||||
Economy economy = UltiTools.getInstance().getEconomy();
|
||||
economy.withdrawPlayer(player, Math.min(economy.getBalance(player), money));
|
||||
}
|
||||
|
||||
public void execCommand(List<String> cmd, String playerName) {
|
||||
for (String s : cmd) {
|
||||
Bukkit.getServer().dispatchCommand(
|
||||
Bukkit.getServer().getConsoleSender(), s.replace("{PLAYER}", playerName)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public void takeItem(Player player, int drop, List<String> whitelist) {
|
||||
Inventory inventory = player.getInventory();
|
||||
List<Integer> inventorySlot = new ArrayList<>();
|
||||
for (int i = 0; i < 45; i++) {
|
||||
ItemStack item = inventory.getItem(i);
|
||||
if (item != null && !whitelist.contains(item.getType().toString())) {
|
||||
inventorySlot.add(i);
|
||||
}
|
||||
}
|
||||
Collections.shuffle(inventorySlot);
|
||||
List<Integer> ints = new ArrayList<>();
|
||||
for (int i = 0; i < drop; i++) {
|
||||
if (i >= inventorySlot.size()) break;
|
||||
ints.add(inventorySlot.get(i));
|
||||
}
|
||||
for (Integer slot : ints) {
|
||||
ItemStack itemStack = inventory.getItem(slot);
|
||||
if (itemStack == null) continue;
|
||||
itemStack.setAmount(itemStack.getAmount() - 1);
|
||||
inventory.setItem(slot, itemStack);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,117 +0,0 @@
|
||||
package com.ultikits.plugins.services;
|
||||
|
||||
import com.ultikits.plugins.BasicFunctions;
|
||||
import com.ultikits.plugins.config.WarpConfig;
|
||||
import com.ultikits.plugins.data.WarpData;
|
||||
import com.ultikits.ultitools.entities.WhereCondition;
|
||||
import com.ultikits.ultitools.entities.common.WorldLocation;
|
||||
import de.bluecolored.bluemap.api.BlueMapAPI;
|
||||
import de.bluecolored.bluemap.api.BlueMapMap;
|
||||
import de.bluecolored.bluemap.api.markers.MarkerSet;
|
||||
import de.bluecolored.bluemap.api.markers.POIMarker;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static com.ultikits.ultitools.abstracts.UltiToolsPlugin.getConfigManager;
|
||||
|
||||
@Service
|
||||
public class WarpService {
|
||||
|
||||
public static Location toLocation(WorldLocation worldLocation) {
|
||||
return new Location(Bukkit.getWorld(worldLocation.getWorld()), worldLocation.getX(), worldLocation.getY(), worldLocation.getZ(), worldLocation.getYaw(), worldLocation.getPitch());
|
||||
}
|
||||
|
||||
public void addWarp(String name, Location location) {
|
||||
WarpData warpData = new WarpData();
|
||||
warpData.setName(name);
|
||||
WorldLocation worldLocation = new WorldLocation(location);
|
||||
warpData.setLocation(worldLocation);
|
||||
BasicFunctions.getInstance().getDataOperator(WarpData.class).insert(warpData);
|
||||
addWarpToBlueMap(warpData);
|
||||
}
|
||||
|
||||
public void removeWarp(String name) {
|
||||
List<WarpData> warpData = BasicFunctions.getInstance().getDataOperator(WarpData.class).getAll(
|
||||
WhereCondition.builder().column("name").value(name).build()
|
||||
);
|
||||
if (warpData.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
BasicFunctions.getInstance().getDataOperator(WarpData.class).del(
|
||||
WhereCondition.builder().column("name").value(name).build()
|
||||
);
|
||||
removeWarpFromBlueMap(warpData.get(0));
|
||||
}
|
||||
|
||||
public Location getWarpLocation(String name) {
|
||||
List<WarpData> warpData = BasicFunctions.getInstance().getDataOperator(WarpData.class).getAll(
|
||||
WhereCondition.builder().column("name").value(name).build()
|
||||
);
|
||||
if (warpData.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return toLocation(warpData.get(0).getLocation());
|
||||
}
|
||||
|
||||
public List<WarpData> getAllWarps() {
|
||||
return BasicFunctions.getInstance().getDataOperator(WarpData.class).getAll();
|
||||
}
|
||||
|
||||
public void initBlueMap(){
|
||||
WarpConfig warpConfig = getConfigManager().getConfigEntity(BasicFunctions.getInstance(), WarpConfig.class);
|
||||
if (!warpConfig.isEnableBlueMap()) {
|
||||
return;
|
||||
}
|
||||
BlueMapAPI.getInstance().ifPresent(api -> {
|
||||
api.getWorlds().forEach(world -> {
|
||||
for (BlueMapMap map : world.getMaps()) {
|
||||
MarkerSet markerSet = MarkerSet.builder()
|
||||
.label(warpConfig.getMarkerSet())
|
||||
.build();
|
||||
map.getMarkerSets().put(warpConfig.getMarkerSet(), markerSet);
|
||||
}
|
||||
});
|
||||
});
|
||||
for (WarpData warpData : getAllWarps()) {
|
||||
addWarpToBlueMap(warpData);
|
||||
}
|
||||
}
|
||||
|
||||
public void addWarpToBlueMap(WarpData warpData) {
|
||||
WarpConfig warpConfig = getConfigManager().getConfigEntity(BasicFunctions.getInstance(), WarpConfig.class);
|
||||
if (!warpConfig.isEnableBlueMap()) {
|
||||
return;
|
||||
}
|
||||
WorldLocation location = warpData.getLocation();
|
||||
POIMarker marker = POIMarker.builder()
|
||||
.label(warpData.getName())
|
||||
.position(location.getX(), location.getY(), location.getZ())
|
||||
.maxDistance(warpConfig.getMaxDistance())
|
||||
.build();
|
||||
|
||||
BlueMapAPI.getInstance().flatMap(api -> api.getWorld(location.getWorld())).ifPresent(world -> {
|
||||
for (BlueMapMap map : world.getMaps()) {
|
||||
MarkerSet markerSet = map.getMarkerSets().get(warpConfig.getMarkerSet());
|
||||
markerSet.getMarkers().put(String.valueOf(warpData.getId()), marker);
|
||||
map.getMarkerSets().put(warpConfig.getMarkerSet(), markerSet);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void removeWarpFromBlueMap(WarpData warpData) {
|
||||
WarpConfig warpConfig = getConfigManager().getConfigEntity(BasicFunctions.getInstance(), WarpConfig.class);
|
||||
if (!warpConfig.isEnableBlueMap()) {
|
||||
return;
|
||||
}
|
||||
BlueMapAPI.getInstance().flatMap(api -> api.getWorld(warpData.getLocation().getWorld())).ifPresent(world -> {
|
||||
for (BlueMapMap map : world.getMaps()) {
|
||||
MarkerSet markerSet = map.getMarkerSets().get(warpConfig.getMarkerSet());
|
||||
markerSet.remove(String.valueOf(warpData.getId()));
|
||||
// map.getMarkerSets().put(warpConfig.getMarkerSet(), markerSet);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -1,30 +0,0 @@
|
||||
package com.ultikits.plugins.tasks;
|
||||
|
||||
import com.nametagedit.plugin.NametagEdit;
|
||||
import com.ultikits.plugins.BasicFunctions;
|
||||
import com.ultikits.plugins.config.PlayerNameTagConfig;
|
||||
import com.ultikits.plugins.utils.TitlesUtils;
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
import static com.ultikits.ultitools.utils.MessageUtils.coloredMsg;
|
||||
|
||||
public class NamePrefixSuffixTask extends BukkitRunnable {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
PlayerNameTagConfig nameTagConfig = BasicFunctions.getInstance().getConfig(PlayerNameTagConfig.class);
|
||||
for (Player player : Bukkit.getOnlinePlayers()) {
|
||||
String prefix = coloredMsg(PlaceholderAPI.setPlaceholders(player, nameTagConfig.getPrefix()));
|
||||
String suffix = coloredMsg(PlaceholderAPI.setPlaceholders(player, nameTagConfig.getSuffix()));
|
||||
if (nameTagConfig.isEnableNameTagEdit()){
|
||||
NametagEdit.getApi().updatePlayerNametag(player.getName(), prefix, suffix);
|
||||
}else {
|
||||
TitlesUtils.setPrefixSuffix(player, prefix, suffix);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,49 +0,0 @@
|
||||
package com.ultikits.plugins.utils;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.scoreboard.Scoreboard;
|
||||
import org.bukkit.scoreboard.Team;
|
||||
|
||||
public class TitlesUtils {
|
||||
|
||||
public static void setPrefixSuffix(Player player, String prefix, String suffix) {
|
||||
for (Player online : Bukkit.getOnlinePlayers()) {
|
||||
if (!online.canSee(player)) {
|
||||
continue;
|
||||
}
|
||||
Scoreboard scoreboard = online.getScoreboard();
|
||||
if (scoreboard == Bukkit.getScoreboardManager().getMainScoreboard()) {
|
||||
scoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
|
||||
}
|
||||
Team team = scoreboard.getTeam(player.getName());
|
||||
if (team == null) {
|
||||
team = scoreboard.registerNewTeam(player.getName());
|
||||
}
|
||||
team.setPrefix(prefix);
|
||||
team.setSuffix(suffix);
|
||||
team.addEntry(player.getName());
|
||||
online.setScoreboard(scoreboard);
|
||||
}
|
||||
for (Player online : Bukkit.getOnlinePlayers()) {
|
||||
if (!online.canSee(player)) {
|
||||
continue;
|
||||
}
|
||||
if (online.getUniqueId().equals(player.getUniqueId()))
|
||||
continue;
|
||||
Scoreboard scoreboard = player.getScoreboard();
|
||||
if (scoreboard == Bukkit.getScoreboardManager().getMainScoreboard()) {
|
||||
scoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
|
||||
}
|
||||
Team team = scoreboard.getTeam(online.getName());
|
||||
if (team == null) {
|
||||
team = scoreboard.registerNewTeam(online.getName());
|
||||
}
|
||||
|
||||
team.setPrefix(player.getScoreboard().getTeam(online.getName()).getPrefix());
|
||||
team.setSuffix(player.getScoreboard().getTeam(online.getName()).getSuffix());
|
||||
team.addEntry(online.getName());
|
||||
player.setScoreboard(scoreboard);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,38 +0,0 @@
|
||||
# 是否开启物品掉落
|
||||
enable_item_drop: false
|
||||
|
||||
# 是否开启金币掉落
|
||||
enable_money_drop: true
|
||||
|
||||
# 是否开启死亡执行后台命令
|
||||
enable_punish_commands: true
|
||||
|
||||
# 死亡后掉落的金币数
|
||||
money_dropped_ondeath: 100
|
||||
|
||||
# 死亡后随机掉落的物品数
|
||||
item_dropped_ondeath: 3
|
||||
|
||||
# 死亡后后台执行的指令({PLAYER}指代玩家名占位符)
|
||||
punish_commands: []
|
||||
|
||||
# 开启死亡随机物品掉落的世界
|
||||
worlds_enabled_item_drop:
|
||||
- world
|
||||
- world_nether
|
||||
- world_the_end
|
||||
|
||||
# 开启死亡后金币掉落的世界
|
||||
worlds_enabled_money_drop:
|
||||
- world
|
||||
- world_nether
|
||||
- world_the_end
|
||||
|
||||
# 开启死亡后执行后台指令的世界
|
||||
worlds_enabled_punish_commands:
|
||||
- world
|
||||
- world_nether
|
||||
- world_the_end
|
||||
|
||||
# 物品掉落白名单
|
||||
item_drop_whitelist: []
|
||||
@ -1,5 +1,5 @@
|
||||
name: BasicFunctions
|
||||
version: '${project.version}'
|
||||
main: com.ultikits.plugins.BasicFunctions
|
||||
api-version: 605
|
||||
api-version: 600
|
||||
authors: [ 'wisdomme' ]
|
||||
|
||||
@ -1,37 +0,0 @@
|
||||
package com.ultikits.plugins.sidebar;
|
||||
|
||||
import com.ultikits.ultitools.abstracts.AbstractCommendExecutor;
|
||||
import com.ultikits.ultitools.annotations.command.*;
|
||||
import fr.mrmicky.fastboard.FastBoard;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import static com.ultikits.ultitools.utils.MessageUtils.coloredMsg;
|
||||
|
||||
@CmdExecutor(alias = {"sb"})
|
||||
@CmdTarget(CmdTarget.CmdTargetType.PLAYER)
|
||||
public class SbCommand extends AbstractCommendExecutor {
|
||||
|
||||
@CmdMapping(format = "close")
|
||||
public void close(@CmdSender Player sender) {
|
||||
SidebarPlugin.getInstance().getBoards().get(sender.getUniqueId()).delete();
|
||||
SidebarPlugin.getInstance().getBoards().remove(sender.getUniqueId());
|
||||
}
|
||||
|
||||
@CmdMapping(format = "reload", requireOp = true)
|
||||
public void reload() {
|
||||
SidebarPlugin.getInstance().registerSelf();
|
||||
}
|
||||
|
||||
@CmdMapping(format = "open")
|
||||
public void open(@CmdSender Player player) {
|
||||
FastBoard board = new FastBoard(player);
|
||||
board.updateTitle(coloredMsg(SidebarPlugin.getInstance().getConfig(SidebarConfig.class).getTitle()));
|
||||
SidebarPlugin.getInstance().getBoards().put(player.getUniqueId(), board);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handleHelp(CommandSender sender) {
|
||||
|
||||
}
|
||||
}
|
||||
@ -1,26 +1,16 @@
|
||||
package com.ultikits.plugins.sidebar;
|
||||
|
||||
import fr.mrmicky.fastboard.FastBoard;
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static com.ultikits.ultitools.utils.MessageUtils.coloredMsg;
|
||||
|
||||
public class UpdateTask extends BukkitRunnable {
|
||||
@Override
|
||||
public void run() {
|
||||
for (FastBoard board : SidebarPlugin.getInstance().getBoards().values()) {
|
||||
if (board.isDeleted() || !board.getPlayer().isOnline()){
|
||||
if (board.isDeleted()){
|
||||
continue;
|
||||
}
|
||||
List<String> list = new ArrayList<>();
|
||||
for (String s : SidebarPlugin.getInstance().getConfig(SidebarConfig.class).getContent()) {
|
||||
list.add(coloredMsg(PlaceholderAPI.setPlaceholders(board.getPlayer(), s)));
|
||||
}
|
||||
board.updateLines(list);
|
||||
SidebarPlugin.getInstance().updateBoard(board);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
name: Sidebar
|
||||
version: '${project.version}'
|
||||
main: com.ultikits.plugins.sidebar.SidebarPlugin
|
||||
api-version: 605
|
||||
authors: [ 'wisdomme' ]
|
||||
api-version: 600
|
||||
authors: [ wisdomme ]
|
||||
|
||||
@ -1,13 +1,11 @@
|
||||
package com.ultikits.ultitools.annotations;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
@Component
|
||||
public @interface EventListener {
|
||||
boolean manualRegister() default false;
|
||||
}
|
||||
|
||||
@ -1,10 +1,12 @@
|
||||
package com.ultikits.ultitools.annotations;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
@Documented
|
||||
public @interface I18n {
|
||||
String[] value() default {};
|
||||
String[] value();
|
||||
}
|
||||
|
||||
@ -1,36 +0,0 @@
|
||||
package com.ultikits.ultitools.annotations;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.annotation.AliasFor;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
@EnableAutoRegister
|
||||
@I18n
|
||||
@ComponentScan
|
||||
@Configuration
|
||||
public @interface UltiToolsModule {
|
||||
@AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
|
||||
String[] scanBasePackages() default {};
|
||||
|
||||
@AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")
|
||||
Class<?>[] scanBasePackageClasses() default {};
|
||||
|
||||
@AliasFor(annotation = EnableAutoRegister.class, attribute = "eventListener")
|
||||
boolean eventListener() default true;
|
||||
|
||||
@AliasFor(annotation = EnableAutoRegister.class, attribute = "cmdExecutor")
|
||||
boolean cmdExecutor() default true;
|
||||
|
||||
@AliasFor(annotation = EnableAutoRegister.class, attribute = "config")
|
||||
boolean config() default true;
|
||||
|
||||
@AliasFor(annotation = I18n.class, attribute = "value")
|
||||
String[] i18n() default {};
|
||||
}
|
||||
@ -1,8 +0,0 @@
|
||||
package com.ultikits.ultitools.interfaces;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.Event;
|
||||
|
||||
public interface TempEventHandler<E extends Event> {
|
||||
boolean handle(E event);
|
||||
}
|
||||
@ -1,12 +0,0 @@
|
||||
package com.ultikits.ultitools.interfaces;
|
||||
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
public interface TempListener extends Listener {
|
||||
void register();
|
||||
|
||||
default void unregister() {
|
||||
HandlerList.unregisterAll(this);
|
||||
}
|
||||
}
|
||||
@ -1,55 +0,0 @@
|
||||
package com.ultikits.ultitools.interfaces.impl;
|
||||
|
||||
import com.ultikits.ultitools.UltiTools;
|
||||
import com.ultikits.ultitools.interfaces.TempEventHandler;
|
||||
import com.ultikits.ultitools.interfaces.TempListener;
|
||||
import lombok.*;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.player.PlayerEvent;
|
||||
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Setter
|
||||
@Getter
|
||||
public class PlayerTempListener<E extends PlayerEvent> implements TempListener {
|
||||
private Class<E> eventClass;
|
||||
private EventPriority priority = EventPriority.NORMAL;
|
||||
private TempEventHandler<E> eventHandler;
|
||||
private Player player;
|
||||
|
||||
public PlayerTempListener(Class<E> eventClass, TempEventHandler<E> eventHandler) {
|
||||
this.eventClass = eventClass;
|
||||
this.eventHandler = eventHandler;
|
||||
}
|
||||
|
||||
public PlayerTempListener(Class<E> eventClass, TempEventHandler<E> eventHandler, Player player) {
|
||||
this.eventClass = eventClass;
|
||||
this.eventHandler = eventHandler;
|
||||
this.player = player;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void register() {
|
||||
Bukkit.getServer().getPluginManager().registerEvent(eventClass, this, priority,
|
||||
(ignored, event) -> {
|
||||
try {
|
||||
if (player == null) {
|
||||
if (eventHandler.handle((E) event)) {
|
||||
unregister();
|
||||
}
|
||||
} else if (((E) event).getPlayer().equals(player)) {
|
||||
if (eventHandler.handle((E) event)) {
|
||||
unregister();
|
||||
}
|
||||
}
|
||||
} catch (ClassCastException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
},
|
||||
UltiTools.getInstance()
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -1,41 +0,0 @@
|
||||
package com.ultikits.ultitools.interfaces.impl;
|
||||
|
||||
import com.ultikits.ultitools.UltiTools;
|
||||
import com.ultikits.ultitools.interfaces.TempEventHandler;
|
||||
import com.ultikits.ultitools.interfaces.TempListener;
|
||||
import lombok.*;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.EventPriority;
|
||||
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Setter
|
||||
@Getter
|
||||
public class SimpleTempListener<E extends Event> implements TempListener {
|
||||
private Class<E> eventClass;
|
||||
private EventPriority priority = EventPriority.NORMAL;
|
||||
private TempEventHandler<E> eventHandler;
|
||||
|
||||
public SimpleTempListener(Class<E> eventClass, TempEventHandler<E> eventHandler) {
|
||||
this.eventClass = eventClass;
|
||||
this.eventHandler = eventHandler;
|
||||
}
|
||||
|
||||
public void register() {
|
||||
Bukkit.getServer().getPluginManager().registerEvent(eventClass, this, priority,
|
||||
(ignored, event) -> {
|
||||
try {
|
||||
//noinspection unchecked
|
||||
if (eventHandler.handle((E) event)){
|
||||
unregister();
|
||||
}
|
||||
} catch (ClassCastException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
},
|
||||
UltiTools.getInstance()
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -1,54 +0,0 @@
|
||||
package com.ultikits.ultitools.listeners;
|
||||
|
||||
import com.ultikits.ultitools.UltiTools;
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class PlayerJoinListener implements Listener {
|
||||
private final List<String> placeholderList = Arrays.asList("player", "server", "math", "vault", "localtime");
|
||||
|
||||
// make the word first letter uppercase
|
||||
public static String toUpperCaseFirstOne(String s) {
|
||||
if (Character.isUpperCase(s.charAt(0)))
|
||||
return s;
|
||||
else
|
||||
return Character.toUpperCase(s.charAt(0)) + s.substring(1);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerJoin(PlayerJoinEvent event) {
|
||||
try {
|
||||
boolean reload = false;
|
||||
long time = 30L;
|
||||
for (String placeholder : placeholderList) {
|
||||
if (!PlaceholderAPI.isRegistered(placeholder)) {
|
||||
reload = true;
|
||||
new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "papi ecloud download " + toUpperCaseFirstOne(placeholder));
|
||||
}
|
||||
}.runTaskLater(UltiTools.getInstance(), time);
|
||||
time += 30L;
|
||||
}
|
||||
}
|
||||
if (reload) {
|
||||
new BukkitRunnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "papi reload");
|
||||
}
|
||||
}.runTaskLater(UltiTools.getInstance(), time + 30L);
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,713 +0,0 @@
|
||||
package com.ultikits.ultitools.utils;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.plugin.RegisteredServiceProvider;
|
||||
import org.bukkit.plugin.ServicePriority;
|
||||
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import java.io.*;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.logging.Level;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
|
||||
/**
|
||||
* bStats collects some data for plugin authors.
|
||||
* <p>
|
||||
* Check out https://bStats.org/ to learn more about bStats!
|
||||
*/
|
||||
@SuppressWarnings({"WeakerAccess", "unused"})
|
||||
public class Metrics {
|
||||
|
||||
// The version of this bStats class
|
||||
public static final int B_STATS_VERSION = 1;
|
||||
// The url to which the data is sent
|
||||
private static final String URL = "https://bStats.org/submitData/bukkit";
|
||||
// Should failed requests be logged?
|
||||
private static boolean logFailedRequests;
|
||||
// Should the sent data be logged?
|
||||
private static boolean logSentData;
|
||||
// Should the response text be logged?
|
||||
private static boolean logResponseStatusText;
|
||||
// The uuid of the server
|
||||
private static String serverUUID;
|
||||
|
||||
static {
|
||||
// You can use the property to disable the check in your test environment
|
||||
if (System.getProperty("bstats.relocatecheck") == null || !System.getProperty("bstats.relocatecheck").equals("false")) {
|
||||
// Maven's Relocate is clever and changes strings, too. So we have to use this little "trick" ... :D
|
||||
final String defaultPackage = new String(
|
||||
new byte[]{'o', 'r', 'g', '.', 'b', 's', 't', 'a', 't', 's', '.', 'b', 'u', 'k', 'k', 'i', 't'});
|
||||
final String examplePackage = new String(new byte[]{'y', 'o', 'u', 'r', '.', 'p', 'a', 'c', 'k', 'a', 'g', 'e'});
|
||||
}
|
||||
}
|
||||
|
||||
// The plugin
|
||||
private final Plugin plugin;
|
||||
// The plugin id
|
||||
private final int pluginId;
|
||||
// A list with all custom charts
|
||||
private final List<CustomChart> charts = new ArrayList<>();
|
||||
// Is bStats enabled on this server?
|
||||
private boolean enabled;
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*
|
||||
* @param plugin The plugin which stats should be submitted.
|
||||
* @param pluginId The id of the plugin.
|
||||
* It can be found at <a href="https://bstats.org/what-is-my-plugin-id">What is my plugin id?</a>
|
||||
*/
|
||||
public Metrics(Plugin plugin, int pluginId) {
|
||||
if (plugin == null) {
|
||||
throw new IllegalArgumentException("Plugin cannot be null!");
|
||||
}
|
||||
this.plugin = plugin;
|
||||
this.pluginId = pluginId;
|
||||
|
||||
// Get the config file
|
||||
File bStatsFolder = new File(plugin.getDataFolder().getParentFile(), "bStats");
|
||||
File configFile = new File(bStatsFolder, "config.yml");
|
||||
YamlConfiguration config = YamlConfiguration.loadConfiguration(configFile);
|
||||
|
||||
// Check if the config file exists
|
||||
if (!config.isSet("serverUuid")) {
|
||||
|
||||
// Add default values
|
||||
config.addDefault("enabled", true);
|
||||
// Every server gets it's unique random id.
|
||||
config.addDefault("serverUuid", UUID.randomUUID().toString());
|
||||
// Should failed request be logged?
|
||||
config.addDefault("logFailedRequests", false);
|
||||
// Should the sent data be logged?
|
||||
config.addDefault("logSentData", false);
|
||||
// Should the response text be logged?
|
||||
config.addDefault("logResponseStatusText", false);
|
||||
|
||||
// Inform the server owners about bStats
|
||||
config.options().header(
|
||||
"bStats collects some data for plugin authors like how many servers are using their plugins.\n" +
|
||||
"To honor their work, you should not disable it.\n" +
|
||||
"This has nearly no effect on the server performance!\n" +
|
||||
"Check out https://bStats.org/ to learn more :)"
|
||||
).copyDefaults(true);
|
||||
try {
|
||||
config.save(configFile);
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
// Load the data
|
||||
enabled = config.getBoolean("enabled", true);
|
||||
serverUUID = config.getString("serverUuid");
|
||||
logFailedRequests = config.getBoolean("logFailedRequests", false);
|
||||
logSentData = config.getBoolean("logSentData", false);
|
||||
logResponseStatusText = config.getBoolean("logResponseStatusText", false);
|
||||
|
||||
if (enabled) {
|
||||
boolean found = false;
|
||||
// Search for all other bStats Metrics classes to see if we are the first one
|
||||
for (Class<?> service : Bukkit.getServicesManager().getKnownServices()) {
|
||||
try {
|
||||
service.getField("B_STATS_VERSION"); // Our identifier :)
|
||||
found = true; // We aren't the first
|
||||
break;
|
||||
} catch (NoSuchFieldException ignored) {
|
||||
}
|
||||
}
|
||||
// Register our service
|
||||
Bukkit.getServicesManager().register(Metrics.class, this, plugin, ServicePriority.Normal);
|
||||
if (!found) {
|
||||
// We are the first!
|
||||
startSubmitting();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends the data to the bStats server.
|
||||
*
|
||||
* @param plugin Any plugin. It's just used to get a logger instance.
|
||||
* @param data The data to send.
|
||||
* @throws Exception If the request failed.
|
||||
*/
|
||||
private static void sendData(Plugin plugin, JsonObject data) throws Exception {
|
||||
if (data == null) {
|
||||
throw new IllegalArgumentException("Data cannot be null!");
|
||||
}
|
||||
if (Bukkit.isPrimaryThread()) {
|
||||
throw new IllegalAccessException("This method must not be called from the main thread!");
|
||||
}
|
||||
if (logSentData) {
|
||||
plugin.getLogger().info("Sending data to bStats: " + data);
|
||||
}
|
||||
HttpsURLConnection connection = (HttpsURLConnection) new URL(URL).openConnection();
|
||||
|
||||
// Compress the data to save bandwidth
|
||||
byte[] compressedData = compress(data.toString());
|
||||
|
||||
// Add headers
|
||||
connection.setRequestMethod("POST");
|
||||
connection.addRequestProperty("Accept", "application/json");
|
||||
connection.addRequestProperty("Connection", "close");
|
||||
connection.addRequestProperty("Content-Encoding", "gzip"); // We gzip our request
|
||||
connection.addRequestProperty("Content-Length", String.valueOf(compressedData.length));
|
||||
connection.setRequestProperty("Content-Type", "application/json"); // We send our data in JSON format
|
||||
connection.setRequestProperty("User-Agent", "MC-Server/" + B_STATS_VERSION);
|
||||
|
||||
// Send data
|
||||
connection.setDoOutput(true);
|
||||
try (DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream())) {
|
||||
outputStream.write(compressedData);
|
||||
}
|
||||
|
||||
StringBuilder builder = new StringBuilder();
|
||||
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
|
||||
String line;
|
||||
while ((line = bufferedReader.readLine()) != null) {
|
||||
builder.append(line);
|
||||
}
|
||||
}
|
||||
|
||||
if (logResponseStatusText) {
|
||||
plugin.getLogger().info("Sent data to bStats and received response: " + builder);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gzips the given String.
|
||||
*
|
||||
* @param str The string to gzip.
|
||||
* @return The gzipped String.
|
||||
* @throws IOException If the compression failed.
|
||||
*/
|
||||
private static byte[] compress(final String str) throws IOException {
|
||||
if (str == null) {
|
||||
return null;
|
||||
}
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
try (GZIPOutputStream gzip = new GZIPOutputStream(outputStream)) {
|
||||
gzip.write(str.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
return outputStream.toByteArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if bStats is enabled.
|
||||
*
|
||||
* @return Whether bStats is enabled or not.
|
||||
*/
|
||||
public boolean isEnabled() {
|
||||
return enabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a custom chart.
|
||||
*
|
||||
* @param chart The chart to add.
|
||||
*/
|
||||
public void addCustomChart(CustomChart chart) {
|
||||
if (chart == null) {
|
||||
throw new IllegalArgumentException("Chart cannot be null!");
|
||||
}
|
||||
charts.add(chart);
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the Scheduler which submits our data every 30 minutes.
|
||||
*/
|
||||
private void startSubmitting() {
|
||||
final Timer timer = new Timer(true); // We use a timer cause the Bukkit scheduler is affected by server lags
|
||||
timer.scheduleAtFixedRate(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (!plugin.isEnabled()) { // Plugin was disabled
|
||||
timer.cancel();
|
||||
return;
|
||||
}
|
||||
// Nevertheless we want our code to run in the Bukkit main thread, so we have to use the Bukkit scheduler
|
||||
// Don't be afraid! The connection to the bStats server is still async, only the stats collection is sync ;)
|
||||
Bukkit.getScheduler().runTask(plugin, () -> submitData());
|
||||
}
|
||||
}, 1000 * 60 * 5, 1000 * 60 * 30);
|
||||
// Submit the data every 30 minutes, first time after 5 minutes to give other plugins enough time to start
|
||||
// WARNING: Changing the frequency has no effect but your plugin WILL be blocked/deleted!
|
||||
// WARNING: Just don't do it!
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the plugin specific data.
|
||||
* This method is called using Reflection.
|
||||
*
|
||||
* @return The plugin specific data.
|
||||
*/
|
||||
public JsonObject getPluginData() {
|
||||
JsonObject data = new JsonObject();
|
||||
|
||||
String pluginName = plugin.getDescription().getName();
|
||||
String pluginVersion = plugin.getDescription().getVersion();
|
||||
|
||||
data.addProperty("pluginName", pluginName); // Append the name of the plugin
|
||||
data.addProperty("id", pluginId); // Append the id of the plugin
|
||||
data.addProperty("pluginVersion", pluginVersion); // Append the version of the plugin
|
||||
JsonArray customCharts = new JsonArray();
|
||||
for (CustomChart customChart : charts) {
|
||||
// Add the data of the custom charts
|
||||
JsonObject chart = customChart.getRequestJsonObject();
|
||||
if (chart == null) { // If the chart is null, we skip it
|
||||
continue;
|
||||
}
|
||||
customCharts.add(chart);
|
||||
}
|
||||
data.add("customCharts", customCharts);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the server specific data.
|
||||
*
|
||||
* @return The server specific data.
|
||||
*/
|
||||
private JsonObject getServerData() {
|
||||
// Minecraft specific data
|
||||
int playerAmount;
|
||||
try {
|
||||
// Around MC 1.8 the return type was changed to a collection from an array,
|
||||
// This fixes java.lang.NoSuchMethodError: org.bukkit.Bukkit.getOnlinePlayers()Ljava/util/Collection;
|
||||
Method onlinePlayersMethod = Class.forName("org.bukkit.Server").getMethod("getOnlinePlayers");
|
||||
playerAmount = onlinePlayersMethod.getReturnType().equals(Collection.class)
|
||||
? ((Collection<?>) onlinePlayersMethod.invoke(Bukkit.getServer())).size()
|
||||
: ((Player[]) onlinePlayersMethod.invoke(Bukkit.getServer())).length;
|
||||
} catch (Exception e) {
|
||||
playerAmount = Bukkit.getOnlinePlayers().size(); // Just use the new method if the Reflection failed
|
||||
}
|
||||
int onlineMode = Bukkit.getOnlineMode() ? 1 : 0;
|
||||
String bukkitVersion = Bukkit.getVersion();
|
||||
String bukkitName = Bukkit.getName();
|
||||
|
||||
// OS/Java specific data
|
||||
String javaVersion = System.getProperty("java.version");
|
||||
String osName = System.getProperty("os.name");
|
||||
String osArch = System.getProperty("os.arch");
|
||||
String osVersion = System.getProperty("os.version");
|
||||
int coreCount = Runtime.getRuntime().availableProcessors();
|
||||
|
||||
JsonObject data = new JsonObject();
|
||||
|
||||
data.addProperty("serverUUID", serverUUID);
|
||||
|
||||
data.addProperty("playerAmount", playerAmount);
|
||||
data.addProperty("onlineMode", onlineMode);
|
||||
data.addProperty("bukkitVersion", bukkitVersion);
|
||||
data.addProperty("bukkitName", bukkitName);
|
||||
|
||||
data.addProperty("javaVersion", javaVersion);
|
||||
data.addProperty("osName", osName);
|
||||
data.addProperty("osArch", osArch);
|
||||
data.addProperty("osVersion", osVersion);
|
||||
data.addProperty("coreCount", coreCount);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Collects the data and sends it afterwards.
|
||||
*/
|
||||
private void submitData() {
|
||||
final JsonObject data = getServerData();
|
||||
|
||||
JsonArray pluginData = new JsonArray();
|
||||
// Search for all other bStats Metrics classes to get their plugin data
|
||||
for (Class<?> service : Bukkit.getServicesManager().getKnownServices()) {
|
||||
try {
|
||||
service.getField("B_STATS_VERSION"); // Our identifier :)
|
||||
|
||||
for (RegisteredServiceProvider<?> provider : Bukkit.getServicesManager().getRegistrations(service)) {
|
||||
try {
|
||||
Object plugin = provider.getService().getMethod("getPluginData").invoke(provider.getProvider());
|
||||
if (plugin instanceof JsonObject) {
|
||||
pluginData.add((JsonObject) plugin);
|
||||
} else { // old bstats version compatibility
|
||||
try {
|
||||
Class<?> jsonObjectJsonSimple = Class.forName("org.json.simple.JSONObject");
|
||||
if (plugin.getClass().isAssignableFrom(jsonObjectJsonSimple)) {
|
||||
Method jsonStringGetter = jsonObjectJsonSimple.getDeclaredMethod("toJSONString");
|
||||
jsonStringGetter.setAccessible(true);
|
||||
String jsonString = (String) jsonStringGetter.invoke(plugin);
|
||||
JsonObject object = new JsonParser().parse(jsonString).getAsJsonObject();
|
||||
pluginData.add(object);
|
||||
}
|
||||
} catch (ClassNotFoundException e) {
|
||||
// minecraft version 1.14+
|
||||
if (logFailedRequests) {
|
||||
this.plugin.getLogger().log(Level.SEVERE, "Encountered unexpected exception", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (NullPointerException | NoSuchMethodException | IllegalAccessException |
|
||||
InvocationTargetException ignored) {
|
||||
}
|
||||
}
|
||||
} catch (NoSuchFieldException ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
data.add("plugins", pluginData);
|
||||
|
||||
// Create a new thread for the connection to the bStats server
|
||||
new Thread(() -> {
|
||||
try {
|
||||
// Send the data
|
||||
sendData(plugin, data);
|
||||
} catch (Exception e) {
|
||||
// Something went wrong! :(
|
||||
if (logFailedRequests) {
|
||||
plugin.getLogger().log(Level.WARNING, "Could not submit plugin stats of " + plugin.getName(), e);
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a custom chart.
|
||||
*/
|
||||
public static abstract class CustomChart {
|
||||
|
||||
// The id of the chart
|
||||
final String chartId;
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*
|
||||
* @param chartId The id of the chart.
|
||||
*/
|
||||
CustomChart(String chartId) {
|
||||
if (chartId == null || chartId.isEmpty()) {
|
||||
throw new IllegalArgumentException("ChartId cannot be null or empty!");
|
||||
}
|
||||
this.chartId = chartId;
|
||||
}
|
||||
|
||||
private JsonObject getRequestJsonObject() {
|
||||
JsonObject chart = new JsonObject();
|
||||
chart.addProperty("chartId", chartId);
|
||||
try {
|
||||
JsonObject data = getChartData();
|
||||
if (data == null) {
|
||||
// If the data is null we don't send the chart.
|
||||
return null;
|
||||
}
|
||||
chart.add("data", data);
|
||||
} catch (Throwable t) {
|
||||
if (logFailedRequests) {
|
||||
Bukkit.getLogger().log(Level.WARNING, "Failed to get data for custom chart with id " + chartId, t);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
return chart;
|
||||
}
|
||||
|
||||
protected abstract JsonObject getChartData() throws Exception;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a custom simple pie.
|
||||
*/
|
||||
public static class SimplePie extends CustomChart {
|
||||
|
||||
private final Callable<String> callable;
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*
|
||||
* @param chartId The id of the chart.
|
||||
* @param callable The callable which is used to request the chart data.
|
||||
*/
|
||||
public SimplePie(String chartId, Callable<String> callable) {
|
||||
super(chartId);
|
||||
this.callable = callable;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected JsonObject getChartData() throws Exception {
|
||||
JsonObject data = new JsonObject();
|
||||
String value = callable.call();
|
||||
if (value == null || value.isEmpty()) {
|
||||
// Null = skip the chart
|
||||
return null;
|
||||
}
|
||||
data.addProperty("value", value);
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a custom advanced pie.
|
||||
*/
|
||||
public static class AdvancedPie extends CustomChart {
|
||||
|
||||
private final Callable<Map<String, Integer>> callable;
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*
|
||||
* @param chartId The id of the chart.
|
||||
* @param callable The callable which is used to request the chart data.
|
||||
*/
|
||||
public AdvancedPie(String chartId, Callable<Map<String, Integer>> callable) {
|
||||
super(chartId);
|
||||
this.callable = callable;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected JsonObject getChartData() throws Exception {
|
||||
JsonObject data = new JsonObject();
|
||||
JsonObject values = new JsonObject();
|
||||
Map<String, Integer> map = callable.call();
|
||||
if (map == null || map.isEmpty()) {
|
||||
// Null = skip the chart
|
||||
return null;
|
||||
}
|
||||
boolean allSkipped = true;
|
||||
for (Map.Entry<String, Integer> entry : map.entrySet()) {
|
||||
if (entry.getValue() == 0) {
|
||||
continue; // Skip this invalid
|
||||
}
|
||||
allSkipped = false;
|
||||
values.addProperty(entry.getKey(), entry.getValue());
|
||||
}
|
||||
if (allSkipped) {
|
||||
// Null = skip the chart
|
||||
return null;
|
||||
}
|
||||
data.add("values", values);
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a custom drilldown pie.
|
||||
*/
|
||||
public static class DrilldownPie extends CustomChart {
|
||||
|
||||
private final Callable<Map<String, Map<String, Integer>>> callable;
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*
|
||||
* @param chartId The id of the chart.
|
||||
* @param callable The callable which is used to request the chart data.
|
||||
*/
|
||||
public DrilldownPie(String chartId, Callable<Map<String, Map<String, Integer>>> callable) {
|
||||
super(chartId);
|
||||
this.callable = callable;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonObject getChartData() throws Exception {
|
||||
JsonObject data = new JsonObject();
|
||||
JsonObject values = new JsonObject();
|
||||
Map<String, Map<String, Integer>> map = callable.call();
|
||||
if (map == null || map.isEmpty()) {
|
||||
// Null = skip the chart
|
||||
return null;
|
||||
}
|
||||
boolean reallyAllSkipped = true;
|
||||
for (Map.Entry<String, Map<String, Integer>> entryValues : map.entrySet()) {
|
||||
JsonObject value = new JsonObject();
|
||||
boolean allSkipped = true;
|
||||
for (Map.Entry<String, Integer> valueEntry : map.get(entryValues.getKey()).entrySet()) {
|
||||
value.addProperty(valueEntry.getKey(), valueEntry.getValue());
|
||||
allSkipped = false;
|
||||
}
|
||||
if (!allSkipped) {
|
||||
reallyAllSkipped = false;
|
||||
values.add(entryValues.getKey(), value);
|
||||
}
|
||||
}
|
||||
if (reallyAllSkipped) {
|
||||
// Null = skip the chart
|
||||
return null;
|
||||
}
|
||||
data.add("values", values);
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a custom single line chart.
|
||||
*/
|
||||
public static class SingleLineChart extends CustomChart {
|
||||
|
||||
private final Callable<Integer> callable;
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*
|
||||
* @param chartId The id of the chart.
|
||||
* @param callable The callable which is used to request the chart data.
|
||||
*/
|
||||
public SingleLineChart(String chartId, Callable<Integer> callable) {
|
||||
super(chartId);
|
||||
this.callable = callable;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected JsonObject getChartData() throws Exception {
|
||||
JsonObject data = new JsonObject();
|
||||
int value = callable.call();
|
||||
if (value == 0) {
|
||||
// Null = skip the chart
|
||||
return null;
|
||||
}
|
||||
data.addProperty("value", value);
|
||||
return data;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a custom multi line chart.
|
||||
*/
|
||||
public static class MultiLineChart extends CustomChart {
|
||||
|
||||
private final Callable<Map<String, Integer>> callable;
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*
|
||||
* @param chartId The id of the chart.
|
||||
* @param callable The callable which is used to request the chart data.
|
||||
*/
|
||||
public MultiLineChart(String chartId, Callable<Map<String, Integer>> callable) {
|
||||
super(chartId);
|
||||
this.callable = callable;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected JsonObject getChartData() throws Exception {
|
||||
JsonObject data = new JsonObject();
|
||||
JsonObject values = new JsonObject();
|
||||
Map<String, Integer> map = callable.call();
|
||||
if (map == null || map.isEmpty()) {
|
||||
// Null = skip the chart
|
||||
return null;
|
||||
}
|
||||
boolean allSkipped = true;
|
||||
for (Map.Entry<String, Integer> entry : map.entrySet()) {
|
||||
if (entry.getValue() == 0) {
|
||||
continue; // Skip this invalid
|
||||
}
|
||||
allSkipped = false;
|
||||
values.addProperty(entry.getKey(), entry.getValue());
|
||||
}
|
||||
if (allSkipped) {
|
||||
// Null = skip the chart
|
||||
return null;
|
||||
}
|
||||
data.add("values", values);
|
||||
return data;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a custom simple bar chart.
|
||||
*/
|
||||
public static class SimpleBarChart extends CustomChart {
|
||||
|
||||
private final Callable<Map<String, Integer>> callable;
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*
|
||||
* @param chartId The id of the chart.
|
||||
* @param callable The callable which is used to request the chart data.
|
||||
*/
|
||||
public SimpleBarChart(String chartId, Callable<Map<String, Integer>> callable) {
|
||||
super(chartId);
|
||||
this.callable = callable;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected JsonObject getChartData() throws Exception {
|
||||
JsonObject data = new JsonObject();
|
||||
JsonObject values = new JsonObject();
|
||||
Map<String, Integer> map = callable.call();
|
||||
if (map == null || map.isEmpty()) {
|
||||
// Null = skip the chart
|
||||
return null;
|
||||
}
|
||||
for (Map.Entry<String, Integer> entry : map.entrySet()) {
|
||||
JsonArray categoryValues = new JsonArray();
|
||||
categoryValues.add(new JsonPrimitive(entry.getValue()));
|
||||
values.add(entry.getKey(), categoryValues);
|
||||
}
|
||||
data.add("values", values);
|
||||
return data;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a custom advanced bar chart.
|
||||
*/
|
||||
public static class AdvancedBarChart extends CustomChart {
|
||||
|
||||
private final Callable<Map<String, int[]>> callable;
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*
|
||||
* @param chartId The id of the chart.
|
||||
* @param callable The callable which is used to request the chart data.
|
||||
*/
|
||||
public AdvancedBarChart(String chartId, Callable<Map<String, int[]>> callable) {
|
||||
super(chartId);
|
||||
this.callable = callable;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected JsonObject getChartData() throws Exception {
|
||||
JsonObject data = new JsonObject();
|
||||
JsonObject values = new JsonObject();
|
||||
Map<String, int[]> map = callable.call();
|
||||
if (map == null || map.isEmpty()) {
|
||||
// Null = skip the chart
|
||||
return null;
|
||||
}
|
||||
boolean allSkipped = true;
|
||||
for (Map.Entry<String, int[]> entry : map.entrySet()) {
|
||||
if (entry.getValue().length == 0) {
|
||||
continue; // Skip this invalid
|
||||
}
|
||||
allSkipped = false;
|
||||
JsonArray categoryValues = new JsonArray();
|
||||
for (int categoryValue : entry.getValue()) {
|
||||
categoryValues.add(new JsonPrimitive(categoryValue));
|
||||
}
|
||||
values.add(entry.getKey(), categoryValues);
|
||||
}
|
||||
if (allSkipped) {
|
||||
// Null = skip the chart
|
||||
return null;
|
||||
}
|
||||
data.add("values", values);
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in new issue