1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-19 19:25:48 +00:00

[Ci skip] Super secret feature

This commit is contained in:
TheBusyBiscuit 2020-01-10 21:43:49 +01:00
parent 13110a4927
commit 8735a8e1a3
6 changed files with 166 additions and 22 deletions

View File

@ -14,6 +14,7 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import io.github.thebusybiscuit.cscorelib2.chat.ChatColors;
import io.github.thebusybiscuit.slimefun4.core.commands.subcommands.CheatCommand;
import io.github.thebusybiscuit.slimefun4.core.commands.subcommands.GiveCommand;
import io.github.thebusybiscuit.slimefun4.core.commands.subcommands.GuideCommand;
@ -101,11 +102,11 @@ public class SlimefunCommand implements CommandExecutor, Listener {
public void sendHelp(CommandSender sender) {
sender.sendMessage("");
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&aSlimefun &2v" + Slimefun.getVersion()));
sender.sendMessage(ChatColors.color("&aSlimefun &2v" + Slimefun.getVersion()));
sender.sendMessage("");
for (SubCommand cmd : commands) {
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&3/sf " + cmd.getName() + " &b") + cmd.getDescription());
sender.sendMessage(ChatColors.color("&3/sf " + cmd.getName() + " &b") + cmd.getDescription(sender));
}
}

View File

@ -1,6 +1,7 @@
package io.github.thebusybiscuit.slimefun4.core.commands;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import me.mrCookieSlime.Slimefun.SlimefunPlugin;
@ -8,13 +9,10 @@ public abstract class SubCommand {
protected final SlimefunPlugin plugin;
protected final SlimefunCommand cmd;
private final String description;
protected SubCommand(SlimefunPlugin plugin, SlimefunCommand cmd) {
this.plugin = plugin;
this.cmd = cmd;
this.description = SlimefunPlugin.getLocal().getMessage(getDescriptionPath());
}
public abstract String getName();
@ -25,8 +23,13 @@ public abstract class SubCommand {
return "commands." + getName();
}
public String getDescription() {
return description;
public String getDescription(CommandSender sender) {
if (sender instanceof Player) {
return SlimefunPlugin.getLocal().getMessage((Player) sender, getDescriptionPath());
}
else {
return SlimefunPlugin.getLocal().getMessage(getDescriptionPath());
}
}
}

View File

@ -74,7 +74,7 @@ public class ChestSlimefunGuide implements ISlimefunGuide {
int index = 9;
int pages = (categories.size() + handlers.size() - 1) / CATEGORY_SIZE + 1;
fillInv(profile, menu, survival);
fillInv(p, profile, menu, survival);
int target = (CATEGORY_SIZE * (page - 1)) - 1;
@ -176,7 +176,7 @@ public class ChestSlimefunGuide implements ISlimefunGuide {
}
ChestMenu menu = create();
fillInv(profile, menu, survival);
fillInv(p, profile, menu, survival);
menu.addItem(1, new CustomItem(ChestMenuUtils.getBackButton(), meta -> meta.setLore(Arrays.asList("", ChatColors.color("&rLeft Click: &7Go back to Main Menu")))));
menu.addMenuClickHandler(1, (pl, s, is, action) -> {
@ -289,7 +289,7 @@ public class ChestSlimefunGuide implements ISlimefunGuide {
}
menu.setEmptySlotsClickable(false);
fillInv(profile, menu, survival);
fillInv(p, profile, menu, survival);
addBackButton(menu, 1, profile, survival);
int index = 9;
@ -501,15 +501,15 @@ public class ChestSlimefunGuide implements ISlimefunGuide {
menu.addItem(16, output, ChestMenuUtils.getEmptyClickHandler());
}
private void fillInv(PlayerProfile profile, ChestMenu menu, boolean survival) {
private void fillInv(Player p, PlayerProfile profile, ChestMenu menu, boolean survival) {
for (int i = 0; i < 9; i++) {
menu.addItem(i, ChestMenuUtils.getBackground(), ChestMenuUtils.getEmptyClickHandler());
}
// Settings Panel
menu.addItem(1, ChestMenuUtils.getMenuButton());
menu.addMenuClickHandler(1, (p, slot, item, action) -> {
GuideSettings.openSettings(p, p.getInventory().getItemInMainHand());
menu.addMenuClickHandler(1, (pl, slot, item, action) -> {
GuideSettings.openSettings(pl, pl.getInventory().getItemInMainHand());
return false;
});
@ -524,12 +524,12 @@ public class ChestSlimefunGuide implements ISlimefunGuide {
*/
// Search feature!
menu.addItem(7, new CustomItem(ChestMenuUtils.getSearchButton(), SlimefunPlugin.getLocal().getMessage("guide.search.name"), SlimefunPlugin.getLocal().getMessagesArray("guide.search.lore")));
menu.addMenuClickHandler(7, (p, slot, item, action) -> {
p.closeInventory();
SlimefunPlugin.getLocal().sendMessage(p, "search.message");
menu.addItem(7, new CustomItem(ChestMenuUtils.getSearchButton(), SlimefunPlugin.getLocal().getMessage(p, "guide.search.name"), SlimefunPlugin.getLocal().getMessages(p, "guide.search.lore").toArray(new String[0])));
menu.addMenuClickHandler(7, (pl, slot, item, action) -> {
pl.closeInventory();
SlimefunPlugin.getLocal().sendMessage(pl, "search.message");
ChatInput.waitForPlayer(SlimefunPlugin.instance, p, msg ->
ChatInput.waitForPlayer(SlimefunPlugin.instance, pl, msg ->
SlimefunGuide.openSearch(profile, msg, survival, true)
);

View File

@ -3,24 +3,40 @@ package io.github.thebusybiscuit.slimefun4.core.services;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.UnaryOperator;
import java.util.logging.Level;
import org.bukkit.ChatColor;
import org.bukkit.Keyed;
import org.bukkit.NamespacedKey;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import io.github.thebusybiscuit.cscorelib2.chat.ChatColors;
import io.github.thebusybiscuit.cscorelib2.config.Localization;
import io.github.thebusybiscuit.cscorelib2.data.PersistentDataAPI;
import me.mrCookieSlime.Slimefun.SlimefunPlugin;
import me.mrCookieSlime.Slimefun.api.Slimefun;
public class LocalizationService extends Localization {
public class LocalizationService extends Localization implements Keyed {
private static final String LANGUAGE_PATH = "language";
private final Map<String, FileConfiguration> languages = new HashMap<>();
private final SlimefunPlugin plugin;
private final NamespacedKey languageKey;
public LocalizationService(SlimefunPlugin plugin) {
super(plugin);
this.plugin = plugin;
languageKey = new NamespacedKey(plugin, LANGUAGE_PATH);
String selectedLanguage = SlimefunPlugin.getSelectedLanguage();
String language = getLanguage();
@ -36,6 +52,107 @@ public class LocalizationService extends Localization {
save();
}
public String getPrefix() {
return getMessage("prefix");
}
public String getMessage(Player p, String key) {
Optional<String> language = PersistentDataAPI.getOptionalString(p, languageKey);
if (language.isPresent()) {
FileConfiguration cfg = languages.computeIfAbsent(language.get(), this::loadLanguage);
return cfg.getString(key);
}
else {
return getMessage(key);
}
}
public List<String> getMessages(Player p, String key) {
Optional<String> language = PersistentDataAPI.getOptionalString(p, languageKey);
if (language.isPresent()) {
FileConfiguration cfg = languages.computeIfAbsent(language.get(), this::loadLanguage);
return cfg.getStringList(key);
}
else {
return getMessages(key);
}
}
@Override
public void sendMessage(CommandSender sender, String key) {
String prefix = getPrefix();
if (sender instanceof Player) {
sender.sendMessage(ChatColors.color(prefix + getMessage((Player) sender, key)));
}
else {
sender.sendMessage(ChatColor.stripColor(ChatColors.color(prefix + getMessage(key))));
}
}
@Override
public void sendMessage(CommandSender sender, String key, boolean addPrefix) {
sendMessage(sender, key);
}
public void sendMessage(CommandSender sender, String key, UnaryOperator<String> function) {
String prefix = getPrefix();
if (sender instanceof Player) {
sender.sendMessage(ChatColors.color(prefix + function.apply(getMessage((Player) sender, key))));
}
else {
sender.sendMessage(ChatColor.stripColor(ChatColors.color(prefix + function.apply(getMessage(key)))));
}
}
@Override
public void sendMessage(CommandSender sender, String key, boolean addPrefix, UnaryOperator<String> function) {
sendMessage(sender, key, function);
}
@Override
public void sendMessages(CommandSender sender, String key) {
String prefix = getPrefix();
if (sender instanceof Player) {
for (String translation : getMessages((Player) sender, key)) {
String message = ChatColors.color(prefix + translation);
sender.sendMessage(message);
}
}
else {
for (String translation : getMessages(key)) {
String message = ChatColors.color(prefix + translation);
sender.sendMessage(ChatColor.stripColor(message));
}
}
}
@Override
public void sendMessages(CommandSender sender, String key, boolean addPrefix, UnaryOperator<String> function) {
sendMessages(sender, key, function);
}
public void sendMessages(CommandSender sender, String key, UnaryOperator<String> function) {
String prefix = getPrefix();
if (sender instanceof Player) {
for (String translation : getMessages((Player) sender, key)) {
String message = ChatColors.color(prefix + function.apply(translation));
sender.sendMessage(message);
}
}
else {
for (String translation : getMessages(key)) {
String message = ChatColors.color(prefix + function.apply(translation));
sender.sendMessage(ChatColor.stripColor(message));
}
}
}
private String getLanguage() {
String language = getConfig().getString(LANGUAGE_PATH);
return language == null ? "en": language;
@ -62,7 +179,25 @@ public class LocalizationService extends Localization {
save();
}
private FileConfiguration loadLanguage(String id) {
if (!hasLanguage(id)) {
return getConfig().getConfiguration();
}
try (BufferedReader reader = new BufferedReader(new InputStreamReader(plugin.getClass().getResourceAsStream("/languages/messages_" + id + ".yml")))) {
return YamlConfiguration.loadConfiguration(reader);
} catch (IOException e) {
Slimefun.getLogger().log(Level.SEVERE, "Failed to load language file into memory: \"messages_" + id + ".yml\"", e);
return getConfig().getConfiguration();
}
}
public boolean hasLanguage(String language) {
return plugin.getClass().getResource("/languages/messages_" + language + ".yml") != null;
}
@Override
public NamespacedKey getKey() {
return languageKey;
}
}

View File

@ -23,7 +23,7 @@ public class FortuneCookie extends SimpleSlimefunItem<ItemConsumptionHandler> {
public ItemConsumptionHandler getItemHandler() {
return (e, p, item) -> {
if (isItem(item)) {
List<String> messages = SlimefunPlugin.getLocal().getMessages("messages.fortune-cookie");
List<String> messages = SlimefunPlugin.getLocal().getMessages(p, "messages.fortune-cookie");
String message = messages.get(ThreadLocalRandom.current().nextInt(messages.size()));
p.sendMessage(ChatColor.translateAlternateColorCodes('&', message));

View File

@ -162,4 +162,9 @@ android:
rating:
own: '&4You cannot rate your own Script!'
already: '&4You have already left a Rating for this Script!'
already: '&4You have already left a Rating for this Script!'
languages:
en: 'English'
de: 'German'
fr: 'French'