001/*
002 * PlotSquared, a land and world management plugin for Minecraft.
003 * Copyright (C) IntellectualSites <https://intellectualsites.com>
004 * Copyright (C) IntellectualSites team and contributors
005 *
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
018 */
019package com.plotsquared.core.command;
020
021import com.plotsquared.core.configuration.caption.TranslatableCaption;
022import com.plotsquared.core.location.BlockLoc;
023import com.plotsquared.core.location.Location;
024import com.plotsquared.core.player.PlotPlayer;
025import com.plotsquared.core.plot.Plot;
026import net.kyori.adventure.text.minimessage.Template;
027
028@CommandDeclaration(command = "sethome",
029        permission = "plots.set.home",
030        usage = "/plot sethome [none]",
031        aliases = {"sh", "seth"},
032        category = CommandCategory.SETTINGS,
033        requiredType = RequiredType.PLAYER)
034public class SetHome extends SetCommand {
035
036    @Override
037    public boolean set(PlotPlayer<?> player, Plot plot, String value) {
038        if (!plot.hasOwner()) {
039            player.sendMessage(TranslatableCaption.of("info.plot_unowned"));
040            return false;
041        }
042        switch (value.toLowerCase()) {
043            case "unset", "reset", "remove", "none" -> {
044                Plot base = plot.getBasePlot(false);
045                base.setHome(null);
046                player.sendMessage(TranslatableCaption.of("position.position_unset"));
047                return true;
048            }
049            case "" -> {
050                Plot base = plot.getBasePlot(false);
051                Location bottom = base.getBottomAbs();
052                Location location = player.getLocationFull();
053                BlockLoc rel = new BlockLoc(
054                        location.getX() - bottom.getX(),
055                        location.getY(), // y is absolute
056                        location.getZ() - bottom.getZ(),
057                        location.getYaw(),
058                        location.getPitch()
059                );
060                base.setHome(rel);
061                player.sendMessage(TranslatableCaption.of("position.position_set"));
062                return true;
063            }
064            default -> {
065                player.sendMessage(
066                        TranslatableCaption.of("commandconfig.command_syntax"),
067                        Template.of("value", "Use /plot set home [none]")
068                );
069                return false;
070            }
071        }
072    }
073
074}