Search in sources :

Example 1 with PlotInventory

use of com.plotsquared.core.plot.PlotInventory in project PlotSquared by IntellectualSites.

the class ComponentPresetManager method buildInventory.

/**
 * Build the component inventory for a player. This also checks
 * if the player is in a compatible plot, and sends appropriate
 * error messages if not
 *
 * @param player player
 * @return Build inventory, if it could be created
 */
@Nullable
public PlotInventory buildInventory(final PlotPlayer<?> player) {
    final Plot plot = player.getCurrentPlot();
    if (plot == null) {
        player.sendMessage(TranslatableCaption.of("errors.not_in_plot"));
        return null;
    } else if (!plot.hasOwner()) {
        player.sendMessage(TranslatableCaption.of("info.plot_unowned"));
        return null;
    } else if (!plot.isOwner(player.getUUID()) && !plot.getTrusted().contains(player.getUUID()) && !Permissions.hasPermission(player, Permission.PERMISSION_ADMIN_COMPONENTS_OTHER)) {
        player.sendMessage(TranslatableCaption.of("permission.no_plot_perms"));
        return null;
    } else if (plot.getVolume() > Integer.MAX_VALUE) {
        player.sendMessage(TranslatableCaption.of("schematics.schematic_too_large"));
        return null;
    }
    final List<ComponentPreset> allowedPresets = new ArrayList<>(this.presets.size());
    for (final ComponentPreset componentPreset : this.presets) {
        if (!componentPreset.getPermission().isEmpty() && !Permissions.hasPermission(player, componentPreset.getPermission())) {
            continue;
        }
        allowedPresets.add(componentPreset);
    }
    if (allowedPresets.isEmpty()) {
        player.sendMessage(TranslatableCaption.of("preset.empty"));
        return null;
    }
    final int size = (int) Math.ceil((double) allowedPresets.size() / 9.0D);
    final PlotInventory plotInventory = new PlotInventory(this.inventoryUtil, player, size, TranslatableCaption.of("preset.title").getComponent(player)) {

        @Override
        public boolean onClick(final int index) {
            if (!getPlayer().getCurrentPlot().equals(plot)) {
                return false;
            }
            if (index < 0 || index >= allowedPresets.size()) {
                return false;
            }
            final ComponentPreset componentPreset = allowedPresets.get(index);
            if (componentPreset == null) {
                return false;
            }
            if (plot.getRunning() > 0) {
                getPlayer().sendMessage(TranslatableCaption.of("errors.wait_for_timer"));
                return false;
            }
            final Pattern pattern = PatternUtil.parse(null, componentPreset.getPattern(), false);
            if (pattern == null) {
                getPlayer().sendMessage(TranslatableCaption.of("preset.preset_invalid"));
                return false;
            }
            if (componentPreset.getCost() > 0.0D) {
                if (!econHandler.isEnabled(plot.getArea())) {
                    getPlayer().sendMessage(TranslatableCaption.of("preset.economy_disabled"), Template.of("preset", componentPreset.getDisplayName()));
                    return false;
                }
                if (econHandler.getMoney(getPlayer()) < componentPreset.getCost()) {
                    getPlayer().sendMessage(TranslatableCaption.of("preset.preset_cannot_afford"));
                    return false;
                } else {
                    econHandler.withdrawMoney(getPlayer(), componentPreset.getCost());
                    getPlayer().sendMessage(TranslatableCaption.of("economy.removed_balance"), Template.of("money", econHandler.format(componentPreset.getCost())));
                }
            }
            BackupManager.backup(getPlayer(), plot, () -> {
                plot.addRunning();
                QueueCoordinator queue = plot.getArea().getQueue();
                queue.setCompleteTask(plot::removeRunning);
                for (Plot current : plot.getConnectedPlots()) {
                    current.getPlotModificationManager().setComponent(componentPreset.getComponent().name(), pattern, player, queue);
                }
                queue.enqueue();
                getPlayer().sendMessage(TranslatableCaption.of("working.generating_component"));
            });
            return false;
        }
    };
    for (int i = 0; i < allowedPresets.size(); i++) {
        final ComponentPreset preset = allowedPresets.get(i);
        final List<String> lore = new ArrayList<>();
        if (preset.getCost() > 0) {
            if (!this.econHandler.isEnabled(plot.getArea())) {
                lore.add(MINI_MESSAGE.serialize(MINI_MESSAGE.parse(TranslatableCaption.of("preset.preset_lore_economy_disabled").getComponent(player))));
            } else {
                lore.add(MINI_MESSAGE.serialize(MINI_MESSAGE.parse(TranslatableCaption.of("preset.preset_lore_cost").getComponent(player), Template.of("cost", String.format("%.2f", preset.getCost())))));
            }
        }
        lore.add(MINI_MESSAGE.serialize(MINI_MESSAGE.parse(TranslatableCaption.of("preset.preset_lore_component").getComponent(player), Template.of("component", preset.getComponent().name().toLowerCase()), Template.of("prefix", TranslatableCaption.of("core.prefix").getComponent(player)))));
        lore.removeIf(String::isEmpty);
        lore.addAll(preset.getDescription());
        plotInventory.setItem(i, new PlotItemStack(preset.getIcon().getId().replace("minecraft:", ""), 1, preset.getDisplayName(), lore.toArray(new String[0])));
    }
    return plotInventory;
}
Also used : Pattern(com.sk89q.worldedit.function.pattern.Pattern) Plot(com.plotsquared.core.plot.Plot) ArrayList(java.util.ArrayList) PlotItemStack(com.plotsquared.core.plot.PlotItemStack) QueueCoordinator(com.plotsquared.core.queue.QueueCoordinator) PlotInventory(com.plotsquared.core.plot.PlotInventory) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 2 with PlotInventory

use of com.plotsquared.core.plot.PlotInventory 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 3 with PlotInventory

use of com.plotsquared.core.plot.PlotInventory in project PlotSquared by IntellectualSites.

the class BukkitInventoryUtil method open.

// Paper deprecation
@SuppressWarnings("deprecation")
@Override
public void open(PlotInventory inv) {
    BukkitPlayer bp = (BukkitPlayer) inv.getPlayer();
    Inventory inventory = Bukkit.createInventory(null, inv.getLines() * 9, ChatColor.translateAlternateColorCodes('&', inv.getTitle()));
    PlotItemStack[] items = inv.getItems();
    for (int i = 0; i < inv.getLines() * 9; i++) {
        PlotItemStack item = items[i];
        if (item != null) {
            inventory.setItem(i, getItem(item));
        }
    }
    bp.player.openInventory(inventory);
}
Also used : PlotItemStack(com.plotsquared.core.plot.PlotItemStack) Inventory(org.bukkit.inventory.Inventory) PlotInventory(com.plotsquared.core.plot.PlotInventory) PlayerInventory(org.bukkit.inventory.PlayerInventory) BukkitPlayer(com.plotsquared.bukkit.player.BukkitPlayer)

Aggregations

PlotInventory (com.plotsquared.core.plot.PlotInventory)3 PlotItemStack (com.plotsquared.core.plot.PlotItemStack)3 Plot (com.plotsquared.core.plot.Plot)2 BukkitPlayer (com.plotsquared.bukkit.player.BukkitPlayer)1 PlotFlagAddEvent (com.plotsquared.core.events.PlotFlagAddEvent)1 PlotFlagRemoveEvent (com.plotsquared.core.events.PlotFlagRemoveEvent)1 Location (com.plotsquared.core.location.Location)1 PlotFlag (com.plotsquared.core.plot.flag.PlotFlag)1 MusicFlag (com.plotsquared.core.plot.flag.implementations.MusicFlag)1 QueueCoordinator (com.plotsquared.core.queue.QueueCoordinator)1 Pattern (com.sk89q.worldedit.function.pattern.Pattern)1 ItemType (com.sk89q.worldedit.world.item.ItemType)1 ArrayList (java.util.ArrayList)1 Inventory (org.bukkit.inventory.Inventory)1 PlayerInventory (org.bukkit.inventory.PlayerInventory)1 Nullable (org.checkerframework.checker.nullness.qual.Nullable)1