Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
OllieJW authored Jun 18, 2021
1 parent e05d09e commit 9956433
Show file tree
Hide file tree
Showing 10 changed files with 801 additions and 0 deletions.
103 changes: 103 additions & 0 deletions src/main/java/com/olliejw/cmdwebhooks/CmdWebhooks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package com.olliejw.cmdwebhooks;

import com.olliejw.cmdwebhooks.Commands.Reload;
import com.olliejw.cmdwebhooks.Commands.Send;
import com.olliejw.cmdwebhooks.Events.OnCmd;
import com.olliejw.cmdwebhooks.Events.OnJoin;
import com.olliejw.cmdwebhooks.Events.OnLeave;
import com.olliejw.cmdwebhooks.Events.OnMsg;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;

public class CmdWebhooks extends JavaPlugin implements Listener {

private String webhook;
private static CmdWebhooks instance;

public static CmdWebhooks instance() {
return instance;
}

public void onEnable() {
this.saveDefaultConfig();

//============== COMMANDS ==============//
this.getCommand("cmdw-reload").setExecutor(new Reload(this));
this.getCommand("cmdw-send").setExecutor(new Send(this));

//============== STARTUP MESSAGE ==============//
this.webhook = this.getConfig().getString("WebhookURL");
String message;
if (this.getConfig().getBoolean("Start")) {
this.sendDiscordMessage(this.getConfig().getString("Start.Message"));
}

//============== REGISTER EVENTS ==============//
if (this.getConfig().getBoolean("Join.Enabled")) {
message = this.getConfig().getString("Join.Message");
if (!this.getConfig().getString("Join.Webhook").equals("DEFAULT")) {
webhook = (this.getConfig().getString("Join.Webhook"));
} else {
webhook = (this.getConfig().getString("DefaultURL"));
}
this.getServer().getPluginManager().registerEvents(new OnJoin(webhook, message), this);
}
if (this.getConfig().getBoolean("Leave.Enabled")) {
message = this.getConfig().getString("Leave.Message");
if (!this.getConfig().getString("Leave.Webhook").equals("DEFAULT")) {
webhook = (this.getConfig().getString("Leave.Webhook"));
} else {
webhook = (this.getConfig().getString("DefaultURL"));
}
this.getServer().getPluginManager().registerEvents(new OnLeave(webhook, message), this);
}
if (this.getConfig().getBoolean("Msg.Enabled")) {
message = this.getConfig().getString("Msg.Message");
if (!this.getConfig().getString("Msg.Webhook").equals("DEFAULT")) {
webhook = (this.getConfig().getString("Msg.Webhook"));
} else {
webhook = (this.getConfig().getString("DefaultURL"));
}
this.getServer().getPluginManager().registerEvents(new OnMsg(webhook, message), this);
}
if (this.getConfig().getBoolean("Cmd.Enabled")) {
message = this.getConfig().getString("Cmd.Message");
if (!this.getConfig().getString("Cmd.Webhook").equals("DEFAULT")) {
webhook = (this.getConfig().getString("Cmd.Webhook"));
} else {
webhook = (this.getConfig().getString("DefaultURL"));
}
this.getServer().getPluginManager().registerEvents(new OnCmd(webhook, message), this);
}
//================= END =================//
}

private void sendDiscordMessage(String message) {
SendWebhook webhook = new SendWebhook(this.webhook);
webhook.setContent(message);

try {
webhook.execute();
} catch (MalformedURLException var4) {
System.out.println("[CmdWebhook] Invalid webhook");
} catch (IOException var5) {
var5.printStackTrace();
}

}

public void onDisable() {
File config = new File(getDataFolder(), "config.yml");
FileConfiguration cfg = YamlConfiguration.loadConfiguration(config);

if (cfg.getBoolean("Stop")) {
this.sendDiscordMessage(cfg.getString("ServerStop"));
}
}
}
28 changes: 28 additions & 0 deletions src/main/java/com/olliejw/cmdwebhooks/Commands/Reload.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.olliejw.cmdwebhooks.Commands;

import com.olliejw.cmdwebhooks.CmdWebhooks;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;

public class Reload implements CommandExecutor {
public CmdWebhooks pl;

public Reload(final CmdWebhooks main) {
this.pl = main;
}

@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (cmd.getName().equalsIgnoreCase("cmdw-reload")) {
if (sender.hasPermission("cmdw.reload")) {
pl.reloadConfig();
sender.sendMessage(ChatColor.GREEN + "Reloaded");
} else {
sender.sendMessage(ChatColor.RED + "You are not permitted to run this command");
}
}
return true;
}
}
44 changes: 44 additions & 0 deletions src/main/java/com/olliejw/cmdwebhooks/Commands/Send.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.olliejw.cmdwebhooks.Commands;

import com.olliejw.cmdwebhooks.CmdWebhooks;
import com.olliejw.cmdwebhooks.SendWebhook;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;

public class Send implements CommandExecutor {
public CmdWebhooks pl;

public Send(final CmdWebhooks main) {
this.pl = main;
}

@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (cmd.getName().equalsIgnoreCase("cmdw-send")) {
if (sender.hasPermission("cmdw.send")) {
String url = pl.getConfig().getString("SendURL");
SendWebhook webhook = new SendWebhook(url);
StringBuilder sb = new StringBuilder();
for (String arg : args) {
sb.append(arg).append(" ");
}
String toSend = sb.toString();
webhook.setContent(toSend);

try {
webhook.execute();
sender.sendMessage(ChatColor.GREEN + "Sent message!");
} catch (Exception e) {
System.out.println("ERROR SENDING VOTE WEBHOOK!");
}

} else {
sender.sendMessage(ChatColor.RED + "You are not permitted to run this command");
}
}
return true;
}
}

38 changes: 38 additions & 0 deletions src/main/java/com/olliejw/cmdwebhooks/Events/OnCmd.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.olliejw.cmdwebhooks.Events;

import com.olliejw.cmdwebhooks.SendWebhook;
import org.bukkit.ChatColor;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;

import java.io.IOException;
import java.net.MalformedURLException;

public class OnCmd implements Listener {
final String url;
final String message;

public OnCmd(String url, String message) {
this.url = url;
this.message = message;
}

@EventHandler
public void onCmd(PlayerCommandPreprocessEvent event) {
String pl = event.getPlayer().getDisplayName();
SendWebhook webhook = new SendWebhook(this.url);
String toSend = String.format(this.message, event.getPlayer().getDisplayName())
.replace("[player]", ChatColor.stripColor(pl))
.replace("[cmd]", event.getMessage());
webhook.setContent(toSend);

try {
webhook.execute();
} catch (MalformedURLException var5) {
System.out.println("[CmdWebhook] Error sending Webhook!");
} catch (IOException var6) {
var6.printStackTrace();
}
}
}
38 changes: 38 additions & 0 deletions src/main/java/com/olliejw/cmdwebhooks/Events/OnJoin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.olliejw.cmdwebhooks.Events;

import com.olliejw.cmdwebhooks.SendWebhook;
import org.bukkit.ChatColor;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;

import java.io.IOException;
import java.net.MalformedURLException;

public class OnJoin implements Listener {
final String url;
final String message;

public OnJoin(String url, String message) {
this.url = url;
this.message = message;
}

@EventHandler
public void onJoin(PlayerJoinEvent event) {
String pl = event.getPlayer().getDisplayName();
SendWebhook webhook = new SendWebhook(this.url);
String toSend = String.format(this.message, event.getPlayer().getDisplayName())
.replace("[player]", ChatColor.stripColor(pl));
webhook.setContent(toSend);

try {
webhook.execute();
} catch (MalformedURLException var5) {
System.out.println("[CmdWebhook] Error sending Webhook!");
} catch (IOException var6) {
var6.printStackTrace();
}

}
}
38 changes: 38 additions & 0 deletions src/main/java/com/olliejw/cmdwebhooks/Events/OnLeave.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.olliejw.cmdwebhooks.Events;

import com.olliejw.cmdwebhooks.SendWebhook;
import org.bukkit.ChatColor;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;

import java.io.IOException;
import java.net.MalformedURLException;

public class OnLeave implements Listener {
final String url;
final String message;

public OnLeave(String url, String message) {
this.url = url;
this.message = message;
}

@EventHandler
public void onLeave(PlayerQuitEvent event) {
String pl = event.getPlayer().getDisplayName();
SendWebhook webhook = new SendWebhook(this.url);
String toSend = String.format(this.message, event.getPlayer().getDisplayName())
.replace("[player]", ChatColor.stripColor(pl));
webhook.setContent(toSend);

try {
webhook.execute();
} catch (MalformedURLException var5) {
System.out.println("[CmdWebhook] Error sending Webhook!");
} catch (IOException var6) {
var6.printStackTrace();
}

}
}
39 changes: 39 additions & 0 deletions src/main/java/com/olliejw/cmdwebhooks/Events/OnMsg.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.olliejw.cmdwebhooks.Events;

import com.olliejw.cmdwebhooks.SendWebhook;
import org.bukkit.ChatColor;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;

import java.io.IOException;
import java.net.MalformedURLException;

public class OnMsg implements Listener {
final String url;
final String message;

public OnMsg(String url, String message) {
this.url = url;
this.message = message;
}

@EventHandler
public void onMsg(AsyncPlayerChatEvent event) {
String pl = event.getPlayer().getDisplayName();
SendWebhook webhook = new SendWebhook(this.url);
String toSend = String.format(this.message, event.getPlayer().getDisplayName())
.replace("[player]", ChatColor.stripColor(pl))
.replace("[msg]", event.getMessage());
webhook.setContent(toSend);

try {
webhook.execute();
} catch (MalformedURLException var5) {
System.out.println("[CmdWebhook] Error sending Webhook!");
} catch (IOException var6) {
var6.printStackTrace();
}

}
}
Loading

0 comments on commit 9956433

Please sign in to comment.