Skip to content

Commit 154307b

Browse files
added CommandHandler support
1 parent 226225f commit 154307b

File tree

4 files changed

+156
-1
lines changed

4 files changed

+156
-1
lines changed

src/main/java/de/florianmichael/vialoadingbase/ViaLoadingBase.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import de.florianmichael.vialoadingbase.model.ComparableProtocolVersion;
2828
import de.florianmichael.vialoadingbase.platform.ViaBackwardsPlatformImpl;
2929
import de.florianmichael.vialoadingbase.platform.ViaRewindPlatformImpl;
30+
import de.florianmichael.vialoadingbase.platform.viaversion.VLBCommandHandler;
3031
import de.florianmichael.vialoadingbase.platform.viaversion.VLBViaProviders;
3132
import de.florianmichael.vialoadingbase.platform.ViaVersionPlatformImpl;
3233
import de.florianmichael.vialoadingbase.platform.viaversion.VLBViaInjector;
@@ -105,7 +106,13 @@ public void initPlatform() {
105106
this.targetProtocolVersion = this.nativeProtocolVersion;
106107

107108
final ViaVersionPlatformImpl viaVersionPlatform = new ViaVersionPlatformImpl(ViaLoadingBase.LOGGER);
108-
final ViaManagerImpl.ViaManagerBuilder builder = ViaManagerImpl.builder().injector(new VLBViaInjector()).loader(new VLBViaProviders()).platform(viaVersionPlatform);
109+
final ViaManagerImpl.ViaManagerBuilder builder = ViaManagerImpl.builder().
110+
platform(viaVersionPlatform).
111+
loader(new VLBViaProviders()).
112+
injector(new VLBViaInjector()).
113+
commandHandler(new VLBCommandHandler())
114+
;
115+
109116
if (this.managerBuilderConsumer != null) this.managerBuilderConsumer.accept(builder);
110117

111118
Via.init(builder.build());
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* This file is part of ViaLoadingBase - https://github.com/FlorianMichael/ViaLoadingBase
3+
* Copyright (C) 2023 FlorianMichael/EnZaXD and contributors
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package de.florianmichael.vialoadingbase.command;
19+
20+
import com.viaversion.viaversion.api.Via;
21+
import com.viaversion.viaversion.api.command.ViaCommandSender;
22+
import com.viaversion.viaversion.api.connection.UserConnection;
23+
24+
import java.util.UUID;
25+
26+
public class UserCommandSender implements ViaCommandSender {
27+
private final UserConnection user;
28+
29+
public UserCommandSender(UserConnection user) {
30+
this.user = user;
31+
}
32+
33+
@Override
34+
public boolean hasPermission(String s) {
35+
return false;
36+
}
37+
38+
@Override
39+
public void sendMessage(String s) {
40+
Via.getPlatform().sendMessage(getUUID(), s);
41+
}
42+
43+
@Override
44+
public UUID getUUID() {
45+
return user.getProtocolInfo().getUuid();
46+
}
47+
48+
@Override
49+
public String getName() {
50+
return user.getProtocolInfo().getUsername();
51+
}
52+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* This file is part of ViaLoadingBase - https://github.com/FlorianMichael/ViaLoadingBase
3+
* Copyright (C) 2023 FlorianMichael/EnZaXD and contributors
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package de.florianmichael.vialoadingbase.command.impl;
19+
20+
import io.netty.util.ResourceLeakDetector;
21+
import com.viaversion.viaversion.api.command.ViaCommandSender;
22+
import com.viaversion.viaversion.api.command.ViaSubCommand;
23+
24+
import java.util.Arrays;
25+
import java.util.List;
26+
import java.util.stream.Collectors;
27+
28+
public class LeakDetectSubCommand extends ViaSubCommand {
29+
30+
@Override
31+
public String name() {
32+
return "leakdetect";
33+
}
34+
35+
@Override
36+
public String description() {
37+
return "Sets ResourceLeakDetector level";
38+
}
39+
40+
@Override
41+
public boolean execute(ViaCommandSender viaCommandSender, String[] strings) {
42+
if (strings.length == 1) {
43+
try {
44+
ResourceLeakDetector.Level level = ResourceLeakDetector.Level.valueOf(strings[0]);
45+
ResourceLeakDetector.setLevel(level);
46+
viaCommandSender.sendMessage("Set leak detector level to " + level);
47+
} catch (IllegalArgumentException e) {
48+
viaCommandSender.sendMessage("Invalid level (" + Arrays.toString(ResourceLeakDetector.Level.values()) + ")");
49+
}
50+
} else {
51+
viaCommandSender.sendMessage("Current leak detection level is " + ResourceLeakDetector.getLevel());
52+
}
53+
return true;
54+
}
55+
56+
@Override
57+
public List<String> onTabComplete(ViaCommandSender sender, String[] args) {
58+
if (args.length == 1) {
59+
return Arrays.stream(ResourceLeakDetector.Level.values()).map(Enum::name).filter(it -> it.startsWith(args[0])).collect(Collectors.toList());
60+
}
61+
return super.onTabComplete(sender, args);
62+
}
63+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* This file is part of ViaLoadingBase - https://github.com/FlorianMichael/ViaLoadingBase
3+
* Copyright (C) 2023 FlorianMichael/EnZaXD and contributors
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package de.florianmichael.vialoadingbase.platform.viaversion;
19+
20+
import com.viaversion.viaversion.commands.ViaCommandHandler;
21+
import de.florianmichael.vialoadingbase.command.impl.LeakDetectSubCommand;
22+
23+
public class VLBCommandHandler extends ViaCommandHandler {
24+
25+
public VLBCommandHandler() {
26+
super();
27+
this.registerVLBDefaults();
28+
}
29+
30+
public void registerVLBDefaults() {
31+
this.registerSubCommand(new LeakDetectSubCommand());
32+
}
33+
}

0 commit comments

Comments
 (0)