Created action system and added new placeholder

This commit is contained in:
Zeshan Aslam 2019-08-10 01:00:06 -04:00
parent ab892b6163
commit b6be979e8d
11 changed files with 295 additions and 7 deletions

View File

@ -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
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'

View File

@ -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:

View File

@ -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<UUID> 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

View File

@ -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<String> getName(ItemStack itemStack) {
List<String> 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;
}
}

View File

@ -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<ActionType, List<Action>> events;
public HashMap<UUID, List<Tagged>> 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<Action> 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<Tagged> taggedList = new ArrayList<>();
taggedList.add(new Tagged(damager, damaged, System.currentTimeMillis()));
tagged.put(damager, taggedList);
}
}
private void sendMessage(UUID user, String message) {
for (List<Tagged> 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<Action> actionList = new ArrayList<>(main.configStore.actionStore.events.get(actionType));
Optional<Action> 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
}
}

View File

@ -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<Tagged> 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();
}
}
}
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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());
}
}

View File

@ -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();