From 9f18b41925ad3165d033b79a7022e473ed093354 Mon Sep 17 00:00:00 2001 From: Teriuihi Date: Tue, 9 Jul 2024 18:32:50 +0200 Subject: [PATCH] Update build configurations and add villager messaging functionality This commit updates the version of the shadow plugin and the Java language version in the build configuration. Modifications have been made in the shadowJar configuration. The main feature in this commit is the introduction of a messaging system for villagers. This includes the necessary methods and fields in the existing villager classes and the addition of a new VillagerMessagesConfig class. Messages can now be added via config and each villager type can potentially return a random message when interacted with. A sound effect has also been added to correlate with the villager type. The WorthConfig class has been additionally refined to handle invalid material cases. --- build.gradle.kts | 20 ++++--- src/main/java/com/alttd/config/Config.java | 1 + .../alttd/config/VillagerMessagesConfig.java | 52 +++++++++++++++++++ .../java/com/alttd/config/WorthConfig.java | 4 ++ .../java/com/alttd/events/VillagerEvents.java | 23 ++++++++ .../objects/BlackMarketVillagerType.java | 26 ++++++++++ .../com/alttd/objects/ShopVillagerType.java | 26 +++++++++- .../java/com/alttd/objects/VillagerType.java | 9 ++++ 8 files changed, 148 insertions(+), 13 deletions(-) create mode 100644 src/main/java/com/alttd/config/VillagerMessagesConfig.java diff --git a/build.gradle.kts b/build.gradle.kts index 697b1cb..070a606 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -2,7 +2,7 @@ import com.github.jengelman.gradle.plugins.shadow.tasks.ConfigureShadowRelocatio plugins { id("java") - id("com.github.johnrengelman.shadow") version "7.1.0" + id("com.github.johnrengelman.shadow") version "7.1.1" id("maven-publish") } @@ -14,7 +14,7 @@ apply() java { toolchain { - languageVersion.set(JavaLanguageVersion.of(17)) + languageVersion.set(JavaLanguageVersion.of(21)) } } @@ -44,24 +44,22 @@ tasks { } shadowJar { - dependsOn(getByName("relocateJars") as ConfigureShadowRelocation) - archiveFileName.set("${project.name}-${project.version}.jar") - minimize() - configurations = listOf(project.configurations.shadow.get()) + archiveFileName.set("${rootProject.name}.jar") + manifest { + attributes("Main-Class" to "VillagerUI") + } } build { dependsOn(shadowJar) } - create("relocateJars") { - target = shadowJar.get() - prefix = "${project.name}.lib" - } } dependencies { - compileOnly("com.alttd:Galaxy-API:1.19-R0.1-SNAPSHOT") + compileOnly("com.alttd:Galaxy-API:1.21-R0.1-SNAPSHOT") { + isChanging = true + } compileOnly("com.github.milkbowl:VaultAPI:1.7") { exclude("org.bukkit","bukkit") } diff --git a/src/main/java/com/alttd/config/Config.java b/src/main/java/com/alttd/config/Config.java index eb7476c..1cc0df0 100644 --- a/src/main/java/com/alttd/config/Config.java +++ b/src/main/java/com/alttd/config/Config.java @@ -203,6 +203,7 @@ public final class Config extends AbstractConfig { ); } }); + VillagerMessagesConfig.reload(); } private static TreeSet loadProducts(ConfigurationSection productsSection) { diff --git a/src/main/java/com/alttd/config/VillagerMessagesConfig.java b/src/main/java/com/alttd/config/VillagerMessagesConfig.java new file mode 100644 index 0000000..f63ef38 --- /dev/null +++ b/src/main/java/com/alttd/config/VillagerMessagesConfig.java @@ -0,0 +1,52 @@ +package com.alttd.config; + +import com.alttd.objects.VillagerType; +import com.alttd.objects.VillagerTypeManager; +import com.alttd.util.Logger; +import org.bukkit.configuration.ConfigurationSection; + +import java.io.File; +import java.util.List; +import java.util.Set; + +public class VillagerMessagesConfig extends AbstractConfig { + static VillagerMessagesConfig config; + static int version; + + protected VillagerMessagesConfig() { + super(new File(System.getProperty("user.home") + File.separator + "share" + File.separator + "configs" + File.separator + "VillagerShopUI"), "villager-messages.yml"); + } + + protected static void reload() { + config = new VillagerMessagesConfig(); + + version = config.getInt("config-version", 1); + config.set("config-version", 1); + + config.readConfig(VillagerMessagesConfig.class, null); + } + + private static void loadVillagerMessages() { + Logger.info("Reloading VillagerMessages config..."); + ConfigurationSection configurationSection = config.getConfigurationSection("messages"); + if (configurationSection == null) { + Logger.warning("No villager messages found in config."); + config.getStringList("messages.example-tag", List.of("This is an example")); + return; + } + + Set keys = configurationSection.getKeys(false); + if (keys.isEmpty()) { + Logger.warning("No villager types found in config."); + } + + keys.forEach(key -> { + VillagerType villagerType = VillagerTypeManager.getVillagerType(key); + if (villagerType == null) { + Logger.info("Unknown villager type: " + key); + return; + } + villagerType.setMessages(configurationSection.getStringList(key)); + }); + } +} diff --git a/src/main/java/com/alttd/config/WorthConfig.java b/src/main/java/com/alttd/config/WorthConfig.java index e2f7dcf..f56204a 100644 --- a/src/main/java/com/alttd/config/WorthConfig.java +++ b/src/main/java/com/alttd/config/WorthConfig.java @@ -87,6 +87,10 @@ public class WorthConfig extends AbstractConfig { Logger.warning("Invalid material % in trade worth", key); continue; } + if (material == null) { + Logger.warning("Invalid material % in trade worth", key); + continue; + } map.put(material, new Price(Utilities.round(worth.getDouble(key), 2), material)); } } diff --git a/src/main/java/com/alttd/events/VillagerEvents.java b/src/main/java/com/alttd/events/VillagerEvents.java index bc4fab5..414fb10 100644 --- a/src/main/java/com/alttd/events/VillagerEvents.java +++ b/src/main/java/com/alttd/events/VillagerEvents.java @@ -5,7 +5,9 @@ import com.alttd.GUI.windows.TradeGUI; import com.alttd.VillagerUI; import com.alttd.config.Config; import com.alttd.config.VillagerConfig; +import com.alttd.galaxy.event.player.PlayerInteractOnEntityEvent; import com.alttd.objects.*; +import org.bukkit.Sound; import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; import org.bukkit.entity.Villager; @@ -65,6 +67,27 @@ public class VillagerEvents implements Listener { }.runTaskAsynchronously(VillagerUI.getInstance()); } + @EventHandler + public void onPlayerInteractOnEntity(PlayerInteractOnEntityEvent event) { + Player player = event.getPlayer(); + + if (!(event.getEntity() instanceof Villager villager)) { + return; + } + + VillagerType loadedVillager = LoadedVillagers.getLoadedVillager(villager.getUniqueId()); + if (loadedVillager == null) { + return; + } + + loadedVillager.getRandomMessage().ifPresent(player::sendMessage); + if (loadedVillager instanceof BlackMarketVillagerType) { + player.playSound(player.getLocation(), Sound.ENTITY_VILLAGER_HURT, 1.0F, 1.0F); + } else if (loadedVillager instanceof ShopVillagerType) { + player.playSound(player.getLocation(), Sound.ENTITY_VILLAGER_YES, 1.0F, 1.0F); + } + } + @EventHandler public void onVillagerDeath(EntityDeathEvent event) { if (!event.getEntityType().equals(EntityType.VILLAGER)) diff --git a/src/main/java/com/alttd/objects/BlackMarketVillagerType.java b/src/main/java/com/alttd/objects/BlackMarketVillagerType.java index 09f2abd..2190a1a 100644 --- a/src/main/java/com/alttd/objects/BlackMarketVillagerType.java +++ b/src/main/java/com/alttd/objects/BlackMarketVillagerType.java @@ -1,5 +1,7 @@ package com.alttd.objects; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.minimessage.MiniMessage; import org.bukkit.entity.Villager; import org.bukkit.inventory.ItemStack; @@ -15,6 +17,9 @@ public class BlackMarketVillagerType implements VillagerType { private final int maxTradesPerReboot; private final HashMap> playerTrades = new HashMap<>(); private final HashMap playerTradeCount = new HashMap<>(); + private final List messages = new ArrayList<>(); + + private final Random random = new Random(); public BlackMarketVillagerType(String name, String displayName, String profession, int maxAvailableItems, int maxTradesPerReboot, Set trading) { this.name = name; @@ -85,4 +90,25 @@ public class BlackMarketVillagerType implements VillagerType { public String getPermission() { return "villagerui.villager." + getName(); } + + @Override + public Optional getRandomMessage() { + if (messages.isEmpty()) { + return Optional.empty(); + } + int index = random.nextInt(messages.size()); + String message = messages.get(index); + return Optional.of(MiniMessage.miniMessage().deserialize(message)); + } + + @Override + public void addMessage(String message) { + messages.add(message); + } + + @Override + public void setMessages(List messages) { + this.messages.clear(); + this.messages.addAll(messages); + } } diff --git a/src/main/java/com/alttd/objects/ShopVillagerType.java b/src/main/java/com/alttd/objects/ShopVillagerType.java index da2be58..2a73761 100644 --- a/src/main/java/com/alttd/objects/ShopVillagerType.java +++ b/src/main/java/com/alttd/objects/ShopVillagerType.java @@ -1,10 +1,11 @@ package com.alttd.objects; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.minimessage.MiniMessage; import org.bukkit.entity.Villager; import org.bukkit.inventory.ItemStack; -import java.util.Set; -import java.util.TreeSet; +import java.util.*; public class ShopVillagerType implements VillagerType{ private final String name; @@ -12,6 +13,9 @@ public class ShopVillagerType implements VillagerType{ private final Set buying; private final Set selling; private final Villager.Profession profession; + private final List messages = new ArrayList<>(); + + private final Random random = new Random(); public ShopVillagerType(String name, String displayName, TreeSet buying, TreeSet selling, String profession) { this.name = name; @@ -44,4 +48,22 @@ public class ShopVillagerType implements VillagerType{ public String getPermission() { return "villagerui.villager." + getName(); } + + public Optional getRandomMessage() { + if (messages.isEmpty()) { + return Optional.empty(); + } + int index = random.nextInt(messages.size()); + String message = messages.get(index); + return Optional.of(MiniMessage.miniMessage().deserialize(message)); + } + + public void addMessage(String message) { + messages.add(message); + } + + public void setMessages(List messages) { + this.messages.clear(); + this.messages.addAll(messages); + } } diff --git a/src/main/java/com/alttd/objects/VillagerType.java b/src/main/java/com/alttd/objects/VillagerType.java index 85afaee..d479027 100644 --- a/src/main/java/com/alttd/objects/VillagerType.java +++ b/src/main/java/com/alttd/objects/VillagerType.java @@ -1,8 +1,11 @@ package com.alttd.objects; +import net.kyori.adventure.text.Component; import org.bukkit.entity.Villager; import org.bukkit.inventory.ItemStack; +import java.util.List; +import java.util.Optional; import java.util.Set; public interface VillagerType { @@ -18,4 +21,10 @@ public interface VillagerType { Villager.Profession getProfession(); String getPermission(); + + Optional getRandomMessage(); + + void addMessage(String message); + + void setMessages(List messages); }