Testing an notifications

Signed-off-by: Etzelia <etzelia@hotmail.com>
grief
Etzelia 2020-07-28 23:30:41 -05:00
parent 5d2e147a89
commit 2e3d7acde1
No known key found for this signature in database
GPG Key ID: 708511AE7ABC5314
3 changed files with 116 additions and 29 deletions

11
pom.xml
View File

@ -34,11 +34,6 @@
<version>1.16.1-R0.1-SNAPSHOT</version> <version>1.16.1-R0.1-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>xyz.etztech</groupId>
<artifactId>EtzCore</artifactId>
<version>1.0.5</version>
</dependency>
<dependency> <dependency>
<groupId>commons-lang</groupId> <groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId> <artifactId>commons-lang</artifactId>
@ -52,17 +47,13 @@
<id>spigotmc-repo</id> <id>spigotmc-repo</id>
<url>https://hub.spigotmc.org/nexus/content/groups/public/</url> <url>https://hub.spigotmc.org/nexus/content/groups/public/</url>
</repository> </repository>
<repository>
<id>etztech-repo</id>
<url>http://repo.etztech.xyz</url>
</repository>
<repository> <repository>
<id>mvn-repo</id> <id>mvn-repo</id>
<url>https://mvnrepository.com/artifact/</url> <url>https://mvnrepository.com/artifact/</url>
</repository> </repository>
<repository> <!-- This repo fixes issues with transitive dependencies --> <repository> <!-- This repo fixes issues with transitive dependencies -->
<id>jcenter</id> <id>jcenter</id>
<url>http://jcenter.bintray.com</url> <url>https://jcenter.bintray.com</url>
</repository> </repository>
<repository> <repository>
<id>jitpack.io</id> <id>jitpack.io</id>

View File

@ -10,7 +10,8 @@ import org.bukkit.command.CommandSender;
public enum Lang { public enum Lang {
NO_PERMISSION("You don't have permission to do that.", Color.ERROR), NO_PERMISSION("You don't have permission to do that.", Color.ERROR),
UNKNOWN_COMMAND("This command wasn't recognized.", Color.ERROR), UNKNOWN_COMMAND("This command wasn't recognized.", Color.ERROR),
PLUGIN_RELOADED("Plugin reloaded.", Color.INFO); PLUGIN_RELOADED("Plugin reloaded.", Color.INFO),
ALERT("%s has found %d %s veins.", null);
private final String message; private final String message;
private final ChatColor color; private final ChatColor color;
@ -20,6 +21,14 @@ public enum Lang {
this.color = color; this.color = color;
} }
public String getMessage() {
return this.message;
}
public ChatColor getColor() {
return this.color;
}
public void sms(CommandSender sender) { public void sms(CommandSender sender) {
TextComponent text = new TextComponent(this.message); TextComponent text = new TextComponent(this.message);
text.setColor(this.color); text.setColor(this.color);
@ -29,7 +38,7 @@ public enum Lang {
public static String getMaterialName(Material material) { public static String getMaterialName(Material material) {
String name = material.name(); String name = material.name();
name = name.replaceAll("_", " "); name = name.replaceAll("_", " ");
name = WordUtils.capitalize(name); name = WordUtils.capitalize(name.toLowerCase());
return name; return name;
} }

View File

@ -1,6 +1,11 @@
package xyz.etztech.orealert.listeners; package xyz.etztech.orealert.listeners;
import org.bukkit.Location; import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
@ -11,7 +16,14 @@ import org.bukkit.event.block.BlockBreakEvent;
import xyz.etztech.orealert.Lang; import xyz.etztech.orealert.Lang;
import xyz.etztech.orealert.OreAlert; import xyz.etztech.orealert.OreAlert;
import java.time.LocalDateTime; import java.awt.*;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Queue;
import java.util.*; import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ConcurrentLinkedQueue;
@ -31,25 +43,24 @@ public class BlockBreakListener implements Listener {
@EventHandler @EventHandler
public void onBlockBreak(BlockBreakEvent event) { public void onBlockBreak(BlockBreakEvent event) {
queue.add(new BlockEvent(event.getPlayer(), event.getBlock(), true)); queue.add(new BlockEvent(event.getPlayer(), event.getBlock().getType(), true));
int radius = this.plugin.getConfig().getInt("radius", 3); int radius = this.plugin.getConfig().getInt("radius", 3);
for (int x = -radius; x < radius; x++) { for (int x = -radius; x < radius; x++) {
for (int y = -radius; y < radius; y++) { for (int y = -radius; y < radius; y++) {
for (int z = -radius; z < radius; z++) { for (int z = -radius; z < radius; z++) {
queue.add(new BlockEvent(event.getPlayer(), event.getBlock().getRelative(x, y, z), false)); queue.add(new BlockEvent(event.getPlayer(), event.getBlock().getRelative(x, y, z).getType(), false));
} }
} }
} }
} }
private void task() { private void task() {
while (true) { while (this.plugin.isEnabled()) {
BlockEvent event = this.queue.poll(); BlockEvent event = this.queue.poll();
if (event != null) { if (event != null) {
List<String> blocks = this.plugin.getConfig().getStringList("blocks"); for (String s: this.plugin.getConfig().getConfigurationSection("blocks").getKeys(false)) {
for (String s: blocks) { if (Lang.getMaterialKey(event.getMaterial()).equals(s)) {
if (Lang.getMaterialKey(event.getBlock().getType()).equals(s)) {
addStrike(event); addStrike(event);
check(event); check(event);
break; break;
@ -66,9 +77,8 @@ public class BlockBreakListener implements Listener {
} }
private void check(BlockEvent event) { private void check(BlockEvent event) {
List<BlockEvent> events = map.getOrDefault(event.getPlayer(), new ArrayList<>());
FileConfiguration config = this.plugin.getConfig(); FileConfiguration config = this.plugin.getConfig();
String blockKey = Lang.getMaterialKey(event.getBlock().getType()); String blockKey = Lang.getMaterialKey(event.getMaterial());
int start = config.getInt(String.format("blocks.%s.start", blockKey), config.getInt("start", 5)); int start = config.getInt(String.format("blocks.%s.start", blockKey), config.getInt("start", 5));
int each = config.getInt(String.format("blocks.%s.each", blockKey), config.getInt("each", 5)); int each = config.getInt(String.format("blocks.%s.each", blockKey), config.getInt("each", 5));
int ping = config.getInt(String.format("blocks.%s.ping", blockKey), config.getInt("ping", 5)); int ping = config.getInt(String.format("blocks.%s.ping", blockKey), config.getInt("ping", 5));
@ -81,7 +91,16 @@ public class BlockBreakListener implements Listener {
} }
} }
// TODO Notify double alert = (double) strikes / start;
if (alert == 1) {
alert(event, strikes, true);
} else if (alert > 1) {
if (alert % ping == 0) {
alert(event, strikes, true);
} else if (alert % each == 0) {
alert(event, strikes, false);
}
}
} }
private void cleanup() { private void cleanup() {
@ -99,23 +118,91 @@ public class BlockBreakListener implements Listener {
int globalPurge = this.plugin.getConfig().getInt("purge", 30); int globalPurge = this.plugin.getConfig().getInt("purge", 30);
while (events.hasNext()) { while (events.hasNext()) {
BlockEvent e = events.next(); BlockEvent e = events.next();
int purge = 1000 * 60 * this.plugin.getConfig().getInt(String.format("blocks.%s.purge", Lang.getMaterialKey(e.getBlock().getType())), globalPurge); int purge = 1000 * 60 * this.plugin.getConfig().getInt(String.format("blocks.%s.purge", Lang.getMaterialKey(e.getMaterial())), globalPurge);
if (new Date(e.getTime().getTime() + purge).before(now)) { if (new Date(e.getTime().getTime() + purge).before(now)) {
events.remove(); events.remove();
} }
} }
} }
private void alert(BlockEvent event, int strikes, boolean ping) {
String message = String.format(Lang.ALERT.getMessage(),
event.getPlayer().getName(), strikes, Lang.getMaterialName(event.getMaterial()));
ChatColor color = ChatColor.of(this.plugin.getConfig().getString(String.format("blocks.%s.color", Lang.getMaterialKey(event.getMaterial())), "#00ffff"));
BaseComponent[] component = new ComponentBuilder()
.append(message).color(color)
.create();
for (Player player : Bukkit.getOnlinePlayers()) {
if (player.hasPermission("orealert.alert")) {
player.spigot().sendMessage(component);
}
}
// Webhook
String globalWebhook = this.plugin.getConfig().getString("webhook", "");
String webhook = this.plugin.getConfig().getString(String.format("blocks.%s.webhook", Lang.getMaterialKey(event.getMaterial())), globalWebhook);
if (!"".equals(webhook)) {
String embed = embed(event, message, ping, color.getColor());
this.plugin.getServer().getScheduler().runTaskAsynchronously(this.plugin, () -> {
try {
sendWebhook(webhook, embed);
} catch (Exception e) {
this.plugin.log(String.format("Could not send webhook: %s", e.getMessage()));
}
});
}
}
private String embed(BlockEvent event, String message, boolean ping, Color color) {
JsonObject json = new JsonObject();
if (ping) {
json.addProperty("content", "@here");
}
JsonArray embeds = new JsonArray();
JsonObject embed = new JsonObject();
embed.addProperty("color", color.getRGB());
embed.addProperty("description", message);
JsonObject author = new JsonObject();
author.addProperty("name", event.getPlayer().getName());
author.addProperty("icon_url", String.format("https://minotar.net/helm/%s/100.png", event.getPlayer().getName()));
embed.add("author", author);
embeds.add(embed);
json.add("embeds", embeds);
return json.toString();
}
private void sendWebhook(String webhook, String embed) throws Exception {
URL url = new URL(webhook);
URLConnection con = url.openConnection();
HttpURLConnection http = (HttpURLConnection) con;
http.setRequestMethod("POST");
http.setDoOutput(true);
byte[] out = embed.getBytes(StandardCharsets.UTF_8);
int length = out.length;
http.setFixedLengthStreamingMode(length);
http.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
http.connect();
OutputStream os = http.getOutputStream();
os.write(out);
}
} }
class BlockEvent { class BlockEvent {
private final Player player; private final Player player;
private final Block block; private final Material material;
private final Boolean parent; private final Boolean parent;
private final Date time; private final Date time;
BlockEvent(Player player, Block block, Boolean parent) { BlockEvent(Player player, Material material, Boolean parent) {
this.player = player; this.player = player;
this.block = block; this.material = material;
this.parent = parent; this.parent = parent;
this.time = Calendar.getInstance().getTime(); this.time = Calendar.getInstance().getTime();
} }
@ -124,8 +211,8 @@ class BlockEvent {
return player; return player;
} }
public Block getBlock() { public Material getMaterial() {
return block; return material;
} }
public Boolean isParent() { public Boolean isParent() {