Skip to content

Commit 0eb6c0d

Browse files
authored
Implement homes subcommand which lists all the plots the sender owns
1 parent 329650b commit 0eb6c0d

10 files changed

Lines changed: 174 additions & 8 deletions

File tree

src/main/java/ms/kevi/plotplugin/PlotPlugin.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import cn.nukkit.level.generator.Generator;
2323
import cn.nukkit.plugin.PluginBase;
2424
import cn.nukkit.utils.Config;
25+
import lombok.Getter;
26+
import lombok.Setter;
2527
import ms.kevi.plotplugin.command.PlotCommand;
2628
import ms.kevi.plotplugin.generator.PlotGenerator;
2729
import ms.kevi.plotplugin.lang.Language;
@@ -34,8 +36,6 @@
3436
import ms.kevi.plotplugin.util.PlotLevelSettings;
3537
import ms.kevi.plotplugin.util.Utils;
3638
import ms.kevi.plotplugin.util.async.TaskExecutor;
37-
import lombok.Getter;
38-
import lombok.Setter;
3939

4040
import java.io.BufferedReader;
4141
import java.io.File;
@@ -77,6 +77,9 @@ public class PlotPlugin extends PluginBase {
7777

7878
private final PlayerNameFunction defaultNameFunction = (name, nameConsumer) -> nameConsumer.accept(name.equalsIgnoreCase("admin") ? "§4Administrator" : name);
7979

80+
@Getter
81+
private int plotsPerPage = 5;
82+
8083
@Override
8184
public void onLoad() {
8285
INSTANCE = this;
@@ -115,6 +118,13 @@ public void onEnable() {
115118
config.save();
116119
}
117120

121+
if(!config.exists("plots_per_page")) {
122+
config.set("plots_per_page", this.plotsPerPage);
123+
config.save();
124+
}
125+
126+
this.plotsPerPage = config.getInt("plots_per_page");
127+
118128
try {
119129
final String defaultLang = config.getString("default_lang");
120130

src/main/java/ms/kevi/plotplugin/command/PlotCommand.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public PlotCommand(PlotPlugin plugin) {
4848
this.subCommands.add(new DisposeCommand(this.plugin, this));
4949
this.subCommands.add(new GenerateCommand(this.plugin, this));
5050
this.subCommands.add(new HomeCommand(this.plugin, this));
51+
this.subCommands.add(new HomesCommand(this.plugin, this));
5152
this.subCommands.add(new InfoCommand(this.plugin, this));
5253
this.subCommands.add(new MergeCommand(this.plugin, this));
5354
this.subCommands.add(new RegenAllRoadsCommand(this.plugin, this));

src/main/java/ms/kevi/plotplugin/command/defaults/HomeCommand.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ public boolean execute(Player player, String[] args) {
6464
if(plotId < plots.size()) {
6565
final Plot plot = plots.get(plotId);
6666
final boolean canPerform = (!plot.isDenied(player.getUniqueId()) && !plot.isDenied(Utils.UUID_EVERYONE)) || player.hasPermission("plot.admin.nodeny");
67-
if(canPerform) {
68-
player.sendMessage(this.translate(player, TranslationKey.HOME_SUCCESS, this.plugin.getCorrectName(targetId)));
69-
plotManager.teleportPlayerToPlot(player, plots.get(plotId));
70-
} else if(targetName.equalsIgnoreCase(player.getName())) {
67+
if(targetName.equalsIgnoreCase(player.getName())) {
7168
player.sendMessage(this.translate(player, TranslationKey.HOME_SUCCESS_OWN));
7269
plotManager.teleportPlayerToPlot(player, plots.get(plotId));
70+
} else if(canPerform) {
71+
player.sendMessage(this.translate(player, TranslationKey.HOME_SUCCESS, this.plugin.getCorrectName(targetId)));
72+
plotManager.teleportPlayerToPlot(player, plots.get(plotId));
7373
} else
7474
player.sendMessage(this.translate(player, TranslationKey.HOME_FAILURE_DENIED));
7575
return true;
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2022 KCodeYT
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package ms.kevi.plotplugin.command.defaults;
18+
19+
import cn.nukkit.Player;
20+
import cn.nukkit.command.data.CommandParamType;
21+
import cn.nukkit.command.data.CommandParameter;
22+
import ms.kevi.plotplugin.PlotPlugin;
23+
import ms.kevi.plotplugin.command.PlotCommand;
24+
import ms.kevi.plotplugin.command.SubCommand;
25+
import ms.kevi.plotplugin.lang.TranslationKey;
26+
import ms.kevi.plotplugin.manager.PlotManager;
27+
import ms.kevi.plotplugin.util.PaginationList;
28+
import ms.kevi.plotplugin.util.Plot;
29+
import ms.kevi.plotplugin.util.Utils;
30+
31+
import java.util.ArrayList;
32+
import java.util.LinkedHashMap;
33+
import java.util.List;
34+
import java.util.Map;
35+
36+
/**
37+
* @author Kevims KCodeYT
38+
*/
39+
public class HomesCommand extends SubCommand {
40+
41+
public HomesCommand(PlotPlugin plugin, PlotCommand parent) {
42+
super(plugin, parent, "homes");
43+
this.addParameter(CommandParameter.newType("page", true, CommandParamType.INT));
44+
}
45+
46+
@Override
47+
public boolean execute(Player player, String[] args) {
48+
final int page = Utils.parseInteger(args.length == 1 ? args[0] : args.length > 1 ? args[1] : "1") - 1;
49+
50+
final Map<PlotManager, List<Plot>> plots = new LinkedHashMap<>();
51+
for(PlotManager plotManager : this.plugin.getPlotManagerMap().values())
52+
plots.put(plotManager, plotManager.getPlotsByOwner(player.getUniqueId()));
53+
54+
if(plots.size() != 0) {
55+
final List<Plot> allPlots = new ArrayList<>();
56+
for(List<Plot> list : plots.values()) allPlots.addAll(list);
57+
58+
final PaginationList<Plot> pages = new PaginationList<>(allPlots, this.plugin.getPlotsPerPage());
59+
60+
if(page < 0 || page >= pages.size()) {
61+
player.sendMessage(this.translate(player, TranslationKey.HOMES_FAILURE_PAGE_DID_NOT_EXIST, page + 1));
62+
return false;
63+
}
64+
65+
player.sendMessage(this.translate(player, TranslationKey.HOMES_TITLE, page + 1, pages.size()));
66+
67+
for(Plot plot : pages.get(page))
68+
player.sendMessage(this.translate(player, TranslationKey.HOMES_ENTRY,
69+
plots.get(plot.getManager()).indexOf(plot) + 1,
70+
plot.getId(),
71+
plot.getManager().getLevel().getName()
72+
));
73+
74+
player.sendMessage(this.translate(player, TranslationKey.HOMES_END));
75+
return true;
76+
} else {
77+
player.sendMessage(this.translate(player, TranslationKey.HOMES_FAILURE));
78+
return false;
79+
}
80+
}
81+
82+
}

src/main/java/ms/kevi/plotplugin/lang/Language.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public String translate(Player player, TranslationKey key, Object... params) {
7676

7777
String message = langEntry.getTranslations().get(key);
7878
for(int i = 0; i < params.length; i++)
79-
message = message.replace("{" + i + "}", Objects.toString(params[0]));
79+
message = message.replace("{" + i + "}", Objects.toString(params[i]));
8080

8181
return message.replaceAll("&", "" + TextFormat.ESCAPE);
8282
}

src/main/java/ms/kevi/plotplugin/lang/TranslationKey.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public enum TranslationKey {
7474
HELP_END,
7575
HELP_GENERATE,
7676
HELP_HOME,
77+
HELP_HOMES,
7778
HELP_INFO,
7879
HELP_MERGE,
7980
HELP_REGENALLROADS,
@@ -95,6 +96,11 @@ public enum TranslationKey {
9596
HOME_FAILURE_OWN_ID,
9697
HOME_SUCCESS,
9798
HOME_SUCCESS_OWN,
99+
HOMES_END,
100+
HOMES_ENTRY,
101+
HOMES_FAILURE,
102+
HOMES_FAILURE_PAGE_DID_NOT_EXIST,
103+
HOMES_TITLE,
98104
INFO_DAMAGE,
99105
INFO_DENIED,
100106
INFO_END,
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2022 KCodeYT
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package ms.kevi.plotplugin.util;
18+
19+
import java.util.AbstractList;
20+
import java.util.Collections;
21+
import java.util.List;
22+
23+
/**
24+
* @author Kevims KCodeYT
25+
*/
26+
public class PaginationList<T> extends AbstractList<List<T>> {
27+
28+
private final List<T> list;
29+
private final int pageSize;
30+
31+
public PaginationList(List<T> list, int pageSize) {
32+
this.list = Collections.unmodifiableList(list);
33+
this.pageSize = pageSize;
34+
}
35+
36+
@Override
37+
public List<T> get(int index) {
38+
final int start = index * this.pageSize;
39+
final int end = Math.min(start + this.pageSize, this.list.size());
40+
41+
if(start > end)
42+
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + this.size());
43+
44+
return this.list.subList(start, end);
45+
}
46+
47+
@Override
48+
public int size() {
49+
return (int) Math.ceil((double) this.list.size() / (double) this.pageSize);
50+
}
51+
52+
}

src/main/resources/config.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
# Language used by this plugin, corresponding file should exist in lang folder!
2-
default_lang: en_US
2+
default_lang: en_US
3+
4+
#Plots per home page (/homes)
5+
plots_per_page: 5

src/main/resources/lang/de_DE.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ help-auto=&6/p auto &7| &6Teleportiere und beanspruche das nächste Freie Grunds
8989
help-claim=&6/p claim &7| &6Beanspruche das Grundstück auf dem du stehst für dich
9090
help-setting=&6/p setting [settingName] [value] &7| &6Setzte Einstellungen für das Grundstück fest
9191
help-home=&6/p home [id] &7| &6Teleportiere dich zu deinem Grundstück
92+
help-homes=&6/p homes [page] &7| &6Listet alle Grundstücke auf die du besitzt
9293
help-warp=&6/p warp [plot-id] &7| &6Teleportiere dich zu dem Grundstück mit der PlotId
9394
help-setowner=&6/p setowner [player] &7| &6Ändere den Besitzer deines Grundstückes
9495
help-addhelper=&6/p addhelper [player] &7| &6Füge einen Helfer zu deinem Grundstück hinzu
@@ -116,3 +117,8 @@ regenroad-start=&6&lCitybuild &8&l» &r&aGeneriere diesen Weg erneut...
116117
regenroad-finished=&6&lCitybuild &8&l» &r&aDieser Weg wurden erfolgreich neu generiert!
117118
regenallroads-start=&6&lCitybuild &8&l» &r&aGeneriere alle Wege erneut...
118119
regenallroads-finished=&6&lCitybuild &8&l» &r&aAlle Wege wurden erfolgreich neu generiert!
120+
homes-title=&7Deine Grundstücke §8[§7{0}/{1}§8]
121+
homes-entry= &7- &6Plot #{0} ({1}) &7in Welt &6{2}
122+
homes-end=§7-----------------
123+
homes-failure=&6&lCitybuild &8&l» &r&cDu besitzt kein Grundstück!
124+
homes-failure-page-did-not-exist=&6&lCitybuild &8&l» &r&cDie Seite &6{0} &cscheint es nicht zu geben!

src/main/resources/lang/en_US.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ help-auto=&6/p auto &7| &6Teleport and claim the next free plot
9191
help-claim=&6/p claim &7| &6Claim the plot you are currently on for yourself
9292
help-setting=&6/p setting [settingName] [value] &7| &6Manage settings for this plot
9393
help-home=&6/p home [id] &7| &6Teleport yourself to your plot
94+
help-homes=&6/p homes [page] &7| &6Lists all the plots you own
9495
help-warp=&6/p warp [plot-id] &7| &6Teleport yourself to a plot with given plot id
9596
help-setowner=&6/p setowner [player] &7| &6Change the owner of your plot
9697
help-addhelper=&6/p addhelper [player] &7| &6Add a helper to your plot
@@ -118,3 +119,8 @@ regenroad-start=&6&lCitybuild &8&l» &r&aRegenerate this road...
118119
regenroad-finished=&6&lCitybuild &8&l» &r&aSuccessfully regenerated this road!
119120
regenallroads-start=&6&lCitybuild &8&l» &r&aRegenerate all roads...
120121
regenallroads-finished=&6&lCitybuild &8&l» &r&aSuccessfully regenerated all roads!
122+
homes-title=&7Your plots §8[§7{0}/{1}§8]
123+
homes-entry= &7- &6Plot #{0} ({1}) &7in world &6{2}
124+
homes-end=§7-----------------
125+
homes-failure=&6&lCitybuild &8&l» &r&cYou do not own a plot!
126+
homes-failure-page-did-not-exist=&6&lCitybuild &8&l» &r&cThe page &6{0} &cseems not to exist!

0 commit comments

Comments
 (0)