Skip to content

Commit

Permalink
Add insideblock trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
TonytheMacaroni committed Jan 15, 2024
1 parent 75024b1 commit 58f27eb
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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> 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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 58f27eb

Please sign in to comment.