From b6be979e8d5e4d5de0b9824f46d456c27db095c7 Mon Sep 17 00:00:00 2001 From: Zeshan Aslam Date: Sat, 10 Aug 2019 01:00:06 -0400 Subject: [PATCH] Created action system and added new placeholder --- config.yml | 23 +++- plugin.yml | 2 +- src/com/zeshanaslam/actionhealth/Main.java | 7 ++ .../actionhealth/action/ActionListener.java | 100 ++++++++++++++++++ .../actionhealth/action/ActionStore.java | 93 ++++++++++++++++ .../actionhealth/action/ActionTask.java | 33 ++++++ .../actionhealth/action/data/Action.java | 12 +++ .../actionhealth/action/data/Tagged.java | 16 +++ .../actionhealth/config/ConfigStore.java | 3 + .../actionhealth/events/HealthListeners.java | 4 + .../actionhealth/utils/HealthUtil.java | 9 +- 11 files changed, 295 insertions(+), 7 deletions(-) create mode 100644 src/com/zeshanaslam/actionhealth/action/ActionListener.java create mode 100644 src/com/zeshanaslam/actionhealth/action/ActionStore.java create mode 100644 src/com/zeshanaslam/actionhealth/action/ActionTask.java create mode 100644 src/com/zeshanaslam/actionhealth/action/data/Action.java create mode 100644 src/com/zeshanaslam/actionhealth/action/data/Tagged.java diff --git a/config.yml b/config.yml index 01874a0..733630c 100644 --- a/config.yml +++ b/config.yml @@ -4,6 +4,7 @@ # {maxhealth} shows the max health of the mob or player. # {usestyle} will use the defined chars. # {displayname} will use player/mob custom name. +# {opponentlastdamage} the amount of damage the enemy last received. # Has support for PlaceholderAPI and MVdWPlaceholderAPI. Health Message: '&7&l{name}: {usestyle}' @@ -98,4 +99,24 @@ Name: # Translate names using Client Language. Need to install LanguageUtils # https://www.spigotmc.org/resources/1-7-x-1-12-language-utils.8859/ -Use Client Language: false \ No newline at end of file +Use Client Language: false + +# If enabled, when a player gets into combat they will be tagged for the configurable amount of time. +# When the enemy triggers an action the provided message will be sent. +# Supports all placeholders from above. +# Supported events: CONSUME, RIGHTCLICK, LEFTCLICK, SWAP +Action: + Enabled: false + # In seconds + TagLength: 20 + # Amount of players a player can tag during combat. -1 for unlimited + TagAmount: 2 + Events: + CONSUME: + GOLDEN_APPLE: '&7&l{name} consumed &cgolden apple&7&l!' + REGENERATION_POTION: '&7&l{name} consumed &cregen potion&7&l!' + RIGHTCLICK: + ENDER_PEARL: '&7&l{name} used &cender pearl&7&l!' + SWAP: + ENDER_PEARL: '&7&l{name} swapped to &cender pearls&7&l. {usestyle}&7&l' + POTION: '&7&l{name} swapped to &cpotion&7&l. {usestyle}&7&l' \ No newline at end of file diff --git a/plugin.yml b/plugin.yml index 06012f0..1659347 100644 --- a/plugin.yml +++ b/plugin.yml @@ -1,6 +1,6 @@ name: ActionHealth main: com.zeshanaslam.actionhealth.Main -version: 3.3.6 +version: 3.4.0 softdepend: [PlaceholderAPI, MVdWPlaceholderAPI, WorldGuard, mcMMO, MythicMobs, LangUtils] commands: Actionhealth: diff --git a/src/com/zeshanaslam/actionhealth/Main.java b/src/com/zeshanaslam/actionhealth/Main.java index 53b25d4..8dd2509 100644 --- a/src/com/zeshanaslam/actionhealth/Main.java +++ b/src/com/zeshanaslam/actionhealth/Main.java @@ -1,6 +1,8 @@ package com.zeshanaslam.actionhealth; import com.sk89q.worldguard.bukkit.WorldGuardPlugin; +import com.zeshanaslam.actionhealth.action.ActionListener; +import com.zeshanaslam.actionhealth.action.ActionTask; import com.zeshanaslam.actionhealth.commands.HealthCommand; import com.zeshanaslam.actionhealth.config.ConfigStore; import com.zeshanaslam.actionhealth.events.HealthListeners; @@ -8,6 +10,7 @@ import com.zeshanaslam.actionhealth.support.WorldGuardAPI; import com.zeshanaslam.actionhealth.utils.HealthUtil; import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.scheduler.BukkitTask; import java.io.File; import java.util.ArrayList; @@ -24,6 +27,7 @@ public class Main extends JavaPlugin { public boolean mcMMOEnabled; public boolean mythicMobsEnabled; public boolean langUtilsEnabled; + public BukkitTask actionTask; public List toggle = new ArrayList<>(); @@ -43,6 +47,7 @@ public class Main extends JavaPlugin { // Register listeners getServer().getPluginManager().registerEvents(new HealthListeners(this), this); + getServer().getPluginManager().registerEvents(new ActionListener(this), this); // Register commands getCommand("Actionhealth").setExecutor(new HealthCommand(this)); @@ -63,6 +68,8 @@ public class Main extends JavaPlugin { if (Bukkit.getServer().getPluginManager().isPluginEnabled("LangUtils")) { langUtilsEnabled = true; } + + actionTask = new ActionTask(this).runTaskTimer(this, 0, 20); } @Override diff --git a/src/com/zeshanaslam/actionhealth/action/ActionListener.java b/src/com/zeshanaslam/actionhealth/action/ActionListener.java new file mode 100644 index 0000000..0f38293 --- /dev/null +++ b/src/com/zeshanaslam/actionhealth/action/ActionListener.java @@ -0,0 +1,100 @@ +package com.zeshanaslam.actionhealth.action; + +import com.zeshanaslam.actionhealth.Main; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; +import org.bukkit.event.entity.EntityDamageByEntityEvent; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.event.player.PlayerItemConsumeEvent; +import org.bukkit.event.player.PlayerItemHeldEvent; +import org.bukkit.inventory.ItemStack; +import org.bukkit.potion.Potion; + +import java.util.ArrayList; +import java.util.List; + +public class ActionListener implements Listener { + + private final Main main; + + public ActionListener(Main main) { + this.main = main; + } + + @EventHandler(ignoreCancelled = true) + public void onComabat(EntityDamageByEntityEvent event) { + if (!main.configStore.actionStore.enabled) + return; + + if (event.getDamager() instanceof Player && event.getEntity() instanceof Player) { + Player damager = (Player) event.getDamager(); + Player damaged = (Player) event.getEntity(); + + main.configStore.actionStore.addTag(damager.getUniqueId(), damaged.getUniqueId()); + } + } + + @EventHandler(ignoreCancelled = true) + public void onConsume(PlayerItemConsumeEvent event) { + if (!main.configStore.actionStore.enabled) + return; + + ActionStore.ActionType actionType = ActionStore.ActionType.CONSUME; + Player player = event.getPlayer(); + + executeTriggers(actionType, player, event.getItem()); + } + + @EventHandler(ignoreCancelled = true) + public void onSwap(PlayerItemHeldEvent event) { + if (!main.configStore.actionStore.enabled) + return; + + ActionStore.ActionType actionType = ActionStore.ActionType.SWAP; + Player player = event.getPlayer(); + + ItemStack itemStack = player.getInventory().getItem(event.getNewSlot()); + + executeTriggers(actionType, player, itemStack); + } + + @EventHandler + public void onInteract(PlayerInteractEvent event) { + if (!main.configStore.actionStore.enabled) + return; + + Player player = event.getPlayer(); + ItemStack itemStack = event.getItem(); + + if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) { + ActionStore.ActionType actionType = ActionStore.ActionType.RIGHTCLICK; + + executeTriggers(actionType, player, itemStack); + } else if (event.getAction() == Action.LEFT_CLICK_AIR || event.getAction() == Action.LEFT_CLICK_BLOCK) { + ActionStore.ActionType actionType = ActionStore.ActionType.LEFTCLICK; + executeTriggers(actionType, player, itemStack); + } + } + + private void executeTriggers(ActionStore.ActionType actionType, Player player, ItemStack itemStack) { + if (itemStack != null) { + for (String name: getName(itemStack)) + main.configStore.actionStore.triggerAction(actionType, player, name); + } + } + + private List getName(ItemStack itemStack) { + List possibleMaterials = new ArrayList<>(); + + String name = itemStack.getType().name(); + possibleMaterials.add(name); + + if (name.contains("POTION")) { + possibleMaterials.add(Potion.fromItemStack(itemStack).getType().getEffectType().getName() + "_" + name); + } + + return possibleMaterials; + } +} diff --git a/src/com/zeshanaslam/actionhealth/action/ActionStore.java b/src/com/zeshanaslam/actionhealth/action/ActionStore.java new file mode 100644 index 0000000..8a5270d --- /dev/null +++ b/src/com/zeshanaslam/actionhealth/action/ActionStore.java @@ -0,0 +1,93 @@ +package com.zeshanaslam.actionhealth.action; +import com.zeshanaslam.actionhealth.Main; +import com.zeshanaslam.actionhealth.action.data.Action; +import com.zeshanaslam.actionhealth.action.data.Tagged; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; + +import java.util.*; + +public class ActionStore { + + private final Main main; + public boolean enabled; + public int tagLength; + public int tagAmount; + public HashMap> events; + public HashMap> tagged = new HashMap<>(); + + public ActionStore(Main main) { + this.main = main; + enabled = main.getConfig().getBoolean("Action.Enabled"); + tagLength = main.getConfig().getInt("Action.TagLength"); + tagAmount = main.getConfig().getInt("Action.TagAmount"); + events = new HashMap<>(); + + for (String action: main.getConfig().getConfigurationSection("Action.Events").getKeys(false)) { + for (String type: main.getConfig().getConfigurationSection("Action.Events." + action).getKeys(false)) { + String output = main.getConfig().getString("Action.Events." + action + "." + type); + ActionType actionType = ActionType.valueOf(action); + + if (events.containsKey(actionType)) { + events.get(actionType).add(new Action(type, output)); + } else { + List actions = new ArrayList<>(); + actions.add(new Action(type, output)); + + events.put(actionType, actions); + } + } + } + } + + public void addTag(UUID damager, UUID damaged) { + if (tagged.containsKey(damager)) { + // Remove oldest if > tag amount to add new player + if (tagAmount != -1 && tagged.get(damager).size() >= tagAmount) + tagged.get(damager).remove(0); + + tagged.get(damager).add(new Tagged(damager, damaged, System.currentTimeMillis())); + } else { + List taggedList = new ArrayList<>(); + taggedList.add(new Tagged(damager, damaged, System.currentTimeMillis())); + + tagged.put(damager, taggedList); + } + } + + private void sendMessage(UUID user, String message) { + for (List taggedList: tagged.values()) { + for (Tagged tagged: taggedList) { + if (tagged.damaged.equals(user)) { + Player damager = Bukkit.getServer().getPlayer(tagged.damager); + Player damaged = Bukkit.getServer().getPlayer(tagged.damaged); + + String output = main.healthUtil.getOutput(damaged.getHealth(), message, damager, damaged); + + if (output != null) + main.healthUtil.sendActionBar(damager, output); + } + } + } + } + + public void triggerAction(ActionType actionType, Player player, String material) { + if (main.configStore.actionStore.events.containsKey(actionType)) { + List actionList = new ArrayList<>(main.configStore.actionStore.events.get(actionType)); + Optional actionOptional = actionList.stream() + .filter(a -> a.material.equalsIgnoreCase(material)).findAny(); + + if (actionOptional.isPresent()) { + Action action = actionOptional.get(); + main.configStore.actionStore.sendMessage(player.getUniqueId(), action.output); + } + } + } + + public enum ActionType { + CONSUME, + SWAP, + RIGHTCLICK, + LEFTCLICK + } +} diff --git a/src/com/zeshanaslam/actionhealth/action/ActionTask.java b/src/com/zeshanaslam/actionhealth/action/ActionTask.java new file mode 100644 index 0000000..a1329d9 --- /dev/null +++ b/src/com/zeshanaslam/actionhealth/action/ActionTask.java @@ -0,0 +1,33 @@ +package com.zeshanaslam.actionhealth.action; + +import com.zeshanaslam.actionhealth.Main; +import com.zeshanaslam.actionhealth.action.data.Tagged; +import org.bukkit.scheduler.BukkitRunnable; + +import java.util.Iterator; +import java.util.UUID; + +public class ActionTask extends BukkitRunnable { + + private final Main main; + + public ActionTask(Main main) { + this.main = main; + } + + @Override + public void run() { + for (UUID key: main.configStore.actionStore.tagged.keySet()) { + Iterator taggedIterator = main.configStore.actionStore.tagged.get(key).iterator(); + while (taggedIterator.hasNext()) { + Tagged tagged = taggedIterator.next(); + long secondsLeft = ((tagged.timestamp / 1000) + main.configStore.actionStore.tagLength) + - (System.currentTimeMillis() / 1000); + + if (secondsLeft <= 0) { + taggedIterator.remove(); + } + } + } + } +} diff --git a/src/com/zeshanaslam/actionhealth/action/data/Action.java b/src/com/zeshanaslam/actionhealth/action/data/Action.java new file mode 100644 index 0000000..277655e --- /dev/null +++ b/src/com/zeshanaslam/actionhealth/action/data/Action.java @@ -0,0 +1,12 @@ +package com.zeshanaslam.actionhealth.action.data; + +public class Action { + + public String material; + public String output; + + public Action(String material, String output) { + this.material = material; + this.output = output; + } +} diff --git a/src/com/zeshanaslam/actionhealth/action/data/Tagged.java b/src/com/zeshanaslam/actionhealth/action/data/Tagged.java new file mode 100644 index 0000000..a076e28 --- /dev/null +++ b/src/com/zeshanaslam/actionhealth/action/data/Tagged.java @@ -0,0 +1,16 @@ +package com.zeshanaslam.actionhealth.action.data; + +import java.util.UUID; + +public class Tagged { + + public UUID damager; + public UUID damaged; + public long timestamp; + + public Tagged(UUID damager, UUID damaged, long timestamp) { + this.damager = damager; + this.damaged = damaged; + this.timestamp = timestamp; + } +} diff --git a/src/com/zeshanaslam/actionhealth/config/ConfigStore.java b/src/com/zeshanaslam/actionhealth/config/ConfigStore.java index b407e4d..e0e6495 100644 --- a/src/com/zeshanaslam/actionhealth/config/ConfigStore.java +++ b/src/com/zeshanaslam/actionhealth/config/ConfigStore.java @@ -2,6 +2,7 @@ package com.zeshanaslam.actionhealth.config; import com.zeshanaslam.actionhealth.LookThread; import com.zeshanaslam.actionhealth.Main; +import com.zeshanaslam.actionhealth.action.ActionStore; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.scheduler.BukkitTask; @@ -46,6 +47,7 @@ public class ConfigStore { public int limitHealth; public boolean showNPC; public boolean showMiniaturePets; + public ActionStore actionStore; public ConfigStore(Main plugin) { // Clear settings for reloads @@ -171,5 +173,6 @@ public class ConfigStore { } } showMiniaturePets = plugin.getConfig().getBoolean("ShowMiniaturePets"); + actionStore = new ActionStore(plugin); } } diff --git a/src/com/zeshanaslam/actionhealth/events/HealthListeners.java b/src/com/zeshanaslam/actionhealth/events/HealthListeners.java index 830165d..538e038 100644 --- a/src/com/zeshanaslam/actionhealth/events/HealthListeners.java +++ b/src/com/zeshanaslam/actionhealth/events/HealthListeners.java @@ -2,6 +2,7 @@ package com.zeshanaslam.actionhealth.events; import com.zeshanaslam.actionhealth.Main; import com.zeshanaslam.actionhealth.utils.FileHandler; +import org.bukkit.Material; import org.bukkit.entity.Entity; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; @@ -10,6 +11,7 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.entity.EntityDamageByEntityEvent; +import org.bukkit.event.player.PlayerItemConsumeEvent; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerQuitEvent; @@ -48,6 +50,8 @@ public class HealthListeners implements Listener { // Send health if (damaged instanceof LivingEntity) { LivingEntity livingEntity = (LivingEntity) damaged; + + livingEntity.setLastDamage(event.getFinalDamage()); plugin.healthUtil.sendHealth(player, livingEntity, livingEntity.getHealth() - event.getFinalDamage()); } } diff --git a/src/com/zeshanaslam/actionhealth/utils/HealthUtil.java b/src/com/zeshanaslam/actionhealth/utils/HealthUtil.java index 873bdcf..11a1dcb 100644 --- a/src/com/zeshanaslam/actionhealth/utils/HealthUtil.java +++ b/src/com/zeshanaslam/actionhealth/utils/HealthUtil.java @@ -64,21 +64,21 @@ public class HealthUtil { new BukkitRunnable() { public void run() { - String output = getOutput(entity.getHealth(), receiver, entity); + String output = getOutput(entity.getHealth(), plugin.configStore.healthMessage, receiver, entity); if (output != null) sendActionBar(receiver, output); } }.runTaskLater(plugin, plugin.configStore.delayTick); } else { - String output = getOutput(health, receiver, entity); + String output = getOutput(health, plugin.configStore.healthMessage, receiver, entity); if (output != null) sendActionBar(receiver, output); } } - private String getOutput(double health, Player receiver, LivingEntity entity) { + public String getOutput(double health, String output, Player receiver, LivingEntity entity) { double maxHealth = entity.getMaxHealth(); if (health < 0.0 || entity.isDead()) health = 0.0; @@ -87,8 +87,6 @@ public class HealthUtil { if (plugin.healthUtil.isBlacklisted(entity, name)) return null; if (plugin.configStore.stripName) name = ChatColor.stripColor(name); - String output = plugin.configStore.healthMessage; - if (entity instanceof Player) { String displayName; Player player = (Player) entity; @@ -121,6 +119,7 @@ public class HealthUtil { output = output.replace("{name}", name); output = output.replace("{health}", String.valueOf((int) health)); output = output.replace("{maxhealth}", String.valueOf((int) maxHealth)); + output = output.replace("{opponentlastdamage}", String.valueOf((int) entity.getLastDamage())); if (output.contains("{usestyle}")) { StringBuilder style = new StringBuilder();