From 58f27eb5097086337160c851c70481d4027100b8 Mon Sep 17 00:00:00 2001 From: TonytheMacaroni Date: Sun, 14 Jan 2024 21:32:14 -0500 Subject: [PATCH] Add insideblock trigger --- .../spells/passive/InsideBlockListener.java | 60 +++++++++++++++++++ .../util/managers/PassiveManager.java | 1 + 2 files changed, 61 insertions(+) create mode 100644 core/src/main/java/com/nisovin/magicspells/spells/passive/InsideBlockListener.java diff --git a/core/src/main/java/com/nisovin/magicspells/spells/passive/InsideBlockListener.java b/core/src/main/java/com/nisovin/magicspells/spells/passive/InsideBlockListener.java new file mode 100644 index 000000000..5339775bf --- /dev/null +++ b/core/src/main/java/com/nisovin/magicspells/spells/passive/InsideBlockListener.java @@ -0,0 +1,60 @@ +package com.nisovin.magicspells.spells.passive; + +import java.util.List; +import java.util.ArrayList; + +import org.bukkit.Bukkit; +import org.bukkit.block.Block; +import org.bukkit.event.EventHandler; +import org.bukkit.entity.LivingEntity; +import org.bukkit.block.data.BlockData; + +import io.papermc.paper.event.entity.EntityInsideBlockEvent; + +import com.nisovin.magicspells.MagicSpells; +import com.nisovin.magicspells.util.OverridePriority; +import com.nisovin.magicspells.spells.passive.util.PassiveListener; + +public class InsideBlockListener extends PassiveListener { + + private List blockData; + + @Override + public void initialize(String var) { + if (var == null || var.isEmpty()) return; + + blockData = new ArrayList<>(); + + for (String data : var.split(",(?![^\\[]*])")) { + try { + blockData.add(Bukkit.createBlockData(data.trim().toLowerCase())); + } catch (IllegalArgumentException e) { + MagicSpells.error("Invalid block data '" + data + "' in insideblock trigger on passive spell '" + passiveSpell.getInternalName() + "'"); + } + } + } + + @OverridePriority + @EventHandler + public void onSwim(EntityInsideBlockEvent event) { + if (!isCancelStateOk(event.isCancelled())) return; + if (!(event.getEntity() instanceof LivingEntity caster) || !canTrigger(caster) || !hasSpell(caster)) return; + + Block block = event.getBlock(); + if (blockData != null && check(block)) return; + + boolean casted = passiveSpell.activate(caster, block.getLocation().add(0.5, 0.5, 0.5)); + if (cancelDefaultAction(casted)) event.setCancelled(true); + } + + private boolean check(Block block) { + BlockData bd = block.getBlockData(); + + for (BlockData data : blockData) + if (bd.matches(data)) + return false; + + return true; + } + +} diff --git a/core/src/main/java/com/nisovin/magicspells/util/managers/PassiveManager.java b/core/src/main/java/com/nisovin/magicspells/util/managers/PassiveManager.java index de0533a86..a7e9329c1 100644 --- a/core/src/main/java/com/nisovin/magicspells/util/managers/PassiveManager.java +++ b/core/src/main/java/com/nisovin/magicspells/util/managers/PassiveManager.java @@ -91,6 +91,7 @@ private void initialize() { addListener("hitarrow", HitArrowListener.class); addListener("hotbardeselect", HotbarDeselectListener.class); addListener("hotbarselect", HotbarSelectListener.class); + addListener("insideblock", InsideBlockListener.class); addListener("inventoryaction", InventoryActionListener.class); addListener("inventoryclick", InventoryClickListener.class); addListener("inventoryclose", InventoryCloseListener.class);