Blueprint manager localization - WIP

This commit is contained in:
tastybento 2019-05-22 12:16:38 -07:00
parent 3a34133d1a
commit 6024423dad
11 changed files with 131 additions and 61 deletions

View File

@ -1,5 +1,11 @@
package world.bentobox.bentobox.api.commands.admin.blueprints;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Color;
import org.bukkit.Particle;
@ -12,12 +18,6 @@ import world.bentobox.bentobox.blueprints.BlueprintClipboard;
import world.bentobox.bentobox.managers.BlueprintsManager;
import world.bentobox.bentobox.panels.BlueprintManagementPanel;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
public class AdminBlueprintCommand extends ConfirmableCommand {
// Clipboards
private Map<UUID, BlueprintClipboard> clipboards;
@ -53,7 +53,7 @@ public class AdminBlueprintCommand extends ConfirmableCommand {
@Override
public boolean execute(User user, String label, List<String> args) {
new BlueprintManagementPanel(getPlugin()).openPanel(user, (GameModeAddon)getAddon());
new BlueprintManagementPanel(getPlugin(), user, (GameModeAddon)getAddon()).openPanel();
return true;
}

View File

@ -7,8 +7,11 @@ import org.bukkit.ChatColor;
import org.bukkit.conversations.ConversationContext;
import org.bukkit.conversations.Prompt;
import org.bukkit.conversations.StringPrompt;
import org.bukkit.entity.Player;
import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.blueprints.dataobjects.BlueprintBundle;
/**
@ -29,23 +32,24 @@ public class DescriptionPrompt extends StringPrompt {
@SuppressWarnings("unchecked")
@Override
public String getPromptText(ConversationContext context) {
User user = User.getInstance((Player)context.getForWhom());
if (context.getSessionData("description") != null) {
StringBuilder sb = new StringBuilder();
for (String line : ((List<String>) context.getSessionData("description"))) {
sb.append(ChatColor.DARK_PURPLE);
sb.append(user.getTranslation("commands.admin.blueprint.management.description.default-color"));
sb.append(line);
sb.append(System.getProperty("line.separator"));
}
return sb.toString();
}
return "Enter a multi line description for " + bb.getDisplayName() + System.getProperty("line.separator")
+ ChatColor.GOLD + " and 'quit' on a line by itself to finish.";
return user.getTranslation("commands.admin.blueprint.management.description.instructions", TextVariables.NAME, bb.getDisplayName());
}
@SuppressWarnings("unchecked")
@Override
public Prompt acceptInput(ConversationContext context, String input) {
if (input.equals("quit")) {
User user = User.getInstance((Player)context.getForWhom());
if (input.equals(user.getTranslation("commands.admin.blueprint.management.description.quit"))) {
return new DescriptionSuccessPrompt(addon, bb);
}
List<String> desc = new ArrayList<>();

View File

@ -29,18 +29,19 @@ public class DescriptionSuccessPrompt extends MessagePrompt {
@Override
public String getPromptText(ConversationContext context) {
User user = User.getInstance((Player)context.getForWhom());
@SuppressWarnings("unchecked")
List<String> description = (List<String>)context.getSessionData("description");
if (description != null) {
bb.setDescription(description);
BentoBox.getInstance().getBlueprintsManager().addBlueprintBundle(addon, bb);
BentoBox.getInstance().getBlueprintsManager().saveBlueprintBundle(addon, bb);
new BlueprintManagementPanel(BentoBox.getInstance()).openBB(User.getInstance((Player)context.getForWhom()), addon, bb);
new BlueprintManagementPanel(BentoBox.getInstance(), user, addon).openBB(bb);
// Set the name
// if successfully
return "Success!";
return user.getTranslation("commands.admin.blueprint.management.description.success");
} else {
return "Cancelling";
return user.getTranslation("commands.admin.blueprint.management.description.cancelling");
}
}

View File

@ -1,13 +1,16 @@
package world.bentobox.bentobox.blueprints.conversation;
import org.bukkit.ChatColor;
import org.bukkit.conversations.ConversationContext;
import org.bukkit.conversations.ConversationPrefix;
import org.bukkit.entity.Player;
import world.bentobox.bentobox.api.user.User;
public class NameConversationPrefix implements ConversationPrefix {
@Override
public String getPrefix(ConversationContext conversationContext) {
return ChatColor.GOLD + "> ";
public String getPrefix(ConversationContext context) {
User user = User.getInstance((Player)context.getForWhom());
return user.getTranslation("commands.admin.blueprint.management.name.conversation-prefix");
}
}

View File

@ -3,9 +3,11 @@ package world.bentobox.bentobox.blueprints.conversation;
import org.bukkit.conversations.ConversationContext;
import org.bukkit.conversations.Prompt;
import org.bukkit.conversations.StringPrompt;
import org.bukkit.entity.Player;
import net.md_5.bungee.api.ChatColor;
import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.user.User;
public class NamePrompt extends StringPrompt {
@ -17,11 +19,13 @@ public class NamePrompt extends StringPrompt {
@Override
public String getPromptText(ConversationContext context) {
return "Enter a name, or 'quit' to quit";
User user = User.getInstance((Player)context.getForWhom());
return user.getTranslation("commands.admin.blueprint.management.name.prompt");
}
@Override
public Prompt acceptInput(ConversationContext context, String input) {
User user = User.getInstance((Player)context.getForWhom());
// Convert color codes
input = ChatColor.translateAlternateColorCodes('&', input);
if (ChatColor.stripColor(input).length() > 32) {
@ -36,7 +40,7 @@ public class NamePrompt extends StringPrompt {
uniqueId.append("x");
}
if (max == 32) {
context.getForWhom().sendRawMessage("Please pick a more unique name");
context.getForWhom().sendRawMessage(user.getTranslation("commands.admin.blueprint.management.name.pick-a-unique-name"));
return this;
}
context.setSessionData("uniqueId", uniqueId.toString());

View File

@ -30,10 +30,11 @@ public class NameSuccessPrompt extends MessagePrompt {
bb.setDisplayName(name);
BentoBox.getInstance().getBlueprintsManager().addBlueprintBundle(addon, bb);
BentoBox.getInstance().getBlueprintsManager().saveBlueprintBundle(addon, bb);
new BlueprintManagementPanel(BentoBox.getInstance()).openPanel(User.getInstance((Player)context.getForWhom()), addon);
User user = User.getInstance((Player)context.getForWhom());
new BlueprintManagementPanel(BentoBox.getInstance(), user, addon).openPanel();
// Set the name
// if successfully
return "Success!";
return user.getTranslation("commands.admin.blueprint.management.description.success");
// Else return failure
}

View File

@ -37,8 +37,6 @@ public abstract class AbstractJSONDatabaseHandler<T> extends AbstractDatabaseHan
GsonBuilder builder = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().enableComplexMapKeySerialization().setPrettyPrinting();
// Register adapter factory
builder.registerTypeAdapterFactory(new BentoboxTypeAdapterFactory(plugin));
// Keep null in the database
builder.serializeNulls();
// Allow characters like < or > without escaping them
builder.disableHtmlEscaping();

View File

@ -133,17 +133,21 @@ public class MySQLDatabaseHandler<T> extends AbstractJSONDatabaseHandler<T> {
String json = resultSet.getString("json");
if (json != null) {
try {
plugin.logDebug("Trying to load " + dataObject.getCanonicalName());
T gsonResult = gson.fromJson(json, dataObject);
if (gsonResult != null) {
list.add(gsonResult);
}
} catch (JsonSyntaxException ex) {
plugin.logError(COULD_NOT_LOAD_OBJECT + ex.getMessage());
ex.printStackTrace();
plugin.logError(json);
}
}
}
} catch (Exception e) {
plugin.logError(COULD_NOT_LOAD_OBJECTS + e.getMessage());
e.printStackTrace();
}
return list;
}

View File

@ -19,9 +19,9 @@ import org.eclipse.jdt.annotation.NonNull;
import com.google.common.collect.ImmutableMap;
import net.md_5.bungee.api.ChatColor;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.panels.Panel;
import world.bentobox.bentobox.api.panels.PanelItem;
import world.bentobox.bentobox.api.panels.builders.PanelBuilder;
@ -32,6 +32,7 @@ import world.bentobox.bentobox.blueprints.conversation.DescriptionPrompt;
import world.bentobox.bentobox.blueprints.conversation.NameConversationPrefix;
import world.bentobox.bentobox.blueprints.conversation.NamePrompt;
import world.bentobox.bentobox.blueprints.dataobjects.BlueprintBundle;
import world.bentobox.bentobox.managers.BlueprintsManager;
import world.bentobox.bentobox.util.Util;
/**
@ -41,36 +42,52 @@ import world.bentobox.bentobox.util.Util;
public class BlueprintManagementPanel {
private static final String INFO = "Click on blueprint then click here";
private BentoBox plugin;
private final static Blueprint NORMAL_BP = new Blueprint().setIcon(Material.GREEN_STAINED_GLASS_PANE).setName("Normal").setDescription(INFO);
private final static Blueprint NETHER_BP = new Blueprint().setIcon(Material.RED_STAINED_GLASS_PANE).setName("Nether").setDescription(INFO);
private final static Blueprint END_BP = new Blueprint().setIcon(Material.YELLOW_STAINED_GLASS_PANE).setName("The End").setDescription(INFO);
private final static Map<Integer, World.Environment> SLOT_TO_ENV = ImmutableMap.of(3, World.Environment.NORMAL, 5, World.Environment.NETHER, 7, World.Environment.THE_END);
private final static Map<World.Environment, Blueprint> ENV_TO_BP = ImmutableMap.of(World.Environment.NORMAL, NORMAL_BP, World.Environment.NETHER, NETHER_BP, World.Environment.THE_END, END_BP);
private final BentoBox plugin;
private final Blueprint NORMAL_BP;
private final Blueprint NETHER_BP;
private final Blueprint END_BP;
private final Map<Integer, World.Environment> SLOT_TO_ENV;
private final Map<World.Environment, Blueprint> ENV_TO_BP;
private static final int MAX_WORLD_SLOT = 9;
private static final int MIN_WORLD_SLOT = 0;
private Entry<Integer, Blueprint> selected;
private Map<Integer, Blueprint> blueprints = new HashMap<>();
private final User user;
private final GameModeAddon addon;
public BlueprintManagementPanel(BentoBox plugin) {
public BlueprintManagementPanel(@NonNull BentoBox plugin, @NonNull User user, @NonNull GameModeAddon addon) {
this.plugin = plugin;
this.user = user;
this.addon = addon;
NORMAL_BP = new Blueprint().setIcon(Material.GREEN_STAINED_GLASS_PANE).setName(t("normal")).setDescription(INFO);
NETHER_BP = new Blueprint().setIcon(Material.RED_STAINED_GLASS_PANE).setName(t("nether")).setDescription(INFO);
END_BP = new Blueprint().setIcon(Material.YELLOW_STAINED_GLASS_PANE).setName(t("end")).setDescription(INFO);
SLOT_TO_ENV = ImmutableMap.of(3, World.Environment.NORMAL, 5, World.Environment.NETHER, 7, World.Environment.THE_END);
ENV_TO_BP = ImmutableMap.of(World.Environment.NORMAL, NORMAL_BP, World.Environment.NETHER, NETHER_BP, World.Environment.THE_END, END_BP);
}
private String t(String t) {
return user.getTranslation("commands.admin.blueprint.management." + t);
}
private String t(String t, String... vars) {
return user.getTranslation("commands.admin.blueprint.management." + t, vars);
}
public void openPanel(@NonNull User user, @NonNull GameModeAddon addon) {
public void openPanel() {
// Show panel of blueprint bundles
// Clicking on a bundle opens up the bundle edit panel
// Create the panel
PanelBuilder pb = new PanelBuilder().name("Blueprint Bundle Manager").user(user).size(45);
PanelBuilder pb = new PanelBuilder().name(t("title")).user(user).size(45);
// Get the bundles
plugin.getBlueprintsManager().getBlueprintBundles(addon).values().stream().limit(36)
.forEach(bb -> pb.item(new PanelItemBuilder()
.name(bb.getDisplayName())
.description("Click to edit")
.description(t("edit"))
.icon(bb.getIcon())
.clickHandler((panel, u, clickType, slot) -> {
u.closeInventory();
openBB(u, addon, bb);
openBB(bb);
return true;
})
.build()));
@ -81,7 +98,7 @@ public class BlueprintManagementPanel {
pb.build();
}
public void openBB(User user, @NonNull GameModeAddon addon, BlueprintBundle bb) {
public void openBB(BlueprintBundle bb) {
int index = 18;
for (Blueprint bp : plugin.getBlueprintsManager().getBlueprints(addon).values()) {
blueprints.put(index++, bp);
@ -90,7 +107,7 @@ public class BlueprintManagementPanel {
PanelBuilder pb = new PanelBuilder().name(bb.getDisplayName()).user(user).size(45);
// Display bundle icon
pb.item(0, new PanelItemBuilder()
.name("Click to edit description")
.name(t("edit-description"))
.description(bb.getDescription())
.icon(bb.getIcon())
.clickHandler((panel, u, clickType, slot) -> {
@ -111,15 +128,15 @@ public class BlueprintManagementPanel {
}
blueprints.entrySet().stream().limit(18).forEach(b -> pb.item(getBlueprintItem(addon, b.getKey(), bb, b.getValue())));
// Buttons for non-default bundle
if (!bb.getUniqueId().equals("default")) {
if (!bb.getUniqueId().equals(BlueprintsManager.DEFAULT_BUNDLE_NAME)) {
// Panel has a Trash icon. If right clicked it is discarded
pb.item(36, getTrashIcon(addon, bb));
// Toggle permission - default is always allowed
pb.item(39, getPermissionIcon(addon, bb));
}
// Panel has a Back icon.
pb.item(44, new PanelItemBuilder().icon(Material.ARROW).name("Back").clickHandler((panel, u, clickType, slot) -> {
openPanel(u,addon);
pb.item(44, new PanelItemBuilder().icon(Material.ARROW).name(t("back")).clickHandler((panel, u, clickType, slot) -> {
openPanel();
return true;
}).build());
@ -129,22 +146,22 @@ public class BlueprintManagementPanel {
private PanelItem getWorldInstrTile(Environment env) {
return new PanelItemBuilder()
.name(Util.prettifyText(env.name()) + " world")
.description("Place bluprint", "to right to set")
.name(t("world-name-syntax", TextVariables.NAME, Util.prettifyText(env.name())))
.description(t("world-instuctions"))
.icon(Material.GRAY_STAINED_GLASS_PANE)
.build();
}
private PanelItem getTrashIcon(@NonNull GameModeAddon addon, BlueprintBundle bb) {
return new PanelItemBuilder()
.name("Trash")
.description("Right click here to delete")
.name(t("trash"))
.description(t("trash-instructions"))
.icon(Material.TNT)
.clickHandler((panel, u, clickType, slot) -> {
if (clickType.equals(ClickType.RIGHT)) {
u.getPlayer().playSound(u.getLocation(), Sound.ENTITY_GENERIC_EXPLODE, 1F, 1F);
plugin.getBlueprintsManager().deleteBlueprintBundle(addon, bb);
openPanel(u,addon);
openPanel();
}
return true;
})
@ -152,9 +169,9 @@ public class BlueprintManagementPanel {
}
private PanelItem getPermissionIcon(@NonNull GameModeAddon addon, BlueprintBundle bb) {
return new PanelItemBuilder().icon(Material.PAINTING).name("Permission")
.description(bb.isRequirePermission() ? ChatColor.RED + "Required" : ChatColor.GREEN + "Not required")
.description(bb.isRequirePermission() ? addon.getPermissionPrefix() + "island.create." + bb.getUniqueId() : "")
return new PanelItemBuilder().icon(Material.PAINTING).name(t("permission"))
.description(bb.isRequirePermission() ? t("perm-required") : t("perm-not-required"))
.description(bb.isRequirePermission() ? t("perm-format") + addon.getPermissionPrefix() + "island.create." + bb.getUniqueId() : "")
.clickHandler((panel, u, clickType, slot) -> {
// Toggle permission
u.getPlayer().playSound(u.getLocation(), Sound.UI_BUTTON_CLICK, 1F, 1F);
@ -171,9 +188,9 @@ public class BlueprintManagementPanel {
List<String> desc = blueprint.getDescription() == null ? new ArrayList<>() : blueprint.getDescription();
if ((!blueprint.equals(END_BP) && !blueprint.equals(NORMAL_BP) && !blueprint.equals(NETHER_BP))) {
if ((pos > MIN_WORLD_SLOT && pos < MAX_WORLD_SLOT)) {
desc.add(ChatColor.RED + "Right click to remove");
desc.add(t("remove"));
} else {
desc.add(ChatColor.GREEN + "Click to select, then add to bundle");
desc.add(t("blueprint-instruction"));
}
}
return new PanelItemBuilder()
@ -193,7 +210,7 @@ public class BlueprintManagementPanel {
// Save
plugin.getBlueprintsManager().saveBlueprintBundle(addon, bb);
} else if (selected == null) {
u.sendRawMessage("Select Blueprint first");
u.sendMessage("commands.admin.blueprint.management.select-first");
u.getPlayer().playSound(u.getLocation(), Sound.BLOCK_ANVIL_HIT, 1F, 1F);
} else {
// Add
@ -235,8 +252,8 @@ public class BlueprintManagementPanel {
private PanelItem getNewBundle(@NonNull User user, @NonNull GameModeAddon addon) {
return new PanelItemBuilder()
.name("New Bundle")
.description("Click to make a new bundle")
.name(t("new-bundle"))
.description(t("new-bundle-instructions"))
.icon(Material.GREEN_BANNER)
.clickHandler((panel, u, clickType, slot) -> {
u.closeInventory();
@ -257,8 +274,7 @@ public class BlueprintManagementPanel {
.withPrefix(new NameConversationPrefix())
.withTimeout(90)
.withFirstPrompt(new NamePrompt(addon))
.withEscapeSequence("exit")
.withEscapeSequence("quit")
.withEscapeSequence(t("name.quit"))
.buildConversation(whom).begin();
}

View File

@ -1,8 +1,12 @@
package world.bentobox.bentobox.panels;
import java.util.List;
import java.util.stream.Collectors;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.eclipse.jdt.annotation.NonNull;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.addons.GameModeAddon;
@ -14,9 +18,6 @@ import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.versions.ServerCompatibility;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author Poslovitch
* @since 1.5.0
@ -72,7 +73,7 @@ public class ManagementPanel {
.name(user.getTranslation(LOCALE_REF + "views.gamemodes.blueprints.name"))
.description(user.getTranslation(LOCALE_REF + "views.gamemodes.blueprints.description"))
.clickHandler((panel, user1, clickType, slot) -> {
new BlueprintManagementPanel(plugin).openPanel(user, gameModeAddon);
new BlueprintManagementPanel(plugin, user, gameModeAddon).openPanel();
return true;
})
.build();

View File

@ -223,7 +223,45 @@ commands:
description: "set 2nd corner of cuboid clipboard"
save:
parameters: "<blueprint name>"
description: "save the copied clipboard"
description: "save the copied clipboard"
management:
instruction: "Click on blueprint then click here"
normal: "Normal"
nether: "Nether"
end: "The End"
title: "Blueprint Bundle Manager"
edit: "Click to edit"
edit-description: "Click to edit description"
world-name-syntax: "[name] world"
world-instructions: |
Place blueprint
to right to set
trash: "Trash"
trash-instructions: "Right click here to delete"
permission: "Permission"
perm-required: "Required"
perm-not-required: "Not Required"
perm-format: "&e"
remove: "Right click to remove"
blueprint-instruction: "Click to select, then add to bundle"
select-first: "Select Blueprint first"
new-bundle: "New Bundle"
new-bundle-instructions: "Click to make a new bundle"
name:
quit: "quit"
prompt: "Enter a name, or 'quit' to quit"
too-long: "&cToo long"
pick-a-unique-name: "Please pick a more unique name"
success: "Success!"
conversation-prefix: ">"
description:
quit: "quit"
instructions: |
Enter a multi line description for [name]
and 'quit' on a line by itself to finish.
default-color: ""
success: "Success!"
cancelling: "Cancelling"
resetflags:
description: "Reset all islands to default flag settings in config.yml"
world: