From 2e0de08aa843a0281010878ab0ffeeb74bd40c3f Mon Sep 17 00:00:00 2001 From: Dylan Xaldin Date: Sat, 3 Oct 2020 21:26:56 -0500 Subject: [PATCH] Add MiniMessage support (via Kyori/Adventure) This adds MiniMessage support to the entire configuration file, allowing you to easily specify colors and do other cool features like gradients and full hex support. Refer to https://docs.adventure.kyori.net/minimessage.html for documentation on the syntax. Using ampersand is still supported for users who prefer that, or have old configuration files. --- pom.xml | 56 ++++++++++++++++++- .../interestingfish/FishInfoFactory.java | 13 +++-- .../config/InterestingConfig.java | 37 +++++++++--- .../listeners/FishListener.java | 13 +---- src/main/resources/plugin.yml | 2 +- 5 files changed, 96 insertions(+), 25 deletions(-) diff --git a/pom.xml b/pom.xml index 33c5f9b..8d963cd 100644 --- a/pom.xml +++ b/pom.xml @@ -6,27 +6,48 @@ net.shotbow interestingfish InterestingFish - 1.16.2 + + 1.16.3 jar UTF-8 + org.spigotmc spigot-api - 1.16.2-R0.1-SNAPSHOT + 1.16.3-R0.1-SNAPSHOT provided + + + net.kyori + adventure-platform-bukkit + 4.0.0-SNAPSHOT + compile + + + + net.kyori + adventure-text-minimessage + 4.0.0-SNAPSHOT + compile + spigot-repo https://hub.spigotmc.org/nexus/content/repositories/snapshots/ + + sonatype-oss + https://oss.sonatype.org/content/repositories/snapshots/ + ${project.name} + clean package @@ -38,10 +59,41 @@ 1.8 + + org.apache.maven.plugins + maven-shade-plugin + 3.2.4 + + true + + + net.kyori.adventure + net.shotbow.interestingfish.lib.net.kyori.adventure + + + net.kyori.examination + net.shotbow.interestingfish.lib.net.kyori.examination + + + org.checkerframework + net.shotbow.interestingfish.lib.org.checkerframework + + + + + + package + + shade + + + + true + src/main/resources diff --git a/src/main/java/net/shotbow/interestingfish/FishInfoFactory.java b/src/main/java/net/shotbow/interestingfish/FishInfoFactory.java index b8aa650..2d5bbcc 100644 --- a/src/main/java/net/shotbow/interestingfish/FishInfoFactory.java +++ b/src/main/java/net/shotbow/interestingfish/FishInfoFactory.java @@ -1,5 +1,7 @@ package net.shotbow.interestingfish; +import net.kyori.adventure.text.minimessage.MiniMessage; +import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.shotbow.interestingfish.config.InterestingConfig; import net.shotbow.interestingfish.objects.Breed; import net.shotbow.interestingfish.objects.Descriptor; @@ -29,6 +31,9 @@ public class FishInfoFactory private Random random = new Random(); private InterestingConfig config; + private final MiniMessage miniMessage = MiniMessage.get(); + private final LegacyComponentSerializer legacyHexSerializer = LegacyComponentSerializer.builder().hexColors().useUnusualXRepeatedCharacterHexFormat().build(); + public FishInfoFactory(InterestingConfig config) { this.config = config; @@ -55,20 +60,20 @@ public FishInfo makeNewFishInfo() if (random.nextInt(100) < config.percentDescriptorChance) { Descriptor descriptor = descriptorIndex.get(weightedDescriptors.get(random.nextInt(weightedDescriptors.size()))); - nameBuilder.append(replaceColorChars(descriptor.getText())).append(" "); + nameBuilder.append(parseColors(descriptor.getText())).append(ChatColor.RESET).append(" "); weight += descriptor.getMinWeightModifier() + random.nextDouble() * descriptor.getMaxWeightModifier() - descriptor.getMinWeightModifier(); } Breed breed = breedIndex.get(weightedBreeds.get(random.nextInt(weightedBreeds.size()))); weight += breed.getMinWeightModifier() + random.nextDouble() * breed.getMaxWeightModifier() - breed.getMinWeightModifier(); - nameBuilder.append(replaceColorChars(breed.getText())); + nameBuilder.append(parseColors(breed.getText())); if (weight < config.minWeight) weight = config.minWeight; return new FishInfo(nameBuilder.toString(), weight); } - private String replaceColorChars(String text) + public String parseColors(String text) { - return text.replace('&', ChatColor.COLOR_CHAR); + return legacyHexSerializer.serialize(miniMessage.parse(text.replace('&', ChatColor.COLOR_CHAR))); } } diff --git a/src/main/java/net/shotbow/interestingfish/config/InterestingConfig.java b/src/main/java/net/shotbow/interestingfish/config/InterestingConfig.java index 7a0046b..93ab936 100644 --- a/src/main/java/net/shotbow/interestingfish/config/InterestingConfig.java +++ b/src/main/java/net/shotbow/interestingfish/config/InterestingConfig.java @@ -24,12 +24,12 @@ public class InterestingConfig extends ConfigObject public boolean requireLuckEnchant = false; public boolean excludeLuckEnchant = false; public boolean showItemFrameInfo = true; - public String weightLabel = "&bWeight: &f"; + public String weightLabel = "Weight: "; public String weightUnit = "lbs"; - public String caughtByLabel = "&6Caught by: &f"; + public String caughtByLabel = "Caught by: "; public boolean caughtByNickname = false; - public String dateLabel = "&6"; - public String dateFormat = "MM/dd/yy hh:mm"; + public String dateLabel = ""; + public String dateFormat = "M/d/yy h:mm aa"; public List> descriptorList = new ArrayList>() {{ add(new HashMap() @@ -37,14 +37,35 @@ public class InterestingConfig extends ConfigObject put("minWeightModifier", 5.0); put("maxWeightModifier", 20.0); put("rollWeight", 1); - put("text", "&aBig"); + put("text", "Big"); }}); add(new HashMap() {{ put("minWeightModifier", -20.0); put("maxWeightModifier", 0.0); put("rollWeight", 1); - put("text", "&aTiny"); + put("text", "Tiny"); + }}); + add(new HashMap() + {{ + put("minWeightModifier", 1.0); + put("maxWeightModifier", 100.0); + put("rollWeight", 1); + put("text", "Kyori"); + }}); + add(new HashMap() + {{ + put("minWeightModifier", 1.0); + put("maxWeightModifier", 100.0); + put("rollWeight", 1); + put("text", "Rainbow"); + }}); + add(new HashMap() + {{ + put("minWeightModifier", 1.0); + put("maxWeightModifier", 100.0); + put("rollWeight", 1); + put("text", "Adventurous"); }}); }}; public List> breedsList = new ArrayList>() @@ -54,14 +75,14 @@ public class InterestingConfig extends ConfigObject put("minWeightModifier", 1.0); put("maxWeightModifier", 20.0); put("rollWeight", 1); - put("text", "Trout"); + put("text", "Trout"); }}); add(new HashMap() {{ put("minWeightModifier", 20.0); put("maxWeightModifier", 50.0); put("rollWeight", 1); - put("text", "Tuna"); + put("text", "Tuna"); }}); }}; diff --git a/src/main/java/net/shotbow/interestingfish/listeners/FishListener.java b/src/main/java/net/shotbow/interestingfish/listeners/FishListener.java index add04f8..f0afe7c 100644 --- a/src/main/java/net/shotbow/interestingfish/listeners/FishListener.java +++ b/src/main/java/net/shotbow/interestingfish/listeners/FishListener.java @@ -4,7 +4,6 @@ import net.shotbow.interestingfish.config.InterestingConfig; import net.shotbow.interestingfish.objects.FishInfo; import net.shotbow.interestingfish.utility.ItemUtility; -import org.bukkit.ChatColor; import org.bukkit.Tag; import org.bukkit.entity.Item; import org.bukkit.event.EventHandler; @@ -56,17 +55,11 @@ public void onFish(PlayerFishEvent e) ItemUtility.renameItem(fish, fishInfo.getName()); ItemUtility.setLore(fish, new ArrayList() {{ - add(replaceColorChars(config.weightLabel) + decimalFormat.format(fishInfo.getWeight()) + replaceColorChars(config.weightUnit)); - add(replaceColorChars(config.caughtByLabel) + name); - add(replaceColorChars(config.dateLabel) + dateFormat.format(new Date())); + add(fishInfoFactory.parseColors(config.weightLabel + decimalFormat.format(fishInfo.getWeight()) + config.weightUnit)); + add(fishInfoFactory.parseColors(config.caughtByLabel + name)); + add(fishInfoFactory.parseColors(config.dateLabel + dateFormat.format(new Date()))); }}); } } } - - - private String replaceColorChars(String text) - { - return text.replace('&', ChatColor.COLOR_CHAR); - } } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 7455165..5674bda 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -4,4 +4,4 @@ version: "${project.version}" api-version: "1.13" authors: ["lazertester", "Puremin0rez"] website: "https://github.com/Puremin0rez/InterestingFish" -description: "InterestingFish, gives fishes names and weight." +description: "Allow your players to catch fish with style - adds fancy names, weights and catch information to caught fish. "