Search in sources :

Example 1 with PlotFlagRemoveEvent

use of com.plotsquared.core.events.PlotFlagRemoveEvent in project PlotSquared by IntellectualSites.

the class Desc method set.

@Override
public boolean set(PlotPlayer<?> player, Plot plot, String desc) {
    if (desc.isEmpty()) {
        PlotFlagRemoveEvent event = this.eventDispatcher.callFlagRemove(plot.getFlagContainer().getFlag(DescriptionFlag.class), plot);
        if (event.getEventResult() == Result.DENY) {
            player.sendMessage(TranslatableCaption.of("events.event_denied"), Template.of("value", "Description removal"));
            return false;
        }
        plot.removeFlag(event.getFlag());
        player.sendMessage(TranslatableCaption.of("desc.desc_unset"));
        return true;
    }
    PlotFlagAddEvent event = this.eventDispatcher.callFlagAdd(plot.getFlagContainer().getFlag(DescriptionFlag.class).createFlagInstance(desc), plot);
    if (event.getEventResult() == Result.DENY) {
        player.sendMessage(TranslatableCaption.of("events.event_denied"), Template.of("value", "Description set"));
        return false;
    }
    boolean result = plot.setFlag(event.getFlag());
    if (!result) {
        player.sendMessage(TranslatableCaption.of("flag.flag_not_added"));
        return false;
    }
    player.sendMessage(TranslatableCaption.of("desc.desc_set"));
    return true;
}
Also used : DescriptionFlag(com.plotsquared.core.plot.flag.implementations.DescriptionFlag) PlotFlagAddEvent(com.plotsquared.core.events.PlotFlagAddEvent) PlotFlagRemoveEvent(com.plotsquared.core.events.PlotFlagRemoveEvent)

Example 2 with PlotFlagRemoveEvent

use of com.plotsquared.core.events.PlotFlagRemoveEvent in project PlotSquared by IntellectualSites.

the class Clear method execute.

@Override
public CompletableFuture<Boolean> execute(final PlotPlayer<?> player, String[] args, RunnableVal3<Command, Runnable, Runnable> confirm, RunnableVal2<Command, CommandResult> whenDone) throws CommandException {
    if (args.length != 0) {
        sendUsage(player);
        return CompletableFuture.completedFuture(false);
    }
    final Plot plot = check(player.getCurrentPlot(), TranslatableCaption.of("errors.not_in_plot"));
    Result eventResult = this.eventDispatcher.callClear(plot).getEventResult();
    if (eventResult == Result.DENY) {
        player.sendMessage(TranslatableCaption.of("events.event_denied"), Template.of("value", "Clear"));
        return CompletableFuture.completedFuture(true);
    }
    if (plot.getVolume() > Integer.MAX_VALUE) {
        player.sendMessage(TranslatableCaption.of("schematics.schematic_too_large"));
        return CompletableFuture.completedFuture(true);
    }
    boolean force = eventResult == Result.FORCE;
    checkTrue(force || plot.isOwner(player.getUUID()) || Permissions.hasPermission(player, "plots.admin.command.clear"), TranslatableCaption.of("permission.no_plot_perms"));
    checkTrue(plot.getRunning() == 0, TranslatableCaption.of("errors.wait_for_timer"));
    checkTrue(force || !Settings.Done.RESTRICT_BUILDING || !DoneFlag.isDone(plot) || Permissions.hasPermission(player, "plots.continue"), TranslatableCaption.of("done.done_already_done"));
    confirm.run(this, () -> {
        if (Settings.Teleport.ON_CLEAR) {
            plot.getPlayersInPlot().forEach(playerInPlot -> plot.teleportPlayer(playerInPlot, TeleportCause.COMMAND_CLEAR, result -> {
            }));
        }
        BackupManager.backup(player, plot, () -> {
            final long start = System.currentTimeMillis();
            boolean result = plot.getPlotModificationManager().clear(true, false, player, () -> {
                plot.getPlotModificationManager().unlink();
                TaskManager.runTask(() -> {
                    plot.removeRunning();
                    // If the state changes, then mark it as no longer done
                    if (DoneFlag.isDone(plot)) {
                        PlotFlag<?, ?> plotFlag = plot.getFlagContainer().getFlag(DoneFlag.class);
                        PlotFlagRemoveEvent event = this.eventDispatcher.callFlagRemove(plotFlag, plot);
                        if (event.getEventResult() != Result.DENY) {
                            plot.removeFlag(event.getFlag());
                        }
                    }
                    if (!plot.getFlag(AnalysisFlag.class).isEmpty()) {
                        PlotFlag<?, ?> plotFlag = plot.getFlagContainer().getFlag(AnalysisFlag.class);
                        PlotFlagRemoveEvent event = this.eventDispatcher.callFlagRemove(plotFlag, plot);
                        if (event.getEventResult() != Result.DENY) {
                            plot.removeFlag(event.getFlag());
                        }
                    }
                    player.sendMessage(TranslatableCaption.of("working.clearing_done"), Template.of("amount", String.valueOf(System.currentTimeMillis() - start)), Template.of("plot", plot.getId().toString()));
                });
            });
            if (!result) {
                player.sendMessage(TranslatableCaption.of("errors.wait_for_timer"));
            } else {
                plot.addRunning();
            }
        });
    }, null);
    return CompletableFuture.completedFuture(true);
}
Also used : TeleportCause(com.plotsquared.core.events.TeleportCause) GlobalBlockQueue(com.plotsquared.core.queue.GlobalBlockQueue) PlotFlagRemoveEvent(com.plotsquared.core.events.PlotFlagRemoveEvent) Permissions(com.plotsquared.core.util.Permissions) NonNull(org.checkerframework.checker.nullness.qual.NonNull) Plot(com.plotsquared.core.plot.Plot) Inject(com.google.inject.Inject) TaskManager(com.plotsquared.core.util.task.TaskManager) CompletableFuture(java.util.concurrent.CompletableFuture) PlotFlag(com.plotsquared.core.plot.flag.PlotFlag) DoneFlag(com.plotsquared.core.plot.flag.implementations.DoneFlag) BackupManager(com.plotsquared.core.backup.BackupManager) Result(com.plotsquared.core.events.Result) RunnableVal3(com.plotsquared.core.util.task.RunnableVal3) PlotPlayer(com.plotsquared.core.player.PlotPlayer) TranslatableCaption(com.plotsquared.core.configuration.caption.TranslatableCaption) RunnableVal2(com.plotsquared.core.util.task.RunnableVal2) Settings(com.plotsquared.core.configuration.Settings) AnalysisFlag(com.plotsquared.core.plot.flag.implementations.AnalysisFlag) Template(net.kyori.adventure.text.minimessage.Template) EventDispatcher(com.plotsquared.core.util.EventDispatcher) Plot(com.plotsquared.core.plot.Plot) PlotFlagRemoveEvent(com.plotsquared.core.events.PlotFlagRemoveEvent) Result(com.plotsquared.core.events.Result)

Example 3 with PlotFlagRemoveEvent

use of com.plotsquared.core.events.PlotFlagRemoveEvent in project PlotSquared by IntellectualSites.

the class Music method onCommand.

@Override
public boolean onCommand(PlotPlayer<?> player, String[] args) {
    Location location = player.getLocation();
    final Plot plot = location.getPlotAbs();
    if (plot == null) {
        player.sendMessage(TranslatableCaption.of("errors.not_in_plot"));
        return false;
    }
    if (!plot.hasOwner()) {
        player.sendMessage(TranslatableCaption.of("info.plot_unowned"));
        return false;
    }
    if (!plot.isAdded(player.getUUID()) && !Permissions.hasPermission(player, Permission.PERMISSION_ADMIN_MUSIC_OTHER)) {
        player.sendMessage(TranslatableCaption.of("permission.no_permission"), Template.of("node", String.valueOf(Permission.PERMISSION_ADMIN_MUSIC_OTHER)));
        return true;
    }
    PlotInventory inv = new PlotInventory(this.inventoryUtil, player, 2, TranslatableCaption.of("plotjukebox.jukebox_header").getComponent(player)) {

        @Override
        public boolean onClick(int index) {
            PlotItemStack item = getItem(index);
            if (item == null) {
                return true;
            }
            if (item.getType() == ItemTypes.BEDROCK) {
                PlotFlag<?, ?> plotFlag = plot.getFlagContainer().getFlag(MusicFlag.class).createFlagInstance(item.getType());
                PlotFlagRemoveEvent event = eventDispatcher.callFlagRemove(plotFlag, plot);
                if (event.getEventResult() == Result.DENY) {
                    getPlayer().sendMessage(TranslatableCaption.of("events.event_denied"), Template.of("value", "Music removal"));
                    return true;
                }
                plot.removeFlag(event.getFlag());
                getPlayer().sendMessage(TranslatableCaption.of("flag.flag_removed"), Template.of("flag", "music"), Template.of("value", "music_disc"));
            } else if (item.getName().toLowerCase(Locale.ENGLISH).contains("disc")) {
                PlotFlag<?, ?> plotFlag = plot.getFlagContainer().getFlag(MusicFlag.class).createFlagInstance(item.getType());
                PlotFlagAddEvent event = eventDispatcher.callFlagAdd(plotFlag, plot);
                if (event.getEventResult() == Result.DENY) {
                    getPlayer().sendMessage(TranslatableCaption.of("events.event_denied"), Template.of("value", "Music addition"));
                    return true;
                }
                plot.setFlag(event.getFlag());
                getPlayer().sendMessage(TranslatableCaption.of("flag.flag_added"), Template.of("flag", "music"), Template.of("value", String.valueOf(event.getFlag().getValue())));
            } else {
                getPlayer().sendMessage(TranslatableCaption.of("flag.flag_not_added"));
            }
            return false;
        }
    };
    int index = 0;
    for (final String disc : DISCS) {
        final String name = String.format("<gold>%s</gold>", disc);
        final String[] lore = { TranslatableCaption.of("plotjukebox.click_to_play").getComponent(player) };
        ItemType type = ItemTypes.get(disc);
        if (type == null) {
            continue;
        }
        final PlotItemStack item = new PlotItemStack(type, 1, name, lore);
        if (inv.setItemChecked(index, item)) {
            index++;
        }
    }
    // Always add the cancel button
    // if (player.getMeta("music") != null) {
    String name = TranslatableCaption.of("plotjukebox.cancel_music").getComponent(player);
    String[] lore = { TranslatableCaption.of("plotjukebox.reset_music").getComponent(player) };
    inv.setItem(index, new PlotItemStack("bedrock", 1, name, lore));
    // }
    inv.openInventory();
    return true;
}
Also used : PlotFlag(com.plotsquared.core.plot.flag.PlotFlag) Plot(com.plotsquared.core.plot.Plot) ItemType(com.sk89q.worldedit.world.item.ItemType) PlotItemStack(com.plotsquared.core.plot.PlotItemStack) PlotInventory(com.plotsquared.core.plot.PlotInventory) PlotFlagAddEvent(com.plotsquared.core.events.PlotFlagAddEvent) PlotFlagRemoveEvent(com.plotsquared.core.events.PlotFlagRemoveEvent) MusicFlag(com.plotsquared.core.plot.flag.implementations.MusicFlag) Location(com.plotsquared.core.location.Location)

Example 4 with PlotFlagRemoveEvent

use of com.plotsquared.core.events.PlotFlagRemoveEvent in project PlotSquared by IntellectualSites.

the class PlotListener method plotEntry.

public boolean plotEntry(final PlotPlayer<?> player, final Plot plot) {
    if (plot.isDenied(player.getUUID()) && !Permissions.hasPermission(player, "plots.admin.entry.denied")) {
        player.sendMessage(TranslatableCaption.of("deny.no_enter"), Template.of("plot", plot.toString()));
        return false;
    }
    try (final MetaDataAccess<Plot> lastPlot = player.accessTemporaryMetaData(PlayerMetaDataKeys.TEMPORARY_LAST_PLOT)) {
        Plot last = lastPlot.get().orElse(null);
        if ((last != null) && !last.getId().equals(plot.getId())) {
            plotExit(player, last);
        }
        if (ExpireManager.IMP != null) {
            ExpireManager.IMP.handleEntry(player, plot);
        }
        lastPlot.set(plot);
    }
    this.eventDispatcher.callEntry(player, plot);
    if (plot.hasOwner()) {
        // This will inherit values from PlotArea
        final TitlesFlag.TitlesFlagValue titlesFlag = plot.getFlag(TitlesFlag.class);
        final boolean titles;
        if (titlesFlag == TitlesFlag.TitlesFlagValue.NONE) {
            titles = Settings.Titles.DISPLAY_TITLES;
        } else {
            titles = titlesFlag == TitlesFlag.TitlesFlagValue.TRUE;
        }
        String greeting = plot.getFlag(GreetingFlag.class);
        if (!greeting.isEmpty()) {
            if (!Settings.Chat.NOTIFICATION_AS_ACTIONBAR) {
                plot.format(StaticCaption.of(greeting), player, false).thenAcceptAsync(player::sendMessage);
            } else {
                plot.format(StaticCaption.of(greeting), player, false).thenAcceptAsync(player::sendActionBar);
            }
        }
        if (plot.getFlag(NotifyEnterFlag.class)) {
            if (!Permissions.hasPermission(player, "plots.flag.notify-enter.bypass")) {
                for (UUID uuid : plot.getOwners()) {
                    final PlotPlayer<?> owner = PlotSquared.platform().playerManager().getPlayerIfExists(uuid);
                    if (owner != null && !owner.getUUID().equals(player.getUUID()) && owner.canSee(player)) {
                        Caption caption = TranslatableCaption.of("notification.notify_enter");
                        notifyPlotOwner(player, plot, owner, caption);
                    }
                }
            }
        }
        final FlyFlag.FlyStatus flyStatus = plot.getFlag(FlyFlag.class);
        if (!Permissions.hasPermission(player, Permission.PERMISSION_ADMIN_FLIGHT)) {
            if (flyStatus != FlyFlag.FlyStatus.DEFAULT) {
                boolean flight = player.getFlight();
                GameMode gamemode = player.getGameMode();
                if (flight != (gamemode == GameModes.CREATIVE || gamemode == GameModes.SPECTATOR)) {
                    try (final MetaDataAccess<Boolean> metaDataAccess = player.accessPersistentMetaData(PlayerMetaDataKeys.PERSISTENT_FLIGHT)) {
                        metaDataAccess.set(player.getFlight());
                    }
                }
                player.setFlight(flyStatus == FlyFlag.FlyStatus.ENABLED);
            }
        }
        final GameMode gameMode = plot.getFlag(GamemodeFlag.class);
        if (!gameMode.equals(GamemodeFlag.DEFAULT)) {
            if (player.getGameMode() != gameMode) {
                if (!Permissions.hasPermission(player, "plots.gamemode.bypass")) {
                    player.setGameMode(gameMode);
                } else {
                    player.sendMessage(TranslatableCaption.of("gamemode.gamemode_was_bypassed"), Template.of("gamemode", String.valueOf(gameMode)), Template.of("plot", plot.getId().toString()));
                }
            }
        }
        final GameMode guestGameMode = plot.getFlag(GuestGamemodeFlag.class);
        if (!guestGameMode.equals(GamemodeFlag.DEFAULT)) {
            if (player.getGameMode() != guestGameMode && !plot.isAdded(player.getUUID())) {
                if (!Permissions.hasPermission(player, "plots.gamemode.bypass")) {
                    player.setGameMode(guestGameMode);
                } else {
                    player.sendMessage(TranslatableCaption.of("gamemode.gamemode_was_bypassed"), Template.of("gamemode", String.valueOf(guestGameMode)), Template.of("plot", plot.getId().toString()));
                }
            }
        }
        long time = plot.getFlag(TimeFlag.class);
        if (time != TimeFlag.TIME_DISABLED.getValue() && !player.getAttribute("disabletime")) {
            try {
                player.setTime(time);
            } catch (Exception ignored) {
                PlotFlag<?, ?> plotFlag = GlobalFlagContainer.getInstance().getFlag(TimeFlag.class);
                PlotFlagRemoveEvent event = this.eventDispatcher.callFlagRemove(plotFlag, plot);
                if (event.getEventResult() != Result.DENY) {
                    plot.removeFlag(event.getFlag());
                }
            }
        }
        player.setWeather(plot.getFlag(WeatherFlag.class));
        ItemType musicFlag = plot.getFlag(MusicFlag.class);
        try (final MetaDataAccess<Location> musicMeta = player.accessTemporaryMetaData(PlayerMetaDataKeys.TEMPORARY_MUSIC)) {
            if (musicFlag != null) {
                final String rawId = musicFlag.getId();
                if (rawId.contains("disc") || musicFlag == ItemTypes.AIR) {
                    Location location = player.getLocation();
                    Location lastLocation = musicMeta.get().orElse(null);
                    if (lastLocation != null) {
                        plot.getCenter(center -> player.playMusic(center.add(0, Short.MAX_VALUE, 0), musicFlag));
                        if (musicFlag == ItemTypes.AIR) {
                            musicMeta.remove();
                        }
                    }
                    if (musicFlag != ItemTypes.AIR) {
                        try {
                            musicMeta.set(location);
                            plot.getCenter(center -> player.playMusic(center.add(0, Short.MAX_VALUE, 0), musicFlag));
                        } catch (Exception ignored) {
                        }
                    }
                }
            } else {
                musicMeta.get().ifPresent(lastLoc -> {
                    musicMeta.remove();
                    player.playMusic(lastLoc, ItemTypes.AIR);
                });
            }
        }
        CommentManager.sendTitle(player, plot);
        if (titles && !player.getAttribute("disabletitles")) {
            String title;
            String subtitle;
            PlotTitle titleFlag = plot.getFlag(PlotTitleFlag.class);
            boolean fromFlag;
            if (titleFlag.title() != null && titleFlag.subtitle() != null) {
                title = titleFlag.title();
                subtitle = titleFlag.subtitle();
                fromFlag = true;
            } else {
                title = "";
                subtitle = "";
                fromFlag = false;
            }
            if (fromFlag || !plot.getFlag(ServerPlotFlag.class) || Settings.Titles.DISPLAY_DEFAULT_ON_SERVER_PLOT) {
                TaskManager.runTaskLaterAsync(() -> {
                    Plot lastPlot;
                    try (final MetaDataAccess<Plot> lastPlotAccess = player.accessTemporaryMetaData(PlayerMetaDataKeys.TEMPORARY_LAST_PLOT)) {
                        lastPlot = lastPlotAccess.get().orElse(null);
                    }
                    if ((lastPlot != null) && plot.getId().equals(lastPlot.getId()) && plot.hasOwner()) {
                        final UUID plotOwner = plot.getOwnerAbs();
                        String owner = PlayerManager.resolveName(plotOwner, true).getComponent(player);
                        Caption header = fromFlag ? StaticCaption.of(title) : TranslatableCaption.of("titles" + ".title_entered_plot");
                        Caption subHeader = fromFlag ? StaticCaption.of(subtitle) : TranslatableCaption.of("titles" + ".title_entered_plot_sub");
                        Template plotTemplate = Template.of("plot", lastPlot.getId().toString());
                        Template worldTemplate = Template.of("world", player.getLocation().getWorldName());
                        Template ownerTemplate = Template.of("owner", owner);
                        Template aliasTemplate = Template.of("alias", plot.getAlias());
                        final Consumer<String> userConsumer = user -> {
                            if (Settings.Titles.TITLES_AS_ACTIONBAR) {
                                player.sendActionBar(header, aliasTemplate, plotTemplate, worldTemplate, ownerTemplate);
                            } else {
                                player.sendTitle(header, subHeader, aliasTemplate, plotTemplate, worldTemplate, ownerTemplate);
                            }
                        };
                        UUID uuid = plot.getOwner();
                        if (uuid == null) {
                            userConsumer.accept("Unknown");
                        } else if (uuid.equals(DBFunc.SERVER)) {
                            userConsumer.accept(MINI_MESSAGE.stripTokens(TranslatableCaption.of("info.server").getComponent(player)));
                        } else {
                            PlotSquared.get().getImpromptuUUIDPipeline().getSingle(plot.getOwner(), (user, throwable) -> {
                                if (throwable != null) {
                                    userConsumer.accept("Unknown");
                                } else {
                                    userConsumer.accept(user);
                                }
                            });
                        }
                    }
                }, TaskTime.seconds(1L));
            }
        }
        TimedFlag.Timed<Integer> feed = plot.getFlag(FeedFlag.class);
        if (feed.getInterval() != 0 && feed.getValue() != 0) {
            feedRunnable.put(player.getUUID(), new Interval(feed.getInterval(), feed.getValue(), 20));
        }
        TimedFlag.Timed<Integer> heal = plot.getFlag(HealFlag.class);
        if (heal.getInterval() != 0 && heal.getValue() != 0) {
            healRunnable.put(player.getUUID(), new Interval(heal.getInterval(), heal.getValue(), 20));
        }
        return true;
    }
    return true;
}
Also used : NotifyEnterFlag(com.plotsquared.core.plot.flag.implementations.NotifyEnterFlag) FeedFlag(com.plotsquared.core.plot.flag.implementations.FeedFlag) Permission(com.plotsquared.core.permissions.Permission) StaticCaption(com.plotsquared.core.configuration.caption.StaticCaption) TimedFlag(com.plotsquared.core.plot.flag.types.TimedFlag) Map(java.util.Map) DenyExitFlag(com.plotsquared.core.plot.flag.implementations.DenyExitFlag) Template(net.kyori.adventure.text.minimessage.Template) EventDispatcher(com.plotsquared.core.util.EventDispatcher) FlyFlag(com.plotsquared.core.plot.flag.implementations.FlyFlag) Caption(com.plotsquared.core.configuration.caption.Caption) DBFunc(com.plotsquared.core.database.DBFunc) ServerPlotFlag(com.plotsquared.core.plot.flag.implementations.ServerPlotFlag) MiniMessage(net.kyori.adventure.text.minimessage.MiniMessage) TimeFlag(com.plotsquared.core.plot.flag.implementations.TimeFlag) UUID(java.util.UUID) Result(com.plotsquared.core.events.Result) HealFlag(com.plotsquared.core.plot.flag.implementations.HealFlag) GameModes(com.sk89q.worldedit.world.gamemode.GameModes) TranslatableCaption(com.plotsquared.core.configuration.caption.TranslatableCaption) GreetingFlag(com.plotsquared.core.plot.flag.implementations.GreetingFlag) GamemodeFlag(com.plotsquared.core.plot.flag.implementations.GamemodeFlag) Optional(java.util.Optional) PlayerMetaDataKeys(com.plotsquared.core.player.PlayerMetaDataKeys) FarewellFlag(com.plotsquared.core.plot.flag.implementations.FarewellFlag) PlotTitleFlag(com.plotsquared.core.plot.flag.implementations.PlotTitleFlag) WeatherFlag(com.plotsquared.core.plot.flag.implementations.WeatherFlag) TaskTime(com.plotsquared.core.util.task.TaskTime) MetaDataAccess(com.plotsquared.core.player.MetaDataAccess) HashMap(java.util.HashMap) PlotFlag(com.plotsquared.core.plot.flag.PlotFlag) PlotWeather(com.plotsquared.core.plot.PlotWeather) CommentManager(com.plotsquared.core.plot.comment.CommentManager) ExpireManager(com.plotsquared.core.plot.expiration.ExpireManager) GuestGamemodeFlag(com.plotsquared.core.plot.flag.implementations.GuestGamemodeFlag) Nullable(org.checkerframework.checker.nullness.qual.Nullable) Location(com.plotsquared.core.location.Location) GlobalFlagContainer(com.plotsquared.core.plot.flag.GlobalFlagContainer) TitlesFlag(com.plotsquared.core.plot.flag.implementations.TitlesFlag) PlotFlagRemoveEvent(com.plotsquared.core.events.PlotFlagRemoveEvent) Permissions(com.plotsquared.core.util.Permissions) Plot(com.plotsquared.core.plot.Plot) Iterator(java.util.Iterator) TaskManager(com.plotsquared.core.util.task.TaskManager) ItemTypes(com.sk89q.worldedit.world.item.ItemTypes) GameMode(com.sk89q.worldedit.world.gamemode.GameMode) Consumer(java.util.function.Consumer) PlotSquared(com.plotsquared.core.PlotSquared) PlayerManager(com.plotsquared.core.util.PlayerManager) PlotPlayer(com.plotsquared.core.player.PlotPlayer) NotifyLeaveFlag(com.plotsquared.core.plot.flag.implementations.NotifyLeaveFlag) PlotTitle(com.plotsquared.core.plot.PlotTitle) Settings(com.plotsquared.core.configuration.Settings) PlotArea(com.plotsquared.core.plot.PlotArea) ItemType(com.sk89q.worldedit.world.item.ItemType) MusicFlag(com.plotsquared.core.plot.flag.implementations.MusicFlag) ItemType(com.sk89q.worldedit.world.item.ItemType) Template(net.kyori.adventure.text.minimessage.Template) WeatherFlag(com.plotsquared.core.plot.flag.implementations.WeatherFlag) FlyFlag(com.plotsquared.core.plot.flag.implementations.FlyFlag) UUID(java.util.UUID) PlotFlagRemoveEvent(com.plotsquared.core.events.PlotFlagRemoveEvent) ServerPlotFlag(com.plotsquared.core.plot.flag.implementations.ServerPlotFlag) PlotFlag(com.plotsquared.core.plot.flag.PlotFlag) TitlesFlag(com.plotsquared.core.plot.flag.implementations.TitlesFlag) Plot(com.plotsquared.core.plot.Plot) StaticCaption(com.plotsquared.core.configuration.caption.StaticCaption) Caption(com.plotsquared.core.configuration.caption.Caption) TranslatableCaption(com.plotsquared.core.configuration.caption.TranslatableCaption) PlotTitle(com.plotsquared.core.plot.PlotTitle) ServerPlotFlag(com.plotsquared.core.plot.flag.implementations.ServerPlotFlag) TimedFlag(com.plotsquared.core.plot.flag.types.TimedFlag) GameMode(com.sk89q.worldedit.world.gamemode.GameMode) TimeFlag(com.plotsquared.core.plot.flag.implementations.TimeFlag) Location(com.plotsquared.core.location.Location)

Example 5 with PlotFlagRemoveEvent

use of com.plotsquared.core.events.PlotFlagRemoveEvent in project PlotSquared by IntellectualSites.

the class Buy method execute.

@Override
public CompletableFuture<Boolean> execute(final PlotPlayer<?> player, String[] args, RunnableVal3<Command, Runnable, Runnable> confirm, final RunnableVal2<Command, CommandResult> whenDone) {
    PlotArea area = player.getPlotAreaAbs();
    check(area, TranslatableCaption.of("errors.not_in_plot_world"));
    check(this.econHandler.isEnabled(area), TranslatableCaption.of("economy.econ_disabled"));
    final Plot plot;
    if (args.length != 0) {
        if (args.length != 1) {
            sendUsage(player);
            return CompletableFuture.completedFuture(false);
        }
        plot = check(Plot.getPlotFromString(player, args[0], true), null);
    } else {
        plot = check(player.getCurrentPlot(), TranslatableCaption.of("errors.not_in_plot"));
    }
    checkTrue(plot.hasOwner(), TranslatableCaption.of("info.plot_unowned"));
    checkTrue(!plot.isOwner(player.getUUID()), TranslatableCaption.of("economy.cannot_buy_own"));
    Set<Plot> plots = plot.getConnectedPlots();
    checkTrue(player.getPlotCount() + plots.size() <= player.getAllowedPlots(), TranslatableCaption.of("permission.cant_claim_more_plots"), Template.of("amount", String.valueOf(player.getAllowedPlots())));
    double price = plot.getFlag(PriceFlag.class);
    if (price <= 0) {
        throw new CommandException(TranslatableCaption.of("economy.not_for_sale"));
    }
    checkTrue(this.econHandler.isSupported(), TranslatableCaption.of("economy.vault_or_consumer_null"));
    checkTrue(this.econHandler.getMoney(player) >= price, TranslatableCaption.of("economy.cannot_afford_plot"), Template.of("money", this.econHandler.format(price)), Template.of("balance", this.econHandler.format(this.econHandler.getMoney(player))));
    this.econHandler.withdrawMoney(player, price);
    // Failure
    // Success
    confirm.run(this, () -> {
        player.sendMessage(TranslatableCaption.of("economy.removed_balance"), Template.of("money", this.econHandler.format(price)));
        this.econHandler.depositMoney(PlotSquared.platform().playerManager().getOfflinePlayer(plot.getOwnerAbs()), price);
        PlotPlayer<?> owner = PlotSquared.platform().playerManager().getPlayerIfExists(plot.getOwnerAbs());
        if (owner != null) {
            owner.sendMessage(TranslatableCaption.of("economy.plot_sold"), Template.of("plot", plot.getId().toString()), Template.of("player", player.getName()), Template.of("price", this.econHandler.format(price)));
        }
        PlotFlag<?, ?> plotFlag = plot.getFlagContainer().getFlag(PriceFlag.class);
        PlotFlagRemoveEvent event = this.eventDispatcher.callFlagRemove(plotFlag, plot);
        if (event.getEventResult() != Result.DENY) {
            plot.removeFlag(event.getFlag());
        }
        plot.setOwner(player.getUUID());
        player.sendMessage(TranslatableCaption.of("working.claimed"), Template.of("plot", plot.getId().toString()));
        whenDone.run(Buy.this, CommandResult.SUCCESS);
    }, () -> {
        this.econHandler.depositMoney(player, price);
        whenDone.run(Buy.this, CommandResult.FAILURE);
    });
    return CompletableFuture.completedFuture(true);
}
Also used : PlotArea(com.plotsquared.core.plot.PlotArea) Plot(com.plotsquared.core.plot.Plot) PlotFlagRemoveEvent(com.plotsquared.core.events.PlotFlagRemoveEvent)

Aggregations

PlotFlagRemoveEvent (com.plotsquared.core.events.PlotFlagRemoveEvent)7 Plot (com.plotsquared.core.plot.Plot)5 PlotFlag (com.plotsquared.core.plot.flag.PlotFlag)3 Settings (com.plotsquared.core.configuration.Settings)2 TranslatableCaption (com.plotsquared.core.configuration.caption.TranslatableCaption)2 PlotFlagAddEvent (com.plotsquared.core.events.PlotFlagAddEvent)2 Result (com.plotsquared.core.events.Result)2 Location (com.plotsquared.core.location.Location)2 PlotPlayer (com.plotsquared.core.player.PlotPlayer)2 PlotArea (com.plotsquared.core.plot.PlotArea)2 MusicFlag (com.plotsquared.core.plot.flag.implementations.MusicFlag)2 ItemType (com.sk89q.worldedit.world.item.ItemType)2 Inject (com.google.inject.Inject)1 PlotSquared (com.plotsquared.core.PlotSquared)1 BackupManager (com.plotsquared.core.backup.BackupManager)1 Caption (com.plotsquared.core.configuration.caption.Caption)1 StaticCaption (com.plotsquared.core.configuration.caption.StaticCaption)1 DBFunc (com.plotsquared.core.database.DBFunc)1 TeleportCause (com.plotsquared.core.events.TeleportCause)1 Permission (com.plotsquared.core.permissions.Permission)1