-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathArgHome.java
More file actions
188 lines (161 loc) · 7.66 KB
/
ArgHome.java
File metadata and controls
188 lines (161 loc) · 7.66 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package dev.espi.protectionstones.commands;
import dev.espi.protectionstones.*;
import dev.espi.protectionstones.gui.screens.home.HomeWorldsGui;
import dev.espi.protectionstones.utils.ChatUtil;
import dev.espi.protectionstones.utils.MiscUtil;
import dev.espi.protectionstones.utils.TextGUI;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.util.StringUtil;
import java.util.*;
import java.util.stream.Collectors;
public class ArgHome implements PSCommandArg {
private static HashMap<UUID, List<String>> tabCache = new HashMap<>();
@Override
public List<String> getNames() {
return Collections.singletonList("home");
}
@Override
public boolean allowNonPlayersToExecute() {
return false;
}
@Override
public List<String> getPermissionsToExecute() {
return Arrays.asList("protectionstones.home");
}
@Override
public HashMap<String, Boolean> getRegisteredFlags() {
HashMap<String, Boolean> h = new HashMap<>();
h.put("-p", true);
return h;
}
// tab completion
@Override
public List<String> tabComplete(CommandSender sender, String alias, String[] args) {
if (!(sender instanceof Player p)) return null;
PSPlayer psp = PSPlayer.fromPlayer(p);
if (args.length == 2) {
// add to cache if not already
if (tabCache.get(p.getUniqueId()) == null) {
List<PSRegion> regions = psp.getHomes(p.getWorld());
List<String> regionNames = new ArrayList<>();
for (PSRegion r : regions) {
if (r.getName() != null) {
regionNames.add(r.getName());
} else {
regionNames.add(r.getId());
}
}
// cache home regions
tabCache.put(p.getUniqueId(), regionNames);
Bukkit.getScheduler().runTaskLater(ProtectionStones.getInstance(), () -> {
tabCache.remove(p.getUniqueId());
}, 200); // remove cache after 10 seconds
}
return StringUtil.copyPartialMatches(args[1], tabCache.get(p.getUniqueId()), new ArrayList<>());
}
return null;
}
private static final int GUI_SIZE = 17;
private void openHomeGUI(PSPlayer psp, List<PSRegion> homes, int page) {
List<TextComponent> entries = new ArrayList<>();
for (PSRegion r : homes) {
String msg;
if (r.getName() == null) {
msg = ChatColor.GRAY + "> " + ChatColor.AQUA + r.getId();
} else {
msg = ChatColor.GRAY + "> " + ChatColor.AQUA + r.getName() + " (" + r.getId() + ")";
}
TextComponent tc = new TextComponent(msg);
tc.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(PSL.HOME_CLICK_TO_TP.msg()).create()));
if (r.getName() == null) {
tc.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/" + ProtectionStones.getInstance().getConfigOptions().base_command + " home " + r.getId()));
} else {
tc.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/" + ProtectionStones.getInstance().getConfigOptions().base_command + " home " + r.getName()));
}
entries.add(tc);
}
TextGUI.displayGUI(psp.getPlayer(), PSL.HOME_HEADER.msg(), "/" + ProtectionStones.getInstance().getConfigOptions().base_command + " home -p %page%", page, GUI_SIZE, entries, true);
if (page * GUI_SIZE + GUI_SIZE < entries.size())
PSL.msg(psp, PSL.HOME_NEXT.msg().replace("%page%", page + 2 + ""));
}
@Override
public boolean executeArgument(CommandSender s, String[] args, HashMap<String, String> flags) {
Player p = (Player) s;
// GUI toggle (inventory GUI vs legacy text-based output)
PSConfig conf = ProtectionStones.getInstance().getConfigOptions();
boolean useInvGui = Boolean.TRUE.equals(conf.guiEnabled) && Boolean.TRUE.equals(conf.guiCommandHome);
// prelim checks
if (!p.hasPermission("protectionstones.home"))
return PSL.msg(p, PSL.NO_PERMISSION_HOME.msg());
if (args.length != 2 && args.length != 1)
return PSL.msg(p, PSL.HOME_HELP.msg());
Bukkit.getScheduler().runTaskAsynchronously(ProtectionStones.getInstance(), () -> {
PSPlayer psp = PSPlayer.fromPlayer(p);
if (args.length == 1) {
// just "/ps home"
if (useInvGui) {
// gather homes across all worlds; if only 1 total, teleport directly
List<PSRegion> allHomes = new ArrayList<>();
for (org.bukkit.World w : Bukkit.getWorlds()) {
allHomes.addAll(psp.getHomes(w));
}
if (allHomes.size() == 1) {
ArgTp.teleportPlayer(p, allHomes.get(0));
} else {
Bukkit.getScheduler().runTask(ProtectionStones.getInstance(), () ->
ProtectionStones.getInstance().getGuiManager().open(p, new HomeWorldsGui(ProtectionStones.getInstance().getGuiManager(), 0))
);
}
} else {
// legacy text-based GUI (current-world homes)
List<PSRegion> regions = psp.getHomes(p.getWorld());
if (regions.size() == 1) { // teleport to home if there is only one home
ArgTp.teleportPlayer(p, regions.get(0));
} else { // otherwise, open the text GUI
openHomeGUI(psp, regions, (flags.get("-p") == null || !MiscUtil.isValidInteger(flags.get("-p")) ? 0 : Integer.parseInt(flags.get("-p")) - 1));
}
}
} else {// /ps home [id]
// get regions from the query
String query = args[1];
List<PSRegion> regions = psp.getHomes(p.getWorld())
.stream()
.filter(region -> region.getId().equals(query)
|| (region.getName() != null && region.getName().equals(query)))
.collect(Collectors.toList());
if (regions.isEmpty()) {
PSL.msg(s, PSL.REGION_DOES_NOT_EXIST.msg());
return;
}
// if there is more than one name in the query
if (regions.size() > 1) {
ChatUtil.displayDuplicateRegionAliases(p, regions);
return;
}
ArgTp.teleportPlayer(p, regions.get(0));
}
});
return true;
}
}