Skip to content

Commit 6ae0f34

Browse files
authored
Merge pull request #44 from Matts/console_commands
Console commands
2 parents fd92e1c + 5db0881 commit 6ae0f34

17 files changed

Lines changed: 646 additions & 52 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ run/
3636
*.war
3737
*.ear
3838

39+
logs
40+
3941
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
4042
hs_err_pid*
4143
/.nb-gradle/
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.mattsmeets.macrokey.command;
2+
3+
import com.mattsmeets.macrokey.MacroKey;
4+
import com.mattsmeets.macrokey.model.LayerInterface;
5+
import net.minecraft.client.Minecraft;
6+
import net.minecraft.client.resources.I18n;
7+
import net.minecraft.command.CommandException;
8+
import net.minecraft.command.ICommandSender;
9+
import net.minecraft.server.MinecraftServer;
10+
import net.minecraft.util.math.BlockPos;
11+
import net.minecraft.util.text.TextComponentString;
12+
import net.minecraft.util.text.TextComponentTranslation;
13+
import scala.actors.threadpool.Arrays;
14+
15+
import javax.annotation.Nullable;
16+
import java.io.IOException;
17+
import java.util.List;
18+
19+
public class CommandLayer extends StrippedCommand {
20+
public final String
21+
layerMasterText = I18n.format("text.layer.master");
22+
23+
@Override
24+
public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException {
25+
if (args.length < 1) {
26+
return;
27+
}
28+
29+
if (args.length == 1) {
30+
this.printLayerInformation(sender);
31+
32+
return;
33+
}
34+
35+
if (args[1].equals("toggle")) {
36+
this.printLayerInformation(sender);
37+
38+
return;
39+
}
40+
41+
sender.sendMessage(new TextComponentString(this.getUsage(sender)));
42+
}
43+
44+
@Override
45+
public List<String> getTabCompletions(MinecraftServer server, ICommandSender sender, String[] args, @Nullable BlockPos targetPos) {
46+
return Arrays.asList(new String[] {"toggle"});
47+
}
48+
49+
@Override
50+
public String getUsage(ICommandSender sender) {
51+
return "Usage: /macrokey layer [toggle]";
52+
}
53+
54+
private void printLayerInformation(ICommandSender sender) {
55+
LayerInterface activeLayer = MacroKey.instance.modState.getActiveLayer();
56+
57+
String layerDisplayName = layerMasterText;
58+
int countMacroEnabled = 0;
59+
60+
if (activeLayer != null) {
61+
layerDisplayName = activeLayer.getDisplayName();
62+
countMacroEnabled = activeLayer.getMacros().size();
63+
} else {
64+
try {
65+
countMacroEnabled = MacroKey.instance.bindingsRepository.findAllMacros(false).size();
66+
} catch (IOException e) {
67+
e.printStackTrace();
68+
}
69+
}
70+
71+
sender.sendMessage(
72+
new TextComponentTranslation(
73+
"command.layer.information",
74+
layerDisplayName,
75+
countMacroEnabled
76+
)
77+
);
78+
}
79+
80+
private void nextLayer(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException {
81+
try {
82+
MacroKey.instance.modState.nextLayer();
83+
84+
this.execute(server, sender, new String[] {args[0]});
85+
} catch (IOException e) {
86+
e.printStackTrace();
87+
}
88+
}
89+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.mattsmeets.macrokey.command;
2+
3+
import net.minecraft.command.CommandBase;
4+
import net.minecraft.command.CommandException;
5+
import net.minecraft.command.ICommand;
6+
import net.minecraft.command.ICommandSender;
7+
import net.minecraft.server.MinecraftServer;
8+
import net.minecraft.util.math.BlockPos;
9+
import net.minecraft.util.text.TextComponentString;
10+
import scala.actors.threadpool.Arrays;
11+
12+
import javax.annotation.Nullable;
13+
import java.util.HashMap;
14+
import java.util.List;
15+
16+
public class CommandMacroKey extends CommandBase implements ICommand {
17+
18+
private HashMap<String, ICommand> subCommands;
19+
20+
public CommandMacroKey() {
21+
this.subCommands = new HashMap<>();
22+
23+
subCommands.put("open", new CommandOpenGUI());
24+
subCommands.put("layer", new CommandLayer());
25+
}
26+
27+
@Override
28+
public String getName() {
29+
return "macrokey";
30+
}
31+
32+
@Override
33+
public String getUsage(ICommandSender sender) {
34+
return "Usage: /macrokey [open / layer]";
35+
}
36+
37+
@Override
38+
public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException {
39+
if (args.length == 0) {
40+
this.subCommands.get("open").execute(server, sender, args);
41+
42+
return;
43+
}
44+
45+
if (this.subCommands.containsKey(args[0].toLowerCase())) {
46+
this.subCommands.get(args[0].toLowerCase()).execute(server, sender, args);
47+
48+
return;
49+
}
50+
51+
sender.sendMessage(new TextComponentString(this.getUsage(sender)));
52+
}
53+
54+
@Override
55+
public boolean checkPermission(MinecraftServer server, ICommandSender sender) {
56+
return true;
57+
}
58+
59+
@Override
60+
public List<String> getTabCompletions(MinecraftServer server, ICommandSender sender, String[] args, @Nullable BlockPos targetPos) {
61+
if (args.length >= 1 && this.subCommands.containsKey(args[0].toLowerCase())) {
62+
return this.subCommands.get(args[0].toLowerCase()).getTabCompletions(server, sender, args, targetPos);
63+
}
64+
65+
return Arrays.asList(this.subCommands.keySet().toArray());
66+
}
67+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.mattsmeets.macrokey.command;
2+
3+
import com.mattsmeets.macrokey.event.ExecuteOnTickEvent;
4+
import com.mattsmeets.macrokey.model.lambda.ExecuteOnTickInterface;
5+
import net.minecraft.command.CommandException;
6+
import net.minecraft.command.ICommandSender;
7+
import net.minecraft.server.MinecraftServer;
8+
import net.minecraft.util.math.BlockPos;
9+
import net.minecraftforge.common.MinecraftForge;
10+
import net.minecraftforge.fml.relauncher.Side;
11+
import net.minecraftforge.fml.relauncher.SideOnly;
12+
import scala.actors.threadpool.Arrays;
13+
14+
import javax.annotation.Nullable;
15+
import java.util.List;
16+
17+
public class CommandOpenGUI extends StrippedCommand {
18+
19+
@Override
20+
@SideOnly(Side.CLIENT)
21+
public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException {
22+
MinecraftForge.EVENT_BUS.post(new ExecuteOnTickEvent(ExecuteOnTickInterface.openMacroKeyGUI));
23+
}
24+
25+
@Override
26+
public List<String> getTabCompletions(MinecraftServer server, ICommandSender sender, String[] args, @Nullable BlockPos targetPos) {
27+
return Arrays.asList(new String[]{});
28+
}
29+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.mattsmeets.macrokey.command;
2+
3+
import net.minecraft.command.CommandException;
4+
import net.minecraft.command.ICommand;
5+
import net.minecraft.command.ICommandSender;
6+
import net.minecraft.server.MinecraftServer;
7+
import net.minecraft.util.math.BlockPos;
8+
9+
import javax.annotation.Nullable;
10+
import java.util.List;
11+
12+
public abstract class StrippedCommand implements ICommand {
13+
@Override
14+
public abstract void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException;
15+
16+
@Override
17+
public abstract List<String> getTabCompletions(MinecraftServer server, ICommandSender sender, String[] args, @Nullable BlockPos targetPos);
18+
19+
@Override
20+
public String getName() {
21+
return null;
22+
}
23+
24+
@Override
25+
public String getUsage(ICommandSender sender) {
26+
return null;
27+
}
28+
29+
@Override
30+
public List<String> getAliases() {
31+
return null;
32+
}
33+
34+
@Override
35+
public boolean checkPermission(MinecraftServer server, ICommandSender sender) {
36+
return false;
37+
}
38+
39+
@Override
40+
public boolean isUsernameIndex(String[] args, int index) {
41+
return false;
42+
}
43+
44+
@Override
45+
public int compareTo(ICommand o) {
46+
return 0;
47+
}
48+
}

src/main/java/com/mattsmeets/macrokey/config/ModState.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.mattsmeets.macrokey.config;
22

3+
import com.mattsmeets.macrokey.MacroKey;
34
import com.mattsmeets.macrokey.model.LayerInterface;
45

56
import java.io.IOException;
@@ -35,4 +36,23 @@ public List<LayerInterface> getLayers(boolean sync) throws IOException {
3536
.sorted(Comparator.comparing(LayerInterface::getULID))
3637
.collect(Collectors.toList());
3738
}
39+
40+
public LayerInterface nextLayer() throws IOException {
41+
List<LayerInterface> layers = this.getLayers(true);
42+
LayerInterface layer = null;
43+
44+
// get the index within all the
45+
// layers for the one currently active
46+
int indexOfCurrent = layers.indexOf(this.getActiveLayer());
47+
48+
// if there are more layers than the next
49+
// layer being selected, then select the next one
50+
if (layers.size() > indexOfCurrent + 1) {
51+
layer = layers.get(indexOfCurrent + 1);
52+
}
53+
54+
this.setActiveLayer(layer);
55+
56+
return layer;
57+
}
3858
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.mattsmeets.macrokey.event;
2+
3+
import com.mattsmeets.macrokey.model.lambda.ExecuteOnTickInterface;
4+
import net.minecraftforge.fml.common.eventhandler.Event;
5+
6+
public class ExecuteOnTickEvent extends Event {
7+
8+
private ExecuteOnTickInterface executor;
9+
10+
public ExecuteOnTickEvent(ExecuteOnTickInterface executor) {
11+
this.executor = executor;
12+
}
13+
14+
public ExecuteOnTickInterface getExecutor() {
15+
return executor;
16+
}
17+
}

src/main/java/com/mattsmeets/macrokey/gui/GuiMacroManagement.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
import net.minecraft.client.gui.GuiButton;
99
import net.minecraft.client.gui.GuiScreen;
1010
import net.minecraft.client.resources.I18n;
11+
import net.minecraft.world.BossInfo;
1112
import net.minecraftforge.common.MinecraftForge;
1213
import org.lwjgl.input.Keyboard;
14+
import org.lwjgl.input.Mouse;
1315

16+
import java.awt.*;
1417
import java.io.IOException;
1518
import java.util.List;
1619

src/main/java/com/mattsmeets/macrokey/handler/MacroKeyHandler.java renamed to src/main/java/com/mattsmeets/macrokey/handler/GameTickHandler.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.mattsmeets.macrokey.handler;
22

3+
import com.mattsmeets.macrokey.model.lambda.ExecuteOnTickInterface;
4+
import com.mattsmeets.macrokey.event.ExecuteOnTickEvent;
35
import com.mattsmeets.macrokey.event.InGameTickEvent;
46
import com.mattsmeets.macrokey.event.MacroActivationEvent;
57
import com.mattsmeets.macrokey.model.MacroInterface;
@@ -11,15 +13,21 @@
1113
import java.util.Set;
1214

1315
@SideOnly(Side.CLIENT)
14-
public class MacroKeyHandler {
16+
public class GameTickHandler {
1517

1618
/**
1719
* private stash of macro's to run
1820
*/
1921
private Set<MacroInterface> macrosToRun;
2022

21-
public MacroKeyHandler() {
22-
this.macrosToRun = new HashSet<>();
23+
/**
24+
* private stash of executor's to run
25+
*/
26+
private Set<ExecuteOnTickInterface> executorsToRun;
27+
28+
public GameTickHandler(HashSet macrosToRun, HashSet executorsToRun) {
29+
this.macrosToRun = macrosToRun == null ? new HashSet<>() : macrosToRun;
30+
this.executorsToRun = executorsToRun == null ? new HashSet<>() : executorsToRun;
2331
}
2432

2533
@SubscribeEvent
@@ -31,6 +39,11 @@ public void onKeyEvent(MacroActivationEvent event) {
3139
}
3240
}
3341

42+
@SubscribeEvent
43+
public void onExecutorEvent(ExecuteOnTickEvent event) {
44+
executorsToRun.add(event.getExecutor());
45+
}
46+
3447
@SubscribeEvent
3548
public void onTick(InGameTickEvent event) {
3649
// loop through all pending macro's sending their command
@@ -42,9 +55,14 @@ public void onTick(InGameTickEvent event) {
4255
.filter(macro -> !macro.willRepeat() || event.isLimitedTick())
4356
.forEach(macro -> macro.getCommand().execute(event.getCurrentPlayer()));
4457

58+
// loop through all executors and run them.
59+
this.executorsToRun
60+
.forEach(executor -> executor.execute(event.isLimitedTick()));
61+
4562
// remove the command from the pending
4663
// list if it is not to be re-executed
4764
this.macrosToRun.removeIf(macro -> !macro.willRepeat());
65+
this.executorsToRun.clear();
4866
}
4967

5068
}

0 commit comments

Comments
 (0)