|
| 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 | +} |
0 commit comments