forked from DimensionalDevelopment/Rift
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathLocalCommandManager.java
More file actions
67 lines (58 loc) · 2.35 KB
/
LocalCommandManager.java
File metadata and controls
67 lines (58 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.dimdev.rift.util;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.ParseResults;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.suggestion.Suggestions;
import java.util.concurrent.CompletableFuture;
import net.minecraft.command.CommandSource;
import org.dimdev.rift.listener.client.LocalCommandAdder;
import org.dimdev.riftloader.RiftLoader;
/**
*
* @author gbl
*/
public class LocalCommandManager {
private static LocalCommandManager instance;
private CommandDispatcher<CommandSource> dispatcher;
private CompletableFuture<Suggestions> suggestions;
private LocalCommandManager() {
}
public static LocalCommandManager getInstance() {
if (instance == null) {
instance=new LocalCommandManager();
instance.dispatcher=new CommandDispatcher<>();
for (LocalCommandAdder localCommandAdder: RiftLoader.instance.getListeners(LocalCommandAdder.class)) {
localCommandAdder.registerLocalCommands(instance.dispatcher);
}
}
return instance;
}
public static void dispatchLocalCommand(String s) throws CommandSyntaxException {
getInstance().dispatcher.execute(s, null);
}
private Suggestions getSuggestionsFor(String command) {
if (!command.startsWith("/")) {
return null;
}
// Don't just pass command; pass a stringreader that skips over the '/',
// or Suggestions.range won't match the server version.
StringReader reader=new StringReader(command);
reader.skip();
ParseResults<CommandSource> parse = dispatcher.parse(reader, null);
// We are losing the advantage of using a separate thread here,
// but the server commands, which imply a network exchange,
// need it much more than we do.
suggestions = dispatcher.getCompletionSuggestions(parse);
Suggestions result = suggestions.join();
return result;
}
public static Suggestions getSuggestions(String s) {
return getInstance().getSuggestionsFor(s);
}
}