Added the listeners

This commit is contained in:
GB6 2019-01-24 13:09:57 +01:00
parent 9a1e9645ed
commit ad1b882842
14 changed files with 191 additions and 122 deletions

View File

@ -65,57 +65,3 @@ public class EnchantCommand extends BaseCommand {
player.getInventory().setItem(slot, result.getLeft());
}
}
/*ABSORPTION
BLINDNESS
CONFUSION
DAMAGE_RESISTANCE
FAST_DIGGING
FIRE_RESISTANCE
HARM
HEAL
HEALTH_BOOST
HUNGER
INCREASE_DAMAGE
INVISIBILITY
JUMP
NIGHT_VISION
POISON
REGENERATION
SATURATION
SLOW
SLOW_DIGGING
SPEED
WATER_BREATHING
WEAKNESS
WITHER*/
/*
ABSORPTION
BLINDNESS
CONFUSION
DAMAGE_RESISTANCE
FAST_DIGGING
FIRE_RESISTANCE
GLOWING
HARM
HEAL
HEALTH_BOOST
HUNGER
INCREASE_DAMAGE
INVISIBILITY
JUMP
LEVITATION
LUCK
NIGHT_VISION
POISON
REGENERATION
SATURATION
SLOW
SLOW_DIGGING
SPEED
UNLUCK
WATER_BREATHING
WEAKNESS
WITHER
*/

View File

@ -1,6 +1,6 @@
package com.songoda.epicenchants.effect;
import com.songoda.epicenchants.enums.EnchantType;
import com.songoda.epicenchants.enums.EffectType;
import com.songoda.epicenchants.enums.EventType;
import com.songoda.epicenchants.objects.LeveledModifier;
import com.songoda.epicenchants.utils.GeneralUtils;
@ -16,15 +16,15 @@ import static com.songoda.epicenchants.effect.EffectExecutor.Who.WEARER;
public abstract class EffectExecutor {
@Getter private final ConfigurationSection section;
@Getter private final EnchantType enchantType;
@Getter private final EffectType effectType;
public EffectExecutor(ConfigurationSection section) {
this.section = section;
this.enchantType = EnchantType.valueOf(section.getString("type"));
this.effectType = EffectType.valueOf(section.getString("type"));
}
public void testAndRun(Player wearer, Player opponent, int level, EnchantType type, Event event, EventType eventType) {
if (type != enchantType) {
public void testAndRun(Player wearer, Player opponent, int level, EffectType type, Event event, EventType eventType) {
if (type != effectType) {
return;
}
@ -55,7 +55,7 @@ public abstract class EffectExecutor {
}
public void consume(Consumer<Player> playerConsumer, Player wearer, Player opponent) {
if (enchantType == EnchantType.HELD_ITEM || enchantType == EnchantType.STATIC_EFFECT) {
if (effectType == EffectType.HELD_ITEM || effectType == EffectType.STATIC_EFFECT) {
playerConsumer.accept(wearer);
return;
}

View File

@ -1,7 +1,7 @@
package com.songoda.epicenchants.effect.effects;
import com.songoda.epicenchants.effect.EffectExecutor;
import com.songoda.epicenchants.enums.EnchantType;
import com.songoda.epicenchants.enums.EffectType;
import com.songoda.epicenchants.enums.EventType;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
@ -15,7 +15,7 @@ public class Fly extends EffectExecutor {
@Override
public void execute(Player wearer, Player opponent, int level, EventType eventType) {
if (getEnchantType() != EnchantType.STATIC_EFFECT && getEnchantType() != EnchantType.HELD_ITEM) {
if (this.getEffectType() != EffectType.STATIC_EFFECT && this.getEffectType() != EffectType.HELD_ITEM) {
throw new IllegalStateException("Fly effect is not a STATIC_EFFECT or HELD_ITEM");
}

View File

@ -1,7 +1,7 @@
package com.songoda.epicenchants.effect.effects;
import com.songoda.epicenchants.effect.EffectExecutor;
import com.songoda.epicenchants.enums.EnchantType;
import com.songoda.epicenchants.enums.EffectType;
import com.songoda.epicenchants.enums.EventType;
import com.songoda.epicenchants.objects.LeveledModifier;
import com.songoda.epicenchants.utils.GeneralUtils;
@ -28,7 +28,7 @@ public class Potion extends EffectExecutor {
return;
}
if (getEnchantType() == EnchantType.STATIC_EFFECT || getEnchantType() == EnchantType.HELD_ITEM) {
if (this.getEffectType() == EffectType.STATIC_EFFECT || this.getEffectType() == EffectType.HELD_ITEM) {
if (eventType == EventType.ON) {
consume(player -> player.addPotionEffect(new PotionEffect(effectType, Integer.MAX_VALUE, ((int) amplifier.get(level, 0)))), wearer, opponent);
} else if (eventType == EventType.OFF) {

View File

@ -0,0 +1,30 @@
package com.songoda.epicenchants.enums;
public enum EffectType {
ATTACK_PLAYER_MELEE,
ATTACK_PLAYER_RANGE,
DEFENSE_PLAYER_MELEE,
DEFENSE_PLAYER_RANGE,
ATTACK_MOB_MELEE,
ATTACK_MOB_RANGE,
DEFENSE_MOB_MELEE,
DEFENSE_MOB_RANGE,
DEATH,
KILLED_PLAYER,
KILLED_MOB,
EXPLOSION_DAMAGE,
FALL_DAMAGE,
FIRE_DAMAGE,
HELD_ITEM,
STATIC_EFFECT,
BLOCK_BREAK,
REPEATING,
RIGHT_CLICK
}

View File

@ -1,23 +0,0 @@
package com.songoda.epicenchants.enums;
public enum EnchantType {
ATTACK_PLAYER,
ATTACK_MOB,
BOW_ATTACK,
BOW_DEFENSE,
DEATH,
DEFENSE_PLAYER,
DEFENSE_MOB,
DEFENSE_BOW,
EXPLOSION,
FALL_DAMAGE,
FIRE,
HELD_ITEM,
MOB_DEATH,
PLAYER_DEATH,
LEFT_CLICK,
BLOCK_BREAK,
REPEATING,
RIGHT_CLICK,
STATIC_EFFECT
}

View File

@ -0,0 +1,103 @@
package com.songoda.epicenchants.listeners;
import com.songoda.epicenchants.EpicEnchants;
import com.songoda.epicenchants.enums.EffectType;
import org.bukkit.entity.Explosive;
import org.bukkit.entity.Mob;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
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.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.projectiles.ProjectileSource;
import static com.songoda.epicenchants.enums.EffectType.*;
import static org.bukkit.entity.EntityType.PLAYER;
import static org.bukkit.event.entity.EntityDamageEvent.DamageCause.*;
public class EntityListener implements Listener {
private final EpicEnchants instance;
public EntityListener(EpicEnchants instance) {
this.instance = instance;
}
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
public void onEntityDeath(EntityDeathEvent event) {
if (event.getEntity() instanceof Mob && event.getEntity().getKiller() != null) {
instance.getEnchantUtils().handlePlayer(event.getEntity().getKiller(), event, KILLED_MOB);
}
}
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
if (event.getDamager() instanceof Projectile) {
ProjectileSource source = ((Projectile) event.getDamager()).getShooter();
if (event.getEntity() instanceof Player) {
instance.getEnchantUtils().handlePlayer(((Player) event.getEntity()), event, source instanceof Player ? DEFENSE_PLAYER_RANGE : DEFENSE_MOB_RANGE);
}
if (!(source instanceof Player)) {
return;
}
instance.getEnchantUtils().handlePlayer(((Player) source), event, event.getEntity() instanceof Player ? ATTACK_PLAYER_RANGE : ATTACK_MOB_RANGE);
}
if (event.getEntity() instanceof Player) {
Player defender = (Player) event.getEntity();
EffectType effectType = null;
if (event.getDamager() instanceof Player) {
effectType = DEFENSE_PLAYER_MELEE;
} else if (event.getDamager() instanceof Mob) {
effectType = DEFENSE_MOB_MELEE;
} else if (event.getDamager() instanceof Explosive) {
effectType = EXPLOSION_DAMAGE;
}
if (effectType != null) {
instance.getEnchantUtils().handlePlayer(defender, event, effectType);
}
}
if (event.getDamager() instanceof Player) {
Player attacker = (Player) event.getDamager();
EffectType effectType = null;
if (event.getEntity() instanceof Player) {
effectType = ATTACK_PLAYER_MELEE;
} else if (event.getEntity() instanceof Mob) {
effectType = ATTACK_MOB_MELEE;
}
if (effectType != null) {
instance.getEnchantUtils().handlePlayer(attacker, event, effectType);
}
instance.getEnchantUtils().handlePlayer(attacker, event, event.getEntityType() == PLAYER ? ATTACK_PLAYER_MELEE : ATTACK_MOB_MELEE);
}
}
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
public void onEntityDamage(EntityDamageEvent event) {
if (!(event.getEntity() instanceof Player)) {
return;
}
if (event.getCause() == FALL) {
instance.getEnchantUtils().handlePlayer(((Player) event.getEntity()), event, FALL_DAMAGE);
return;
}
if (event.getCause() == FIRE || event.getCause() == FIRE_TICK) {
instance.getEnchantUtils().handlePlayer(((Player) event.getEntity()), event, FIRE_DAMAGE);
}
}
}

View File

@ -3,17 +3,18 @@ package com.songoda.epicenchants.listeners;
import com.songoda.epicenchants.EpicEnchants;
import com.songoda.epicenchants.events.ArmorEquipEvent;
import com.songoda.epicenchants.objects.Enchant;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
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.block.Action;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerItemHeldEvent;
import java.util.Map;
import static com.songoda.epicenchants.enums.EnchantType.*;
import static com.songoda.epicenchants.enums.EffectType.*;
import static com.songoda.epicenchants.enums.EventType.OFF;
import static com.songoda.epicenchants.enums.EventType.ON;
@ -33,19 +34,6 @@ public class PlayerListener implements Listener {
newArmorMap.forEach((enchant, level) -> enchant.onAction(event.getPlayer(), null, event, level, STATIC_EFFECT, ON));
}
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
if (event.getEntityType() == EntityType.PLAYER) {
Player defender = (Player) event.getEntity();
instance.getEnchantUtils().handlePlayer(defender, event, event.getDamager() instanceof Player ? DEFENSE_PLAYER : DEFENSE_MOB);
}
if (event.getDamager() instanceof Player) {
Player attacker = (Player) event.getDamager();
instance.getEnchantUtils().handlePlayer(attacker, event, ATTACK_PLAYER);
}
}
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
public void onPlayerItemHeld(PlayerItemHeldEvent event) {
instance.getEnchantUtils().getEnchants(event.getPlayer().getInventory().getItem(event.getNewSlot()))
@ -55,4 +43,25 @@ public class PlayerListener implements Listener {
.forEach((enchant, level) -> enchant.onAction(event.getPlayer(), null, event, level, HELD_ITEM, OFF));
}
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
public void onPlayerDeath(PlayerDeathEvent event) {
instance.getEnchantUtils().handlePlayer(event.getEntity(), event, DEATH);
if (event.getEntity().getKiller() != null) {
instance.getEnchantUtils().handlePlayer(event.getEntity().getKiller(), event, KILLED_PLAYER);
}
}
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
public void onPlayerInteract(PlayerInteractEvent event) {
if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
instance.getEnchantUtils().handlePlayer(event.getPlayer(), event, RIGHT_CLICK);
}
}
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
public void onBlockBreak(BlockBreakEvent event) {
instance.getEnchantUtils().handlePlayer(event.getPlayer(), event, BLOCK_BREAK);
}
}

View File

@ -1,7 +1,7 @@
package com.songoda.epicenchants.objects;
import com.songoda.epicenchants.effect.EffectExecutor;
import com.songoda.epicenchants.enums.EnchantType;
import com.songoda.epicenchants.enums.EffectType;
import com.songoda.epicenchants.enums.EventType;
import com.songoda.epicenchants.wrappers.MobWrapper;
import lombok.Builder;
@ -26,8 +26,8 @@ public class Enchant {
private BookItem bookItem;
private LeveledModifier modifyDamage;
public void onAction(Player wearer, Player attacker, Event event, int level, EnchantType enchantType, EventType eventType) {
effectExecutors.forEach(effect -> effect.testAndRun(wearer, attacker, level, enchantType, event, eventType));
mobs.forEach(mobWrapper -> mobWrapper.trySpawn(wearer, attacker, level, enchantType));
public void onAction(Player wearer, Player attacker, Event event, int level, EffectType effectType, EventType eventType) {
effectExecutors.forEach(effect -> effect.testAndRun(wearer, attacker, level, effectType, event, eventType));
mobs.forEach(mobWrapper -> mobWrapper.trySpawn(wearer, attacker, level, effectType));
}
}

View File

@ -1,8 +1,8 @@
package com.songoda.epicenchants.utils;
import com.songoda.epicenchants.EpicEnchants;
import com.songoda.epicenchants.enums.EffectType;
import com.songoda.epicenchants.enums.EnchantResult;
import com.songoda.epicenchants.enums.EnchantType;
import com.songoda.epicenchants.enums.EventType;
import com.songoda.epicenchants.objects.Enchant;
import de.tr7zw.itemnbtapi.NBTCompound;
@ -17,8 +17,8 @@ import org.bukkit.inventory.ItemStack;
import java.util.*;
import java.util.stream.Collectors;
import static com.songoda.epicenchants.enums.EffectType.HELD_ITEM;
import static com.songoda.epicenchants.enums.EnchantResult.*;
import static com.songoda.epicenchants.enums.EnchantType.HELD_ITEM;
public class EnchantUtils {
@ -66,12 +66,16 @@ public class EnchantUtils {
.collect(Collectors.toMap(key -> instance.getEnchantManager().getEnchantUnsafe(key), compound::getInteger));
}
public void handlePlayer(Player player, Event event, EnchantType enchantType) {
public void handlePlayer(Player player, Event event, EffectType effectType) {
if (player == null) {
return;
}
List<ItemStack> stacks = new ArrayList<>(Arrays.asList(player.getInventory().getArmorContents()));
stacks.add(player.getItemInHand());
stacks.removeIf(Objects::isNull);
if (enchantType == HELD_ITEM) {
if (effectType == HELD_ITEM) {
stacks = Collections.singletonList(player.getItemInHand());
}
@ -80,7 +84,7 @@ public class EnchantUtils {
((EntityDamageByEntityEvent) event).getDamager() instanceof Player ?
((Player) ((EntityDamageByEntityEvent) event).getDamager()) : null : null;
enchant.onAction(player, opponent, event, level, enchantType, EventType.NONE);
enchant.onAction(player, opponent, event, level, effectType, EventType.NONE);
}));
}
}

View File

@ -1,7 +1,7 @@
package com.songoda.epicenchants.utils.parser;
import com.songoda.epicenchants.effect.EffectManager;
import com.songoda.epicenchants.enums.EnchantType;
import com.songoda.epicenchants.enums.EffectType;
import com.songoda.epicenchants.objects.BookItem;
import com.songoda.epicenchants.objects.Enchant;
import com.songoda.epicenchants.objects.LeveledModifier;
@ -47,7 +47,7 @@ public class ConfigParser {
public static MobWrapper parseMobWrapper(ConfigurationSection section) {
return section != null ? MobWrapper.builder()
.enchantType(EnchantType.valueOf(section.getString("effect-type")))
.effectType(EffectType.valueOf(section.getString("effect-type")))
.entityType(EntityType.valueOf(section.getName()))
.maxAmount(LeveledModifier.of(section.getString("max-amount")))
.spawnPercentage(LeveledModifier.of(section.getString("spawn-percentage")))

View File

@ -1,6 +1,6 @@
package com.songoda.epicenchants.wrappers;
import com.songoda.epicenchants.enums.EnchantType;
import com.songoda.epicenchants.enums.EffectType;
import com.songoda.epicenchants.objects.LeveledModifier;
import com.songoda.epicenchants.utils.GeneralUtils;
import com.songoda.epicenchants.utils.ItemBuilder;
@ -20,7 +20,7 @@ public class MobWrapper {
private String displayName;
private EntityType entityType;
private LeveledModifier attackDamage;
private EnchantType enchantType;
private EffectType effectType;
private LeveledModifier equipmentDropChance;
private LeveledModifier spawnPercentage;
private LeveledModifier health;
@ -28,8 +28,8 @@ public class MobWrapper {
private boolean hostile;
private LeveledModifier maxAmount;
public void trySpawn(@NotNull Player player, Player opponent, int level, EnchantType enchantType) {
if (this.enchantType != enchantType) {
public void trySpawn(@NotNull Player player, Player opponent, int level, EffectType effectType) {
if (this.effectType != effectType) {
return;
}

View File

@ -36,7 +36,7 @@ effects:
#The "-1" is added because every effect key has to be unique.
POTION-1:
#The EffectType
type: DEFENSE_PLAYER
type: DEFENSE_PLAYER_MELEE
#What player should the effect be ran on
who: WEARER
potion-type: SPEED
@ -54,7 +54,7 @@ mobs:
#EntityType
ZOMBIE:
#Max amount zombies that will be spawned
effect-type: DEFENSE_PLAYER
effect-type: DEFENSE_PLAYER_MELEE
max-amount: "{level}"
spawn-percentage: "20 * {level}"
equipment-drop-chance: "10 * {level}"

View File

@ -16,4 +16,4 @@ contents:
exp-cost: 20
eco-cost: 2000
row: 1
column: 4
column: 5