Added event based placeholders.

Added ModifyBlock
This commit is contained in:
GB6 2019-03-26 17:21:22 +01:00
parent 6f34341b57
commit f8bd9e31eb
15 changed files with 141 additions and 60 deletions

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>EpicEnchants-Parent</artifactId>
<groupId>com.songoda</groupId>
<version>1.0.4-ALPHA</version>
<version>1.0.5-ALPHA</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -31,7 +31,7 @@ public abstract class EffectExecutor {
}
public void testAndRun(@NotNull Player user, @Nullable LivingEntity opponent, int level, TriggerType type, Event event, EventType eventType) {
if (triggerTypes.contains(type)) {
if (!triggerTypes.contains(type)) {
return;
}
@ -39,7 +39,7 @@ public abstract class EffectExecutor {
return;
}
if (!condition.get(user, opponent, level, false)) {
if (!condition.get(user, opponent, level, event, false)) {
return;
}
@ -53,7 +53,7 @@ public abstract class EffectExecutor {
public abstract void execute(@NotNull Player user, @Nullable LivingEntity opponent, int level, EventType eventType);
public Who who() {
protected Who who() {
if (section.isString("who")) {
if (section.getString("who").equalsIgnoreCase("user")) return USER;
else if (section.getString("who").equalsIgnoreCase("opponent")) return OPPONENT;

View File

@ -2,7 +2,7 @@ package com.songoda.epicenchants.effect.effects;
import com.songoda.epicenchants.effect.EffectExecutor;
import com.songoda.epicenchants.enums.EventType;
import com.songoda.epicenchants.managers.HookManager;
import com.songoda.epicenchants.utils.single.Placeholders;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.LivingEntity;
@ -17,6 +17,6 @@ public class ConsoleCommand extends EffectExecutor {
@Override
public void execute(@NotNull Player user, LivingEntity opponent, int level, EventType eventType) {
Bukkit.dispatchCommand(Bukkit.getConsoleSender(),
HookManager.setPAPIPlaceholders(getSection().getString("command"), user, opponent, level));
Placeholders.setPlaceholders(getSection().getString("command"), user, opponent, level));
}
}

View File

@ -2,7 +2,7 @@ package com.songoda.epicenchants.effect.effects;
import com.songoda.epicenchants.effect.EffectExecutor;
import com.songoda.epicenchants.enums.EventType;
import com.songoda.epicenchants.managers.HookManager;
import com.songoda.epicenchants.utils.single.Placeholders;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
@ -18,6 +18,6 @@ public class Message extends EffectExecutor {
@Override
public void execute(@NotNull Player user, LivingEntity opponent, int level, EventType eventType) {
if (eventType == EventType.ON || eventType == EventType.NONE)
consume(entity -> entity.sendMessage(color(HookManager.setPAPIPlaceholders(getSection().getString("message"), user, opponent, level))), user, opponent);
consume(entity -> entity.sendMessage(color(Placeholders.setPlaceholders(getSection().getString("message"), user, opponent, level))), user, opponent);
}
}

View File

@ -0,0 +1,28 @@
package com.songoda.epicenchants.effect.effects;
import com.songoda.epicenchants.effect.EffectEventExecutor;
import com.songoda.epicenchants.enums.EventType;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.block.BlockEvent;
import org.bukkit.event.player.PlayerInteractEvent;
public class ModifyBlock extends EffectEventExecutor {
public ModifyBlock(ConfigurationSection section) {
super(section);
}
@Override
public void execute(Player user, LivingEntity opponent, int level, Event event, EventType eventType) {
if (!(event instanceof BlockEvent) && !(event instanceof PlayerInteractEvent && ((PlayerInteractEvent) event).hasBlock())) {
return;
}
Block block = event instanceof BlockEvent ? ((BlockEvent) event).getBlock() : ((PlayerInteractEvent) event).getClickedBlock();
block.setType(Material.getMaterial(getSection().getString("material")));
}
}

View File

@ -2,7 +2,7 @@ package com.songoda.epicenchants.effect.effects;
import com.songoda.epicenchants.effect.EffectExecutor;
import com.songoda.epicenchants.enums.EventType;
import com.songoda.epicenchants.managers.HookManager;
import com.songoda.epicenchants.utils.single.Placeholders;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
@ -27,7 +27,7 @@ public class PlayerCommand extends EffectExecutor {
return;
}
consume(entity -> ((Player) entity).performCommand(HookManager.setPAPIPlaceholders(getSection().getString("command"), user, opponent, level)), user, opponent);
consume(entity -> ((Player) entity).performCommand(Placeholders.setPlaceholders(getSection().getString("command"), user, opponent, level)), user, opponent);
}
}

View File

@ -29,7 +29,7 @@ public class Potion extends EffectExecutor {
return;
}
if (this.getTriggerTypes() == TriggerType.STATIC_EFFECT || this.getTriggerTypes() == TriggerType.HELD_ITEM) {
if (getTriggerTypes().contains(TriggerType.STATIC_EFFECT) || getTriggerTypes().contains(TriggerType.HELD_ITEM)) {
if (eventType == EventType.ON) {
consume(entity -> entity.addPotionEffect(new PotionEffect(effectType, Integer.MAX_VALUE, (int) amplifier.get(level - 1, 0, user, opponent),
false, false)), user, opponent);

View File

@ -1,14 +1,9 @@
package com.songoda.epicenchants.managers;
import com.songoda.epicenchants.objects.Placeholder;
import com.songoda.ultimatebottles.UltimateBottles;
import lombok.Getter;
import me.clip.placeholderapi.PlaceholderAPI;
import org.bukkit.Bukkit;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import java.util.Arrays;
import java.util.Optional;
@Getter
@ -23,31 +18,4 @@ public class HookManager {
return Optional.ofNullable(ultimateBottles);
}
public static String setPAPIPlaceholders(String input, Player user, LivingEntity opponent, int level) {
for (Placeholder pair : Arrays.asList(
Placeholder.of("level", level),
Placeholder.of("user_health", user.getHealth()),
Placeholder.of("attacker_health", opponent == null ? -1 : opponent.getHealth()),
Placeholder.of("user_food", user.getFoodLevel()),
Placeholder.of("attacker_food", opponent instanceof Player ? ((Player) opponent).getFoodLevel() : 0),
Placeholder.of("user_is_sneaking", user.isSneaking()),
Placeholder.of("attacker_is_sneaking", opponent instanceof Player && ((Player) opponent).isSneaking()),
Placeholder.of("world", user.getWorld().getName()),
Placeholder.of("players_near", user.getNearbyEntities(4, 4, 4).size()),
Placeholder.of("user_on_fire", user.getFireTicks() != 0),
Placeholder.of("attacker_on_fire", opponent != null && opponent.getFireTicks() != 0)
)) {
input = input.replace(pair.getPlaceholder(), pair.getToReplace().toString());
}
if (Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) {
input = PlaceholderAPI.setPlaceholders(user, input);
if (opponent instanceof Player) {
input = PlaceholderAPI.setRelationalPlaceholders(user, (Player) opponent, input);
}
}
return input;
}
}

View File

@ -1,10 +1,11 @@
package com.songoda.epicenchants.objects;
import com.songoda.epicenchants.managers.HookManager;
import com.songoda.epicenchants.utils.single.Placeholders;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;
import javax.script.ScriptEngine;
@ -22,13 +23,15 @@ public class Condition {
return new Condition(string);
}
public boolean get(Player user, @Nullable LivingEntity attacker, int level, boolean def) {
public boolean get(Player user, @Nullable LivingEntity attacker, int level, @Nullable Event event, boolean def) {
if (string == null || string.isEmpty()) {
return true;
}
ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("JavaScript");
String toValidate = ChatColor.stripColor(HookManager.setPAPIPlaceholders(string, user, attacker, level));
String toValidate = ChatColor.stripColor(Placeholders.setPlaceholders(string, user, attacker, level, event));
System.out.println("Verifying: " + toValidate);
try {
return Boolean.parseBoolean(scriptEngine.eval(toValidate).toString());

View File

@ -1,6 +1,6 @@
package com.songoda.epicenchants.objects;
import com.songoda.epicenchants.managers.HookManager;
import com.songoda.epicenchants.utils.single.Placeholders;
import org.bukkit.Bukkit;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
@ -30,7 +30,7 @@ public class LeveledModifier {
}
ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("JavaScript");
String toTest = HookManager.setPAPIPlaceholders(string, user, opponent, level);
String toTest = Placeholders.setPlaceholders(string, user, opponent, level);
try {
return Double.parseDouble(scriptEngine.eval(toTest).toString());

View File

@ -8,6 +8,7 @@ import de.tr7zw.itemnbtapi.NBTItem;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
@ -269,9 +270,9 @@ public class ItemBuilder {
return item;
}
public ItemStack buildWithWrappers(int level) {
public ItemStack buildWithWrappers(int level, Player user, LivingEntity opponent) {
item.setItemMeta(meta);
enchantmentWrappers.forEach(enchant -> item.addUnsafeEnchantment(enchant.getEnchantment(), enchant.getAmplifier(level)));
enchantmentWrappers.forEach(enchant -> item.addUnsafeEnchantment(enchant.getEnchantment(), enchant.getAmplifier(level, user, opponent)));
return item;
}

View File

@ -0,0 +1,73 @@
package com.songoda.epicenchants.utils.single;
import me.clip.placeholderapi.PlaceholderAPI;
import org.bukkit.Bukkit;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiFunction;
import java.util.function.Function;
public class Placeholders {
private static final Map<String, Function<Event, String>> EVENT_FUNCTIONS = new HashMap<String, Function<Event, String>>() {{
put("{block_type}", event -> {
if (event instanceof BlockBreakEvent) {
return ((BlockBreakEvent) event).getBlock().getType().toString();
} else if (event instanceof BlockPlaceEvent) {
return ((BlockPlaceEvent) event).getBlockPlaced().getType().toString();
} else return "N/A";
});
put("{clicked_type}", event -> {
if (event instanceof PlayerInteractEvent && ((PlayerInteractEvent) event).hasBlock()) {
return ((PlayerInteractEvent) event).getClickedBlock().getType().toString();
} else if (event instanceof PlayerInteractEntityEvent) {
return ((PlayerInteractEntityEvent) event).getRightClicked().getType().toString();
} else return "N/A";
});
}};
private static final Map<String, BiFunction<Player, LivingEntity, Object>> PLAYER_FUNCTIONS = new HashMap<String, BiFunction<Player, LivingEntity, Object>>() {{
put("{user_health}", (user, opponent) -> user.getHealth());
put("{attacker_health}", (user, opponent) -> opponent == null ? -1 : opponent.getHealth());
put("{user_food}", (user, opponent) -> user.getFoodLevel());
put("{attacker_food}", (user, opponent) -> opponent instanceof Player ? ((Player) opponent).getFoodLevel() : 0);
put("{user_is_sneaking}", (user, opponent) -> user.isSneaking());
put("{attacker_is_sneaking}", (user, opponent) -> opponent instanceof Player && ((Player) opponent).isSneaking());
put("{world}", (user, opponent) -> user.getWorld().getName());
put("{players_near}", (user, opponent) -> user.getNearbyEntities(4, 4, 4).size());
put("{user_on_fire}", (user, opponent) -> user.getFireTicks() != 0);
put("{attacker_on_fire}", (user, opponent) -> opponent != null && opponent.getFireTicks() != 0);
}};
public static String setPlaceholders(String input, Player user, LivingEntity opponent, int level) {
return setPlaceholders(input, user, opponent, level, null);
}
public static String setPlaceholders(String input, Player user, LivingEntity opponent, int level, Event event) {
AtomicReference<String> output = new AtomicReference<>(input.replace("{level}", "" + level));
PLAYER_FUNCTIONS.forEach((toReplace, function) -> output.updateAndGet(string -> string.replace(toReplace, function.apply(user, opponent).toString())));
Optional.ofNullable(event).ifPresent(e -> EVENT_FUNCTIONS.forEach((toReplace, function) -> output.updateAndGet(string -> "'" + string.replace(toReplace, function.apply(e)) + "'")));
if (Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) {
output.updateAndGet(string -> PlaceholderAPI.setPlaceholders(user, string));
if (opponent instanceof Player) {
output.updateAndGet(string -> PlaceholderAPI.setRelationalPlaceholders(user, (Player) opponent, input));
}
}
return output.get();
}
}

View File

@ -3,13 +3,17 @@ package com.songoda.epicenchants.wrappers;
import com.songoda.epicenchants.objects.LeveledModifier;
import lombok.Builder;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@Builder
public class EnchantmentWrapper {
private LeveledModifier amplifier;
private Enchantment enchantment;
public int getAmplifier(int level) {
public int getAmplifier(int level, @NotNull Player user, @Nullable LivingEntity opponent) {
return (int) amplifier.get(level, 0, user, opponent);
}

View File

@ -29,7 +29,7 @@ public class MobWrapper {
private boolean hostile;
private LeveledModifier maxAmount;
public void trySpawn(@NotNull Player player, @Nullable LivingEntity opponent, int level, TriggerType triggerType) {
public void trySpawn(@NotNull Player user, @Nullable LivingEntity opponent, int level, TriggerType triggerType) {
if (this.triggerType != triggerType) {
return;
}
@ -38,7 +38,7 @@ public class MobWrapper {
return;
}
Location location = player.getLocation();
Location location = user.getLocation();
for (int i = 0; i < current().nextInt((int) (maxAmount.get(level, 1, user, opponent) + 1)); i++) {
Location spawnLocation = location.clone().add(current().nextInt(-3, 3), 0, current().nextInt(-3, 3));
@ -57,16 +57,20 @@ public class MobWrapper {
LivingEntity livingEntity = (LivingEntity) entity;
int dropChance = (int) equipmentDropChance.get(level, 0, user, opponent);
if (helmet != null) livingEntity.getEquipment().setHelmet(helmet.buildWithWrappers(level));
if (chestPlate != null) livingEntity.getEquipment().setChestplate(chestPlate.buildWithWrappers(level));
if (leggings != null) livingEntity.getEquipment().setLeggings(leggings.buildWithWrappers(level));
if (boots != null) livingEntity.getEquipment().setBoots(boots.buildWithWrappers(level));
if (helmet != null)
livingEntity.getEquipment().setHelmet(helmet.buildWithWrappers(level, user, opponent));
if (chestPlate != null)
livingEntity.getEquipment().setChestplate(chestPlate.buildWithWrappers(level, user, opponent));
if (leggings != null)
livingEntity.getEquipment().setLeggings(leggings.buildWithWrappers(level, user, opponent));
if (boots != null) livingEntity.getEquipment().setBoots(boots.buildWithWrappers(level, user, opponent));
livingEntity.getEquipment().setHelmetDropChance(dropChance);
livingEntity.getEquipment().setLeggingsDropChance(dropChance);
livingEntity.getEquipment().setHelmetDropChance(dropChance);
livingEntity.getEquipment().setChestplateDropChance(dropChance);
if (handItem != null) livingEntity.getEquipment().setItemInHand(handItem.buildWithWrappers(level));
if (handItem != null)
livingEntity.getEquipment().setItemInHand(handItem.buildWithWrappers(level, user, opponent));
livingEntity.getEquipment().setItemInHandDropChance(dropChance);
}
@ -76,7 +80,7 @@ public class MobWrapper {
NBTEntity nbtEntity = new NBTEntity(entity);
nbtEntity.setBoolean(player.getUniqueId().toString(), true);
nbtEntity.setBoolean(user.getUniqueId().toString(), true);
NBTList list = nbtEntity.getList("Attributes", NBTType.NBTTagCompound);

View File

@ -7,7 +7,7 @@
<groupId>com.songoda</groupId>
<artifactId>EpicEnchants-Parent</artifactId>
<packaging>pom</packaging>
<version>1.0.4-ALPHA</version>
<version>1.0.5-ALPHA</version>
<modules>
<module>core</module>
</modules>