Search in sources :

Example 1 with WorldEditException

use of com.sk89q.worldedit.WorldEditException in project Plot-System by AlpsBTE.

the class ReviewPlotMenu method setItemClickEventsAsync.

@Override
protected void setItemClickEventsAsync() {
    // Set click event for close item
    getMenu().getSlot(50).setClickHandler((clickPlayer, clickInformation) -> clickPlayer.closeInventory());
    // Set click event for plot info item
    getMenu().getSlot(4).setClickHandler((clickPlayer, clickInformation) -> {
        clickPlayer.closeInventory();
        try {
            new PlotActionsMenu(clickPlayer, plot);
        } catch (SQLException ex) {
            Bukkit.getLogger().log(Level.SEVERE, "A SQL error occurred!", ex);
        }
    });
    // Set click event for submit item
    getMenu().getSlot(48).setClickHandler((clickPlayer, clickInformation) -> {
        CompletableFuture.runAsync(() -> {
            try {
                StringBuilder score = new StringBuilder();
                int totalRating = 0;
                boolean isRejected = false;
                for (int i = 0; i < 4; i++) {
                    for (int j = 0; j < 6; j++) {
                        if (getMenu().getSlot(11 + (i * 9) + j).getItem(clickPlayer).getItemMeta().hasEnchant(Enchantment.ARROW_DAMAGE)) {
                            if (i == 3) {
                                score.append(j);
                            } else {
                                score.append(j).append(",");
                            }
                            totalRating += j;
                            if (j <= 0) {
                                isRejected = true;
                            }
                        }
                    }
                }
                if (totalRating <= 8)
                    isRejected = true;
                if (totalRating == 0 && !sentWarning) {
                    clickPlayer.sendMessage(Utils.getInfoMessageFormat(LangUtil.get(getMenuPlayer(), LangPaths.Message.Info.PLOT_WILL_GET_ABANDONED)));
                    clickPlayer.playSound(clickPlayer.getLocation(), Utils.CreatePlotSound, 1, 1);
                    sentWarning = true;
                    return;
                } else if (isRejected && !sentWarning) {
                    clickPlayer.sendMessage(Utils.getInfoMessageFormat(LangUtil.get(getMenuPlayer(), LangPaths.Message.Info.PLOT_WILL_GET_REJECTED)));
                    clickPlayer.playSound(clickPlayer.getLocation(), Utils.CreatePlotSound, 1, 1);
                    sentWarning = true;
                    return;
                } else if (totalRating == 0) {
                    plot.setStatus(Status.unfinished);
                    Bukkit.getScheduler().runTask(PlotSystem.getPlugin(), () -> clickPlayer.performCommand("plot abandon " + plot.getID()));
                    return;
                }
                if (plot.isReviewed()) {
                    plot.getReview().setRating(score.toString());
                    plot.getReview().setReviewer(clickPlayer.getUniqueId());
                } else {
                    new Review(plot.getID(), clickPlayer.getUniqueId(), score.toString());
                }
                double totalRatingWithMultiplier = totalRating * PlotManager.getMultiplierByDifficulty(plot.getDifficulty());
                totalRating = (int) Math.floor(totalRatingWithMultiplier);
                plot.setTotalScore(totalRating);
                String reviewerConfirmationMessage;
                clickPlayer.closeInventory();
                if (!isRejected) {
                    clickPlayer.sendMessage(Utils.getInfoMessageFormat(LangUtil.get(getMenuPlayer(), LangPaths.Message.Info.SAVING_PLOT)));
                    try {
                        if (!PlotManager.savePlotAsSchematic(plot)) {
                            clickPlayer.sendMessage(Utils.getErrorMessageFormat(LangUtil.get(getMenuPlayer(), LangPaths.Message.Error.ERROR_OCCURRED)));
                            Bukkit.getLogger().log(Level.WARNING, "Could not save finished plot schematic (ID: " + plot.getID() + ")!");
                            return;
                        }
                    } catch (IOException | SQLException | WorldEditException ex) {
                        Bukkit.getLogger().log(Level.WARNING, "Could not save finished plot schematic (ID: " + plot.getID() + ")!", ex);
                    }
                    plot.setStatus(Status.completed);
                    plot.getReview().setFeedbackSent(false);
                    plot.getReview().setFeedback("No Feedback");
                    plot.getPlotOwner().addCompletedBuild(1);
                    // Remove Plot from Owner
                    plot.getPlotOwner().removePlot(plot.getSlot());
                    if (plot.getPlotMembers().isEmpty()) {
                        // Plot was made alone
                        reviewerConfirmationMessage = Utils.getInfoMessageFormat(LangUtil.get(getMenuPlayer(), LangPaths.Message.Info.PLOT_MARKED_REVIEWED, Integer.toString(plot.getID()), plot.getPlotOwner().getName()));
                        // Builder gets 100% of score
                        plot.getPlotOwner().addScore(totalRating);
                    } else {
                        // Plot was made in a group
                        StringBuilder sb = new StringBuilder();
                        for (int i = 0; i < plot.getPlotMembers().size(); i++) {
                            sb.append(i == plot.getPlotMembers().size() - 1 ? plot.getPlotMembers().get(i).getName() : plot.getPlotMembers().get(i).getName() + ", ");
                        }
                        reviewerConfirmationMessage = Utils.getInfoMessageFormat(LangUtil.get(getMenuPlayer(), LangPaths.Message.Info.PLOT_MARKED_REVIEWED, Integer.toString(plot.getID()), sb.toString()));
                        // Score gets split between all participants
                        plot.getPlotOwner().addScore(plot.getSharedScore());
                        for (Builder builder : plot.getPlotMembers()) {
                            // Score gets split between all participants
                            builder.addScore(plot.getSharedScore());
                            builder.addCompletedBuild(1);
                            // Remove Slot from Member
                            builder.removePlot(builder.getSlot(plot));
                        }
                    }
                } else {
                    if (plot.getPlotMembers().size() != 0) {
                        // Plot was made alone
                        reviewerConfirmationMessage = Utils.getInfoMessageFormat(LangUtil.get(getMenuPlayer(), LangPaths.Message.Info.PLOT_REJECTED, Integer.toString(plot.getID()), plot.getPlotOwner().getName()));
                    } else {
                        // Plot was made in a group
                        StringBuilder sb = new StringBuilder();
                        for (int i = 0; i < plot.getPlotMembers().size(); i++) {
                            sb.append(i == plot.getPlotMembers().size() - 1 ? plot.getPlotMembers().get(i).getName() : plot.getPlotMembers().get(i).getName() + ", ");
                        }
                        reviewerConfirmationMessage = Utils.getInfoMessageFormat(LangUtil.get(getMenuPlayer(), LangPaths.Message.Info.PLOT_REJECTED, Integer.toString(plot.getID()), sb.toString()));
                    }
                    PlotHandler.undoSubmit(plot);
                }
                boolean finalIsRejected = isRejected;
                Bukkit.getScheduler().runTask(PlotSystem.getPlugin(), () -> {
                    for (Player player : plot.getWorld().getBukkitWorld().getPlayers()) {
                        player.teleport(Utils.getSpawnLocation());
                    }
                    // Delete plot world after reviewing
                    if (!finalIsRejected)
                        plot.getWorld().deleteWorld();
                    clickPlayer.sendMessage(reviewerConfirmationMessage);
                    clickPlayer.playSound(clickPlayer.getLocation(), Utils.FinishPlotSound, 1f, 1f);
                });
                for (Builder member : plot.getPlotMembers()) {
                    if (member.isOnline()) {
                        PlotHandler.sendFeedbackMessage(Collections.singletonList(plot), member.getPlayer());
                    }
                }
                if (plot.getPlotOwner().isOnline()) {
                    PlotHandler.sendFeedbackMessage(Collections.singletonList(plot), plot.getPlotOwner().getPlayer());
                    plot.getReview().setFeedbackSent(true);
                }
            } catch (SQLException ex) {
                Bukkit.getLogger().log(Level.SEVERE, "A SQL error occurred!", ex);
            }
        });
    });
    // Set click event for point selection items
    for (int i = 0; i < 54; i++) {
        int slot = i;
        int column = (slot % 9) + 1;
        int row = (slot - (slot % 9)) / 9 + 1;
        ItemMeta meta = getMenu().getSlot(slot).getItem(getMenuPlayer()).getItemMeta();
        if (column > 2 && column < 9 && row > 1 && row < 6) {
            // Go through the whole points row
            getMenu().getSlot(i).setClickHandler((clickPlayer, clickInformation) -> {
                for (int j = 0; j < 6; j++) {
                    if (getMenu().getSlot(slot - (column - 1) + j + 2).getItem(clickPlayer).getItemMeta().hasEnchant(Enchantment.ARROW_DAMAGE)) {
                        ItemStack itemPrevious = getMenu().getSlot(slot - (column - 1) + j + 2).getItem(clickPlayer);
                        ItemMeta metaPrevious = itemPrevious.getItemMeta();
                        metaPrevious.removeEnchant(Enchantment.ARROW_DAMAGE);
                        itemPrevious.setItemMeta(metaPrevious);
                        getMenu().getSlot(slot - (column - 1) + j + 2).setItem(itemPrevious);
                    }
                }
                meta.addEnchant(Enchantment.ARROW_DAMAGE, 1, true);
                ItemStack newItem = getMenu().getSlot(slot).getItem(clickPlayer);
                newItem.setItemMeta(meta);
                getMenu().getSlot(slot).setItem(newItem);
                sentWarning = false;
            });
        }
    }
}
Also used : Player(org.bukkit.entity.Player) SQLException(java.sql.SQLException) LoreBuilder(com.alpsbte.plotsystem.utils.items.builder.LoreBuilder) Builder(com.alpsbte.plotsystem.core.system.Builder) ItemBuilder(com.alpsbte.plotsystem.utils.items.builder.ItemBuilder) Review(com.alpsbte.plotsystem.core.system.Review) IOException(java.io.IOException) WorldEditException(com.sk89q.worldedit.WorldEditException) ItemStack(org.bukkit.inventory.ItemStack) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Example 2 with WorldEditException

use of com.sk89q.worldedit.WorldEditException in project DragonsOnline by UniverseCraft.

the class DragonsResidences method pasteSchematic.

public static void pasteSchematic(Clipboard clipboard, EditSession session, Location loc) {
    Operation operation = new ClipboardHolder(clipboard).createPaste(session).to(BlockVector3.at(loc.getBlockX(), loc.getBlockY() + clipboard.getHeight(), loc.getBlockZ())).ignoreAirBlocks(true).build();
    try {
        Operations.complete(operation);
    } catch (WorldEditException e) {
        e.printStackTrace();
    }
    session.close();
}
Also used : ClipboardHolder(com.sk89q.worldedit.session.ClipboardHolder) Operation(com.sk89q.worldedit.function.operation.Operation) WorldEditException(com.sk89q.worldedit.WorldEditException)

Example 3 with WorldEditException

use of com.sk89q.worldedit.WorldEditException in project PrivateMinesOOP by UntouchedOdin0.

the class WE7Adapter method pasteSchematic.

@Override
public CuboidRegion pasteSchematic(Location location, Path file) {
    ClipboardFormat clipboardFormat = ClipboardFormats.findByFile(file.toFile());
    World world = BukkitAdapter.adapt(Objects.requireNonNull(location.getWorld()));
    if (clipboardFormat == null) {
        throw new IllegalArgumentException("File is not a valid schematic");
    }
    try (InputStream fix = Files.newInputStream(file);
        ClipboardReader clipboardReader = clipboardFormat.getReader(fix)) {
        Clipboard clipboard = clipboardReader.read();
        if (clipboard == null) {
            throw new IllegalArgumentException("Clipboard is null");
        }
        try (EditSession editSession = WorldEdit.getInstance().newEditSessionBuilder().world(world).build()) {
            BlockVector3 centerVector = BlockVector3.at(location.getX(), location.getY(), location.getZ());
            // If the clipboard isn't null prepare to create a paste operation, complete it and set the region stuff.
            Operation operation = new ClipboardHolder(clipboard).createPaste(editSession).to(centerVector).ignoreAirBlocks(true).build();
            Operations.complete(operation);
            Region region = clipboard.getRegion();
            region.shift(centerVector.subtract(clipboard.getOrigin()));
            return new CuboidRegion(BukkitAdapter.adapt(location.getWorld(), region.getMinimumPoint()), BukkitAdapter.adapt(location.getWorld(), region.getMaximumPoint()));
        }
    } catch (IOException | WorldEditException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : ClipboardHolder(com.sk89q.worldedit.session.ClipboardHolder) Operation(com.sk89q.worldedit.function.operation.Operation) CuboidRegion(redempt.redlib.region.CuboidRegion) World(com.sk89q.worldedit.world.World) BukkitWorld(com.sk89q.worldedit.bukkit.BukkitWorld) BlockVector3(com.sk89q.worldedit.math.BlockVector3) ClipboardFormat(com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat) CuboidRegion(redempt.redlib.region.CuboidRegion) Region(com.sk89q.worldedit.regions.Region) Clipboard(com.sk89q.worldedit.extent.clipboard.Clipboard) EditSession(com.sk89q.worldedit.EditSession) ClipboardReader(com.sk89q.worldedit.extent.clipboard.io.ClipboardReader) WorldEditException(com.sk89q.worldedit.WorldEditException)

Example 4 with WorldEditException

use of com.sk89q.worldedit.WorldEditException in project Histeria by zelytra.

the class LuckyBlock method WEgenerate.

private void WEgenerate(World world, String fileName, double locX, double locY, double locZ) {
    // Load the file selected
    InputStream is = Histeria.getInstance().getResource("luckyBlock/" + fileName + ".schem");
    BuiltInClipboardFormat format = BuiltInClipboardFormat.SPONGE_SCHEMATIC;
    Clipboard clipboard = null;
    try (ClipboardReader reader = format.getReader(is)) {
        clipboard = reader.read();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try (EditSession editSession = WorldEdit.getInstance().getEditSessionFactory().getEditSession(world, -1)) {
        Operation operation = new ClipboardHolder(clipboard).createPaste(editSession).to(BlockVector3.at(locX, locY, locZ)).ignoreAirBlocks(true).build();
        Operations.complete(operation);
    } catch (WorldEditException e) {
        e.printStackTrace();
    }
}
Also used : ClipboardHolder(com.sk89q.worldedit.session.ClipboardHolder) InputStream(java.io.InputStream) BuiltInClipboardFormat(com.sk89q.worldedit.extent.clipboard.io.BuiltInClipboardFormat) Clipboard(com.sk89q.worldedit.extent.clipboard.Clipboard) IOException(java.io.IOException) EditSession(com.sk89q.worldedit.EditSession) Operation(com.sk89q.worldedit.function.operation.Operation) ClipboardReader(com.sk89q.worldedit.extent.clipboard.io.ClipboardReader) WorldEditException(com.sk89q.worldedit.WorldEditException)

Example 5 with WorldEditException

use of com.sk89q.worldedit.WorldEditException in project FastAsyncWorldEdit by IntellectualSites.

the class RhinoCraftScriptEngine method evaluate.

@Override
public Object evaluate(String script, String filename, Map<String, Object> args) throws Throwable {
    RhinoContextFactory factory = new RhinoContextFactory(timeLimit);
    Context cx = factory.enterContext();
    cx.setClassShutter(new MinecraftHidingClassShutter());
    ScriptableObject scriptable = new ImporterTopLevel(cx);
    Scriptable scope = cx.initStandardObjects(scriptable);
    for (Map.Entry<String, Object> entry : args.entrySet()) {
        ScriptableObject.putProperty(scope, entry.getKey(), Context.javaToJS(entry.getValue(), scope));
    }
    try {
        return cx.evaluateString(scope, script, filename, 1, null);
    } catch (Error e) {
        throw new ScriptException(e.getMessage());
    } catch (RhinoException e) {
        if (e instanceof WrappedException) {
            Throwable cause = e.getCause();
            if (cause instanceof WorldEditException) {
                throw cause;
            }
        }
        String msg;
        int line = (line = e.lineNumber()) == 0 ? -1 : line;
        if (e instanceof JavaScriptException) {
            msg = String.valueOf(((JavaScriptException) e).getValue());
        } else {
            msg = e.getMessage();
        }
        ScriptException scriptException = new ScriptException(msg, e.sourceName(), line);
        scriptException.initCause(e);
        throw scriptException;
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) ImporterTopLevel(org.mozilla.javascript.ImporterTopLevel) WrappedException(org.mozilla.javascript.WrappedException) ScriptableObject(org.mozilla.javascript.ScriptableObject) Scriptable(org.mozilla.javascript.Scriptable) JavaScriptException(org.mozilla.javascript.JavaScriptException) JavaScriptException(org.mozilla.javascript.JavaScriptException) ScriptException(javax.script.ScriptException) ScriptableObject(org.mozilla.javascript.ScriptableObject) RhinoException(org.mozilla.javascript.RhinoException) WorldEditException(com.sk89q.worldedit.WorldEditException) Map(java.util.Map)

Aggregations

WorldEditException (com.sk89q.worldedit.WorldEditException)18 BlockVector3 (com.sk89q.worldedit.math.BlockVector3)10 EditSession (com.sk89q.worldedit.EditSession)8 Operation (com.sk89q.worldedit.function.operation.Operation)8 ClipboardHolder (com.sk89q.worldedit.session.ClipboardHolder)8 IOException (java.io.IOException)8 Clipboard (com.sk89q.worldedit.extent.clipboard.Clipboard)7 Region (com.sk89q.worldedit.regions.Region)5 World (com.sk89q.worldedit.world.World)5 ClipboardReader (com.sk89q.worldedit.extent.clipboard.io.ClipboardReader)4 BukkitWorld (com.sk89q.worldedit.bukkit.BukkitWorld)3 Actor (com.sk89q.worldedit.extension.platform.Actor)3 ClipboardFormat (com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat)3 CuboidRegion (com.sk89q.worldedit.regions.CuboidRegion)3 FileInputStream (java.io.FileInputStream)3 Location (org.bukkit.Location)3 Caption (com.fastasyncworldedit.core.configuration.Caption)2 ByteArrayTag (com.sk89q.jnbt.ByteArrayTag)2 CompoundTag (com.sk89q.jnbt.CompoundTag)2 IntTag (com.sk89q.jnbt.IntTag)2