Skip to content
This repository was archived by the owner on Jan 25, 2024. It is now read-only.

Commit b74e822

Browse files
authored
Add word wrapping to descriptions, fix being able to edit other's home icons (#41)
1 parent 55edf27 commit b74e822

File tree

7 files changed

+61
-23
lines changed

7 files changed

+61
-23
lines changed

src/main/java/net/william278/huskhomes/gui/config/Locales.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@
2222
import de.themoep.minedown.adventure.MineDown;
2323
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
2424
import net.william278.annotaml.YamlFile;
25+
import net.william278.huskhomes.gui.HuskHomesGui;
2526
import org.apache.commons.text.StringEscapeUtils;
2627
import org.jetbrains.annotations.NotNull;
2728

28-
import java.util.Arrays;
29-
import java.util.HashMap;
30-
import java.util.Map;
31-
import java.util.Optional;
29+
import java.util.*;
30+
import java.util.regex.Matcher;
31+
import java.util.regex.Pattern;
32+
33+
import static org.bukkit.Bukkit.getLogger;
3234

3335
@YamlFile(header = """
3436
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
@@ -141,4 +143,21 @@ public static String escapeText(@NotNull String string) {
141143
}
142144

143145

146+
/**
147+
* Wraps the given string to a new line after every (int) characters.
148+
*
149+
* @param string the string to be wrapped, cannot be null
150+
* @return the wrapped string
151+
* @throws NullPointerException if the string is null
152+
*/
153+
public static String textWrap(@NotNull HuskHomesGui plugin, @NotNull String string) {
154+
Matcher matcher = Pattern.compile(".{1,"+ plugin.getSettings().getTextWrapLength() +"}").matcher(string);
155+
StringBuilder out = new StringBuilder();
156+
157+
while (matcher.find()) {
158+
out.append(plugin.getLocales().getLocale("item_description_line_style", matcher.group().trim()));
159+
}
160+
return String.valueOf(out);
161+
}
162+
144163
}

src/main/java/net/william278/huskhomes/gui/config/Settings.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ public class Settings {
4545
private boolean showMenuControls = true;
4646
@YamlKey("menu.display_controls_help_in_lore")
4747
private boolean displayControlsHelpInCore = false;
48+
@YamlKey("menu.text_wrap_length")
49+
private int textWrapLength = 25;
4850

4951
@YamlKey("menu.items.homes_filler")
5052
private String homesFillerItem = "minecraft:orange_stained_glass_pane";
@@ -104,6 +106,10 @@ public boolean camelCase() {
104106
return displayControlsHelpInCore;
105107
}
106108

109+
public int getTextWrapLength() {
110+
return textWrapLength;
111+
}
112+
107113
@NotNull
108114
public Material getHomesFillerItem() {
109115
return getMaterial(homesFillerItem);

src/main/java/net/william278/huskhomes/gui/menu/EditMenu.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import java.util.List;
3737
import java.util.function.Consumer;
3838

39+
import static net.william278.huskhomes.gui.config.Locales.textWrap;
40+
3941
/**
4042
* A menu for editing a saved position
4143
*/
@@ -213,7 +215,7 @@ protected Consumer<InventoryGui> buildMenu() {
213215

214216
// description
215217
(!position.getMeta().getDescription().isBlank() ?
216-
plugin.getLocales().getLocale("edit_description_default_message", position.getMeta().getDescription())
218+
plugin.getLocales().getLocale("edit_description_default_message", textWrap(plugin, position.getMeta().getDescription()))
217219
: plugin.getLocales().getLocale("edit_description_default_message_blank"))));
218220

219221
// Editing home privacy
@@ -279,10 +281,6 @@ protected Consumer<InventoryGui> buildMenu() {
279281
new ItemStack(Material.OAK_SIGN),
280282
// Name
281283
plugin.getLocales().getLocale("item_info_name", position.getName()),
282-
// Description
283-
(!position.getMeta().getDescription().isBlank() ?
284-
plugin.getLocales().getLocale("item_info_description", position.getMeta().getDescription())
285-
: plugin.getLocales().getLocale("item_info_description_blank")),
286284
// World name
287285
plugin.getLocales().getLocale("item_info_world", position.getWorld().getName()),
288286
// Server name

src/main/java/net/william278/huskhomes/gui/menu/ListMenu.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import java.util.List;
3737
import java.util.function.Consumer;
3838

39+
import static net.william278.huskhomes.gui.config.Locales.textWrap;
40+
3941
/**
4042
* A menu for displaying a list of saved positions
4143
*/
@@ -139,7 +141,6 @@ private DynamicGuiElement getPositionButton(@NotNull HuskHomesGui plugin, @NotNu
139141
final OnlineUser user = api.adaptUser(player);
140142
switch (click.getType()) {
141143
case LEFT -> {
142-
// Update the icon with the item on the cursor
143144
final ItemStack newItem = player.getItemOnCursor();
144145
if (newItem.getType() == Material.AIR) {
145146
// teleport
@@ -157,9 +158,23 @@ private DynamicGuiElement getPositionButton(@NotNull HuskHomesGui plugin, @NotNu
157158
}
158159

159160
// Update the icon with the item on the cursor
160-
if (!player.hasPermission(EDIT_HOME_PERMISSION)
161-
&& !player.hasPermission(EDIT_HOME_OTHER_PERMISSION)) {
162-
return true;
161+
switch (type) {
162+
case HOME, PUBLIC_HOME -> {
163+
if (player.getUniqueId().equals(((Home) position).getOwner().getUuid())) {
164+
if (!player.hasPermission(EDIT_HOME_PERMISSION)) {
165+
return true;
166+
}
167+
} else {
168+
if (!player.hasPermission(EDIT_HOME_OTHER_PERMISSION)) {
169+
return true;
170+
}
171+
}
172+
}
173+
case WARP -> {
174+
if (!player.hasPermission(EDIT_WARP_PERMISSION)) {
175+
return true;
176+
}
177+
}
163178
}
164179
setPositionMaterial(position, newItem.getType());
165180
click.getGui().draw();
@@ -203,7 +218,7 @@ private DynamicGuiElement getPositionButton(@NotNull HuskHomesGui plugin, @NotNu
203218

204219
// description
205220
(!position.getMeta().getDescription().isBlank() ?
206-
plugin.getLocales().getLocale("item_description", position.getMeta().getDescription())
221+
plugin.getLocales().getLocale("item_description", textWrap(plugin, position.getMeta().getDescription()))
207222
: plugin.getLocales().getLocale("item_description_blank")),
208223

209224
// player name

src/main/resources/locales/en-gb.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ public_homes_menu_title: 'Public Homes'
33
warps_menu_title: 'Warps'
44
item_name: '[%1%](#00fb9a)'
55
item_name_public: '[%1%](green)'
6-
item_description: '&7ℹ %1%'
6+
item_description: '[ℹ](gray) %1%'
7+
item_description_line_style: '[%1%](gray)\n'
78
item_description_blank: ''
89
item_controls_space: ''
910
home_owner_name: '[[Created by %1%]](#00fb9a)'
@@ -25,14 +26,13 @@ edit_privacy_button: '[Edit Privacy](#00fb9a)'
2526
delete_button: '[Delete](#00fb9a)'
2627
delete_button_describe: '[ • Right Click: Delete](gray)'
2728
item_info_name: '[Name:](#00fb9a) %1%'
28-
item_info_description: '%1%'
2929
item_info_world: '[World:](#00fb9a) %1%'
3030
item_info_server: '[Server:](#00fb9a) %1%'
3131
item_info_coordinates: '[Coordinates:](#00fb9a) x: %1%, y: %2%, z: %3%'
3232
item_controls: '[ • Left Click: Teleport](gray)\n[ • Right Click: Edit](gray)\n[ • Place Item: Set icon](gray)\n'
3333
item_deleted_name: '&c[DEL] [%1%](#00fb9a)'
3434
edit_location_default_message: '[Coordinates:](#00fb9a) x: %1%, y: %2%, z: %3%'
35-
edit_description_default_message: '&7ℹ %1%'
35+
edit_description_default_message: '[ℹ](gray) %1%'
3636
edit_description_default_message_blank: ''
3737
edit_description_default_input: ''
3838
edit_privacy_message: '[currently](#00fb9a) %1%'

src/main/resources/locales/es-es.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ public_homes_menu_title: 'Hogares Publicos'
33
warps_menu_title: 'Warps'
44
item_name: '[%1%](#00fb9a)'
55
item_name_public: '[%1%](green)'
6-
item_description: '&7ℹ %1%'
6+
item_description: '[ℹ](gray) %1%'
7+
item_description_line_style: '[%1%](gray)\n'
78
item_description_blank: ''
89
item_controls_space: ''
910
home_owner_name: '[[Creado por %1%]](#00fb9a)'
@@ -25,14 +26,13 @@ edit_privacy_button: '[Editar Privacidad](#00fb9a)'
2526
delete_button: '[Delete](#00fb9a)'
2627
delete_button_describe: '[ • Right Click: Delete](gray)'
2728
item_info_name: '[Nombre:](#00fb9a) %1%'
28-
item_info_description: '%1%'
2929
item_info_world: '[Mundo:](#00fb9a) %1%'
3030
item_info_server: '[Servidor:](#00fb9a) %1%'
3131
item_info_coordinates: '[Coordenades:](#00fb9a) x: %1%, y: %2%, z: %3%'
3232
item_controls: '[ • Click Izquierdo: Teleport](gray)\n[ • Click Derecho: Editar](gray)\n[ • Colocar Item: Establecer icono](gray)\n'
3333
item_deleted_name: '&c[BORRAR] [%1%](#00fb9a)'
3434
edit_location_default_message: '[Coordenades:](#00fb9a) x: %1%, y: %2%, z: %3%'
35-
edit_description_default_message: '&7ℹ %1%'
35+
edit_description_default_message: '[ℹ](gray) %1%'
3636
edit_description_default_message_blank: ''
3737
edit_description_default_input: ''
3838
edit_privacy_message: '[currently](#00fb9a) %1%'

src/main/resources/locales/zh-cn.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ home_editor_title: '编辑家: %1%'
55
warp_editor_title: '编辑地标: %1%'
66
item_name: '[%1%](#00fb9a)'
77
item_name_public: '[%1%](green)'
8-
item_description: '&7ℹ %1%'
8+
item_description: '[ℹ](gray) %1%'
9+
item_description_line_style: '[%1%](gray)\n'
910
item_description_blank: ''
1011
item_controls_space: ''
1112
item_controls: '[ • 左键点击: 传送](gray)\n[ • 右键点击: 编辑](gray)\n[ • 放置物品: 设置图标](gray)\n'
@@ -17,7 +18,7 @@ edit_name_title: '修改名称: %1%'
1718
edit_name_button: '[修改名称](#00fb9a)'
1819
edit_description_title: '修改描述: %1%'
1920
edit_description_button: '[修改描述](#00fb9a)'
20-
edit_description_default_message: '&7ℹ %1%'
21+
edit_description_default_message: '[ℹ](gray) %1%'
2122
edit_description_default_message_blank: ''
2223
edit_description_default_input: ''
2324
edit_privacy_button: '[切换开放状态](#00fb9a)'
@@ -27,7 +28,6 @@ edit_privacy_message_private: '&6私有'
2728
delete_button: '[删除](#00fb9a)'
2829
delete_button_describe: '[ • 右键点击: 删除](gray)'
2930
item_info_name: '[名称:](#00fb9a) %1%'
30-
item_info_description: '&7ℹ %1%'
3131
item_info_world: '[世界:](#00fb9a) %1%'
3232
item_info_server: '[服务器:](#00fb9a) %1%'
3333
item_info_coordinates: '[坐标:](#00fb9a) x: %1%, y: %2%, z: %3%'

0 commit comments

Comments
 (0)