Skip to content
This repository has been archived by the owner on Feb 4, 2018. It is now read-only.

Commit

Permalink
Added Commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexeption committed Feb 23, 2017
1 parent 7da356f commit 70f71fd
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 8 deletions.
15 changes: 8 additions & 7 deletions src/main/java/uk/co/hexeption/darkforge/DarkForge.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import net.minecraftforge.fml.relauncher.SideOnly;
import uk.co.hexeption.darkforge.api.APIModuleSetup;
import uk.co.hexeption.darkforge.api.logger.LogHelper;
import uk.co.hexeption.darkforge.command.CommandManager;
import uk.co.hexeption.darkforge.config.FileManager;
import uk.co.hexeption.darkforge.events.EventManager;
import uk.co.hexeption.darkforge.module.ModuleManager;
Expand All @@ -37,20 +38,16 @@
@Mod(modid = ClientInfo.MOD_ID, name = ClientInfo.MOD_NAME, version = ClientInfo.VERSION_BUILD)
public class DarkForge {

@Mod.Instance(ClientInfo.MOD_ID)
public static DarkForge instance;

/**
* TODO: Fix Static Crash Bug!
*/
public static final EventManager EVENT_MANAGER = new EventManager();

public static final ModuleManager MODULE_MANAGER = new ModuleManager();

public static final CommandManager COMMAND_MANAGER = new CommandManager();
public static final FontManager FONT_MANAGER = new FontManager();

public static final FileManager FILE_MANAGER = new FileManager();

@Mod.Instance(ClientInfo.MOD_ID)
public static DarkForge instance;
public String commandPrefix = "#";


Expand All @@ -69,6 +66,10 @@ public void onFMLInitialization(FMLInitializationEvent event) {
LogHelper.info("Loading Modules...");
MODULE_MANAGER.Initialization();

LogHelper.info("Loading Commands...");
COMMAND_MANAGER.Initialization();


LogHelper.info("Registering Forge Events");
MinecraftForge.EVENT_BUS.register(EVENT_MANAGER);

Expand Down
67 changes: 67 additions & 0 deletions src/main/java/uk/co/hexeption/darkforge/command/Command.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*******************************************************************************
* DarkForge a Forge Hacked Client
* Copyright (C) 2017 Hexeption (Keir Davis)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

package uk.co.hexeption.darkforge.command;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

public abstract class Command {

private String[] name = getClass().getAnnotation(CMDInfo.class).name();

private String help = getClass().getAnnotation(CMDInfo.class).help();

private String description = getClass().getAnnotation(CMDInfo.class).descrption();

public String[] getName() {
return name;
}

public void setName(String[] name) {
this.name = name;
}

public String getHelp() {
return help;
}

public void setHelp(String help) {
this.help = help;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public abstract void execute(String input, String[] args) throws Exception;

@Retention(RetentionPolicy.RUNTIME)
public @interface CMDInfo {
String[] name();

String help();

String descrption();

}
}
103 changes: 103 additions & 0 deletions src/main/java/uk/co/hexeption/darkforge/command/CommandManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*******************************************************************************
* DarkForge a Forge Hacked Client
* Copyright (C) 2017 Hexeption (Keir Davis)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

package uk.co.hexeption.darkforge.command;

import uk.co.hexeption.darkforge.api.logger.LogHelper;
import uk.co.hexeption.darkforge.command.commands.TestCommand;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CommandManager {

private final Pattern patten = Pattern.compile("([^\\\"']\\\\S*|\\\".+?\\\"|'.+?')\\\\s*");
private final List<Command> commands = new ArrayList<>();


public void Initialization() {
initCommands();
LogHelper.info(String.format("Commands Loaded: %s!", commands.size()));
}

private void initCommands() {
this.commands.add(new TestCommand());
}

public List<Command> getCommands() {
return commands;
}

public boolean executeCommand(String message) {
String commandName = message.contains(" ") ? message.split(" ")[0] : message;
for (Command command : commands) {
for (String alias : command.getName()) {
if (message.contains(" ")) {
if (message.split(" ")[1].equalsIgnoreCase("aliases")) {
listAllNames(command);
return true;
}
}
tryCommand(command, message);
return true;
}
}
return false;
}


public void addCommand(Command... command) {
synchronized (this.commands) {
for (final Command command1 : command) {
this.commands.add(command1);
}
}
}

private String[] getArguments(String input) {
Matcher matcher = patten.matcher(input);
List<String> list = new ArrayList<>();
while (matcher.find()) {
list.add(matcher.group(1).replaceAll("\"", "").replaceAll("'", ""));
}
return list.toArray(new String[list.size()]);
}

private void listAllNames(Command command) {
String namesList = "Available names: ";
String[] name = command.getName();
for (int i = 0; i < name.length; i++) {
String names = name[i];
namesList += names + (i != name.length ? "," : "");
System.out.println(namesList);
}
}

public void tryCommand(Command command, String input) {
try {
String[] args = input.contains(" ") ? getArguments(input.substring(input.indexOf("") + 1)) : null;
command.execute(input, args);
} catch (Exception e) {
e.printStackTrace();

}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*******************************************************************************
* DarkForge a Forge Hacked Client
* Copyright (C) 2017 Hexeption (Keir Davis)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

package uk.co.hexeption.darkforge.command.commands;

import uk.co.hexeption.darkforge.command.Command;

@Command.CMDInfo(name = {"test", "ts"}, help = "test is good", descrption = "This is a test")

public class TestCommand extends Command {

@Override
public void execute(String input, String[] args) throws Exception {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ public void onClientEvent(final TickEvent.ClientTickEvent clientTickEvent) {

@SubscribeEvent
public void onChatSend(final ServerChatEvent serverChatEvent) {
//TODO: Commands Send
if (serverChatEvent.getMessage().startsWith(DarkForge.instance.commandPrefix)) {
DarkForge.COMMAND_MANAGER.executeCommand(serverChatEvent.getMessage());
serverChatEvent.setCanceled(true);
}
}

@SubscribeEvent
Expand Down

0 comments on commit 70f71fd

Please sign in to comment.