-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathAutoLevelingCommands.java
More file actions
81 lines (75 loc) · 3.69 KB
/
AutoLevelingCommands.java
File metadata and controls
81 lines (75 loc) · 3.69 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package daripher.autoleveling.command;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import daripher.autoleveling.AutoLevelingMod;
import daripher.autoleveling.saveddata.GlobalLevelingData;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
import net.minecraft.server.MinecraftServer;
import net.minecraftforge.event.RegisterCommandsEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
@EventBusSubscriber(modid = AutoLevelingMod.MOD_ID)
public class AutoLevelingCommands {
@SubscribeEvent
public static void onRegisterCommands(RegisterCommandsEvent event) {
LiteralArgumentBuilder<CommandSourceStack> addGlobalLevelCommand =
Commands.literal("autoleveling")
.then(
Commands.literal("level")
.then(
Commands.literal("add")
.then(
Commands.argument("value", IntegerArgumentType.integer())
.executes(AutoLevelingCommands::executeAddLevelCommand))))
.requires(AutoLevelingCommands::hasPermission);
event.getDispatcher().register(addGlobalLevelCommand);
LiteralArgumentBuilder<CommandSourceStack> getGlobalLevelCommand =
Commands.literal("autoleveling")
.then(
Commands.literal("level")
.then(
Commands.literal("get")
.executes(AutoLevelingCommands::executeGetLevelCommand)))
.requires(AutoLevelingCommands::hasPermission);
event.getDispatcher().register(getGlobalLevelCommand);
LiteralArgumentBuilder<CommandSourceStack> setGlobalLevelCommand =
Commands.literal("autoleveling")
.then(
Commands.literal("level")
.then(
Commands.literal("set")
.then(
Commands.argument("value", IntegerArgumentType.integer())
.executes(AutoLevelingCommands::executeSetLevelCommand))))
.requires(AutoLevelingCommands::hasPermission);
event.getDispatcher().register(setGlobalLevelCommand);
}
private static int executeAddLevelCommand(CommandContext<CommandSourceStack> ctx) {
MinecraftServer server = ctx.getSource().getServer();
GlobalLevelingData globalLevelingData = GlobalLevelingData.get(server);
Integer levelBonus = ctx.getArgument("value", Integer.class);
int oldLevelBonus = globalLevelingData.getLevelBonus();
globalLevelingData.setLevel(oldLevelBonus + levelBonus);
return 1;
}
private static int executeGetLevelCommand(CommandContext<CommandSourceStack> ctx) {
MinecraftServer server = ctx.getSource().getServer();
GlobalLevelingData globalLevelingData = GlobalLevelingData.get(server);
int levelBonus = globalLevelingData.getLevelBonus();
ctx.getSource().sendSystemMessage(Component.literal("Global level bonus is " + levelBonus));
return 1;
}
private static int executeSetLevelCommand(CommandContext<CommandSourceStack> ctx) {
MinecraftServer server = ctx.getSource().getServer();
GlobalLevelingData globalLevelingData = GlobalLevelingData.get(server);
Integer newLevel = ctx.getArgument("value", Integer.class);
globalLevelingData.setLevel(newLevel);
return 1;
}
private static boolean hasPermission(CommandSourceStack commandSourceStack) {
return commandSourceStack.hasPermission(2);
}
}