Skip to content

Commit 6ffa4f7

Browse files
author
nazrin
committed
Rewrite (Part 1)
Частично изменено построение проекта Добавлена часть дженерик-методов для системы базы данных Удаление лишнего бойлерплейта, перепись некоторых моментов Заготовлена часть классов и методов для следующей части Некоторые методы и переменные были переименованы Использование рефлексии для инициализации команд Планируется: - Добавление автоматической системы обновлений через GitHub - Полное изменение структуры проекта - Создание адекватной конфиг-системы - Возможность кастомизации без изменения кода - Изменение структуры ембедов, добавление новых ембедов - Перепись некоторых команд - Устранение багов - Удаление ненужных функций и говнокода
1 parent 8567e9f commit 6ffa4f7

File tree

98 files changed

+618
-378
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+618
-378
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ version '1.0'
44

55
sourceCompatibility = JavaVersion.VERSION_16
66

7-
sourceSets.main.java.srcDirs = ["src"]
7+
sourceSets.main.java.srcDirs = ["src/java"]
88

99
compileJava.options.encoding = "UTF-8"
1010
compileTestJava.options.encoding = "UTF-8"
Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,26 @@
55
import arc.util.Timer;
66
import mindustry.Vars;
77
import mindustry.game.EventType;
8+
import mindustry.gen.Player;
89
import mindustry.mod.*;
910
import mindustry.net.Packets;
10-
import smp.commands.CommandRegister;
11-
import smp.commands.mindustry.admin.AdminChatCommand;
12-
import smp.commands.mindustry.admin.RevertCommand;
13-
import smp.commands.mindustry.basic.*;
14-
import smp.database.players.Counter;
11+
import smp.commandSystem.CommandRegister;
12+
import smp.commandSystem.mindustry.MindustryCommand;
1513

1614
import java.net.UnknownHostException;
15+
import java.util.List;
1716

1817
import static mindustry.Vars.netServer;
19-
import static org.bson.codecs.configuration.CodecRegistries.fromProviders;
20-
import static org.bson.codecs.configuration.CodecRegistries.fromRegistries;
18+
import static mindustry.Vars.player;
2119
import static smp.antigrief.NodeGriefingWarning.initializeNodeGriefingWarnings;
20+
import static smp.commandSystem.CommandRegister.getCommandsFromPackage;
2221
import static smp.database.players.PlayerChecks.*;
2322
import static smp.discord.Bot.initBot;
24-
import static smp.database.InitializeDatabase.initDatabase;
23+
import static smp.database.DatabaseSystem.initDatabase;
24+
import static smp.events.GameOverEvent.gameOverEvent;
2525
import static smp.events.PlayerJoinEvent.joinEvent;
2626
import static smp.events.PlayerLeaveEvent.leaveEvent;
27+
import static smp.events.SendMessageEvent.rtvEvent;
2728
import static smp.functions.Checks.kickIfBanned;
2829
import static smp.history.History.loadHistory;
2930
import static smp.history.History.loadRevert;
@@ -32,7 +33,7 @@
3233
import static smp.other.InitializeSettings.initializeSettings;
3334

3435
public class Main extends Plugin{
35-
public static CommandRegister register;
36+
public static CommandRegister<MindustryCommand<Player>> register;
3637

3738
//called when game initializes
3839
@Override
@@ -44,16 +45,15 @@ public void init() {
4445
initBot();
4546
initializeNodeGriefingWarnings();
4647
initializeRanks();
47-
Counter.initializeCounter();
48-
MongoDbCheck();
48+
initializeCounter();
4949
try {
5050
initializeSettings();
5151
} catch (UnknownHostException e) {
5252
throw new RuntimeException(e);
5353
}
5454
Vars.net.handleServer(Packets.Connect.class, (con, connect) -> {
5555
Events.fire(new EventType.ConnectionEvent(con));
56-
MongoDbPlayerIpCheck(con);
56+
CheckPlayerIP(con);
5757

5858
if (netServer.admins.isIPBanned(connect.addressTCP) || netServer.admins.isSubnetBanned(connect.addressTCP)){
5959
con.kick(Packets.KickReason.banned);
@@ -66,6 +66,12 @@ public void init() {
6666
Events.on(EventType.PlayerLeave.class, playerLeave -> {
6767
leaveEvent(playerLeave.player);
6868
});
69+
Events.on(EventType.GameOverEvent.class, playerLeave -> {
70+
gameOverEvent();
71+
});
72+
Events.on(EventType.TextInputEvent.class, textInputEvent -> {
73+
rtvEvent(textInputEvent.text, player);
74+
});
6975
}
7076

7177
@Override
@@ -77,36 +83,12 @@ public void registerServerCommands(CommandHandler handler){
7783
@Override
7884
//register commands that player can invoke in-game
7985
public void registerClientCommands(CommandHandler handler){
80-
TestCommand testCommand = new TestCommand();
81-
HelpCommand helpCommand = new HelpCommand();
82-
WhisperCommand whisperCommand = new WhisperCommand();
83-
SyncCommand syncCommand = new SyncCommand();
84-
TeamChatCommand teamChatCommand = new TeamChatCommand();
85-
VotekickCommand votekickCommand = new VotekickCommand();
86-
VoteCommand voteCommand = new VoteCommand();
87-
HistoryCommand historyCommand = new HistoryCommand();
88-
StatsCommand statsCommands = new StatsCommand();
89-
ServerHopCommand serverHopCommand = new ServerHopCommand();
90-
register = new CommandRegister(handler);
91-
register.registerCommand(testCommand);
92-
register.registerCommand(helpCommand);
93-
register.registerCommand(whisperCommand);
94-
register.registerCommand(syncCommand);
95-
register.registerCommand(teamChatCommand);
96-
register.registerCommand(votekickCommand);
97-
register.registerCommand(voteCommand,
98-
historyCommand,
99-
statsCommands,
100-
serverHopCommand);
10186

87+
register = new CommandRegister<>(handler);
88+
89+
register.registerCommands((List<MindustryCommand<Player>>) getCommandsFromPackage("smp.commandSystem.commands.mindustry"));
10290
Timer.schedule(() -> {
10391
register.updateCommands();
10492
}, 5);
105-
/* admin commands */
106-
107-
AdminChatCommand adminChatCommand = new AdminChatCommand();
108-
// OverwriteCommand overwriteCommand = new OverwriteCommand();
109-
RevertCommand revertCommand = new RevertCommand();
110-
register.registerCommand(adminChatCommand, revertCommand);
11193
}
11294
}
Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package smp;
22

33
import arc.struct.ObjectMap;
4+
import arc.struct.Seq;
45
import arc.util.Nullable;
56
import arc.util.Timekeeper;
67
import mindustry.core.NetServer;
78
import mindustry.gen.Groups;
9+
import mindustry.gen.Player;
810
import mindustry.net.Administration;
9-
import smp.commands.mindustry.basic.VotekickCommand;
11+
import smp.commandSystem.commands.mindustry.basic.VotekickCommand;
12+
import smp.models.Setting;
13+
14+
import java.util.concurrent.atomic.AtomicInteger;
1015

1116
public class Variables {
1217

@@ -24,12 +29,17 @@ public class Variables {
2429
public static int votesRequired(){
2530
return 2 + (Groups.player.size() > 4 ? 1 : 0);
2631
}
27-
/** VOTEKICK DONE */
2832

29-
public static Administration admins = new Administration();
33+
/** RTV SYSTEM */
34+
public static AtomicInteger votes = new AtomicInteger(0);
35+
public static Seq<Player> votedPlayer = new Seq<>();
36+
public static boolean isVoting = false;
3037

31-
public static NetServer.ChatFormatter chatFormatter = (player, message) -> player == null ? message : "[coral][[" + player.coloredName() + "[coral]]:[white] " + message;
38+
public static Administration admins = new Administration();
39+
public static NetServer.ChatFormatter chatFormatter = (player, message) -> player == null ?
40+
message : "[coral][[" + player.coloredName() + "[coral]]:[white] " + message;
3241

42+
/** DISCORD SYSTEM */
3343
public static long mindustryModeratorID;
3444

3545
public static long mindustryConsoleID;
@@ -42,4 +52,6 @@ public static int votesRequired(){
4252

4353
public static String discordToken;
4454

55+
public static Setting currentSetting;
56+
4557
}

src/smp/antigrief/NodeGriefingWarning.java renamed to src/java/smp/antigrief/NodeGriefingWarning.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ public static void initializeNodeGriefingWarnings() {
1515
Events.on(EventType.ConfigEvent.class, event -> {
1616
if (event.tile.block() == Blocks.powerNode || event.tile.block() == Blocks.powerNodeLarge) {
1717
if (event.tile.buildOn().power.links.isEmpty() && !(event.value instanceof Integer) && event.player != null) {
18-
String str = "[orange]Warning! Possible node griefing at: " + event.tile.tileX() + " " + event.tile.tileY() + " by: " + event.player.coloredName();
18+
String str = "[orange]Warning! Possible node griefing at: "
19+
+ event.tile.tileX()
20+
+ " "
21+
+ event.tile.tileY() + " by: "
22+
+ event.player.coloredName();
1923
Groups.player.each(Player::admin, a -> a.sendMessage(str));
2024
}
2125
}

src/smp/commands/CommandParameter.java renamed to src/java/smp/commandSystem/CommandParameter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
package smp.commands;
1+
package smp.commandSystem;
22

33
public class CommandParameter {
44
public final String name, rawname;
55
public final boolean optional, variable;
66

77
public CommandParameter(String name) {
8-
this.name = name.substring(1, name.length() - 1);;
8+
this.name = name.substring(1, name.length() - 1);
99
this.rawname = name;
1010

1111
this.optional = name.startsWith("[") && name.endsWith("]");

src/smp/commands/CommandRegister.java renamed to src/java/smp/commandSystem/CommandRegister.java

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,35 @@
1-
package smp.commands;
1+
package smp.commandSystem;
22

33
import arc.struct.Seq;
44
import arc.util.CommandHandler;
55
import arc.util.Log;
66
import mindustry.Vars;
77
import mindustry.gen.Player;
8-
import smp.commands.ds.discord.DiscordCommand;
8+
import smp.commandSystem.discord.DiscordCommand;
9+
import smp.commandSystem.mindustry.MindustryCommand;
10+
11+
import java.io.IOException;
12+
import java.lang.reflect.InvocationTargetException;
13+
import java.util.ArrayList;
14+
import java.util.List;
15+
import java.util.Objects;
916

1017
import static arc.util.Log.log;
18+
import static smp.functions.Utilities.notNullElse;
19+
import static smp.system.reflect.ReflectSystem.exportClasses;
1120

12-
public class CommandRegister<T extends BasicCommand<Player>> {
21+
public class CommandRegister<T extends MindustryCommand<Player>> {
1322

14-
public final Seq<T> commands = new Seq<>();
23+
public final Seq<T> mindustryCommands = new Seq<>();
1524
public static Seq<DiscordCommand> discordCommands = new Seq<>();
1625
public final Seq<T> adminCommands = new Seq<>();
1726
public final CommandHandler handler;
1827

1928
public CommandRegister(CommandHandler handler) {
2029
this.handler = handler;
2130
}
22-
@SafeVarargs
23-
public final void registerCommand(T... commands) {
31+
32+
public final void registerCommands(List<T> commands) {
2433
for (var command: commands) {
2534
if (handler.getCommandList().contains(t -> t.text.equalsIgnoreCase(command.name))) handler.removeCommand(command.name);
2635

@@ -29,7 +38,7 @@ public final void registerCommand(T... commands) {
2938
if (command.isAdmin){
3039
this.adminCommands.add(command);
3140
} else {
32-
this.commands.add(command);
41+
this.mindustryCommands.add(command);
3342
}
3443

3544
for (var param : command.params) builder.append(param.rawname).append(" ");
@@ -42,11 +51,13 @@ public final void registerCommand(T... commands) {
4251
}
4352
}
4453
}
45-
public static void registerDiscordCommand(DiscordCommand... commands) {
54+
55+
public static void registerDiscordCommands(List<DiscordCommand> commands) {
4656
for (var command : commands) {
4757
discordCommands.add(command);
4858
}
4959
}
60+
5061
public void updateCommands(){
5162
for (CommandHandler.Command cmd : Vars.netServer.clientCommands.getCommandList()){
5263
Seq<String> params = new Seq<>();
@@ -62,15 +73,33 @@ public void updateCommands(){
6273
}
6374
params.add(paramName);
6475
}
65-
BasicCommand<Player> command = new BasicCommand<>(cmd.text, cmd.description, params);
66-
if(findCommand(command.name) != null) Log.err("Same command detected! " + command.name);
67-
else commands.add((T) command);
76+
MindustryCommand<Player> command = new MindustryCommand<>(cmd.text, cmd.description, params);
77+
if (findCommand(command.name) != null)
78+
Log.err("Same command detected! " + command.name);
79+
else {
80+
mindustryCommands.add((T) command);
81+
}
6882
}
6983
}
84+
7085
public T findCommand(String arg){
71-
return commands.find(cmd -> cmd.name.equalsIgnoreCase(arg));
86+
return notNullElse(mindustryCommands.find(cmd -> cmd.name.equalsIgnoreCase(arg)), adminCommands.find(cmd -> cmd.name.equalsIgnoreCase(arg)));
7287
}
73-
public static String formatParams(BasicCommand<Player> command){
88+
89+
public static List<?> getCommandsFromPackage(String pckg){
90+
try {
91+
List<Class<?>> classes = exportClasses(pckg);
92+
List<Object> commands = new ArrayList<>();
93+
for (Class<?> clazz : classes){
94+
commands.add(clazz.getConstructors()[0].newInstance());
95+
}
96+
return commands;
97+
} catch (IOException | IllegalAccessException | InvocationTargetException | InstantiationException e) {
98+
throw new RuntimeException(e);
99+
}
100+
}
101+
102+
public static String formatParams(MindustryCommand<Player> command){
74103
if (command.params.isEmpty()) return " ";
75104
StringBuilder builder = new StringBuilder();
76105
for (var param : command.params) builder.append(param.rawname).append(" ");

src/smp/commands/ds/discord/admin/AdminAddCommand.java renamed to src/java/smp/commandSystem/commands/discord/admin/AdminAddCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
package smp.commands.ds.discord.admin;
1+
package smp.commandSystem.commands.discord.admin;
22

33
import mindustry.gen.Groups;
44
import mindustry.gen.Player;
55
import org.javacord.api.event.message.MessageCreateEvent;
6-
import smp.commands.ds.discord.DiscordCommand;
6+
import smp.commandSystem.discord.DiscordCommand;
77

88
import static mindustry.Vars.netServer;
99

src/smp/commands/ds/discord/admin/AdminRewCommand.java renamed to src/java/smp/commandSystem/commands/discord/admin/AdminRewCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
package smp.commands.ds.discord.admin;
1+
package smp.commandSystem.commands.discord.admin;
22

33
import mindustry.gen.Groups;
44
import org.javacord.api.event.message.MessageCreateEvent;
5-
import smp.commands.ds.discord.DiscordCommand;
5+
import smp.commandSystem.discord.DiscordCommand;
66
import smp.database.players.FindPlayerData;
77
import smp.models.PlayerData;
88

src/smp/commands/ds/discord/admin/BanCommand.java renamed to src/java/smp/commandSystem/commands/discord/admin/BanCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
package smp.commands.ds.discord.admin;
1+
package smp.commandSystem.commands.discord.admin;
22

33
import org.javacord.api.event.message.MessageCreateEvent;
4-
import smp.commands.ds.discord.DiscordCommand;
4+
import smp.commandSystem.discord.DiscordCommand;
55
import smp.database.players.FindPlayerData;
66
import smp.functions.Utilities;
77
import smp.models.PlayerData;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package smp.commandSystem.commands.discord.admin;
2+
3+
import org.javacord.api.event.message.MessageCreateEvent;
4+
import smp.commandSystem.discord.DiscordCommand;
5+
6+
public class ExitCommand extends DiscordCommand {
7+
public ExitCommand() {
8+
super("exit", " -> Stops the server.", 0,true, false);
9+
}
10+
11+
@Override
12+
public void run(MessageCreateEvent listener) {
13+
Runtime.getRuntime().exit(0);
14+
}
15+
}

0 commit comments

Comments
 (0)