Skip to content

Commit

Permalink
Merge pull request #832 from TonytheMacaroni/main
Browse files Browse the repository at this point in the history
Add startpose/stoppose/insideblock triggers, pose and fixedpose conditions, and PoseSpell
  • Loading branch information
Chronoken authored Jan 15, 2024
2 parents fe8328a + f8f457e commit ec0af62
Show file tree
Hide file tree
Showing 8 changed files with 273 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.nisovin.magicspells.castmodifiers.conditions;

import org.bukkit.Location;
import org.bukkit.entity.LivingEntity;

import com.nisovin.magicspells.castmodifiers.Condition;

public class FixedPoseCondition extends Condition {

@Override
public boolean initialize(String var) {
return var.isEmpty();
}

@Override
public boolean check(LivingEntity caster) {
return caster.hasFixedPose();
}

@Override
public boolean check(LivingEntity caster, LivingEntity target) {
return target.hasFixedPose();
}

@Override
public boolean check(LivingEntity caster, Location location) {
return false;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.nisovin.magicspells.castmodifiers.conditions;

import java.util.Set;
import java.util.EnumSet;

import org.bukkit.Location;
import org.bukkit.entity.Pose;
import org.bukkit.entity.LivingEntity;

import com.nisovin.magicspells.castmodifiers.Condition;

public class PoseCondition extends Condition {

private final Set<Pose> poses = EnumSet.noneOf(Pose.class);

@Override
public boolean initialize(String var) {
String[] split = var.split(",");

for (String pose : split) {
try {
poses.add(Pose.valueOf(pose.trim().toUpperCase()));
} catch (IllegalArgumentException e) {
return false;
}
}

return !poses.isEmpty();
}

@Override
public boolean check(LivingEntity caster) {
return poses.contains(caster.getPose());
}

@Override
public boolean check(LivingEntity caster, LivingEntity target) {
return poses.contains(target.getPose());
}

@Override
public boolean check(LivingEntity caster, Location location) {
return false;
}

}
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
@@ -0,0 +1,43 @@
package com.nisovin.magicspells.spells.passive;

import java.util.Set;
import java.util.EnumSet;

import org.bukkit.entity.Pose;
import org.bukkit.event.EventHandler;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.entity.EntityPoseChangeEvent;

import com.nisovin.magicspells.MagicSpells;
import com.nisovin.magicspells.util.OverridePriority;
import com.nisovin.magicspells.spells.passive.util.PassiveListener;

public class StartPoseListener extends PassiveListener {

private Set<Pose> poses;

@Override
public void initialize(String var) {
if (var == null || var.isEmpty()) return;

poses = EnumSet.noneOf(Pose.class);

for (String pose : var.split(",")) {
try {
poses.add(Pose.valueOf(pose.trim().toUpperCase()));
} catch (IllegalArgumentException e) {
MagicSpells.error("Invalid pose '" + pose + "' in startpose trigger on passive spell '" + passiveSpell.getInternalName() + "'");
}
}
}

@OverridePriority
@EventHandler
public void onPoseChange(EntityPoseChangeEvent event) {
if (!(event.getEntity() instanceof LivingEntity caster) || !canTrigger(caster) || !hasSpell(caster)) return;
if (poses != null && !poses.contains(event.getPose())) return;

passiveSpell.activate(caster);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.nisovin.magicspells.spells.passive;

import java.util.Set;
import java.util.EnumSet;

import org.bukkit.entity.Pose;
import org.bukkit.event.EventHandler;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.entity.EntityPoseChangeEvent;

import com.nisovin.magicspells.MagicSpells;
import com.nisovin.magicspells.util.OverridePriority;
import com.nisovin.magicspells.spells.passive.util.PassiveListener;

public class StopPoseListener extends PassiveListener {

private Set<Pose> poses;

@Override
public void initialize(String var) {
if (var == null || var.isEmpty()) return;

poses = EnumSet.noneOf(Pose.class);

for (String pose : var.split(",")) {
try {
poses.add(Pose.valueOf(pose.trim().toUpperCase()));
} catch (IllegalArgumentException e) {
MagicSpells.error("Invalid pose '" + pose + "' in startpose trigger on passive spell '" + passiveSpell.getInternalName() + "'");
}
}
}

@OverridePriority
@EventHandler
public void onPoseChange(EntityPoseChangeEvent event) {
if (!(event.getEntity() instanceof LivingEntity caster) || !canTrigger(caster) || !hasSpell(caster)) return;
if (poses != null && !poses.contains(caster.getPose())) return;

passiveSpell.activate(caster);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.nisovin.magicspells.spells.targeted;

import org.bukkit.entity.Pose;
import org.bukkit.entity.LivingEntity;

import com.nisovin.magicspells.util.SpellData;
import com.nisovin.magicspells.util.CastResult;
import com.nisovin.magicspells.util.TargetInfo;
import com.nisovin.magicspells.util.MagicConfig;
import com.nisovin.magicspells.spells.TargetedSpell;
import com.nisovin.magicspells.util.config.ConfigData;
import com.nisovin.magicspells.spells.TargetedEntitySpell;

public class PoseSpell extends TargetedSpell implements TargetedEntitySpell {

private final ConfigData<Pose> pose;

private final ConfigData<Boolean> fixed;

public PoseSpell(MagicConfig config, String spellName) {
super(config, spellName);

pose = getConfigDataEnum("pose", Pose.class, null);
fixed = getConfigDataBoolean("fixed", false);
}

@Override
public CastResult cast(SpellData data) {
TargetInfo<LivingEntity> info = getTargetedEntity(data);
if (info.noTarget()) return noTarget(info);

return castAtEntity(info.spellData());
}

@Override
public CastResult castAtEntity(SpellData data) {
Pose pose = this.pose.get(data);
if (pose == null) return new CastResult(PostCastAction.ALREADY_HANDLED, data);

data.target().setPose(pose, fixed.get(data));

playSpellEffects(data);
return new CastResult(PostCastAction.HANDLE_NORMALLY, data);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ private void initialize() {
addCondition("passable", PassableCondition.class);
addCondition("replaceable", ReplaceableCondition.class);
addCondition("solid", SolidCondition.class);
addCondition("pose", PoseCondition.class);
addCondition("fixedpose", FixedPoseCondition.class);
}

}
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 Expand Up @@ -132,11 +133,13 @@ private void initialize() {
addListener("spelltarget", SpellTargetListener.class);
addListener("startfly", StartFlyListener.class);
addListener("startglide", StartGlideListener.class);
addListener("startpose", StartPoseListener.class);
addListener("startsneak", StartSneakListener.class);
addListener("startsprint", StartSprintListener.class);
addListener("startswim", StartSwimListener.class);
addListener("stopfly", StopFlyListener.class);
addListener("stopglide", StopGlideListener.class);
addListener("stoppose", StopPoseListener.class);
addListener("stopsneak", StopSneakListener.class);
addListener("stopsprint", StopSprintListener.class);
addListener("stopswim", StopSwimListener.class);
Expand Down

0 comments on commit ec0af62

Please sign in to comment.