Skip to content

Commit

Permalink
0.0.3 $ Fixed OPC System Bug & Added SVC & Discord Webhook System
Browse files Browse the repository at this point in the history
  • Loading branch information
hacimertgokhan committed Aug 19, 2022
1 parent a51f527 commit df05313
Show file tree
Hide file tree
Showing 13 changed files with 407 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Elecration-Server/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>nachospigot</artifactId>
<artifactId>elecration</artifactId>
<packaging>jar</packaging>
<version>1.8.8-R0.1-SNAPSHOT</version>
<name>Elecration</name>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
package eu.mixeration.Elecration;

import com.google.common.collect.Sets;
import dev.cobblesword.nachospigot.Nacho;
import dev.cobblesword.nachospigot.hitdetection.LagCompensator;
import dev.cobblesword.nachospigot.protocol.MovementListener;
import dev.cobblesword.nachospigot.protocol.PacketListener;
import eu.mixeration.Elecration.utils.WebhookUtils;
import me.elier.nachospigot.config.NachoConfig;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import xyz.sculas.nacho.anticrash.AntiCrash;
import xyz.sculas.nacho.async.AsyncExplosions;

import java.util.Set;

public class Elecration {

public static final Logger LOGGER = LogManager.getLogger(Elecration.class);
private static Elecration INSTANCE;

public Elecration() {
INSTANCE = this;
}

public static Elecration get() {
return INSTANCE == null ? new Elecration() : INSTANCE;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import com.google.common.base.Throwables;
import eu.mixeration.Elecration.commands.Management_OP;
import eu.mixeration.Elecration.commands.Management_PCC;
import eu.mixeration.Elecration.commands.Management_SVC;
import eu.mixeration.Elecration.utils.StringUtils;
import net.minecraft.server.MinecraftServer;
import eu.mixeration.Elecration.utils.WebhookUtils;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.configuration.InvalidConfigurationException;
Expand All @@ -22,6 +23,8 @@
import java.util.Map;
import java.util.logging.Level;

import static net.minecraft.server.MinecraftServer.getServer;

public class ElecrationConfig {

private static final String HEADER = "Elecration, All rights reserved.";
Expand All @@ -36,7 +39,7 @@ public class ElecrationConfig {

public static void registerCommands() {
for (Map.Entry<String, Command> entry : commands.entrySet()) {
MinecraftServer.getServer().server.getCommandMap().register(entry.getKey(), "Elecration", entry.getValue());
getServer().server.getCommandMap().register(entry.getKey(), "Elecration", entry.getValue());
}
}

Expand Down Expand Up @@ -134,13 +137,24 @@ public static void init(File configFile) {
version = getInt("config-version", 0);
set("mxr$elecration", false);
set("config-version", 0);
elecrationValues();
set("mxr$elecration", true);
if(config.getBoolean("mxr$elecration")) {
Elecration.LOGGER.info("elecration.yml loaded...");
} else {
elecrationValues();
}
if (config.getBoolean("elecration.settings.use-operator-password-security")) {
commands.put("Op", new Management_OP("Op"));
commands.put("DeOp", new Management_OP("DeOp"));
}
commands.put("Pcc", new Management_PCC("Pcc"));
commands.put("Svc", new Management_SVC("Svc"));
readConfig(ElecrationConfig.class, null);
if(ElecrationConfig.config.getBoolean("elecration.settings.use-discord")) {
Elecration.LOGGER.info( "Sending test message for discord..." );
WebhookUtils.sendMessageToDiscord(ElecrationConfig.config.getString("elecration.settings.discord.token"), "Elecration", "Your server has just started...");
} else {
Elecration.LOGGER.info( "Elecration Discord Module is not enable, Skipping..." );
}
}

public static String generateRandomPassword(int len) {
Expand Down Expand Up @@ -178,24 +192,68 @@ public static void elecrationValues() {
help_message.add("");
/* PCC | Plugin Control Command | */

/* PCC | Plugin Control Command | */
List<String> server_message = new ArrayList<>();
server_message.add("");
server_message.add("§8- §a/Svc reload motd §7: Refresh server motd.");
server_message.add("§8- §a/Svc pvp deny/allow §7: Enable or disable pvp.");
server_message.add("§8- §a/Svc flight deny/allow §7: Enable or disable pvp.");
server_message.add("§8- §a/Svc lmt-ambient §7: Change ambient entity limits.");
server_message.add("§8- §a/Svc lmt-monster §7: Change ambient entity limits.");
server_message.add("§8- §a/Svc lmt-animal §7: Change ambient entity limits.");
server_message.add("§8- §a/Svc lmt-wateranimal §7: Change ambient entity limits.");
server_message.add("§8- §a/Svc create-world-backup <world-name> §7: Create world backup (Copy)");
server_message.add("");
/* PCC | Plugin Control Command | */

if (!config.getBoolean("mxr$elecration")) {
config.set("elecration.messages.help.pcc", help_message);
config.set("elecration.messages.help.operator", operator_message);
config.set("elecration.messages.help.server", server_message);
config.set("elecration.messages.no-permission", no_permission);
config.set("elecration.messages.operator.wrong-password", wrong_password);
config.set("elecration.messages.operator.unknow", unknow_or_null);
config.set("elecration.messages.operator.gived", operator_gived);
config.set("elecration.messages.operator.tooked", operator_tooked);
config.set("elecration.messages.operator.usage", usage);
config.set("elecration.messages.operator.title", title);
config.set("elecration.messages.server.must-be-int", "§7Cant understand, must be §c(0-9/INTEGER)");
config.set("elecration.messages.server.unknow-value", "§7Cant understand, must be §c(%s)");
config.set("elecration.messages.server.motd-changed", "§7Server motd just changed");
config.set("elecration.messages.server.pvp.allow", "§7Players can fight (PCF): Allow");
config.set("elecration.messages.server.pvp.deny", "§7Players can fight (PCF): Deny");
config.set("elecration.messages.server.flight.allow", "§7Players can flight (PCFLY): Allow");
config.set("elecration.messages.server.world-not-found", "§7Cant found world with selected name.");
config.set("elecration.messages.server.backup-done", "§2§lBACKUP DONE ! (COPIED) §a(World: %s)");
config.set("elecration.messages.server.backup-name", "§7Backup folder name is: §a(%s)");
config.set("elecration.messages.server.flight.deny", "§7Players can flight (PCFLY): Deny");
config.set("elecration.messages.server.spawn-limit-changed", "§7Limit: §e(%s) §f- §bType: (%s) §f- §cWorld: (%s)");
config.set("elecration.settings.use-operator-password-security", true);
config.set("elecration.settings.operator.password", generateRandomPassword(5));
config.set("elecration.settings.use-motd", true);
config.set("elecration.settings.use-discord", true);
config.set("elecration.settings.discord.token", "https://discord.com/api/webhooks/1010134066357600316/Tsn48ucRV46FPzjA4TKVzLy3LkbI-Oo4him2sCoZlS5lADUkwwjJ54ubC1qk7vva5w_4");
config.set("elecration.settings.operator.password", generateRandomPassword(7));
set("mxr$elecration", true);
changeMotd();
saveElecration();

}

}

public static void changeMotd(){
if(config.getBoolean("elecration.settings.use-motd")) {

/* MOTD | */
List<String> motd_texts = new ArrayList<>();
motd_texts.add("§9§lELECRATION §8- §7Like electron §8- §fMixeration<line>§fElecration Spigot 1.8.8 Modernized Fork");
/* MOTD | */

config.set("elecration.settings.motd", motd_texts);

}
}

public static String getMessage(String message) {
return StringUtils.doColor(config.getString("elecration.messages." + message));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package eu.mixeration.Elecration.commands;

import eu.mixeration.Elecration.Elecration;
import eu.mixeration.Elecration.ElecrationConfig;
import eu.mixeration.Elecration.utils.IntegerUtils;
import eu.mixeration.Elecration.utils.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.entity.Player;

import java.io.File;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;

import static eu.mixeration.Elecration.ElecrationConfig.getMessage;
import static eu.mixeration.Elecration.utils.StringUtils.doColor;
import static eu.mixeration.Elecration.utils.WorldUtils.copyWorld;
import static org.bukkit.Bukkit.getServer;

public class Management_SVC extends Command {
public String right = doColor("&8&l»");
public String name = doColor("&9&lElecration");

public Management_SVC(String name) {
super(name);
this.description = "Server Control Command.";
this.usageMessage = "/svc";
this.setPermission("elecration.management.server");
}

@Override
@Deprecated
public boolean execute(CommandSender sender, String currentAlias, String[] args) {
if (testPermission(sender)) {
if (sender instanceof Player) {
Player player = ((Player) sender).getPlayer();
if (player.isOp()) {
if(args.length == 0 || args[0].equalsIgnoreCase("help")) {
help(player);
} else if (args.length == 2) {
if(args[0].equalsIgnoreCase("reload")) {
if(args[1].equalsIgnoreCase("motd")) {
player.sendMessage(getMessage("server.motd-changed"));
for(String motd : ElecrationConfig.config.getStringList("elecration.settings.motd")) {
CraftServer.console.setMotd(doColor(motd.replace("<line>", "\n").replace("<online>", String.valueOf(getServer().getOnlinePlayers()))));
}
} else {
help(player);
}
} else if (args[0].equalsIgnoreCase("pvp")) {
if(args[1].equalsIgnoreCase("deny")) {
CraftServer.console.setPVP(false);
player.sendMessage(getMessage("server.pvp.deny"));
} else if (args[1].equalsIgnoreCase("allow")) {
CraftServer.console.setPVP(true);
player.sendMessage(getMessage("server.pvp.allow"));
} else {
player.sendMessage(String.format(getMessage("server.unknow-value"), "deny/allow"));
}
} else if (args[0].equalsIgnoreCase("flight")) {
if(args[1].equalsIgnoreCase("deny")) {
CraftServer.console.setAllowFlight(false);
player.sendMessage(getMessage("server.flight.deny"));
} else if (args[1].equalsIgnoreCase("allow")) {
CraftServer.console.setAllowFlight(true);
player.sendMessage(getMessage("server.flight.allow"));
} else {
player.sendMessage(String.format(getMessage("server.unknow-value"), "deny/allow"));
}
} else if (args[0].equalsIgnoreCase("lmt-ambient")) {
if (IntegerUtils.isInt(args[1])) {
player.sendMessage(String.format(getMessage("server.spawn-limit-changed"), args[1], "TYPE-OF: Ambient", player.getWorld().getName()));
Bukkit.getWorld(player.getWorld().getName()).setAmbientSpawnLimit(Integer.parseInt(args[1]));
} else {
player.sendMessage(getMessage("server.must-be-int"));
}
} else if (args[0].equalsIgnoreCase("lmt-monster")) {
if (IntegerUtils.isInt(args[1])) {
player.sendMessage(String.format(getMessage("server.spawn-limit-changed"), args[1], "TYPE-OF: Monster", player.getWorld().getName()));
Bukkit.getWorld(player.getWorld().getName()).setMonsterSpawnLimit(Integer.parseInt(args[1]));
} else {
player.sendMessage(getMessage("server.must-be-int"));
}
} else if (args[0].equalsIgnoreCase("lmt-animal")) {
if (IntegerUtils.isInt(args[1])) {
player.sendMessage(String.format(getMessage("server.spawn-limit-changed"), args[1], "TYPE-OF: Animal", player.getWorld().getName()));
Bukkit.getWorld(player.getWorld().getName()).setAnimalSpawnLimit(Integer.parseInt(args[1]));
} else {
player.sendMessage(getMessage("server.must-be-int"));
}
} else if (args[0].equalsIgnoreCase("lmt-wateranimal")) {
if (IntegerUtils.isInt(args[1])) {
player.sendMessage(String.format(getMessage("server.spawn-limit-changed"), args[1], "TYPE-OF: Water Animal", player.getWorld().getName()));
Bukkit.getWorld(player.getWorld().getName()).setWaterAnimalSpawnLimit(Integer.parseInt(args[1]));
} else {
player.sendMessage(getMessage("server.must-be-int"));
}
} else if (args[0].equalsIgnoreCase("create-world-backup")) {
World world = Bukkit.getWorld(args[1]);
if (world == null) {
player.sendMessage(getMessage("server.world-not-found"));
} else {
Format dayName = new SimpleDateFormat("EEEE");
String str = dayName.format(new Date());
String backupName = args[1] + " " + str + "$" + StringUtils.random(4);
File worldFolder = Bukkit.getWorld(args[1]).getWorldFolder();
File folder = new File(getServer().getWorldContainer().getAbsolutePath(), File.separator + "backup-worlds" + File.separator + backupName);
if (!folder.exists()) {
try {
folder.mkdir();
} catch (Exception e) {
e.printStackTrace();
}
}
copyWorld(worldFolder, folder);
player.sendMessage(String.format(getMessage("server.backup-done"), world.getName()));
player.sendMessage(String.format(getMessage("server.backup-name"), backupName));
}
} else {
help(player);
}
} else {
help(player);
}
} else {
player.sendMessage(getMessage("no-permission"));
}
} else {
Elecration.LOGGER.warn("Hey ! I am sorry but you cannot use that command from console...");
}
}
return true;
}

public void help(CommandSender sender) {
if (sender instanceof Player) {
Player player = ((Player) sender).getPlayer();
for (String help : ElecrationConfig.config.getStringList("elecration.messages.help.server")) {
player.sendMessage(ChatColor.translateAlternateColorCodes('&', help));
}
} else {
for (String help : ElecrationConfig.config.getStringList("elecration.messages.help.server")) {
Bukkit.getConsoleSender().sendMessage(ChatColor.translateAlternateColorCodes('&', help));
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package eu.mixeration.Elecration.utils;

public class IntegerUtils {

public static boolean isInt(String s) {
try {
Integer.parseInt(s);
} catch (NumberFormatException nfe) {
return false;
}
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.bukkit.ChatColor;

import java.awt.*;
import java.security.SecureRandom;

public class StringUtils {

Expand Down Expand Up @@ -34,4 +35,15 @@ public static Color HexToColor(String hex) {
return null;
}

public static String random(int len) {
final String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
SecureRandom random = new SecureRandom();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < len; i++) {
int randomIndex = random.nextInt(chars.length());
sb.append(chars.charAt(randomIndex));
}
return sb.toString();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package eu.mixeration.Elecration.utils;

import javax.net.ssl.HttpsURLConnection;
import java.io.OutputStream;
import java.net.URL;

public class WebhookUtils {

public static void sendMessageToDiscord(String token, String title, String message) {
String jsonBrut = "";
jsonBrut += "{\"embeds\": [{"
+ "\"title\": \""+ title +"\","
+ "\"description\": \""+ message +"\","
+ "\"color\": 15258703"
+ "}]}";
try {
URL url = new URL(token);
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.addRequestProperty("Content-Type", "application/json");
con.addRequestProperty("User-Agent", "Java-DiscordWebhook-BY-Gelox_");
con.setDoOutput(true);
con.setRequestMethod("POST");
OutputStream stream = con.getOutputStream();
stream.write(jsonBrut.getBytes());
stream.flush();
stream.close();
con.getInputStream().close();
con.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}

}
Loading

0 comments on commit df05313

Please sign in to comment.