Add back title method that somehow went missing and add a template utility class to make templates less annoying to work with

This commit is contained in:
Alexander Söderberg 2020-07-14 15:04:31 +02:00
parent f93714a44e
commit fbf6a3517d
No known key found for this signature in database
GPG Key ID: C0207FF7EA146678
4 changed files with 176 additions and 17 deletions

View File

@ -1,3 +1,28 @@
/*
* _____ _ _ _____ _
* | __ \| | | | / ____| | |
* | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
* | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
* | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
* |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
* | |
* |_|
* PlotSquared plot management system for Minecraft
* Copyright (C) 2020 IntellectualSites
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.plotsquared.core.configuration.caption; package com.plotsquared.core.configuration.caption;
import com.plotsquared.core.configuration.Caption; import com.plotsquared.core.configuration.Caption;

View File

@ -0,0 +1,111 @@
/*
* _____ _ _ _____ _
* | __ \| | | | / ____| | |
* | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
* | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
* | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
* |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
* | |
* |_|
* PlotSquared plot management system for Minecraft
* Copyright (C) 2020 IntellectualSites
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.plotsquared.core.configuration.caption;
import com.plotsquared.core.configuration.Caption;
import com.plotsquared.core.plot.PlotArea;
import com.plotsquared.core.util.MainUtil;
import lombok.experimental.UtilityClass;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.Template;
import org.jetbrains.annotations.NotNull;
import java.util.UUID;
/**
* Utility class that generates {@link net.kyori.adventure.text.minimessage.Template templates}
*/
@UtilityClass
public class Templates {
private final MiniMessage MINI_MESSAGE = MiniMessage.builder().build();
/**
* Create a {@link net.kyori.adventure.text.minimessage.Template} from a PlotSquared {@link Caption}
*
* @param localeHolder Locale holder
* @param key Template key
* @param caption Caption object
* @param replacements Replacements
* @return Generated template
*/
@NotNull public Template of(@NotNull final LocaleHolder localeHolder,
@NotNull final String key,
@NotNull final Caption caption,
@NotNull final Template... replacements) {
return Template.of(key, MINI_MESSAGE.parse(caption.getComponent(localeHolder), replacements));
}
/**
* Create a {@link Template} from a username (using UUID mappings)
*
* @param key Template key
* @param uuid Player UUID
* @return Generated template
*/
@NotNull public Template of(@NotNull final String key,
@NotNull final UUID uuid) {
final String username = MainUtil.getName(uuid);
return Template.of(key, username);
}
/**
* Create a {@link Template} from a string
*
* @param key Template key
* @param value Template value
* @return Generated template
*/
@NotNull public Template of(@NotNull final String key,
@NotNull final String value) {
return Template.of(key, value);
}
/**
* Create a {@link Template} from a plot area
*
* @param key Template Key
* @param area Plot area
* @return Generated template
*/
@NotNull public Template of(@NotNull final String key,
@NotNull final PlotArea area) {
return Template.of(key, area.toString());
}
/**
* Create a {@link Template} from a number
*
* @param key Template key
* @param number Number
* @return Generated template
*/
@NotNull public Template of(@NotNull final String key,
@NotNull final Number number) {
return Template.of(key, number.toString());
}
}

View File

@ -29,6 +29,7 @@ import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.collection.ByteArrayUtilities; import com.plotsquared.core.collection.ByteArrayUtilities;
import com.plotsquared.core.configuration.Captions; import com.plotsquared.core.configuration.Captions;
import com.plotsquared.core.configuration.Settings; import com.plotsquared.core.configuration.Settings;
import com.plotsquared.core.configuration.caption.Templates;
import com.plotsquared.core.configuration.caption.TranslatableCaption; import com.plotsquared.core.configuration.caption.TranslatableCaption;
import com.plotsquared.core.events.PlotFlagRemoveEvent; import com.plotsquared.core.events.PlotFlagRemoveEvent;
import com.plotsquared.core.events.Result; import com.plotsquared.core.events.Result;
@ -264,12 +265,12 @@ public class PlotListener {
player.sendTitle( player.sendTitle(
TranslatableCaption.of("titles.title_entered_plot"), TranslatableCaption.of("titles.title_entered_plot"),
TranslatableCaption.of("titles.title_entered_plot_sub"), TranslatableCaption.of("titles.title_entered_plot_sub"),
Template.of("x", Integer.toString(lastPlot.getId().getX())), Templates.of("x", lastPlot.getId().getX()),
Template.of("z", Integer.toString(lastPlot.getId().getY())), Templates.of("z", lastPlot.getId().getY()),
Template.of("world", plot.getArea().toString()), Templates.of("world", plot.getArea()),
Template.of("greeting", greeting), Templates.of("greeting", greeting),
Template.of("alias", plot.toString()), Templates.of("alias", plot.getAlias()),
Template.of("owner", MainUtil.getName(plot.getOwner())) Templates.of("owner", plot.getOwner())
); );
} }
}, 20); }, 20);

View File

@ -34,6 +34,7 @@ import com.plotsquared.core.configuration.Caption;
import com.plotsquared.core.configuration.Captions; import com.plotsquared.core.configuration.Captions;
import com.plotsquared.core.configuration.Settings; import com.plotsquared.core.configuration.Settings;
import com.plotsquared.core.configuration.caption.LocaleHolder; import com.plotsquared.core.configuration.caption.LocaleHolder;
import com.plotsquared.core.configuration.caption.Templates;
import com.plotsquared.core.configuration.caption.TranslatableCaption; import com.plotsquared.core.configuration.caption.TranslatableCaption;
import com.plotsquared.core.database.DBFunc; import com.plotsquared.core.database.DBFunc;
import com.plotsquared.core.events.TeleportCause; import com.plotsquared.core.events.TeleportCause;
@ -688,7 +689,8 @@ public abstract class PlotPlayer<P> implements CommandCaller, OfflinePlotPlayer,
TaskManager.runTask(() -> { TaskManager.runTask(() -> {
if (getMeta("teleportOnLogin", true)) { if (getMeta("teleportOnLogin", true)) {
teleport(location); teleport(location);
sendMessage(TranslatableCaption.of("teleport.teleported_to_plot")); sendMessage(
TranslatableCaption.of("teleport.teleported_to_plot"));
} }
}); });
} else if (!PlotSquared.get().isMainThread(Thread.currentThread())) { } else if (!PlotSquared.get().isMainThread(Thread.currentThread())) {
@ -698,7 +700,8 @@ public abstract class PlotPlayer<P> implements CommandCaller, OfflinePlotPlayer,
if (getMeta("teleportOnLogin", true)) { if (getMeta("teleportOnLogin", true)) {
if (plot.isLoaded()) { if (plot.isLoaded()) {
teleport(location); teleport(location);
sendMessage(TranslatableCaption.of("teleport.teleported_to_plot")); sendMessage(TranslatableCaption
.of("teleport.teleported_to_plot"));
} }
} }
})); }));
@ -740,11 +743,30 @@ public abstract class PlotPlayer<P> implements CommandCaller, OfflinePlotPlayer,
* @param replacements Variable replacements * @param replacements Variable replacements
*/ */
public void sendTitle(@NotNull final Caption title, @NotNull final Caption subtitle, public void sendTitle(@NotNull final Caption title, @NotNull final Caption subtitle,
final int fadeIn, final int stay, final int fadeOut, @NotNull final Template ... replacements) { @NotNull final Template... replacements) {
sendTitle(title, subtitle, 10, 50, 20, replacements);
}
/**
* Send a title to to the player
*
* @param title Title
* @param subtitle Subtitle
* @param fadeIn Fade in time (in ticks)
* @param stay The the title stays for (in ticks)
* @param fadeOut Fade out time (in ticks)
* @param replacements Variable replacements
*/
public void sendTitle(@NotNull final Caption title, @NotNull final Caption subtitle,
final int fadeIn, final int stay, final int fadeOut,
@NotNull final Template... replacements) {
final Component titleComponent = MINI_MESSAGE.parse(title.getComponent(this), replacements); final Component titleComponent = MINI_MESSAGE.parse(title.getComponent(this), replacements);
final Component subtitleComponent = MINI_MESSAGE.parse(subtitle.getComponent(this), replacements); final Component subtitleComponent =
getAudience().showTitle(Title.of(titleComponent, subtitleComponent, Duration.of(fadeIn * 50, MINI_MESSAGE.parse(subtitle.getComponent(this), replacements);
ChronoUnit.MILLIS), Duration.of(stay * 50, ChronoUnit.MILLIS), Duration.of(fadeOut * 50, ChronoUnit.MILLIS))); getAudience().showTitle(Title
.of(titleComponent, subtitleComponent, Duration.of(fadeIn * 50, ChronoUnit.MILLIS),
Duration.of(stay * 50, ChronoUnit.MILLIS),
Duration.of(fadeOut * 50, ChronoUnit.MILLIS)));
} }
@Override public void sendMessage(@NotNull final Caption caption, @Override public void sendMessage(@NotNull final Caption caption,
@ -755,11 +777,11 @@ public abstract class PlotPlayer<P> implements CommandCaller, OfflinePlotPlayer,
} }
// Create the template list, and add the prefix as a replacement // Create the template list, and add the prefix as a replacement
final List<Template> templates = Arrays.asList(replacements); final List<Template> templates = Arrays.asList(replacements);
templates.add(Template.of("prefix", MINI_MESSAGE.parse( templates.add(Templates.of(this, "prefix", TranslatableCaption.of("core.prefix")));
TranslatableCaption.of("core.prefix").getComponent(this))));
// Parse the message // Parse the message
final Component component = MINI_MESSAGE.parse(message, templates); final Component component = MINI_MESSAGE.parse(message, templates);
if (!Objects.equal(component, this.getMeta("lastMessage")) || System.currentTimeMillis() - this.<Long>getMeta("lastMessageTime") > 5000) { if (!Objects.equal(component, this.getMeta("lastMessage"))
|| System.currentTimeMillis() - this.<Long>getMeta("lastMessageTime") > 5000) {
setMeta("lastMessage", component); setMeta("lastMessage", component);
setMeta("lastMessageTime", System.currentTimeMillis()); setMeta("lastMessageTime", System.currentTimeMillis());
getAudience().sendMessage(component); getAudience().sendMessage(component);