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;
}
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;
}
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;
}
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;
}
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);
}
Aggregations