Advertisement
WayKillerZ

MerchantConversation

Jun 15th, 2021
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. package me.raptor.merchant;
  2.  
  3. import java.util.List;
  4. import java.util.Random;
  5.  
  6. import org.bukkit.Bukkit;
  7. import org.bukkit.ChatColor;
  8. import org.bukkit.entity.Entity;
  9. import org.bukkit.entity.LivingEntity;
  10. import org.bukkit.entity.Player;
  11. import org.bukkit.scheduler.BukkitRunnable;
  12.  
  13. import me.raptor.main.Main;
  14.  
  15. public class MerchantConversation {
  16.  
  17. static Main main;
  18. static Random r = new Random();
  19.  
  20. public MerchantConversation(Main main) {
  21. MerchantConversation.main = main;
  22. }
  23.  
  24. public String getDialogue(String path) {
  25. return main.getCustomDialogueConfig().get(path).toString();
  26. }
  27.  
  28. public void setDialogue(String dialogue, String path) {
  29. main.getCustomDialogueConfig().set(path, dialogue);
  30. main.saveData();
  31. }
  32.  
  33. //Make the entity chat when player is in range. Dialogues and sounds can be found in dialogue.yml.
  34. //Warning: The amount of sounds and dialogues must be the same
  35. public static void activateSpeakingAbility(LivingEntity v, String path, int interval, int range, String prefix) {
  36. if (randomIndex(path + ".dialogue") == - 1) return; // cancel if no message was found
  37. new BukkitRunnable() {
  38. @Override
  39. public void run() {
  40. if (v == null || v.isDead()) {
  41. cancel();
  42. return;
  43. }
  44. if (v.isSleeping()) return;
  45. try {
  46. speakDialogue(v, range, prefix, path);
  47. } catch (IndexOutOfBoundsException e) {
  48. Bukkit.getServer().broadcastMessage("Error: " + path);
  49. e.printStackTrace();
  50. }
  51. }
  52. }.runTaskTimer(main, 0, 20 * interval);
  53. }
  54.  
  55. //Make the entity play sound when player is in range. Sounds can be found in dialogue.yml
  56. public static void activateSoundAbility(LivingEntity v, String path, int interval) {
  57. if (randomIndex(path + ".sounds") == - 1) return; // cancel if no sound was found
  58. new BukkitRunnable() {
  59. @Override
  60. public void run() {
  61. if (v == null || v.isDead()) {
  62. cancel();
  63. return;
  64. }
  65. v.getWorld().playSound(v.getLocation(), getData(path + ".sounds", randomIndex(path + ".sounds")), 3, 1);
  66. }
  67. }.runTaskTimer(main, 0, 20 * interval);
  68. }
  69.  
  70. //Send a message to all players in range
  71. public static void speakDialogue(LivingEntity v, int range, String prefix, String path) {
  72. for (Entity e : v.getNearbyEntities(range, range, range)) {
  73. if (e instanceof Player) {
  74. Player p = (Player) e;
  75. int index = randomIndex(path + ".dialogue");
  76. if (index == -1) return;
  77. // If the player want to see the merchant's dialogue
  78. if (checkWantToHear(p, p.getName() + ".enableMerchantChat")) {
  79. p.sendMessage(prefix + getData(path + ".dialogue", index));
  80. if (getData(path + ".sounds", index) == null) return;
  81. p.playSound(p.getLocation(), getData(path + ".sounds", index), 7, 1);
  82. }
  83. }
  84. }
  85. }
  86.  
  87. //Send a message and sound to all player in range
  88. public static void speak(LivingEntity v, int range, String prefix, String path) {
  89. for (Entity e : v.getNearbyEntities(range, range, range)) {
  90. if (e instanceof Player) {
  91. Player p = (Player) e;
  92. int index = randomIndex(path);
  93. if (index == -1) return;
  94. p.sendMessage(prefix + getData(path, index));
  95. }
  96. }
  97. }
  98.  
  99. //choose random message from the given path in dialogue.yml
  100. public static int randomIndex(String path) {
  101. List<String> messages = main.getCustomDialogueConfig().getStringList(path);
  102. if (messages.size() == 0) return -1;
  103. return r.nextInt(messages.size());
  104. }
  105.  
  106. //get a data from the dialogue config with the given path and index
  107. public static String getData(String path, int index) {
  108. List<String> messages = main.getCustomDialogueConfig().getStringList(path);
  109. if (index > messages.size() - 1) return null;
  110. return colored(messages.get(index));
  111. }
  112.  
  113. //choose a random name from name.yml with given path
  114. public static String chooseRandomName(String path) {
  115. List<String> messages = main.getCustomNameConfig().getStringList(path + ".names");
  116. if (messages.size() == 0) return "";
  117. return colored(main.getCustomNameConfig().getString(path + ".format").replace
  118. ("<NAME>",
  119. messages.get(r.nextInt(messages.size()))
  120. ));
  121. }
  122.  
  123. //transform '&' into color codes
  124. public static String colored(String s) {
  125. return ChatColor.translateAlternateColorCodes('&', s);
  126. }
  127.  
  128. //get the boolean in the given path. Toggle command: /merchant chat on/off
  129. public static boolean checkWantToHear(Player p, String path) {
  130. if (!main.getConfig().contains(p.getName() + ".enableMerchantChat")) {
  131. main.getConfig().set(p.getName() + ".enableMerchantChat", true);
  132. main.saveConfig();
  133. return true;
  134. }
  135. return main.getConfig().getBoolean(path);
  136. }
  137.  
  138. }
  139.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement