Search in sources :

Example 1 with PlotItemStack

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

the class BukkitInventoryUtil method setItemChecked.

@Override
public boolean setItemChecked(PlotInventory inv, int index, PlotItemStack item) {
    BukkitPlayer bp = (BukkitPlayer) inv.getPlayer();
    InventoryView opened = bp.player.getOpenInventory();
    ItemStack stack = getItem(item);
    if (stack == null) {
        return false;
    }
    if (!inv.isOpen()) {
        return true;
    }
    opened.setItem(index, stack);
    bp.player.updateInventory();
    return true;
}
Also used : InventoryView(org.bukkit.inventory.InventoryView) PlotItemStack(com.plotsquared.core.plot.PlotItemStack) ItemStack(org.bukkit.inventory.ItemStack) BukkitPlayer(com.plotsquared.bukkit.player.BukkitPlayer)

Example 2 with PlotItemStack

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

use of com.plotsquared.core.plot.PlotItemStack 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 PlotItemStack

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

the class BukkitInventoryUtil method getItem.

@Nullable
private static ItemStack getItem(PlotItemStack item) {
    if (item == null) {
        return null;
    }
    Material material = BukkitAdapter.adapt(item.getType());
    if (material == null) {
        return null;
    }
    ItemStack stack = new ItemStack(material, item.getAmount());
    ItemMeta meta = null;
    if (item.getName() != null) {
        meta = stack.getItemMeta();
        Component nameComponent = BukkitUtil.MINI_MESSAGE.parse(item.getName());
        meta.setDisplayName(BukkitUtil.LEGACY_COMPONENT_SERIALIZER.serialize(nameComponent));
    }
    if (item.getLore() != null) {
        if (meta == null) {
            meta = stack.getItemMeta();
        }
        List<String> lore = new ArrayList<>();
        for (String entry : item.getLore()) {
            lore.add(BukkitUtil.LEGACY_COMPONENT_SERIALIZER.serialize(BukkitUtil.MINI_MESSAGE.deserialize(entry)));
        }
        meta.setLore(lore);
    }
    if (meta != null) {
        stack.setItemMeta(meta);
    }
    return stack;
}
Also used : ArrayList(java.util.ArrayList) Material(org.bukkit.Material) PlotItemStack(com.plotsquared.core.plot.PlotItemStack) ItemStack(org.bukkit.inventory.ItemStack) Component(net.kyori.adventure.text.Component) ItemMeta(org.bukkit.inventory.meta.ItemMeta) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 5 with PlotItemStack

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

the class BukkitInventoryUtil method getItem.

// Paper deprecation
@SuppressWarnings("deprecation")
public PlotItemStack getItem(ItemStack item) {
    if (item == null) {
        return null;
    }
    // int id = item.getTypeId();
    Material id = item.getType();
    ItemMeta meta = item.getItemMeta();
    int amount = item.getAmount();
    String name = null;
    String[] lore = null;
    if (item.hasItemMeta()) {
        assert meta != null;
        if (meta.hasDisplayName()) {
            name = meta.getDisplayName();
        }
        if (meta.hasLore()) {
            List<String> itemLore = meta.getLore();
            assert itemLore != null;
            lore = itemLore.toArray(new String[0]);
        }
    }
    return new PlotItemStack(id.name(), amount, name, lore);
}
Also used : PlotItemStack(com.plotsquared.core.plot.PlotItemStack) Material(org.bukkit.Material) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Aggregations

PlotItemStack (com.plotsquared.core.plot.PlotItemStack)7 PlotInventory (com.plotsquared.core.plot.PlotInventory)4 BukkitPlayer (com.plotsquared.bukkit.player.BukkitPlayer)3 ArrayList (java.util.ArrayList)3 Material (org.bukkit.Material)3 ItemStack (org.bukkit.inventory.ItemStack)3 ItemMeta (org.bukkit.inventory.meta.ItemMeta)3 Nullable (org.checkerframework.checker.nullness.qual.Nullable)3 Plot (com.plotsquared.core.plot.Plot)2 Component (net.kyori.adventure.text.Component)2 Inventory (org.bukkit.inventory.Inventory)2 InventoryView (org.bukkit.inventory.InventoryView)2 PlayerInventory (org.bukkit.inventory.PlayerInventory)2 Singleton (com.google.inject.Singleton)1 PlotFlagAddEvent (com.plotsquared.core.events.PlotFlagAddEvent)1 PlotFlagRemoveEvent (com.plotsquared.core.events.PlotFlagRemoveEvent)1 Location (com.plotsquared.core.location.Location)1 PlotPlayer (com.plotsquared.core.player.PlotPlayer)1 PlotFlag (com.plotsquared.core.plot.flag.PlotFlag)1 MusicFlag (com.plotsquared.core.plot.flag.implementations.MusicFlag)1