Skip to content
This repository was archived by the owner on Jan 16, 2023. It is now read-only.

Commit 3691d57

Browse files
committed
Cleanup
-Readded reflection based module loading - Added update notification - Moved mineplex values from BukkitValues into MineplexValues - Removed Commons libary, gsoup caused issues with forge client launch - Made the entire stuff more modular - Cleanup
1 parent ac7dc90 commit 3691d57

36 files changed

+553
-375
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ dependencies {
4242
compile 'org.spongepowered:mixin:0.7.11-SNAPSHOT'
4343

4444
compile 'com.github.ben-manes.caffeine:caffeine:2.8.5'
45+
compile 'org.reflections:reflections:0.9.12'
4546

4647
compileOnly 'org.projectlombok:lombok:1.18.12'
4748
annotationProcessor 'org.projectlombok:lombok:1.18.12'
Lines changed: 53 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,109 @@
11
package de.timmi6790.mpmod;
22

33
import de.timmi6790.mpmod.command.CommandManager;
4+
import de.timmi6790.mpmod.listeners.UpdateChecker;
45
import de.timmi6790.mpmod.listeners.events.EventListener;
56
import de.timmi6790.mpmod.listeners.events.MineplexEventListener;
67
import de.timmi6790.mpmod.modules.AbstractModule;
7-
import de.timmi6790.mpmod.modules.community.CommunityModule;
88
import de.timmi6790.mpmod.tabsupport.TabSupportManager;
9-
import de.timmi6790.mpmod.utilities.TaskScheduler;
9+
import de.timmi6790.mpmod.utilities.EventUtilities;
1010
import lombok.Getter;
11+
import lombok.Setter;
1112
import lombok.extern.log4j.Log4j2;
12-
import net.minecraftforge.common.MinecraftForge;
1313
import net.minecraftforge.common.config.Configuration;
1414
import net.minecraftforge.fml.common.Mod;
15-
import net.minecraftforge.fml.common.Mod.EventHandler;
1615
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
1716
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
17+
import org.reflections.Reflections;
1818

1919
import java.io.File;
2020
import java.util.HashMap;
2121
import java.util.Map;
2222
import java.util.Optional;
23+
import java.util.Set;
2324

2425
@Mod(
2526
modid = Reference.MODID,
2627
name = Reference.NAME,
2728
version = Reference.VERSION,
28-
acceptedMinecraftVersions = Reference.MC_VERSION,
29-
guiFactory = Reference.GUI_FACTORY_CLASS,
29+
acceptedMinecraftVersions = "1.8.9",
30+
guiFactory = "de.timmi6790.mpmod.gui.GuiFactory",
3031
clientSideOnly = true
3132
)
3233
@Log4j2
34+
@Getter
35+
@Setter
3336
public class McMod {
34-
private static final Map<Class<? extends AbstractModule>, AbstractModule> modules = new HashMap<>();
3537
@Getter
36-
private static final ModCache modCache = new ModCache();
37-
@Getter
38-
private static final TaskScheduler taskScheduler = new TaskScheduler();
39-
@Getter
40-
private static final TabSupportManager tabSupportManager = new TabSupportManager();
41-
@Getter
42-
private static final CommandManager commandManager = new CommandManager();
43-
@Getter
44-
private static Configuration configuration;
45-
private static String configDirectory;
38+
@Mod.Instance(value = Reference.MODID)
39+
private static McMod instance;
4640

47-
private static void addModules(final AbstractModule... modules) {
48-
for (final AbstractModule module : modules) {
49-
McMod.modules.put(module.getClass(), module);
41+
private final Map<Class<? extends AbstractModule>, AbstractModule> modules = new HashMap<>();
42+
43+
private final ModCache modCache = new ModCache();
44+
private final TabSupportManager tabSupportManager = new TabSupportManager();
45+
private final CommandManager commandManager = new CommandManager();
46+
private Configuration configuration;
47+
private String configDirectory;
48+
49+
private final String modId = Reference.MODID;
50+
private final String modName = Reference.NAME;
51+
private final String versionUrl = Reference.VERSION_URL;
52+
private final String version = Reference.VERSION;
53+
private final String downloadUrl = Reference.DOWNLOAD_URL;
54+
55+
private void loadModules() {
56+
final Reflections reflections = new Reflections("de.timmi6790");
57+
final Set<Class<? extends AbstractModule>> modules = reflections.getSubTypesOf(AbstractModule.class);
58+
for (final Class<? extends AbstractModule> module : modules) {
59+
try {
60+
this.addModules(module.getConstructor(McMod.class).newInstance(this));
61+
} catch (final Exception e) {
62+
log.error("Trying to initialize {}", module, e);
63+
}
5064
}
5165
}
5266

53-
public static <T extends AbstractModule> Optional<T> getModule(final Class<T> clazz) {
54-
return (Optional<T>) Optional.ofNullable(modules.get(clazz));
67+
public <T extends AbstractModule> Optional<T> getModule(final Class<T> clazz) {
68+
return (Optional<T>) Optional.ofNullable(this.modules.get(clazz));
5569
}
5670

57-
public static <T extends AbstractModule> T getModuleOrThrow(final Class<T> clazz) {
58-
return getModule(clazz).orElseThrow(RuntimeException::new);
71+
public <T extends AbstractModule> T getModuleOrThrow(final Class<T> clazz) {
72+
return this.getModule(clazz).orElseThrow(RuntimeException::new);
5973
}
6074

61-
public static void registerEvents(final Object... events) {
62-
for (final Object event : events) {
63-
MinecraftForge.EVENT_BUS.register(event);
75+
protected void addModules(final AbstractModule... modules) {
76+
for (final AbstractModule module : modules) {
77+
this.modules.put(module.getClass(), module);
6478
}
6579
}
6680

67-
public static void unRegisterEvents(final Object... events) {
68-
for (final Object event : events) {
69-
MinecraftForge.EVENT_BUS.unregister(event);
70-
}
71-
}
7281

73-
@EventHandler
82+
@Mod.EventHandler
7483
public void preInit(final FMLPreInitializationEvent event) {
75-
addModules(
76-
new CommunityModule()
77-
);
84+
this.configDirectory = event.getModConfigurationDirectory().toString();
85+
final File path = new File(this.configDirectory + File.separator + this.getModId() + ".cfg");
86+
this.configuration = new Configuration(path);
7887

79-
configDirectory = event.getModConfigurationDirectory().toString();
80-
if (configuration == null) {
81-
final File path = new File(configDirectory + "/" + Reference.MODID + ".cfg");
82-
configuration = new Configuration(path);
83-
}
88+
this.loadModules();
8489

85-
for (final AbstractModule module : McMod.modules.values()) {
90+
for (final AbstractModule module : this.getModules().values()) {
8691
log.info("PreInnit module {}", module.getName());
8792
module.preInit(event);
8893
}
8994
}
9095

91-
@EventHandler
96+
@Mod.EventHandler
9297
public void init(final FMLInitializationEvent event) {
93-
for (final AbstractModule module : modules.values()) {
98+
for (final AbstractModule module : this.getModules().values()) {
9499
log.info("Innit module {}", module.getName());
95100
module.init(event);
96101
}
97102

98-
registerEvents(
99-
this,
100-
tabSupportManager,
101-
taskScheduler,
102-
commandManager,
103-
new MineplexEventListener(),
104-
new EventListener()
103+
EventUtilities.registerEvents(
104+
new MineplexEventListener(this),
105+
new EventListener(),
106+
new UpdateChecker(this)
105107
);
106108
}
107109
}

src/main/java/de/timmi6790/mpmod/Reference.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
public class Reference {
44
public static final String MODID = "RMM";
5-
static final String NAME = "RandomMineplexMod";
6-
static final String MC_VERSION = "1.8.9";
7-
static final String VERSION = "0.3.2.0";
8-
static final String GUI_FACTORY_CLASS = "de.timmi6790.mpmod.gui.GuiFactory";
5+
public static final String NAME = "RandomMineplexMod";
6+
public static final String VERSION = "1.0.0";
7+
public static final String VERSION_URL = "https://gist.githubusercontent.com/Timmi6790/2ead90ef6f97baeb689d076094e88c09/raw/RandomMineplexMod-Version";
8+
public static final String DOWNLOAD_URL = "https://github.com/Timmi6790/RandomMineplexMod/releases";
99
}

src/main/java/de/timmi6790/mpmod/command/AbstractCommand.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@
33
import de.timmi6790.mpmod.command.exceptions.CommandException;
44
import de.timmi6790.mpmod.utilities.DataUtilities;
55
import de.timmi6790.mpmod.utilities.MessageBuilder;
6+
import lombok.NonNull;
67
import net.minecraft.command.CommandBase;
78
import net.minecraft.command.ICommandSender;
89
import net.minecraft.util.EnumChatFormatting;
910
import org.apache.commons.lang3.StringUtils;
11+
import org.checkerframework.checker.nullness.qual.Nullable;
1012

1113
import java.util.ArrayList;
1214
import java.util.Collection;
1315
import java.util.List;
1416

1517
public abstract class AbstractCommand extends CommandBase {
1618
private final String name;
17-
private final List<String> aliases;
19+
private final List<String> aliases = new ArrayList<>();
1820
private String prefix;
1921
private int minArgs = 0;
2022

@@ -32,14 +34,14 @@ public AbstractCommand(final String name, final String prefix) {
3234
this(name, prefix, null);
3335
}
3436

35-
public AbstractCommand(final String name, final String prefix, final List<String> aliases) {
37+
public AbstractCommand(@NonNull final String name,
38+
@Nullable final String prefix,
39+
@Nullable final List<String> aliases) {
3640
this.name = name;
3741
this.prefix = prefix;
3842

39-
if (aliases == null) {
40-
this.aliases = new ArrayList<>();
41-
} else {
42-
this.aliases = aliases;
43+
if (aliases != null) {
44+
this.aliases.addAll(aliases);
4345
}
4446
}
4547

@@ -90,7 +92,9 @@ protected void returnTellNotValidCommand(final String commandName) {
9092

9193
protected void returnTellMissingArgs() {
9294
new MessageBuilder(StringUtils.capitalize(this.getCommandName()) + "-Command\n", EnumChatFormatting.GOLD)
93-
.addMessage("\n" + "/" + this.getCommandName() + " " + this.getCommandName() + " " + String.join(" ", this.getSyntax()), EnumChatFormatting.YELLOW).sendToPlayerWithBox();
95+
.addMessage("\n" + "/" + this.getCommandName() + " " + this.getCommandName() + " " + String.join(" ", this.getSyntax()), EnumChatFormatting.YELLOW)
96+
.addBoxedToMessage()
97+
.sendToPlayer();
9498
this.returnTell(null);
9599
}
96100

src/main/java/de/timmi6790/mpmod/command/AbstractCommandGroup.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
package de.timmi6790.mpmod.command;
22

33
import de.timmi6790.mpmod.utilities.MessageBuilder;
4+
import lombok.Getter;
5+
import lombok.Setter;
46
import net.minecraft.command.ICommandSender;
57
import net.minecraft.util.BlockPos;
68
import net.minecraft.util.EnumChatFormatting;
79
import org.apache.commons.lang3.StringUtils;
810

911
import java.util.*;
1012

13+
@Setter
14+
@Getter
1115
public abstract class AbstractCommandGroup extends AbstractCommand {
1216
private final List<AbstractCommand> subCommands = new ArrayList<>();
1317
private String[][] tabCompleteOptions = new String[0][0];
18+
/**
19+
* Stores the pre command group names, used for the help command
20+
*/
21+
private String preCommand;
1422

1523
public AbstractCommandGroup(final String commandName) {
1624
super(commandName);
@@ -32,14 +40,30 @@ protected Optional<AbstractCommand> getSubCommand(final String name) {
3240
return Optional.empty();
3341
}
3442

43+
protected String getPreCommand() {
44+
if (this.preCommand == null) {
45+
return "";
46+
}
47+
return this.preCommand;
48+
}
49+
3550
@Override
3651
public void onCommand(final ICommandSender sender, final String[] args) {
3752
if (args.length == 0) {
38-
final MessageBuilder helpMessage = new MessageBuilder(StringUtils.capitalize(this.getCommandName()) + "-Command\n", EnumChatFormatting.GOLD);
53+
final MessageBuilder helpMessage = new MessageBuilder(
54+
StringUtils.capitalize(this.getCommandName()) + "-Command\n",
55+
EnumChatFormatting.GOLD
56+
);
3957
for (final AbstractCommand command : this.subCommands) {
40-
helpMessage.addMessage("\n" + "/" + this.getCommandName() + " " + command.getCommandName() + " " + String.join(" ", command.getSyntax()), EnumChatFormatting.YELLOW);
58+
helpMessage.addMessage(
59+
EnumChatFormatting.YELLOW,
60+
"\n/%s %s %s",
61+
this.getPreCommand() + this.getCommandName(),
62+
command.getCommandName(),
63+
String.join(" ", command.getSyntax())
64+
);
4165
}
42-
helpMessage.sendToPlayerWithBox();
66+
helpMessage.addBoxedToMessage().sendToPlayer();
4367
return;
4468
}
4569

@@ -84,6 +108,11 @@ protected final void registerSubCommand(final AbstractCommand command) {
84108

85109
protected final void registerSubCommands(final AbstractCommand... commands) {
86110
for (final AbstractCommand command : commands) {
111+
if (command instanceof AbstractCommandGroup) {
112+
final AbstractCommandGroup commandGroup = (AbstractCommandGroup) command;
113+
commandGroup.setPreCommand(this.getPreCommand() + this.getCommandName() + " ");
114+
}
115+
87116
this.registerSubCommand(command);
88117
}
89118
}

src/main/java/de/timmi6790/mpmod/command/CommandManager.java

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
package de.timmi6790.mpmod.command;
22

33
import de.timmi6790.mpmod.events.mineplex.MineplexServerJoinEvent;
4+
import de.timmi6790.mpmod.utilities.EventUtilities;
45
import net.minecraft.command.ICommand;
56
import net.minecraftforge.client.ClientCommandHandler;
67
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
78
import net.minecraftforge.fml.common.network.FMLNetworkEvent;
89

910
import java.util.ArrayList;
10-
import java.util.HashMap;
1111
import java.util.List;
1212
import java.util.Map;
1313

1414
public class CommandManager {
1515
private final List<ICommand> mineplexOnlyCommands = new ArrayList<>();
16-
private final Map<String, ICommand> registeredCommands = new HashMap<>();
17-
private final Map<String, String> registeredCommandsAlias = new HashMap<>();
16+
17+
public CommandManager() {
18+
EventUtilities.registerEvents(this);
19+
}
1820

1921
@SubscribeEvent
2022
public void onMineplexJoin(final MineplexServerJoinEvent event) {
@@ -25,33 +27,37 @@ public void onMineplexJoin(final MineplexServerJoinEvent event) {
2527

2628
@SubscribeEvent
2729
public void onServerLeave(final FMLNetworkEvent.ClientDisconnectionFromServerEvent event) {
28-
final Map<String, ICommand> commandMap = ClientCommandHandler.instance.getCommands();
30+
this.removeCommands(this.mineplexOnlyCommands.toArray(new ICommand[0]));
31+
}
32+
33+
public void addCommands(final ICommand... commands) {
34+
for (final ICommand command : commands) {
35+
this.addCommand(command);
36+
}
37+
}
2938

30-
this.mineplexOnlyCommands.forEach(command -> {
31-
commandMap.remove(command.getCommandName());
39+
public void addCommand(final ICommand command) {
40+
if (MineplexOnlyCommand.class.isAssignableFrom(command.getClass())) {
41+
this.mineplexOnlyCommands.add(command);
3242

33-
command.getCommandAliases().stream()
34-
.filter(alias -> {
35-
final ICommand icommand = commandMap.get(alias);
36-
return icommand != null && icommand.equals(command);
37-
})
38-
.forEach(commandMap::remove);
39-
});
43+
} else {
44+
ClientCommandHandler.instance.registerCommand(command);
45+
}
4046
}
4147

42-
public void addCommands(final ICommand... commands) {
48+
public void removeCommands(final ICommand... commands) {
4349
for (final ICommand command : commands) {
44-
final String commandNameLower = command.getCommandName().toLowerCase();
45-
this.registeredCommands.put(commandNameLower, command);
46-
for (final String alias : command.getCommandAliases()) {
47-
this.registeredCommandsAlias.put(alias.toLowerCase(), commandNameLower);
48-
}
50+
this.removeCommand(command);
51+
}
52+
}
4953

50-
if (MineplexOnlyCommand.class.isAssignableFrom(command.getClass())) {
51-
this.mineplexOnlyCommands.add(command);
54+
public void removeCommand(final ICommand command) {
55+
final Map<String, ICommand> commandMap = ClientCommandHandler.instance.getCommands();
56+
commandMap.remove(command.getCommandName());
5257

53-
} else {
54-
ClientCommandHandler.instance.registerCommand(command);
58+
for (final String commandAlias : command.getCommandAliases()) {
59+
if (command.equals(commandMap.get(commandAlias))) {
60+
commandMap.remove(commandAlias);
5561
}
5662
}
5763
}

0 commit comments

Comments
 (0)