From 4ec4c7641a398cb62d0cd36c1f1c5530f52f5a76 Mon Sep 17 00:00:00 2001 From: DriHut <63098227+DriHut@users.noreply.github.com> Date: Sun, 7 Jun 2020 19:53:08 +0200 Subject: [PATCH 1/4] testing but nothing works (for now) --- config.yml | 19 +++++++- src/fr/topeka/HelpRulesBook/Book.java | 68 +++++++++++++++++++++++++-- 2 files changed, 81 insertions(+), 6 deletions(-) diff --git a/config.yml b/config.yml index 4a05356..5410d25 100644 --- a/config.yml +++ b/config.yml @@ -2,8 +2,18 @@ book: title: "Un magnifique titre" author: "Auteur du livre" pages: + #You can add clickable text by writing it like the following example + #random_text {your_text_with_abilities,hover_message,click_event(action,argument)} other_random_text + # ACTION_LIST: CHANGE_PAGE, COPY_TO_CLIPBOARD, OPEN_URL, RUN_COMMAND, SUGGEST_COMMAND + #examples: + # - This is an example + # if a player click {here,he would see this message by hovering,true(CHANGE_PAGE.4)} + # he would open page 4 + # if she exist + #note that if you want to write an { or } in your text put a \ before + #colors are also supported with &, help available on https://minecraft.tools/en/color-code.php - | - Premier page + Premiere page C'est beau tu trouve pas ? - | ça c'est Seconde page. @@ -11,4 +21,9 @@ book: - | Et cette troisème alors ? la meilleure de toutes ! - Tu trouves pas ? \ No newline at end of file + Tu trouves pas ? + - | + This is an example + if a player click {here,he would see this message by hovering,true(CHANGE_PAGE.1)} + he would open page 1 + if she exist \ No newline at end of file diff --git a/src/fr/topeka/HelpRulesBook/Book.java b/src/fr/topeka/HelpRulesBook/Book.java index 6cca36c..d67c2bd 100644 --- a/src/fr/topeka/HelpRulesBook/Book.java +++ b/src/fr/topeka/HelpRulesBook/Book.java @@ -1,5 +1,6 @@ package fr.topeka.HelpRulesBook; +import java.util.ArrayList; import java.util.List; import org.bukkit.Material; @@ -7,6 +8,12 @@ import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.BookMeta; +import net.md_5.bungee.api.chat.BaseComponent; +import net.md_5.bungee.api.chat.ClickEvent; +import net.md_5.bungee.api.chat.ComponentBuilder; +import net.md_5.bungee.api.chat.HoverEvent; +import net.md_5.bungee.api.chat.TextComponent; + public class Book { private ItemStack book; @@ -23,10 +30,64 @@ public class Book { BookMeta meta = (BookMeta) book.getItemMeta(); meta.setTitle(_title); meta.setAuthor(_author); - for(int i=0;i<_pages.size();i++) - meta.addPage(_pages.get(i).replace("&", "§")); - if(book.setItemMeta(meta)) + int lastCaracter = 0; + BaseComponent[] page = new ComponentBuilder().create(); + List subPage = new ArrayList(); + for (String rawPage: _pages){ + if (rawPage.contains("{") && rawPage.contains("}")) { + while (rawPage.length() != 0) { + if (!rawPage.substring(rawPage.indexOf("{", lastCaracter)-1, rawPage.indexOf("{", lastCaracter)+1).contains("\\")) { + subPage.add(rawPage.substring(0, rawPage.indexOf("{", lastCaracter))); + rawPage = rawPage.substring(rawPage.indexOf("{", lastCaracter), rawPage.length()); + while (!rawPage.substring(rawPage.indexOf("}", lastCaracter)-1, rawPage.indexOf("}", lastCaracter)+1).contains("\\")) { + lastCaracter = rawPage.indexOf("}", lastCaracter)+1; + } + subPage.add(rawPage.substring(0, rawPage.indexOf("}", lastCaracter))); + rawPage = rawPage.substring(rawPage.indexOf("}", lastCaracter), rawPage.length()); + } + else if (rawPage.substring(rawPage.indexOf("{", lastCaracter)-1, rawPage.indexOf("{", lastCaracter)+1).contains("\\")) { + lastCaracter = rawPage.indexOf("{", lastCaracter); + } + else { + subPage.add(rawPage.replace("&", "§")); + rawPage = ""; + } + } + for (int i = subPage.size()-1; i != 0; i--) { + if (!subPage.get(i).startsWith("{")) { + page = new ComponentBuilder(subPage.get(i)).append(page).create(); + } + else if (subPage.get(i).startsWith("{")) { + TextComponent eventBuilder = new TextComponent(subPage.get(i).split(",")[0].substring(1)); + if (!subPage.get(i).split(",")[1].equals("")) { + eventBuilder.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(subPage.get(i).split(",")[1]).create())); + } + if(subPage.get(i).split(",")[2].startsWith("true")) { + if (subPage.get(i).split(",")[2].startsWith("CHANGE_PAGE", 5)) { + eventBuilder.setClickEvent(new ClickEvent(ClickEvent.Action.CHANGE_PAGE, subPage.get(i).split(",")[2].substring(subPage.get(i).split(",")[2].indexOf(".") + 1, subPage.get(i).split(",")[2].length() - 2))); + } + else if (subPage.get(i).split(",")[2].startsWith("COPY_TO_CLIPBOARD", 5)) { + eventBuilder.setClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, subPage.get(i).split(",")[2].substring(subPage.get(i).split(",")[2].indexOf(".") + 1, subPage.get(i).split(",")[2].length() - 2))); + } + else if (subPage.get(i).split(",")[2].startsWith("OPEN_URL", 5)) { + eventBuilder.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, subPage.get(i).split(",")[2].substring(subPage.get(i).split(",")[2].indexOf(".") + 1, subPage.get(i).split(",")[2].length() - 2))); + } + else if (subPage.get(i).split(",")[2].startsWith("RUN_COMMAND", 5)) { + eventBuilder.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, subPage.get(i).split(",")[2].substring(subPage.get(i).split(",")[2].indexOf(".") + 1, subPage.get(i).split(",")[2].length() - 2))); + } + else if (subPage.get(i).split(",")[2].startsWith("SUGGEST_COMMAND", 5)) { + eventBuilder.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, subPage.get(i).split(",")[2].substring(subPage.get(i).split(",")[2].indexOf(".") + 1, subPage.get(i).split(",")[2].length() - 2))); + } + } + page = new ComponentBuilder(eventBuilder).append(page).create(); + } + } + } + meta.spigot().addPage(page); + } + if(book.setItemMeta(meta)) { return true; + } return false; } @@ -39,5 +100,4 @@ public class Book { public void openBook(Player player) { player.openBook(book); } - } From c37f8ccb6c4b34523fe72d4b8f4598714d2f3f5d Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Sun, 7 Jun 2020 22:41:06 +0200 Subject: [PATCH 2/4] Update Book.java --- src/fr/topeka/HelpRulesBook/Book.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/fr/topeka/HelpRulesBook/Book.java b/src/fr/topeka/HelpRulesBook/Book.java index d67c2bd..1f1075c 100644 --- a/src/fr/topeka/HelpRulesBook/Book.java +++ b/src/fr/topeka/HelpRulesBook/Book.java @@ -31,7 +31,7 @@ public class Book { meta.setTitle(_title); meta.setAuthor(_author); int lastCaracter = 0; - BaseComponent[] page = new ComponentBuilder().create(); + BaseComponent[] page = new ComponentBuilder("").create(); List subPage = new ArrayList(); for (String rawPage: _pages){ if (rawPage.contains("{") && rawPage.contains("}")) { @@ -66,9 +66,6 @@ public class Book { if (subPage.get(i).split(",")[2].startsWith("CHANGE_PAGE", 5)) { eventBuilder.setClickEvent(new ClickEvent(ClickEvent.Action.CHANGE_PAGE, subPage.get(i).split(",")[2].substring(subPage.get(i).split(",")[2].indexOf(".") + 1, subPage.get(i).split(",")[2].length() - 2))); } - else if (subPage.get(i).split(",")[2].startsWith("COPY_TO_CLIPBOARD", 5)) { - eventBuilder.setClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, subPage.get(i).split(",")[2].substring(subPage.get(i).split(",")[2].indexOf(".") + 1, subPage.get(i).split(",")[2].length() - 2))); - } else if (subPage.get(i).split(",")[2].startsWith("OPEN_URL", 5)) { eventBuilder.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, subPage.get(i).split(",")[2].substring(subPage.get(i).split(",")[2].indexOf(".") + 1, subPage.get(i).split(",")[2].length() - 2))); } From 5e47d3c0674d10b7ebb75a76cd563e48fd83736f Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Mon, 8 Jun 2020 00:44:34 +0200 Subject: [PATCH 3/4] Updated Book.java - Still WIP --- src/fr/topeka/HelpRulesBook/Book.java | 82 ++++++++++++--------------- 1 file changed, 35 insertions(+), 47 deletions(-) diff --git a/src/fr/topeka/HelpRulesBook/Book.java b/src/fr/topeka/HelpRulesBook/Book.java index 1f1075c..a7a592d 100644 --- a/src/fr/topeka/HelpRulesBook/Book.java +++ b/src/fr/topeka/HelpRulesBook/Book.java @@ -1,6 +1,5 @@ package fr.topeka.HelpRulesBook; -import java.util.ArrayList; import java.util.List; import org.bukkit.Material; @@ -8,7 +7,6 @@ import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.BookMeta; -import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.ClickEvent; import net.md_5.bungee.api.chat.ComponentBuilder; import net.md_5.bungee.api.chat.HoverEvent; @@ -30,57 +28,47 @@ public class Book { BookMeta meta = (BookMeta) book.getItemMeta(); meta.setTitle(_title); meta.setAuthor(_author); - int lastCaracter = 0; - BaseComponent[] page = new ComponentBuilder("").create(); - List subPage = new ArrayList(); + ComponentBuilder page = new ComponentBuilder(""); for (String rawPage: _pages){ + rawPage = rawPage.replace("&", "§"); if (rawPage.contains("{") && rawPage.contains("}")) { - while (rawPage.length() != 0) { - if (!rawPage.substring(rawPage.indexOf("{", lastCaracter)-1, rawPage.indexOf("{", lastCaracter)+1).contains("\\")) { - subPage.add(rawPage.substring(0, rawPage.indexOf("{", lastCaracter))); - rawPage = rawPage.substring(rawPage.indexOf("{", lastCaracter), rawPage.length()); - while (!rawPage.substring(rawPage.indexOf("}", lastCaracter)-1, rawPage.indexOf("}", lastCaracter)+1).contains("\\")) { - lastCaracter = rawPage.indexOf("}", lastCaracter)+1; + String[] sections = rawPage.split("\\{|\\}"); + boolean edit = false; + TextComponent message = new TextComponent(); + for(String section : sections) { + if(!edit) { + message.addExtra(message); + }else { + String[] elements = section.split(","); + TextComponent eventBuilder = new TextComponent(elements[0]); + if(!elements[1].equals("")) { + eventBuilder.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(elements[1]).create())); + } + if(elements[2].startsWith("true")) { + if(elements[2].startsWith("OPEN_URL", 5)) { + eventBuilder.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, elements[2].substring(elements[2].indexOf(".") + 1, elements[2].length() - 2))); + } + if(elements[2].startsWith("RUN_COMMAND", 5)) { + eventBuilder.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, elements[2].substring(elements[2].indexOf(".") + 1, elements[2].length() - 2))); + + } + if(elements[2].startsWith("SUGGEST_COMMAND", 5)) { + eventBuilder.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, elements[2].substring(elements[2].indexOf(".") + 1, elements[2].length() - 2))); + + } + if(elements[2].startsWith("CHANGE_PAGE", 5)) { + eventBuilder.setClickEvent(new ClickEvent(ClickEvent.Action.CHANGE_PAGE, elements[2].substring(elements[2].indexOf(".") + 1, elements[2].length() - 2))); + + } + page.append(eventBuilder); } - subPage.add(rawPage.substring(0, rawPage.indexOf("}", lastCaracter))); - rawPage = rawPage.substring(rawPage.indexOf("}", lastCaracter), rawPage.length()); - } - else if (rawPage.substring(rawPage.indexOf("{", lastCaracter)-1, rawPage.indexOf("{", lastCaracter)+1).contains("\\")) { - lastCaracter = rawPage.indexOf("{", lastCaracter); - } - else { - subPage.add(rawPage.replace("&", "§")); - rawPage = ""; - } - } - for (int i = subPage.size()-1; i != 0; i--) { - if (!subPage.get(i).startsWith("{")) { - page = new ComponentBuilder(subPage.get(i)).append(page).create(); - } - else if (subPage.get(i).startsWith("{")) { - TextComponent eventBuilder = new TextComponent(subPage.get(i).split(",")[0].substring(1)); - if (!subPage.get(i).split(",")[1].equals("")) { - eventBuilder.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(subPage.get(i).split(",")[1]).create())); - } - if(subPage.get(i).split(",")[2].startsWith("true")) { - if (subPage.get(i).split(",")[2].startsWith("CHANGE_PAGE", 5)) { - eventBuilder.setClickEvent(new ClickEvent(ClickEvent.Action.CHANGE_PAGE, subPage.get(i).split(",")[2].substring(subPage.get(i).split(",")[2].indexOf(".") + 1, subPage.get(i).split(",")[2].length() - 2))); - } - else if (subPage.get(i).split(",")[2].startsWith("OPEN_URL", 5)) { - eventBuilder.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, subPage.get(i).split(",")[2].substring(subPage.get(i).split(",")[2].indexOf(".") + 1, subPage.get(i).split(",")[2].length() - 2))); - } - else if (subPage.get(i).split(",")[2].startsWith("RUN_COMMAND", 5)) { - eventBuilder.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, subPage.get(i).split(",")[2].substring(subPage.get(i).split(",")[2].indexOf(".") + 1, subPage.get(i).split(",")[2].length() - 2))); - } - else if (subPage.get(i).split(",")[2].startsWith("SUGGEST_COMMAND", 5)) { - eventBuilder.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, subPage.get(i).split(",")[2].substring(subPage.get(i).split(",")[2].indexOf(".") + 1, subPage.get(i).split(",")[2].length() - 2))); - } - } - page = new ComponentBuilder(eventBuilder).append(page).create(); } + edit = !edit; } + }else { + page.append(rawPage); } - meta.spigot().addPage(page); + meta.spigot().addPage(page.create()); } if(book.setItemMeta(meta)) { return true; From d56fc7d192ae88b7be16c4d139c7aaf6c818208a Mon Sep 17 00:00:00 2001 From: Quentin Legot Date: Mon, 8 Jun 2020 11:31:01 +0200 Subject: [PATCH 4/4] ClickEvent now work --- config.yml | 7 +++++-- src/fr/topeka/HelpRulesBook/Book.java | 18 ++++++++++-------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/config.yml b/config.yml index 5410d25..172efc3 100644 --- a/config.yml +++ b/config.yml @@ -11,7 +11,7 @@ book: # he would open page 4 # if she exist #note that if you want to write an { or } in your text put a \ before - #colors are also supported with &, help available on https://minecraft.tools/en/color-code.php + #colors are also supported with & and §, help available on https://minecraft.tools/en/color-code.php - | Premiere page C'est beau tu trouve pas ? @@ -26,4 +26,7 @@ book: This is an example if a player click {here,he would see this message by hovering,true(CHANGE_PAGE.1)} he would open page 1 - if she exist \ No newline at end of file + if she exist + {Link,he would see this message by hovering,true(OPEN_URL.https://spigotmc.org)} + {Command,he would see this message by hovering,true(RUN_COMMAND./say salut)} + {Suggest,he would see this message by hovering,true(SUGGEST_COMMAND./say salut)} diff --git a/src/fr/topeka/HelpRulesBook/Book.java b/src/fr/topeka/HelpRulesBook/Book.java index a7a592d..9bcb18a 100644 --- a/src/fr/topeka/HelpRulesBook/Book.java +++ b/src/fr/topeka/HelpRulesBook/Book.java @@ -28,16 +28,16 @@ public class Book { BookMeta meta = (BookMeta) book.getItemMeta(); meta.setTitle(_title); meta.setAuthor(_author); - ComponentBuilder page = new ComponentBuilder(""); for (String rawPage: _pages){ - rawPage = rawPage.replace("&", "§"); + ComponentBuilder page = new ComponentBuilder(""); + rawPage = rawPage.replace("&", "§"); if (rawPage.contains("{") && rawPage.contains("}")) { String[] sections = rawPage.split("\\{|\\}"); boolean edit = false; TextComponent message = new TextComponent(); for(String section : sections) { if(!edit) { - message.addExtra(message); + message.addExtra(section); }else { String[] elements = section.split(","); TextComponent eventBuilder = new TextComponent(elements[0]); @@ -45,26 +45,28 @@ public class Book { eventBuilder.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(elements[1]).create())); } if(elements[2].startsWith("true")) { +// System.out.println(elements[2].substring(5, elements[2].length() - 1)); if(elements[2].startsWith("OPEN_URL", 5)) { - eventBuilder.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, elements[2].substring(elements[2].indexOf(".") + 1, elements[2].length() - 2))); + eventBuilder.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, elements[2].substring(elements[2].indexOf(".") + 1, elements[2].length() - 1))); } if(elements[2].startsWith("RUN_COMMAND", 5)) { - eventBuilder.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, elements[2].substring(elements[2].indexOf(".") + 1, elements[2].length() - 2))); + eventBuilder.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, elements[2].substring(elements[2].indexOf(".") + 1, elements[2].length() - 1))); } if(elements[2].startsWith("SUGGEST_COMMAND", 5)) { - eventBuilder.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, elements[2].substring(elements[2].indexOf(".") + 1, elements[2].length() - 2))); + eventBuilder.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, elements[2].substring(elements[2].indexOf(".") + 1, elements[2].length() - 1))); } if(elements[2].startsWith("CHANGE_PAGE", 5)) { - eventBuilder.setClickEvent(new ClickEvent(ClickEvent.Action.CHANGE_PAGE, elements[2].substring(elements[2].indexOf(".") + 1, elements[2].length() - 2))); + eventBuilder.setClickEvent(new ClickEvent(ClickEvent.Action.CHANGE_PAGE, elements[2].substring(elements[2].indexOf(".") + 1, elements[2].length() - 1))); } - page.append(eventBuilder); } + message.addExtra(eventBuilder); } edit = !edit; } + page.append(message); }else { page.append(rawPage); }