This repository has been archived by the owner on Apr 12, 2022. It is now read-only.
/
SetGameRuleCommand.java
101 lines (91 loc) · 3.96 KB
/
SetGameRuleCommand.java
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package com.denizenscript.denizen2sponge.commands.world;
import com.denizenscript.denizen2core.commands.AbstractCommand;
import com.denizenscript.denizen2core.commands.CommandEntry;
import com.denizenscript.denizen2core.commands.CommandQueue;
import com.denizenscript.denizen2core.tags.AbstractTagObject;
import com.denizenscript.denizen2core.tags.objects.MapTag;
import com.denizenscript.denizen2core.utilities.debugging.ColorSet;
import com.denizenscript.denizen2sponge.tags.objects.WorldTag;
import com.denizenscript.denizen2sponge.utilities.GameRules;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.world.storage.WorldProperties;
import java.util.Map;
import java.util.Optional;
public class SetGameRuleCommand extends AbstractCommand {
// <--[explanation]
// @Since 0.3.0
// @Name Default Game Rules
// @Group Useful Lists
// @Description
// A list of all default game rules can be found here:
// <@link url https://jd.spongepowered.org/7.0.0-SNAPSHOT/org/spongepowered/api/world/gamerule/DefaultGameRules.html>default game rule list<@/link>
// These can be used with the setgamerule command. Note: these Sponge game rule
// ids are used by Denizen2, but they don't match the Minecraft ones. For example,
// 'keepInventory' in Minecraft becomes 'keep_inventory' in Sponge/Denizen2.
// -->
// <--[command]
// @Since 0.3.0
// @Name setgamerule
// @Arguments <world> <map of rules>
// @Short modifies the game rules of a world.
// @Updated 2017/05/16
// @Group World
// @Minimum 2
// @Maximum 2
// @Description
// Modifies the existing game rules of a world or creates new ones according
// to the map specified. Related information: <@link explanation Default Game Rules>default game rules<@/link>.
// @Example
// # Sets the default game rule 'mob_griefing' to 'false' in world 'Games'.
// - setgamerule Games mob_griefing:false
// @Example
// # Sets the custom game rules 'xp_multiplier' and 'currency_multiplier' to
// '3' and '5' respectively in world 'Adventure'.
// - setgamerule Adventure xp_multiplier:3|currency_multiplier:5
// -->
@Override
public String getName() {
return "setgamerule";
}
@Override
public String getArguments() {
return "<world> <map of rules>";
}
@Override
public int getMinimumArguments() {
return 2;
}
@Override
public int getMaximumArguments() {
return 2;
}
@Override
public void execute(CommandQueue queue, CommandEntry entry) {
AbstractTagObject world = entry.getArgumentObject(queue, 0);
WorldProperties properties;
if (world instanceof WorldTag) {
properties = ((WorldTag) world).getInternal().getProperties();
}
else {
Optional<WorldProperties> opt = Sponge.getServer().getWorldProperties(world.toString());
if (!opt.isPresent()) {
queue.handleError(entry, "Invalid world specified!");
return;
}
properties = opt.get();
}
MapTag map = MapTag.getFor(queue.error, entry.getArgumentObject(queue, 1));
for (Map.Entry<String, AbstractTagObject> mapEntry : map.getInternal().entrySet()) {
if (GameRules.SpongeToMinecraft.containsKey(mapEntry.getKey())) {
properties.setGameRule(GameRules.SpongeToMinecraft.get(mapEntry.getKey()), mapEntry.getValue().toString());
}
else {
properties.setGameRule(mapEntry.getKey(), mapEntry.getValue().toString());
}
}
if (queue.shouldShowGood()) {
queue.outGood("Modified game rules of world '" + ColorSet.emphasis + properties.getWorldName()
+ ColorSet.good + "' according to map: " + ColorSet.emphasis + map.debug() + ColorSet.good + "!");
}
}
}