Added underline and FontStyleSearchUI

This commit is contained in:
DidlDimpfe committed 2023-07-05 16:47:09 +02:00
1 parent 4bee81198c
commit 95e3a24b67
11 files changed
+293 -38

No files matched your search

@@ -36,6 +36,26 @@ public class CurrentEditedText {
private int generateSuccess;
// Utility for Constructors
private BukkitTask generateTask;
public void checkFirstGenerateDone() {
generateTask = Bukkit.getScheduler().runTaskTimer(TextGenerator.getInstance(), () -> {
if (!blockBuilder.isRunning()) {
blinkAroundTheEdge();
playSound();
NoMoveWhileGenerateListener.remove(player.getUniqueId());
generateTask.cancel();
}
}, 1, 1);
}
private boolean notTheSameAsItWas;
public boolean isNotTheSameAsItWas() {
return notTheSameAsItWas;
}
public CurrentEditedText(Player player, String wantedText) { // For first Generate
this.player = player;
this.dragToMoveTasks = new ArrayList<>();
@@ -53,7 +73,7 @@ public class CurrentEditedText {
.withText(wantedText)
.withDirection(Direction.valueOf(player.getFacing().toString()).getRightDirection())
.withPlacementRange(placementRange)
.withDragToMove(ConfigManager.isDragToMove(true))
.withDragToMove(ConfigManager.isDragToMove())
.build();
if (textInstance.getFontSize() < 9) {
if (GenerateUtil.isNotTextValidForSpecificFontSize(wantedText)) {
@@ -75,25 +95,6 @@ public class CurrentEditedText {
checkFirstGenerateDone();
}
private BukkitTask generateTask;
public void checkFirstGenerateDone() {
generateTask = Bukkit.getScheduler().runTaskTimer(TextGenerator.getInstance(), () -> {
if (!blockBuilder.isRunning()) {
blinkAroundTheEdge();
playSound();
NoMoveWhileGenerateListener.remove(player.getUniqueId());
generateTask.cancel();
}
}, 1, 1);
}
private boolean notTheSameAsItWas;
public boolean isNotTheSameAsItWas() {
return notTheSameAsItWas;
}
public CurrentEditedText(Player player, TextInstance textInstance) { // For Edit
GeneratedTextsManager.removeTextInstance(textInstance.getUuid());
this.player = player;
@@ -311,16 +312,18 @@ public class CurrentEditedText {
private boolean isEnoughSpaceForText() {
for (int heightIndex = 0; heightIndex < blocks.length; heightIndex++) {
for (int widthIndex = 0; widthIndex < blocks[0].length; widthIndex++) {
if (blocks[heightIndex][widthIndex] == null) continue;
Block block = Objects.requireNonNull(textInstance.getTopLeftLocation().getWorld()).getBlockAt(
Objects.requireNonNull(GenerateUtil.editLocation(textInstance,
textInstance.getTopLeftLocation(), widthIndex,
heightIndex, 0, 0, 0, 0)));
if (block.getBlockData().getMaterial() != Material.AIR) {
if (!block.hasMetadata(FastBlockUpdate.metaDataKey)) {
return false;
try {
if (blocks[heightIndex][widthIndex] == null) continue;
Block block = Objects.requireNonNull(textInstance.getTopLeftLocation().getWorld()).getBlockAt(
Objects.requireNonNull(GenerateUtil.editLocation(textInstance,
textInstance.getTopLeftLocation(), widthIndex,
heightIndex, 0, 0, 0, 0)));
if (block.getBlockData().getMaterial() != Material.AIR) {
if (!block.hasMetadata(FastBlockUpdate.metaDataKey)) {
return false;
}
}
} catch (IndexOutOfBoundsException ignored) {}
}
}
return true;
@@ -504,6 +507,14 @@ public class CurrentEditedText {
refreshBlocksByPlayersSight();
}
public boolean setFontStyle(int fontStyle) {
if (textInstance.getFontSize() < 9) return false;
textInstance.setFontStyle(fontStyle);
updateBlockArray();
updateBlocksInWorld(true);
return true;
}
public boolean setBlock(de.philw.textgenerator.ui.value.Block block) {
if (textInstance.getFontSize() < 9 && block.getSlabAndStairsID() == null) return false;
this.textInstance.setBlock(block);
@@ -529,6 +540,12 @@ public class CurrentEditedText {
else stopDragToMoveTasks();
}
public void setUnderline(boolean underline) {
this.textInstance.setUnderline(underline);
updateBlockArray();
updateBlocksInWorld(true);
}
public boolean isFirstGenerate() {
return textInstance.getPreviousTextInstance() == null;
}
@@ -75,6 +75,11 @@ public class ConfigManager {
return FileUtil.fromFontStyleStringToInt(fontStyle);
}
public static void setFontStyle(String fontStyle) {
config.set("textSettings.fontStyle", fontStyle);
saveConfig();
}
public static boolean isUnderline() {
String bool = config.getString("textSettings.underline");
if (Validator.isNoValidBoolean(Objects.requireNonNull(bool))) {
@@ -85,6 +90,11 @@ public class ConfigManager {
return Boolean.parseBoolean(bool.toLowerCase());
}
public static void setUnderline(boolean underline) {
config.set("textSettings.underline", underline);
saveConfig();
}
public static int getLineSpacing() {
String spaceBetweenEachLine = config.getString("textSettings.lineSpacing");
if (!Validator.isValidLineSpacing(spaceBetweenEachLine)) {
@@ -116,12 +126,11 @@ public class ConfigManager {
saveConfig();
}
public static boolean isDragToMove(boolean printNotValidMessage) {
public static boolean isDragToMove() {
String dragToMove = config.getString("dragToMove");
if (Validator.isNoValidBoolean(Objects.requireNonNull(dragToMove))) {
if (printNotValidMessage)
printNotValidMessage(dragToMove, String.valueOf(Objects.requireNonNull(config.getDefaults()).getInt(
"dragToMove")), "drag to move");
printNotValidMessage(dragToMove, String.valueOf(Objects.requireNonNull(config.getDefaults()).getInt(
"dragToMove")), "drag to move");
return Objects.requireNonNull(config.getDefaults()).getBoolean("dragToMove");
}
return Boolean.parseBoolean(dragToMove.toLowerCase());
@@ -0,0 +1,67 @@
package de.philw.textgenerator.ui;
import de.philw.textgenerator.TextGenerator;
import de.philw.textgenerator.manager.ConfigManager;
import de.philw.textgenerator.ui.value.FontStyle;
import de.philw.textgenerator.utils.FileUtil;
import de.philw.textgenerator.utils.UIUtil;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.ArrayList;
import java.util.Objects;
public class FontStyleSearchUI extends SearchUI {
public FontStyleSearchUI(Player player) {
super(ChatColor.GREEN + "Select font style", "Search a font style...", player);
}
@Override
public ArrayList<ItemStack> getSearchedItems(String searched) {
ArrayList<ItemStack> searchedItems = new ArrayList<>();
for (FontStyle fontStyle : FontStyle.values()) {
if (fontStyle.getDisplay().toLowerCase().contains(searched.toLowerCase())) {
searchedItems.add(getFontStyleItemStack(fontStyle));
}
}
return searchedItems;
}
@Override
public void updateAllItems() {
allItems.clear();
for (FontStyle fontStyle : FontStyle.values()) {
allItems.add(getFontStyleItemStack(fontStyle));
}
}
private ItemStack getFontStyleItemStack(FontStyle fontStyle) {
String fontStyleDisplay = fontStyle.getDisplay();
ItemStack itemStack = UIUtil.getSkullByString(fontStyle.getSkullData());
ItemMeta itemMeta = Objects.requireNonNull(itemStack).getItemMeta();
Objects.requireNonNull(itemMeta).setDisplayName(ChatColor.GREEN + fontStyleDisplay);
int fontStyleInNumber = FileUtil.fromFontStyleStringToInt(fontStyle.getSavedInFile());
if (TextGenerator.getInstance().getTextGeneratorCommand().getCurrentEditedTexts().containsKey(uuid)) {
if (fontStyleInNumber == TextGenerator.getInstance().getTextGeneratorCommand().getCurrentEditedTexts().get(uuid).getTextInstance().getFontStyle()) {
itemMeta.setLore(UIUtil.getLore(ChatColor.RED, "You have already assigned this value"));
} else {
itemMeta.setLore(UIUtil.getLore(ChatColor.YELLOW, "Click to change the font style of your" +
" current edited text to " + fontStyleDisplay));
}
} else {
if (fontStyleInNumber == ConfigManager.getFontStyle()) {
itemMeta.setLore(UIUtil.getLore(ChatColor.RED, "You have already assigned this value"));
} else {
itemMeta.setLore(UIUtil.getLore(ChatColor.YELLOW, "Click to change the default font style " +
"to " + fontStyleDisplay));
}
}
itemMeta.setLocalizedName(SearchUI.FONT_STYLE_SEARCH_UI + ";" + fontStyle.getSavedInFile());
itemStack.setItemMeta(itemMeta);
return itemStack;
}
}
@@ -22,6 +22,7 @@ public abstract class SearchUI {
public final static int FONT_SIZE_SEARCH_UI = 1;
public final static int LINE_SPACING_SEARCH_UI = 2;
public final static int PLACEMENT_RANGE_SEARCH_UI = 3;
public final static int FONT_STYLE_SEARCH_UI = 4;
protected final static int SPACES = 45;
protected final static int RETURN_ARROW_INDEX = 45;
@@ -5,6 +5,7 @@ import de.philw.textgenerator.letters.CurrentEditedText;
import de.philw.textgenerator.manager.ConfigManager;
import de.philw.textgenerator.manager.MessagesManager;
import de.philw.textgenerator.ui.value.Block;
import de.philw.textgenerator.utils.FileUtil;
import de.philw.textgenerator.utils.UIUtil;
import net.wesjd.anvilgui.AnvilGUI;
import org.bukkit.Material;
@@ -109,6 +110,9 @@ public class SearchUIListener implements Listener {
case SearchUI.PLACEMENT_RANGE_SEARCH_UI:
placementRangeSearchUIValueClicked(player, information);
break;
case SearchUI.FONT_STYLE_SEARCH_UI:
fontStyleSearchUIValueClicked(player, information);
break;
}
}
}
@@ -240,4 +244,35 @@ public class SearchUIListener implements Listener {
}
}
private void fontStyleSearchUIValueClicked(Player player, String[] information) {
String fontStyle = information[1];
int fontStyleAsNumber = FileUtil.fromFontStyleStringToInt(information[1]);
if (!TextGenerator.getInstance().getTextGeneratorCommand().getCurrentEditedTexts().containsKey(player.getUniqueId())) {
ConfigManager.setFontStyle(fontStyle);
for (SearchUI searchUI :
TextGenerator.getInstance().getSearchUIListener().getSearchUISToListenTo().values()) {
if (!(searchUI instanceof FontStyleSearchUI)) continue;
searchUI.updateCurrentSearchUIItems();
}
} else {
CurrentEditedText currentEditedText =
TextGenerator.getInstance().getTextGeneratorCommand().getCurrentEditedTexts().get(player.getUniqueId());
if (fontStyleAsNumber == currentEditedText.getTextInstance().getFontStyle()) {
player.sendMessage(MessagesManager.getMessage("changedValueOfCurrentText" +
".deniedBecauseValueAlreadySelected", "font style", fontStyle));
} else {
player.sendMessage(MessagesManager.getMessage("changedValueOfCurrentText.success", "font style",
fontStyle));
if (currentEditedText.setFontStyle(fontStyleAsNumber)) {
player.sendMessage(MessagesManager.getMessage("changedValueOfCurrentText.success", "font style",
fontStyle));
} else {
player.sendMessage(MessagesManager.getMessage("changedValueOfCurrentText.deniedBecauseItWouldNotMakeADifference", "font style",
fontStyle));
}
}
player.closeInventory();
}
}
}
@@ -11,6 +11,7 @@ import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.List;
import java.util.Objects;
public class SettingsUI {
@@ -20,6 +21,8 @@ public class SettingsUI {
protected final static int BLOCKS_INDEX = 2;
protected final static int DRAG_TO_MOVE_INDEX = 3;
protected final static int PLACE_RANGE_INDEX = 4;
protected final static int FONT_STYLE_INDEX = 5;
protected final static int UNDERLINE_INDEX = 6;
private final Player player;
@@ -32,6 +35,8 @@ public class SettingsUI {
inventory.setItem(BLOCKS_INDEX, getChangeBlocksItemStack());
inventory.setItem(DRAG_TO_MOVE_INDEX, getDragToMoveItemStack());
inventory.setItem(PLACE_RANGE_INDEX, getPlacementRangeItemStack());
inventory.setItem(FONT_STYLE_INDEX, getFontStyleItemStack());
inventory.setItem(UNDERLINE_INDEX, getUnderlineItemStack());
player.openInventory(inventory);
}
@@ -82,17 +87,21 @@ public class SettingsUI {
ItemStack itemStack = UIUtil.getSkullByString(SkullData.DRAG_TO_MOVE);
ItemMeta itemMeta = Objects.requireNonNull(itemStack).getItemMeta();
Objects.requireNonNull(itemMeta).setDisplayName(ChatColor.GREEN + "Drag to move");
List<String> carefulLore = UIUtil.getLore(ChatColor.RED, "Careful: Enabling this feature with a text containing many blocks, this can cause issues!");
carefulLore.add("");
if (isEditMode()) {
if (TextGenerator.getInstance().getTextGeneratorCommand().getCurrentEditedTexts().get(player.getUniqueId()).getTextInstance().isDragToMove()) {
itemMeta.setLore(UIUtil.getLore(ChatColor.YELLOW, "Stop your current edited text follow your view"));
itemMeta.setLore(UIUtil.getLore(ChatColor.YELLOW, "Click to stop your current edited text follow your view"));
} else {
itemMeta.setLore(UIUtil.getLore(ChatColor.YELLOW, "Make your current edited text follow your view"));
carefulLore.addAll(UIUtil.getLore(ChatColor.YELLOW, "Click to make your current edited text follow your view"));
itemMeta.setLore(carefulLore);
}
} else {
if (ConfigManager.isDragToMove(false)) {
itemMeta.setLore(UIUtil.getLore(ChatColor.YELLOW, "Stop default texts follow your view"));
if (ConfigManager.isDragToMove()) {
itemMeta.setLore(UIUtil.getLore(ChatColor.YELLOW, "Click to stop default texts follow your view"));
} else {
itemMeta.setLore(UIUtil.getLore(ChatColor.YELLOW, "Make default texts follow your view"));
carefulLore.addAll(UIUtil.getLore(ChatColor.YELLOW, "Click to make default texts follow your view"));
itemMeta.setLore(carefulLore);
}
}
itemStack.setItemMeta(itemMeta);
@@ -112,5 +121,38 @@ public class SettingsUI {
return itemStack;
}
private ItemStack getFontStyleItemStack() {
ItemStack itemStack = UIUtil.getSkullByString(SkullData.FONT_STYLE);
ItemMeta itemMeta = Objects.requireNonNull(itemStack).getItemMeta();
Objects.requireNonNull(itemMeta).setDisplayName(ChatColor.GREEN + "Font style");
if (!isEditMode()) {
itemMeta.setLore(UIUtil.getLore(ChatColor.YELLOW, "Change the default font style"));
} else {
itemMeta.setLore(UIUtil.getLore(ChatColor.YELLOW, "Change the font style of your current edited text"));
}
itemStack.setItemMeta(itemMeta);
return itemStack;
}
private ItemStack getUnderlineItemStack() {
ItemStack itemStack = UIUtil.getSkullByString(SkullData.UNDERLINE);
ItemMeta itemMeta = Objects.requireNonNull(itemStack).getItemMeta();
Objects.requireNonNull(itemMeta).setDisplayName(ChatColor.GREEN + "Underline");
if (isEditMode()) {
if (TextGenerator.getInstance().getTextGeneratorCommand().getCurrentEditedTexts().get(player.getUniqueId()).getTextInstance().isUnderline()) {
itemMeta.setLore(UIUtil.getLore(ChatColor.YELLOW, "Click to remove the underline of your current edited text"));
} else {
itemMeta.setLore(UIUtil.getLore(ChatColor.YELLOW, "Click to add underline to your current edited text"));
}
} else {
if (ConfigManager.isUnderline()) {
itemMeta.setLore(UIUtil.getLore(ChatColor.YELLOW, "Click to remove underline from your default texts"));
} else {
itemMeta.setLore(UIUtil.getLore(ChatColor.YELLOW, "Click to add underline from your default texts"));
}
}
itemStack.setItemMeta(itemMeta);
return itemStack;
}
}
@@ -38,12 +38,18 @@ public class SettingsUIListener implements Listener {
case SettingsUI.PLACE_RANGE_INDEX:
placeRangeIndexClicked(player);
break;
case SettingsUI.FONT_STYLE_INDEX:
new FontStyleSearchUI(player);
break;
case SettingsUI.UNDERLINE_INDEX:
toggleUnderline(player);
break;
}
}
private void toggleDragToMove(Player player) {
if (!TextGenerator.getInstance().getTextGeneratorCommand().getCurrentEditedTexts().containsKey(player.getUniqueId())) {
boolean dragToMove = !ConfigManager.isDragToMove(false);
boolean dragToMove = !ConfigManager.isDragToMove();
ConfigManager.setDragToMove(dragToMove);
new SettingsUI(player);
} else {
@@ -70,4 +76,20 @@ public class SettingsUIListener implements Listener {
}
}
private void toggleUnderline(Player player) {
if (!TextGenerator.getInstance().getTextGeneratorCommand().getCurrentEditedTexts().containsKey(player.getUniqueId())) {
boolean underline = !ConfigManager.isUnderline();
ConfigManager.setUnderline(underline);
new SettingsUI(player);
} else {
CurrentEditedText currentEditedText =
TextGenerator.getInstance().getTextGeneratorCommand().getCurrentEditedTexts().get(player.getUniqueId());
boolean underline = !currentEditedText.getTextInstance().isUnderline();
currentEditedText.setUnderline(underline);
player.sendMessage(MessagesManager.getMessage("changedValueOfCurrentText.success", "underline",
underline));
player.closeInventory();
}
}
}
@@ -340,4 +340,8 @@ public enum Block {
return "minecraft:" + toString().toLowerCase();
}
public String getBottomSlap() {
return "minecraft:" + getSlabAndStairsID().get(0).toLowerCase() + "[type=bottom,waterlogged=false]";
}
}
@@ -0,0 +1,37 @@
package de.philw.textgenerator.ui.value;
import de.philw.textgenerator.utils.SkullData;
public enum FontStyle {
PLAIN("Plain", SkullData.PLAIN),
BOLD("Bold", SkullData.BOLD),
ITALIC("Italic", SkullData.ITALIC),
BOLD_ITALIC("Bold + Italic", SkullData.BOLD_ITALIC);
private final String display;
private final String skullData;
FontStyle(String display, String skullData) {
this.display = display;
this.skullData = skullData;
}
public String getDisplay() {
return display;
}
public String getSkullData() {
return skullData;
}
public String getSavedInFile() {
String[] words = name().split("_");
StringBuilder stringBuilder = new StringBuilder();
for (String word : words) {
word = word.toLowerCase();
word = word.substring(0, 1).toUpperCase() + word.substring(1);
stringBuilder.append(word);
}
return stringBuilder.toString();
}
}
@@ -300,6 +300,7 @@ public class GenerateUtil {
private static String[][] getLine(TextInstance textInstance, String text, SpecificFontSize specificFontSize) {
int height = Objects.requireNonNull(getLetter('a', textInstance, specificFontSize)).length;
if (textInstance.isUnderline()) height++;
int width = 0;
for (char character: text.toLowerCase().toCharArray()) {
if (character == ' ') {
@@ -323,6 +324,12 @@ public class GenerateUtil {
}
currentWidth += (letter[0].length + 1);
}
int lowestRow = height - 1;
if (textInstance.isUnderline()) {
for (int i = 0; i < width; i++) {
line[lowestRow][i] = textInstance.getBlock().getBottomSlap();
}
}
return line;
}
@@ -25,6 +25,20 @@ public class SkullData {
"";
public static final String PLACE_RANGE =
"eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvNjZjYTlmNGJiOWExM2Q4YjJlYzkxOTFlYWZmMjJkM2U4ODcwZGI1OTRmOTE5NDI0ODViOTc3MmE3MzI0ZTkzZiJ9fX0=";
public static final String FONT_STYLE =
"eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvOGQyMTBhOTgxZmZiNGUzOGM5MWJhZmZmYzg2MzA5NTkyN2Q3YTNkMjg3NjlkNDFhZDg3NTY1ZGQ5OTg4OTY2OCJ9fX0=";
public static final String UNDERLINE =
"eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvOTgxYWY5MzljMTZmNzMyZDJkMDQ3NzQxODQ0ZmEyN2UwYjNjMjViNjdjYzNjMjg0OWViZjc1ZmU4YzU0ODI0ZCJ9fX0=";
// Text styles
public static final String PLAIN =
"eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvYWU0NmQzYmJjMzc5ZWI1NWEzZmU5NmFkMjQzNjRjNmE3M2U2N2UyNjk1YjVkZDA5NzQyZDhjMzc3NzM5NDBkYiJ9fX0=";
public static final String BOLD =
"eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvN2Q0Y2VmZDc1ODZmYWUxMDYzMmMzOGQwYWJmMGExNTI5ZDQxYjkyMDQ3NmI2NjhkYmNiOWQzMDVlY2NlYjE1NyJ9fX0=";
public static final String ITALIC =
"eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvZjJmMThkZGFkZjM2YmFiODhkNWI0MGVhNzE3MmMwZDhmNWJiODM5YzAzZjg2YTQ0NzljMjgwYTdkMTI3OWU4NiJ9fX0=";
public static final String BOLD_ITALIC =
"eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvMmY2Y2QyOTQxNjU1ZGY3NDliYmY2MTBiMWE1MGNlYzQ1NjA0NTg0NDc0Y2E4MGYzYWEwMjUzY2U3Yzg5MTE2OCJ9fX0=";
// Numbers
public static final String ZERO =