Skip to content
This repository has been archived by the owner on Oct 1, 2023. It is now read-only.

Add world support #8

Merged
merged 7 commits into from
Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/main/java/io/github/dediamondpro/xcatch/XCatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.bstats.bukkit.Metrics;
import org.bstats.charts.SingleLineChart;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.java.JavaPlugin;

Expand All @@ -41,9 +42,12 @@ public final class XCatch extends JavaPlugin {
public static final HashMap<Integer, ArrayList<String>> commands = new HashMap<>();
public static int metricFlags = 0;

private NamespacedKey actionDataKey;

@Override
public void onEnable() {
INSTANCE = this;
actionDataKey = new NamespacedKey(this, "action_data");

if (!new File(getDataFolder(), "config.yml").exists()) {
saveDefaultConfig();
Expand Down Expand Up @@ -98,6 +102,10 @@ public void onDisable() {
PersistentData.saveData(getDataFolder().getAbsolutePath() + "/data.json.gz");
}

public NamespacedKey getActionDataKey() {
return actionDataKey;
}

public static void loadConfigParts() {
rareOres.clear();
ArrayList<HashMap<String, Integer>> list = (ArrayList<HashMap<String, Integer>>) config.get("rare-ores");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
import io.github.dediamondpro.xcatch.listeners.OnBlockBreak;
import io.github.dediamondpro.xcatch.utils.FlagHandler;
import io.github.dediamondpro.xcatch.utils.Utils;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
Expand All @@ -44,6 +45,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
"§7/xcatch info, get some statics about XCatch on your server.",
"§7/xcatch reload, reload XCatch's config.",
"§7/xcatch test <player>, add a flag to a player to test things",
"§7/xcatch tp <world> <x> <y> <z>, teleport to specific coordinates",
"§7/xcatch debug <player>, give debug statistics of a player."
});
return true;
Expand Down Expand Up @@ -138,6 +140,31 @@ else if (XCatch.config.getInt("ban-flags") != 0)
}
FlagHandler.addFlag(new BlockBreakEvent(null, player), true);
return true;
case "tp":
if (!(sender instanceof Player)) {
sender.sendMessage("§8[§cXCatch§8] §cSince this command teleports you, it can only be used from in-game.");
return true;
}
if (args.length < 5) {
sender.sendMessage("§8[§cXCatch§8] §cMissing argument <world> <x> <y> <z>.");
return false;
}
World world = XCatch.INSTANCE.getServer().getWorld(args[1]);
if (world == null) {
sender.sendMessage("§8[§cXCatch§8] §cWorld not found.");
return false;
}
double[] coordinates = new double[3];
try {
coordinates[0] = Double.parseDouble(args[2]);
coordinates[1] = Double.parseDouble(args[3]);
coordinates[2] = Double.parseDouble(args[4]);
} catch (NumberFormatException ignored) {
sender.sendMessage("§8[§cXCatch§8] §cCoordinates contain invalid argument.");
return false;
}
((Player) sender).teleport(new Location(world, coordinates[0], coordinates[1], coordinates[2]));
return true;
}
sender.sendMessage("§8[§cXCatch§8] §cUnknown sub-command.");
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.github.dediamondpro.xcatch.commands;

import io.github.dediamondpro.xcatch.XCatch;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
Expand All @@ -25,6 +26,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class XCatchTabCompleter implements TabCompleter {
private static final ArrayList<String> subCommands = new ArrayList<String>() {{
Expand All @@ -33,6 +35,7 @@ public class XCatchTabCompleter implements TabCompleter {
add("reload");
add("clear");
add("test");
add("tp");
add("debug");
add("info");
}};
Expand Down Expand Up @@ -60,7 +63,11 @@ public List<String> onTabComplete(CommandSender sender, Command command, String
case "view":
case "test":
return getPlayerList(args[1]);
case "tp":
return getWorldList(args[1]);
}
} else if (args.length <= 5 && args[0].equals("tp")) {
return Collections.emptyList();
}
return Collections.emptyList();
}
Expand All @@ -77,4 +84,12 @@ public static List<String> getPlayerList(String filter) {
matchedPlayers.sort(String.CASE_INSENSITIVE_ORDER);
return matchedPlayers;
}

private List<String> getWorldList(String argument) {
return StringUtil.copyPartialMatches(
argument,
XCatch.INSTANCE.getServer().getWorlds().stream().map(World::getName).collect(Collectors.toList()),
new ArrayList<>()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,24 @@
import com.google.gson.annotations.SerializedName;
import org.jetbrains.annotations.NotNull;

import java.util.UUID;

public class ActionData implements Comparable<ActionData> {
public ActionType type;
public long time;
public String ore;
public int amount;
public UUID worldUID;
public int x;
public int y;
public int z;

public ActionData(ActionType type, long time, String ore, int amount, int x, int y, int z) {
public ActionData(ActionType type, long time, String ore, int amount, UUID worldUID, int x, int y, int z) {
this.type = type;
this.time = time;
this.ore = ore;
this.amount = amount;
this.worldUID = worldUID;
this.x = x;
this.y = y;
this.z = z;
Expand Down
29 changes: 21 additions & 8 deletions src/main/java/io/github/dediamondpro/xcatch/gui/ViewGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@
import io.github.dediamondpro.xcatch.data.PlayerData;
import io.github.dediamondpro.xcatch.utils.Utils;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;

import java.sql.Date;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -79,11 +82,16 @@ public static void openPlayerGui(Player p, UUID uuid, String name, int page) {

for (int i = 45 * (page - 1); i < Math.min(45 * (page), PersistentData.data.actions.get(uuid).size()); i++) {
ActionData data = PersistentData.data.actions.get(uuid).get(i);
gui.setItem(i - 45 * (page - 1), Utils.createItem(

World world = XCatch.INSTANCE.getServer().getWorld(data.worldUID);
String worldName = world == null ? p.getWorld().getName() : world.getName();

gui.setItem(i - 45 * (page - 1), Utils.createActionDataItem(
data.type == ActionData.ActionType.BAN ? Material.RED_CONCRETE : Material.YELLOW_CONCRETE,
data.type == ActionData.ActionType.BAN ? "§cBan" : "§eFlag",
i,
(data.ore != null ? "§7" + data.amount + " " + Utils.capitalize(data.ore) : "§7Unknown Ore"),
"§7" + data.x + " " + data.y + " " + data.z + " (click to teleport)",
"§7" + worldName + " " + data.x + " " + data.y + " " + data.z + " (click to teleport)",
"§7" + format.format(Date.from(Instant.ofEpochSecond(data.time)))
));
}
Expand Down Expand Up @@ -136,15 +144,20 @@ public void onInventoryClick(InventoryClickEvent event) {
default:
if (event.getSlot() < 45 && event.getCurrentItem() != null && (event.getCurrentItem().getType() == Material.RED_CONCRETE
|| event.getCurrentItem().getType() == Material.YELLOW_CONCRETE)) {
List<String> lore = event.getCurrentItem().getItemMeta().getLore();
String[] coordinates = lore.get(1).replace("§7", "").split(" ");
if (coordinates.length < 3) return;
Integer actionIndex = event.getCurrentItem().getItemMeta().getPersistentDataContainer().get(
XCatch.INSTANCE.getActionDataKey(), PersistentDataType.INTEGER
);
if (actionIndex == null) return;
ActionData actionData = PersistentData.data.actions.get(uuid).get(actionIndex);
World world = XCatch.INSTANCE.getServer().getWorld(actionData.worldUID);
String worldName = world == null ? event.getWhoClicked().getWorld().getName() : world.getName();
try {
HashMap<String, String> variables = new HashMap<String, String>() {{
put("{player}", name);
put("{x}", coordinates[0]);
put("{y}", coordinates[1]);
put("{z}", coordinates[2]);
put("{world}", worldName);
put("{x}", String.valueOf(actionData.x));
put("{y}", String.valueOf(actionData.y));
put("{z}", String.valueOf(actionData.z));
}};
event.getWhoClicked().closeInventory();
XCatch.INSTANCE.getServer().dispatchCommand(event.getWhoClicked(), Utils.replaceVariables(XCatch.config.getString("view-click-command"), variables));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public static void addFlag(BlockBreakEvent event, boolean test) {
put("{x}", String.valueOf(location.getBlockX()));
put("{y}", String.valueOf(location.getBlockY()));
put("{z}", String.valueOf(location.getBlockZ()));
put("{world}", location.getWorld().getName());
}};
if (XCatch.config.getInt("ban-flags") != 0 && flags.get(uuid).flags >= XCatch.config.getInt("ban-flags")
&& !event.getPlayer().hasPermission("xcatch.noban")) {
Expand All @@ -74,7 +75,7 @@ public static void addFlag(BlockBreakEvent event, boolean test) {
if (XCatch.config.getBoolean("message-ban"))
Utils.sendMessage(Utils.replaceVariables(XCatch.config.getString("ban-message-discord"), variables));
PersistentData.data.actions.get(uuid).add(new ActionData(ActionData.ActionType.BAN, Instant.now().getEpochSecond(), ore, amountMined,
location.getBlockX(), location.getBlockY(), location.getBlockZ()));
location.getWorld().getUID(), location.getBlockX(), location.getBlockY(), location.getBlockZ()));
PersistentData.data.totalBans++;
} else if (XCatch.config.getInt("alert-flags") != 0 && flags.get(uuid).flags >= XCatch.config.getInt("alert-flags")) {
TextComponent component = new TextComponent(Utils.replaceVariables(XCatch.config.getString("alert-message"), variables));
Expand All @@ -85,10 +86,10 @@ public static void addFlag(BlockBreakEvent event, boolean test) {
if (XCatch.config.getBoolean("message-alert"))
Utils.sendMessage(Utils.replaceVariables(XCatch.config.getString("alert-message-discord"), variables));
PersistentData.data.actions.get(uuid).add(new ActionData(ActionData.ActionType.FLAG, Instant.now().getEpochSecond(), ore, amountMined,
location.getBlockX(), location.getBlockY(), location.getBlockZ()));
location.getWorld().getUID(), location.getBlockX(), location.getBlockY(), location.getBlockZ()));
} else // no ban and no alert but still a flag
PersistentData.data.actions.get(uuid).add(new ActionData(ActionData.ActionType.FLAG, Instant.now().getEpochSecond(), ore, amountMined,
location.getBlockX(), location.getBlockY(), location.getBlockZ()));
location.getWorld().getUID(), location.getBlockX(), location.getBlockY(), location.getBlockZ()));
if (XCatch.commands.containsKey(flags.get(uuid).flags)) {
ArrayList<String> commands = XCatch.commands.get(flags.get(uuid).flags);
for (String command : commands) {
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/io/github/dediamondpro/xcatch/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.persistence.PersistentDataType;

import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
Expand Down Expand Up @@ -80,6 +81,23 @@ public static ItemStack createItem(Material material, String displayName, String
return itemStack;
}

public static ItemStack createActionDataItem(Material material, String displayName, int actionDataIndex, String... lore) {
ItemStack itemStack = new ItemStack(material);
ItemMeta itemMeta = itemStack.getItemMeta();
itemMeta.addItemFlags(ItemFlag.HIDE_POTION_EFFECTS);
itemStack.setItemMeta(itemMeta);
if (lore != null)
itemMeta.setLore(Arrays.asList(lore));
itemMeta.setDisplayName(displayName);
itemMeta.getPersistentDataContainer().set(
XCatch.INSTANCE.getActionDataKey(),
PersistentDataType.INTEGER,
actionDataIndex
);
itemStack.setItemMeta(itemMeta);
return itemStack;
}

public static UUID getOfflineUUID(String name) {
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(name);
if (offlinePlayer.hasPlayedBefore()) {
Expand Down
10 changes: 9 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ check-update: true
# {x}: X Coordinate location where the ore was found
# {y}: Y Coordinate location where the ore was found
# {z}: Z Coordinate location where the ore was found
# {world}: The name of the world where the ore was found
alert-click-command: 'tp {player}'

# The command executed when you click a flag inside the flags gui
Expand All @@ -57,7 +58,8 @@ alert-click-command: 'tp {player}'
# {x}: X Coordinate location where the ore was found
# {y}: Y Coordinate location where the ore was found
# {z}: Z Coordinate location where the ore was found
view-click-command: 'tp {x} {y} {z}'
# {world}: The name of the world where the ore was found
view-click-command: 'xcatch tp {world} {x} {y} {z}'

# The commands XCatch should execute when a player has been flagged
# Number specifies how many flags a player must have
Expand All @@ -70,6 +72,7 @@ view-click-command: 'tp {x} {y} {z}'
# {x}: X Coordinate location where the ore was found
# {y}: Y Coordinate location where the ore was found
# {z}: Z Coordinate location where the ore was found
# {world}: The name of the world where the ore was found
# Example:
# commands:
# - '1':
Expand Down Expand Up @@ -99,6 +102,7 @@ commands:
# {x}: X Coordinate location where the ore was found
# {y}: Y Coordinate location where the ore was found
# {z}: Z Coordinate location where the ore was found
# {world}: The name of the world where the ore was found
ban-reason: '§8[§cXCatch§8] §7You have been banned for §cx-ray§7 for §c{duration}§7.'

# Alert message that is sent to staff
Expand All @@ -110,6 +114,7 @@ ban-reason: '§8[§cXCatch§8] §7You have been banned for §cx-ray§7 for §c{d
# {x}: X Coordinate location where the ore was found
# {y}: Y Coordinate location where the ore was found
# {z}: Z Coordinate location where the ore was found
# {world}: The name of the world where the ore was found
alert-message: '§8[§cXCatch§8] §7Flagged §c{player}§7 for potential §cx-ray§7. §c{player}§7 mined §c{amount} {ore}§7. (§c{flags}§7)'

# Message that is sent to staff if someone gets banned
Expand All @@ -122,6 +127,7 @@ alert-message: '§8[§cXCatch§8] §7Flagged §c{player}§7 for potential §cx-r
# {x}: X Coordinate location where the ore was found
# {y}: Y Coordinate location where the ore was found
# {z}: Z Coordinate location where the ore was found
# {world}: The name of the world where the ore was found
ban-message: '§8[§cXCatch§8] §7Banned §c{player}§7 for §cx-ray§7 for §c{duration}§7. §c{player}§7 mined §c{amount} {ore}§7. (§c{flags}§7)'

############################################################
Expand All @@ -147,6 +153,7 @@ message-alert: true
# {x}: X Coordinate location where the ore was found
# {y}: Y Coordinate location where the ore was found
# {z}: Z Coordinate location where the ore was found
# {world}: The name of the world where the ore was found
alert-message-discord: '[XCatch] Flagged {player} for potential x-ray. {player} mined {amount} {ore}. ({flags})'

# Should XCatch send a Discord message for a ban?
Expand All @@ -162,6 +169,7 @@ message-ban: true
# {x}: X Coordinate location where the ore was found
# {y}: Y Coordinate location where the ore was found
# {z}: Z Coordinate location where the ore was found
# {world}: The name of the world where the ore was found
ban-message-discord: '[XCatch] Banned {player} for x-ray. {player} mined {amount} {ore}. ({flags})'

############################################################
Expand Down