Skip to content

Commit 33f114d

Browse files
Refactor variable construction to have a dedicated class handle selecting which type based upon the type parameter in the config.
Provide a method that can be overridden by variable classes to load additional data from the config. Added a new option to passive spells called require-cancelled-event which defaults to false. When set to true, the spell will not cast if the event is not cancelled. Corrected the handling of triggers which care about the state of an event being cancelled. WARNING: this may break spells which depended upon previously flawed behavior. Added a new variable type which may be referenced by specifying the config field, type, to distancetolocation There are additional configuration fields to specify for this variable type. There is a field called target-location which accepts a string in the format of world,x,y,z and defaults to world,0,0,0. There is a field called cross-world which accepts a boolean and defaults to false. When set to true, distance will still be reported instead of returning the default value. There is a field called cross-world-distance-multiplier which accepts a double and defaults to 1.0. When a distance is being reported that is actually between multiple worlds, the coordinate difference will be multiplied by this value. Just a side note that this variable cannot be manually modified nor will its values be saved (it can't be set as a permanent variable). Added a variant of the distancetolocation variable type referencable as squareddistancetolocation. Instead of calculating the distance between the locations, it just gives you the squared distance. Use this instead of the distancetolocation type whenever possible as it is much faster and avoids using the square root function (a very intensive function). Explicitly cast null in some places in the memory addon to avoid overloaded method ambiguity. Giving the cripple spell a little attention. Added new options to it. use-slowness-effect accepts a boolean and defaults to true. When set to false, the cripple spell will no longer apply a slowness effect to the target. apply-portal-cooldown accepts a boolean and defaults to false. When set to true, this spell will set the portal cooldown field of the target, if the duration of the new cooldown is longer than the existing cooldown. portal-cooldown-ticks accepts an integer and defaults to 100. This number will be used as the target's new portal cooldown, if it is greater than the target's current portal cooldown (and if apply-portal-cooldown is set to true). STRING VARIABLES That's right, String variables are finally coming to MagicSpells. To use them, start by setting type to playerstring To set your default string value, just use the field default-value Note: it is highly recommended to back up your variable data before loading this. This update contains significant changes and new concepts. I don't expect it to be problematic, but the nature of the changes warrants some extra caution. That said, enjoy.
1 parent 3d4e86e commit 33f114d

File tree

64 files changed

+465
-48
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+465
-48
lines changed

src/com/nisovin/magicspells/MagicSpells.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,7 @@ public static String doSubjectVariableReplacements(Player player, String string)
938938
while (matcher.find()) {
939939
String varText = matcher.group();
940940
String[] varData = varText.substring(5, varText.length() - 1).split(":");
941-
double val = plugin.variableManager.getValue(varData[0], player);
941+
String val = plugin.variableManager.getStringValue(varData[0], player);
942942
String sval = varData.length == 1 ? Util.getStringNumber(val, -1) : Util.getStringNumber(val, Integer.parseInt(varData[1]));
943943
string = string.replace(varText, sval);
944944
}
@@ -956,7 +956,7 @@ public static String doVariableReplacements(Player player, String string) {
956956
String varText = matcher.group();
957957
String[] varData = varText.substring(11, varText.length() - 1).split(":");
958958
String variableOwnerName = varData[0];
959-
double val = plugin.variableManager.getValue(varData[1], variableOwnerName);
959+
String val = plugin.variableManager.getStringValue(varData[1], variableOwnerName);
960960
String sval = varData.length == 2 ? Util.getStringNumber(val, -1) : Util.getStringNumber(val, Integer.parseInt(varData[2]));
961961
string = string.replace(varText, sval);
962962
}

src/com/nisovin/magicspells/spells/PassiveSpell.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ public class PassiveSpell extends Spell {
5252
@ConfigData(field="ignore-cancelled", dataType="boolean", defaultValue="true", description="Don't cast if the event has been canceled")
5353
private boolean ignoreCancelled;
5454

55+
@ConfigData(field="require-cancelled-event", dataType="boolean", defaultValue="false", description="Don't cast if the event hasn't been cancelled.")
56+
private boolean requireCancelledEvent;
57+
5558
@ConfigData(field="send-failure-messages", dataType="boolean", defaultValue="false")
5659
private boolean sendFailureMessages;
5760

@@ -72,6 +75,7 @@ public PassiveSpell(MagicConfig config, String spellName) {
7275
cancelDefaultAction = getConfigBoolean("cancel-default-action", false);
7376
cancelDefaultActionWhenCastFails = getConfigBoolean("cancel-default-action-when-cast-fails", false);
7477
ignoreCancelled = getConfigBoolean("ignore-cancelled", true);
78+
requireCancelledEvent = getConfigBoolean("require-cancelled-event", false);
7579
sendFailureMessages = getConfigBoolean("send-failure-messages", false);
7680

7781
spellNames = getConfigStringList("spells", null);
@@ -305,6 +309,10 @@ public boolean ignoreCancelled() {
305309
return ignoreCancelled;
306310
}
307311

312+
public boolean requireCancelledEvent() {
313+
return requireCancelledEvent;
314+
}
315+
308316
@Override
309317
public boolean canBind(CastItem item) {
310318
return false;

src/com/nisovin/magicspells/spells/TargetedSpell.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ private String prepareMessage(String message, Player caster, String targetName,
8383
while (matcher.find()) {
8484
String varText = matcher.group();
8585
String[] varData = varText.substring(5, varText.length() - 1).split(":");
86-
double val = MagicSpells.getVariableManager().getValue(varData[0], playerTarget);
86+
String val = MagicSpells.getVariableManager().getStringValue(varData[0], playerTarget);
8787
String sval = varData.length == 1 ? Util.getStringNumber(val, -1) : Util.getStringNumber(val, Integer.parseInt(varData[1]));
8888
message = message.replace(varText, sval);
8989
}
@@ -93,7 +93,7 @@ private String prepareMessage(String message, Player caster, String targetName,
9393
while (matcher.find()) {
9494
String varText = matcher.group();
9595
String[] varData = varText.substring(5, varText.length() - 1).split(":");
96-
double val = MagicSpells.getVariableManager().getValue(varData[0], caster);
96+
String val = MagicSpells.getVariableManager().getStringValue(varData[0], caster);
9797
String sval = varData.length == 1 ? Util.getStringNumber(val, -1) : Util.getStringNumber(val, Integer.parseInt(varData[1]));
9898
message = message.replace(varText, sval); //TODO make an alternative to overriding the parameter
9999
}

src/com/nisovin/magicspells/spells/passive/BlockBreakListener.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public void onBlockBreak(BlockBreakEvent event) {
5454
Spellbook spellbook = MagicSpells.getSpellbook(event.getPlayer());
5555
if (allTypes.size() > 0) {
5656
for (PassiveSpell spell : allTypes) {
57+
if (!isCancelStateOk(spell, event.isCancelled())) continue;
5758
if (spellbook.hasSpell(spell, false)) {
5859
boolean casted = spell.activate(event.getPlayer(), event.getBlock().getLocation().add(0.5, 0.5, 0.5));
5960
if (PassiveListener.cancelDefaultAction(spell, casted)) {
@@ -66,6 +67,7 @@ public void onBlockBreak(BlockBreakEvent event) {
6667
List<PassiveSpell> list = getSpells(event.getBlock());
6768
if (list != null) {
6869
for (PassiveSpell spell : list) {
70+
if (!isCancelStateOk(spell, event.isCancelled())) continue;
6971
if (spellbook.hasSpell(spell, false)) {
7072
boolean casted = spell.activate(event.getPlayer(), event.getBlock().getLocation().add(0.5, 0.5, 0.5));
7173
if (PassiveListener.cancelDefaultAction(spell, casted)) {

src/com/nisovin/magicspells/spells/passive/BlockPlaceListener.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public void onBlockPlace(BlockPlaceEvent event) {
5454
Spellbook spellbook = MagicSpells.getSpellbook(event.getPlayer());
5555
if (allTypes.size() > 0) {
5656
for (PassiveSpell spell : allTypes) {
57+
if (!isCancelStateOk(spell, event.isCancelled())) continue;
5758
if (spellbook.hasSpell(spell, false)) {
5859
boolean casted = spell.activate(event.getPlayer(), event.getBlock().getLocation().add(0.5, 0.5, 0.5));
5960
if (PassiveListener.cancelDefaultAction(spell, casted)) {
@@ -66,6 +67,7 @@ public void onBlockPlace(BlockPlaceEvent event) {
6667
List<PassiveSpell> list = getSpells(event.getBlock());
6768
if (list != null) {
6869
for (PassiveSpell spell : list) {
70+
if (!isCancelStateOk(spell, event.isCancelled())) continue;
6971
if (spellbook.hasSpell(spell, false)) {
7072
boolean casted = spell.activate(event.getPlayer(), event.getBlock().getLocation().add(0.5, 0.5, 0.5));
7173
if (PassiveListener.cancelDefaultAction(spell, casted)) {

src/com/nisovin/magicspells/spells/passive/CraftListener.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,11 @@ public void onCraft(CraftItemEvent event) {
6767
if (allTypes.size() > 0) {
6868
Spellbook spellbook = MagicSpells.getSpellbook(player);
6969
for (PassiveSpell spell : allTypes) {
70-
if (spell.ignoreCancelled() || !event.isCancelled()) {
71-
if (spellbook.hasSpell(spell)) {
72-
boolean casted = spell.activate(player);
73-
if (PassiveListener.cancelDefaultAction(spell, casted)) {
74-
event.setCancelled(true);
75-
}
70+
if (!isCancelStateOk(spell, event.isCancelled())) continue;
71+
if (spellbook.hasSpell(spell)) {
72+
boolean casted = spell.activate(player);
73+
if (PassiveListener.cancelDefaultAction(spell, casted)) {
74+
event.setCancelled(true);
7675
}
7776
}
7877
}
@@ -83,12 +82,11 @@ public void onCraft(CraftItemEvent event) {
8382
if (list != null) {
8483
Spellbook spellbook = MagicSpells.getSpellbook(player);
8584
for (PassiveSpell spell : list) {
86-
if (spell.ignoreCancelled() || !event.isCancelled()) {
87-
if (spellbook.hasSpell(spell)) {
88-
boolean casted = spell.activate(player);
89-
if (PassiveListener.cancelDefaultAction(spell, casted)) {
90-
event.setCancelled(true);
91-
}
85+
if (!isCancelStateOk(spell, event.isCancelled())) continue;
86+
if (spellbook.hasSpell(spell)) {
87+
boolean casted = spell.activate(player);
88+
if (PassiveListener.cancelDefaultAction(spell, casted)) {
89+
event.setCancelled(true);
9290
}
9391
}
9492
}

src/com/nisovin/magicspells/spells/passive/DropItemListener.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public void onDrop(PlayerDropItemEvent event) {
6363
if (allTypes.size() > 0) {
6464
Spellbook spellbook = MagicSpells.getSpellbook(event.getPlayer());
6565
for (PassiveSpell spell : allTypes) {
66+
if (!isCancelStateOk(spell, event.isCancelled())) continue;
6667
if (spellbook.hasSpell(spell)) {
6768
boolean casted = spell.activate(event.getPlayer());
6869
if (PassiveListener.cancelDefaultAction(spell, casted)) {
@@ -77,6 +78,7 @@ public void onDrop(PlayerDropItemEvent event) {
7778
if (list != null) {
7879
Spellbook spellbook = MagicSpells.getSpellbook(event.getPlayer());
7980
for (PassiveSpell spell : list) {
81+
if (!isCancelStateOk(spell, event.isCancelled())) continue;
8082
if (spellbook.hasSpell(spell)) {
8183
boolean casted = spell.activate(event.getPlayer());
8284
if (PassiveListener.cancelDefaultAction(spell, casted)) {

src/com/nisovin/magicspells/spells/passive/EnterBedListener.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public void registerSpell(PassiveSpell spell, PassiveTrigger trigger, String var
2626
public void onDeath(PlayerBedEnterEvent event) {
2727
Spellbook spellbook = MagicSpells.getSpellbook(event.getPlayer());
2828
for (PassiveSpell spell : spells) {
29+
if (!isCancelStateOk(spell, event.isCancelled())) continue;
2930
if (spellbook.hasSpell(spell)) {
3031
spell.activate(event.getPlayer()); // TODO is this safe to cancel?
3132
}

src/com/nisovin/magicspells/spells/passive/FatalDamageListener.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ void onDamage(EntityDamageEvent event) {
3030
if (event.getFinalDamage() >= player.getHealth()) {
3131
Spellbook spellbook = MagicSpells.getSpellbook(player);
3232
for (PassiveSpell spell : spells) {
33+
if (!isCancelStateOk(spell, event.isCancelled())) continue;
3334
if (spellbook.hasSpell(spell)) {
3435
boolean casted = spell.activate(player);
3536
if (PassiveListener.cancelDefaultAction(spell, casted)) {

src/com/nisovin/magicspells/spells/passive/FishListener.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public void onFish(PlayerFishEvent event) {
6969
Spellbook spellbook = MagicSpells.getSpellbook(player);
7070
Entity entity = event.getCaught();
7171
for (PassiveSpell spell : allTypes) {
72+
if (!isCancelStateOk(spell, event.isCancelled())) continue;
7273
if (spellbook.hasSpell(spell)) {
7374
boolean casted = spell.activate(player, entity instanceof LivingEntity ? (LivingEntity)entity : null);
7475
if (PassiveListener.cancelDefaultAction(spell, casted)) {
@@ -81,6 +82,7 @@ public void onFish(PlayerFishEvent event) {
8182
if (state == State.IN_GROUND && ground.size() > 0) {
8283
Spellbook spellbook = MagicSpells.getSpellbook(player);
8384
for (PassiveSpell spell : ground) {
85+
if (!isCancelStateOk(spell, event.isCancelled())) continue;
8486
if (spellbook.hasSpell(spell)) {
8587
boolean casted = spell.activate(player, event.getHook().getLocation());
8688
if (PassiveListener.cancelDefaultAction(spell, casted)) {
@@ -91,6 +93,7 @@ public void onFish(PlayerFishEvent event) {
9193
} else if (state == State.CAUGHT_FISH && fish.size() > 0) {
9294
Spellbook spellbook = MagicSpells.getSpellbook(player);
9395
for (PassiveSpell spell : fish) {
96+
if (!isCancelStateOk(spell, event.isCancelled())) continue;
9497
if (spellbook.hasSpell(spell)) {
9598
boolean casted = spell.activate(player, event.getHook().getLocation());
9699
if (PassiveListener.cancelDefaultAction(spell, casted)) {
@@ -101,6 +104,7 @@ public void onFish(PlayerFishEvent event) {
101104
} else if (state == State.FAILED_ATTEMPT && fail.size() > 0) {
102105
Spellbook spellbook = MagicSpells.getSpellbook(player);
103106
for (PassiveSpell spell : fail) {
107+
if (!isCancelStateOk(spell, event.isCancelled())) continue;
104108
if (spellbook.hasSpell(spell)) {
105109
boolean casted = spell.activate(player, event.getHook().getLocation());
106110
if (PassiveListener.cancelDefaultAction(spell, casted)) {
@@ -113,6 +117,7 @@ public void onFish(PlayerFishEvent event) {
113117
if (entity != null && types.containsKey(entity.getType())) {
114118
Spellbook spellbook = MagicSpells.getSpellbook(player);
115119
for (PassiveSpell spell : fail) {
120+
if (!isCancelStateOk(spell, event.isCancelled())) continue;
116121
if (spellbook.hasSpell(spell)) {
117122
boolean casted = spell.activate(player, entity instanceof LivingEntity ? (LivingEntity)entity : null);
118123
if (PassiveListener.cancelDefaultAction(spell, casted)) {

0 commit comments

Comments
 (0)