Skip to content

Commit

Permalink
Merge pull request #837 from TonytheMacaroni/main
Browse files Browse the repository at this point in the history
Use material tags to check if items are equippable, adjust gamemode targeting
  • Loading branch information
Chronoken authored Jan 23, 2024
2 parents 15596ee + b7218f3 commit 7ce7a43
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.kyori.adventure.text.Component;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.ChatColor;
import org.bukkit.util.Vector;
import org.bukkit.entity.Item;
Expand All @@ -24,6 +25,8 @@
import org.bukkit.event.entity.EntityPickupItemEvent;
import org.bukkit.event.inventory.InventoryOpenEvent;

import com.destroystokyo.paper.MaterialTags;

import com.nisovin.magicspells.Spell;
import com.nisovin.magicspells.util.*;
import com.nisovin.magicspells.MagicSpells;
Expand Down Expand Up @@ -319,16 +322,18 @@ private void conjureItems(Player player, SpellData data) {
boolean added = false;
PlayerInventory inv = player.getInventory();
if (autoEquip && item.getAmount() == 1) {
if (item.getType().name().endsWith("HELMET") && isNothing(inv.getHelmet())) {
Material type = item.getType();

if (MaterialTags.HEAD_EQUIPPABLE.isTagged(type) && isNothing(inv.getHelmet())) {
inv.setHelmet(item);
added = true;
} else if (item.getType().name().endsWith("CHESTPLATE") && isNothing(inv.getChestplate())) {
} else if (MaterialTags.CHEST_EQUIPPABLE.isTagged(type) && isNothing(inv.getChestplate())) {
inv.setChestplate(item);
added = true;
} else if (item.getType().name().endsWith("LEGGINGS") && isNothing(inv.getLeggings())) {
} else if (MaterialTags.LEGGINGS.isTagged(type) && isNothing(inv.getLeggings())) {
inv.setLeggings(item);
added = true;
} else if (item.getType().name().endsWith("BOOTS") && isNothing(inv.getBoots())) {
} else if (MaterialTags.BOOTS.isTagged(type) && isNothing(inv.getBoots())) {
inv.setBoots(item);
added = true;
}
Expand Down
16 changes: 0 additions & 16 deletions core/src/main/java/com/nisovin/magicspells/util/EntityData.java
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,6 @@ public Entity spawn(@NotNull Location location, @Nullable SpellData data, @Nulla
if (entityType == null || (!entityType.isSpawnable() && entityType != EntityType.FALLING_BLOCK && entityType != EntityType.DROPPED_ITEM))
return null;

boolean[] displayHack = new boolean[] {false, false};
Entity entity = switch (entityType) {
case FALLING_BLOCK -> {
BlockData blockData = fallingBlockData.get(data);
Expand Down Expand Up @@ -371,25 +370,10 @@ public Entity spawn(@NotNull Location location, @Nullable SpellData data, @Nulla
transformer.apply(e, data);

if (consumer != null) consumer.accept(e);

if (e instanceof Display) {
String version = org.bukkit.Bukkit.getMinecraftVersion();
if ("1.19.4".equals(version) || "1.20".equals(version)) {
displayHack[0] = true;
displayHack[1] = e.isVisibleByDefault();

e.setVisibleByDefault(false);
}
}
});
}
};

if (displayHack[0]) {
entity.teleport(startLoc);
entity.setVisibleByDefault(displayHack[1]);
}

return entity;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ public enum TargetingElement {

}

private static final Set<GameMode> DEFAULT_GAME_MODES = EnumSet.of(GameMode.SURVIVAL, GameMode.ADVENTURE);

private final Set<GameMode> gameModes = EnumSet.noneOf(GameMode.class);
private final Set<EntityType> types = EnumSet.noneOf(EntityType.class);

Expand Down Expand Up @@ -95,7 +93,6 @@ private void init(Spell spell, List<String> list) {

switch (s.toLowerCase()) {
case "self", "caster" -> targetSelf = true;
case "player", "players" -> targetPlayers = true;
case "invisible", "invisibles" -> targetInvisibles = true;
case "nonplayer", "nonplayers" -> targetNonPlayers = true;
case "monster", "monsters" -> targetMonsters = true;
Expand All @@ -105,6 +102,11 @@ private void init(Spell spell, List<String> list) {
case "castermount", "selfmount" -> targetCasterMount = true;
case "casterpassenger", "selfpassenger" -> targetCasterPassenger = true;
case "entitytarget", "mobtarget" -> targetEntityTarget = true;
case "player", "players" -> {
gameModes.add(GameMode.SURVIVAL);
gameModes.add(GameMode.ADVENTURE);
targetPlayers = true;
}
default -> {
try {
gameModes.add(GameMode.valueOf(s.toUpperCase()));
Expand All @@ -116,6 +118,11 @@ private void init(Spell spell, List<String> list) {
else MagicSpells.error("Spell '" + spell.getInternalName() + "' has an invalid target type defined: " + s);
}
}

if (gameModes.isEmpty()) {
gameModes.add(GameMode.SURVIVAL);
gameModes.add(GameMode.ADVENTURE);
}
}
}

Expand All @@ -133,7 +140,7 @@ public boolean canTarget(LivingEntity caster, Entity target, boolean targetPlaye
if (!(target instanceof LivingEntity) && !targetNonLivingEntities) return false;
boolean targetIsPlayer = target instanceof Player;

if (targetIsPlayer && !canTargetGameMode(((Player) target).getGameMode())) return false;
if (targetIsPlayer && !gameModes.contains(((Player) target).getGameMode())) return false;

if (targetSelf && target.equals(caster)) return true;
if (!targetSelf && target.equals(caster)) return false;
Expand All @@ -159,7 +166,7 @@ public boolean canTarget(Entity target) {
if (!(target instanceof LivingEntity) && !targetNonLivingEntities) return false;
boolean targetIsPlayer = target instanceof Player;

if (targetIsPlayer && !canTargetGameMode(((Player) target).getGameMode())) return false;
if (targetIsPlayer && !gameModes.contains(((Player) target).getGameMode())) return false;

if (targetPlayers && targetIsPlayer) return true;
if (targetNonPlayers && !targetIsPlayer) return true;
Expand All @@ -174,16 +181,11 @@ public boolean canTarget(Entity target) {
return false;
}

private boolean canTargetGameMode(GameMode gameMode) {
if (gameModes.isEmpty()) return DEFAULT_GAME_MODES.contains(gameMode);
return gameModes.contains(gameMode);
}

public boolean canTarget(Entity target, boolean ignoreGameMode) {
if (!(target instanceof LivingEntity) && !targetNonLivingEntities) return false;
boolean targetIsPlayer = target instanceof Player;

if (!ignoreGameMode && targetIsPlayer && !canTargetGameMode(((Player) target).getGameMode())) return false;
if (!ignoreGameMode && targetIsPlayer && !gameModes.contains(((Player) target).getGameMode())) return false;

if (targetPlayers && targetIsPlayer) return true;
if (targetNonPlayers && !targetIsPlayer) return true;
Expand Down

0 comments on commit 7ce7a43

Please sign in to comment.