Search in sources :

Example 1 with DoubleFlag

use of com.plotsquared.core.plot.flag.types.DoubleFlag in project PlotSquared by IntellectualSites.

the class Plot method format.

public CompletableFuture<Caption> format(final Caption iInfo, PlotPlayer<?> player, final boolean full) {
    final CompletableFuture<Caption> future = new CompletableFuture<>();
    int num = this.getConnectedPlots().size();
    String alias = !this.getAlias().isEmpty() ? this.getAlias() : TranslatableCaption.of("info.none").getComponent(player);
    Location bot = this.getCorners()[0];
    PlotSquared.platform().worldUtil().getBiome(Objects.requireNonNull(this.getWorldName()), bot.getX(), bot.getZ(), biome -> {
        Component trusted = PlayerManager.getPlayerList(this.getTrusted(), player);
        Component members = PlayerManager.getPlayerList(this.getMembers(), player);
        Component denied = PlayerManager.getPlayerList(this.getDenied(), player);
        String seen;
        if (Settings.Enabled_Components.PLOT_EXPIRY && ExpireManager.IMP != null) {
            if (this.isOnline()) {
                seen = TranslatableCaption.of("info.now").getComponent(player);
            } else {
                int time = (int) (ExpireManager.IMP.getAge(this, false) / 1000);
                if (time != 0) {
                    seen = TimeUtil.secToTime(time);
                } else {
                    seen = TranslatableCaption.of("info.unknown").getComponent(player);
                }
            }
        } else {
            seen = TranslatableCaption.of("info.never").getComponent(player);
        }
        String description = this.getFlag(DescriptionFlag.class);
        if (description.isEmpty()) {
            description = TranslatableCaption.of("info.plot_no_description").getComponent(player);
        }
        Component flags;
        Collection<PlotFlag<?, ?>> flagCollection = this.getApplicableFlags(true);
        if (flagCollection.isEmpty()) {
            flags = MINI_MESSAGE.parse(TranslatableCaption.of("info.none").getComponent(player));
        } else {
            TextComponent.Builder flagBuilder = Component.text();
            String prefix = "";
            for (final PlotFlag<?, ?> flag : flagCollection) {
                Object value;
                if (flag instanceof DoubleFlag && !Settings.General.SCIENTIFIC) {
                    value = FLAG_DECIMAL_FORMAT.format(flag.getValue());
                } else {
                    value = flag.toString();
                }
                Component snip = MINI_MESSAGE.parse(prefix + CaptionUtility.format(player, TranslatableCaption.of("info.plot_flag_list").getComponent(player)), Template.of("flag", flag.getName()), Template.of("value", CaptionUtility.formatRaw(player, value.toString())));
                flagBuilder.append(snip);
                prefix = ", ";
            }
            flags = flagBuilder.build();
        }
        boolean build = this.isAdded(player.getUUID());
        Component owner;
        if (this.getOwner() == null) {
            owner = Component.text("unowned");
        } else if (this.getOwner().equals(DBFunc.SERVER)) {
            owner = Component.text(MINI_MESSAGE.stripTokens(TranslatableCaption.of("info.server").getComponent(player)));
        } else {
            owner = PlayerManager.getPlayerList(this.getOwners(), player);
        }
        Template headerTemplate = Template.of("header", TranslatableCaption.of("info.plot_info_header").getComponent(player));
        Template footerTemplate = Template.of("footer", TranslatableCaption.of("info.plot_info_footer").getComponent(player));
        Template areaTemplate;
        if (this.getArea() != null) {
            areaTemplate = Template.of("area", this.getArea().getWorldName() + (this.getArea().getId() == null ? "" : "(" + this.getArea().getId() + ")"));
        } else {
            areaTemplate = Template.of("area", TranslatableCaption.of("info.none").getComponent(player));
        }
        long creationDate = Long.parseLong(String.valueOf(timestamp));
        SimpleDateFormat sdf = new SimpleDateFormat(Settings.Timeformat.DATE_FORMAT);
        sdf.setTimeZone(TimeZone.getTimeZone(Settings.Timeformat.TIME_ZONE));
        String newDate = sdf.format(creationDate);
        Template idTemplate = Template.of("id", this.getId().toString());
        Template aliasTemplate = Template.of("alias", alias);
        Template numTemplate = Template.of("num", String.valueOf(num));
        Template descTemplate = Template.of("desc", description);
        Template biomeTemplate = Template.of("biome", biome.toString().toLowerCase());
        Template ownerTemplate = Template.of("owner", owner);
        Template membersTemplate = Template.of("members", members);
        Template playerTemplate = Template.of("player", player.getName());
        Template trustedTemplate = Template.of("trusted", trusted);
        Template helpersTemplate = Template.of("helpers", members);
        Template deniedTemplate = Template.of("denied", denied);
        Template seenTemplate = Template.of("seen", seen);
        Template flagsTemplate = Template.of("flags", flags);
        Template creationTemplate = Template.of("creationdate", newDate);
        Template buildTemplate = Template.of("build", String.valueOf(build));
        Template sizeTemplate = Template.of("size", String.valueOf(getConnectedPlots().size()));
        String component = iInfo.getComponent(player);
        if (component.contains("<rating>") || component.contains("<likes>")) {
            TaskManager.runTaskAsync(() -> {
                Template ratingTemplate;
                Template likesTemplate;
                if (Settings.Ratings.USE_LIKES) {
                    ratingTemplate = Template.of("rating", String.format("%.0f%%", Like.getLikesPercentage(this) * 100D));
                    likesTemplate = Template.of("likes", String.format("%.0f%%", Like.getLikesPercentage(this) * 100D));
                } else {
                    int max = 10;
                    if (Settings.Ratings.CATEGORIES != null && !Settings.Ratings.CATEGORIES.isEmpty()) {
                        max = 8;
                    }
                    if (full && Settings.Ratings.CATEGORIES != null && Settings.Ratings.CATEGORIES.size() > 1) {
                        double[] ratings = this.getAverageRatings();
                        StringBuilder rating = new StringBuilder();
                        String prefix = "";
                        for (int i = 0; i < ratings.length; i++) {
                            rating.append(prefix).append(Settings.Ratings.CATEGORIES.get(i)).append('=').append(String.format("%.1f", ratings[i]));
                            prefix = ",";
                        }
                        ratingTemplate = Template.of("rating", rating.toString());
                    } else {
                        double rating = this.getAverageRating();
                        if (Double.isFinite(rating)) {
                            ratingTemplate = Template.of("rating", String.format("%.1f", rating) + '/' + max);
                        } else {
                            ratingTemplate = Template.of("rating", TranslatableCaption.of("info.none").getComponent(player));
                        }
                    }
                    likesTemplate = Template.of("likes", "N/A");
                }
                future.complete(StaticCaption.of(MINI_MESSAGE.serialize(MINI_MESSAGE.parse(iInfo.getComponent(player), headerTemplate, areaTemplate, idTemplate, aliasTemplate, numTemplate, descTemplate, biomeTemplate, ownerTemplate, membersTemplate, playerTemplate, trustedTemplate, helpersTemplate, deniedTemplate, seenTemplate, flagsTemplate, buildTemplate, ratingTemplate, creationTemplate, sizeTemplate, likesTemplate, footerTemplate))));
            });
            return;
        }
        future.complete(StaticCaption.of(MINI_MESSAGE.serialize(MINI_MESSAGE.parse(iInfo.getComponent(player), headerTemplate, areaTemplate, idTemplate, aliasTemplate, numTemplate, descTemplate, biomeTemplate, ownerTemplate, membersTemplate, playerTemplate, trustedTemplate, helpersTemplate, deniedTemplate, seenTemplate, flagsTemplate, buildTemplate, creationTemplate, sizeTemplate, footerTemplate))));
    });
    return future;
}
Also used : ServerPlotFlag(com.plotsquared.core.plot.flag.implementations.ServerPlotFlag) PlotFlag(com.plotsquared.core.plot.flag.PlotFlag) TextComponent(net.kyori.adventure.text.TextComponent) DoubleFlag(com.plotsquared.core.plot.flag.types.DoubleFlag) StaticCaption(com.plotsquared.core.configuration.caption.StaticCaption) Caption(com.plotsquared.core.configuration.caption.Caption) TranslatableCaption(com.plotsquared.core.configuration.caption.TranslatableCaption) Template(net.kyori.adventure.text.minimessage.Template) CompletableFuture(java.util.concurrent.CompletableFuture) TextComponent(net.kyori.adventure.text.TextComponent) Component(net.kyori.adventure.text.Component) SimpleDateFormat(java.text.SimpleDateFormat) Location(com.plotsquared.core.location.Location)

Aggregations

Caption (com.plotsquared.core.configuration.caption.Caption)1 StaticCaption (com.plotsquared.core.configuration.caption.StaticCaption)1 TranslatableCaption (com.plotsquared.core.configuration.caption.TranslatableCaption)1 Location (com.plotsquared.core.location.Location)1 PlotFlag (com.plotsquared.core.plot.flag.PlotFlag)1 ServerPlotFlag (com.plotsquared.core.plot.flag.implementations.ServerPlotFlag)1 DoubleFlag (com.plotsquared.core.plot.flag.types.DoubleFlag)1 SimpleDateFormat (java.text.SimpleDateFormat)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Component (net.kyori.adventure.text.Component)1 TextComponent (net.kyori.adventure.text.TextComponent)1 Template (net.kyori.adventure.text.minimessage.Template)1