Action system improvements and add damage action type

This commit is contained in:
Zeshan Aslam 2020-04-06 21:28:06 -04:00
parent 61d9d83fdf
commit a7383137ed
7 changed files with 179 additions and 50 deletions

View File

@ -116,7 +116,7 @@ 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
# Supported events: CONSUME, RIGHTCLICK, LEFTCLICK, SWAP, DAMAGE
Action:
Enabled: false
# In seconds
@ -131,4 +131,9 @@ Action:
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'
POTION: '&7&l{name} swapped to &cpotion&7&l. {usestyle}&7&l'
DAMAGE:
ANY: '&7&l{name}: {usestyle}'
# Supports DamageCause if 'ANY' not being used
#LAVA: '&4On fire!'
#ENTITY_SWEEP_ATTACK: '&7You swept them!'

View File

@ -1,6 +1,6 @@
name: ActionHealth
main: com.zeshanaslam.actionhealth.Main
version: 3.4.5
version: 3.4.6
softdepend: [PlaceholderAPI, MVdWPlaceholderAPI, WorldGuard, mcMMO, MythicMobs, LangUtils]
commands:
Actionhealth:

View File

@ -1,6 +1,7 @@
package com.zeshanaslam.actionhealth;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.zeshanaslam.actionhealth.action.ActionHelper;
import com.zeshanaslam.actionhealth.action.ActionListener;
import com.zeshanaslam.actionhealth.action.ActionTask;
import com.zeshanaslam.actionhealth.commands.HealthCommand;
@ -47,7 +48,7 @@ public class Main extends JavaPlugin {
// Register listeners
getServer().getPluginManager().registerEvents(new HealthListeners(this), this);
getServer().getPluginManager().registerEvents(new ActionListener(this), this);
getServer().getPluginManager().registerEvents(new ActionListener(this, new ActionHelper(this)), this);
// Register commands
getCommand("Actionhealth").setExecutor(new HealthCommand(this));

View File

@ -0,0 +1,92 @@
package com.zeshanaslam.actionhealth.action;
import com.zeshanaslam.actionhealth.Main;
import com.zeshanaslam.actionhealth.action.data.Action;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.PotionData;
import org.bukkit.potion.PotionEffect;
import org.bukkit.projectiles.ProjectileSource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
public class ActionHelper {
private final Main main;
public ActionHelper(Main main) {
this.main = main;
}
public void executeTriggers(ActionStore.ActionType actionType, Player player, ItemStack itemStack) {
if (itemStack != null) {
for (String name: getName(itemStack))
executeTriggers(actionType, player, name);
}
}
public void executeTriggers(ActionStore.ActionType actionType, LivingEntity entity, String name) {
main.configStore.actionStore.triggerAction(actionType, entity, name);
}
public void executeTriggers(ActionStore.ActionType actionType, LivingEntity entity, String name, double health) {
main.configStore.actionStore.triggerAction(actionType, entity, name, Optional.of(health));
}
public int getActionTypeEventAmount(ActionStore.ActionType actionType) {
if (main.configStore.actionStore.events.containsKey(actionType)) {
List<Action> actions = main.configStore.actionStore.events.get(actionType);
return actions.size();
}
return 0;
}
public Player getDamagerFromEntity(Entity entity) {
Player damager = null;
if (entity instanceof Projectile) {
Projectile projectile = (Projectile) entity;
ProjectileSource projectileSource = projectile.getShooter();
if (projectileSource instanceof Player) {
damager = (Player) projectileSource;
}
}
return damager;
}
public List<String> getName(ItemStack itemStack) {
List<String> possibleMaterials = new ArrayList<>();
String name = itemStack.getType().name();
possibleMaterials.add(name);
if (itemStack.hasItemMeta()) {
ItemMeta itemMeta = itemStack.getItemMeta();
if (itemMeta instanceof PotionMeta) {
PotionMeta potionMeta = (PotionMeta) itemStack.getItemMeta();
PotionData potionData = potionMeta.getBasePotionData();
possibleMaterials.add(potionData.getType().getEffectType().getName() + "_" + name);
if (potionMeta.hasCustomEffects()) {
for (PotionEffect potionEffect : potionMeta.getCustomEffects()) {
possibleMaterials.add(potionEffect.getType().getName() + "_" + name);
}
}
}
}
return possibleMaterials;
}
}

View File

@ -1,11 +1,16 @@
package com.zeshanaslam.actionhealth.action;
import com.zeshanaslam.actionhealth.Main;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
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.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerItemConsumeEvent;
import org.bukkit.event.player.PlayerItemHeldEvent;
@ -15,6 +20,7 @@ import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.Potion;
import org.bukkit.potion.PotionData;
import org.bukkit.potion.PotionEffect;
import org.bukkit.projectiles.ProjectileSource;
import java.util.ArrayList;
import java.util.List;
@ -22,21 +28,50 @@ import java.util.List;
public class ActionListener implements Listener {
private final Main main;
private ActionHelper actionHelper;
public ActionListener(Main main) {
public ActionListener(Main main, ActionHelper actionHelper) {
this.main = main;
this.actionHelper = actionHelper;
}
@EventHandler(ignoreCancelled = true)
public void onComabat(EntityDamageByEntityEvent event) {
public void onCombat(EntityDamageByEntityEvent event) {
if (!main.configStore.actionStore.enabled)
return;
if (event.getDamager() instanceof Player && event.getEntity() instanceof Player) {
Player damager = (Player) event.getDamager();
ActionStore.ActionType actionType = ActionStore.ActionType.DAMAGE;
Player damager = actionHelper.getDamagerFromEntity(event.getDamager());
if (damager == null && event.getDamager() instanceof Player) {
damager = (Player) event.getDamager();
}
if (damager != null && event.getEntity() instanceof Player) {
Player damaged = (Player) event.getEntity();
main.configStore.actionStore.addTag(damager.getUniqueId(), damaged.getUniqueId());
} else if (damager != null && main.configStore.actionStore.events.containsKey(actionType)) {
main.configStore.actionStore.addTag(damager.getUniqueId(), event.getEntity().getUniqueId());
}
if (!main.configStore.actionStore.isUsingAnyDamageCause) {
EntityDamageEvent.DamageCause damageCause = event.getCause();
if (event.getEntity() instanceof LivingEntity)
actionHelper.executeTriggers(actionType, (LivingEntity) event.getEntity(), damageCause.name());
}
}
@EventHandler(ignoreCancelled = true)
public void onDamage(EntityDamageEvent event) {
if (!main.configStore.actionStore.enabled)
return;
Entity entity = event.getEntity();
ActionStore.ActionType actionType = ActionStore.ActionType.DAMAGE;
if (entity instanceof LivingEntity) {
LivingEntity livingEntity = (LivingEntity) entity;
actionHelper.executeTriggers(actionType, livingEntity, "ANY", livingEntity.getHealth() - event.getFinalDamage());
}
}
@ -48,7 +83,7 @@ public class ActionListener implements Listener {
ActionStore.ActionType actionType = ActionStore.ActionType.CONSUME;
Player player = event.getPlayer();
executeTriggers(actionType, player, event.getItem());
actionHelper.executeTriggers(actionType, player, event.getItem());
}
@EventHandler(ignoreCancelled = true)
@ -61,7 +96,7 @@ public class ActionListener implements Listener {
ItemStack itemStack = player.getInventory().getItem(event.getNewSlot());
executeTriggers(actionType, player, itemStack);
actionHelper.executeTriggers(actionType, player, itemStack);
}
@EventHandler
@ -75,42 +110,16 @@ public class ActionListener implements Listener {
if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
ActionStore.ActionType actionType = ActionStore.ActionType.RIGHTCLICK;
executeTriggers(actionType, player, itemStack);
actionHelper.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);
actionHelper.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 (itemStack.hasItemMeta()) {
ItemMeta itemMeta = itemStack.getItemMeta();
if (itemMeta instanceof PotionMeta) {
PotionMeta potionMeta = (PotionMeta) itemStack.getItemMeta();
PotionData potionData = potionMeta.getBasePotionData();
possibleMaterials.add(potionData.getType().getEffectType().getName() + "_" + name);
if (potionMeta.hasCustomEffects()) {
for (PotionEffect potionEffect : potionMeta.getCustomEffects()) {
possibleMaterials.add(potionEffect.getType().getName() + "_" + name);
}
}
}
}
return possibleMaterials;
@EventHandler
public void onDeath(EntityDeathEvent event) {
Entity entity = event.getEntity();
main.configStore.actionStore.remove(entity.getUniqueId());
}
}

View File

@ -3,6 +3,7 @@ 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.LivingEntity;
import org.bukkit.entity.Player;
import java.util.*;
@ -15,6 +16,7 @@ public class ActionStore {
public int tagAmount;
public HashMap<ActionType, List<Action>> events;
public HashMap<UUID, List<Tagged>> tagged = new HashMap<>();
public boolean isUsingAnyDamageCause = false;
public ActionStore(Main main) {
this.main = main;
@ -28,6 +30,10 @@ public class ActionStore {
String output = main.getConfig().getString("Action.Events." + action + "." + type);
ActionType actionType = ActionType.valueOf(action);
if (actionType == ActionType.DAMAGE && output != null && output.equalsIgnoreCase("any")) {
isUsingAnyDamageCause = true;
}
if (events.containsKey(actionType)) {
events.get(actionType).add(new Action(type, output));
} else {
@ -41,6 +47,9 @@ public class ActionStore {
}
public void addTag(UUID damager, UUID damaged) {
if (damager == damaged)
return;
if (tagged.containsKey(damager)) {
// Remove oldest if > tag amount to add new player
if (tagAmount != -1 && tagged.get(damager).size() >= tagAmount)
@ -55,14 +64,18 @@ public class ActionStore {
}
}
private void sendMessage(UUID user, String message) {
public void remove(UUID remove) {
tagged.remove(remove);
tagged.values().forEach(l -> l.removeIf(c -> c.damaged.equals(remove)));
}
private void sendMessage(LivingEntity entity, String message, Optional<Double> health) {
for (List<Tagged> taggedList: tagged.values()) {
for (Tagged tagged: taggedList) {
if (tagged.damaged.equals(user)) {
if (tagged.damaged.equals(entity.getUniqueId())) {
Player damager = Bukkit.getServer().getPlayer(tagged.damager);
Player damaged = Bukkit.getServer().getPlayer(tagged.damaged);
String output = main.healthUtil.getOutput(damaged.getHealth(), message, damager, damaged);
String output = main.healthUtil.getOutput(health.orElseGet(entity::getHealth), message, damager, entity);
if (output != null)
main.healthUtil.sendActionBar(damager, output);
@ -71,15 +84,19 @@ public class ActionStore {
}
}
public void triggerAction(ActionType actionType, Player player, String material) {
public void triggerAction(ActionType actionType, LivingEntity entity, String name) {
triggerAction(actionType, entity, name, Optional.empty());
}
public void triggerAction(ActionType actionType, LivingEntity entity, String name, Optional<Double> health) {
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();
.filter(a -> a.material.equalsIgnoreCase(name)).findAny();
if (actionOptional.isPresent()) {
Action action = actionOptional.get();
main.configStore.actionStore.sendMessage(player.getUniqueId(), action.output);
main.configStore.actionStore.sendMessage(entity, action.output, health);
}
}
}
@ -88,6 +105,7 @@ public class ActionStore {
CONSUME,
SWAP,
RIGHTCLICK,
LEFTCLICK
LEFTCLICK,
DAMAGE
}
}

View File

@ -29,6 +29,10 @@ public class HealthListeners implements Listener {
return;
}
if (plugin.configStore.actionStore.isUsingAnyDamageCause) {
return;
}
Entity damaged = event.getEntity();
Player player = null;
if (event.getDamager() instanceof Projectile) {