Don't kill creative players when stealing their spells, but do steal their spells

This commit is contained in:
Sollace 2023-08-28 14:07:20 +01:00
parent 77c993bfa7
commit 05256fdaf6
No known key found for this signature in database
GPG key ID: E52FACE7B5C773DB

View file

@ -27,6 +27,7 @@ import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.particle.ParticleTypes; import net.minecraft.particle.ParticleTypes;
import net.minecraft.predicate.entity.EntityPredicates;
import net.minecraft.sound.SoundCategory; import net.minecraft.sound.SoundCategory;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import net.minecraft.util.ActionResult; import net.minecraft.util.ActionResult;
@ -43,13 +44,16 @@ public class EnchantedStaffItem extends StaffItem implements EnchantableItem, Ch
return spellType; return spellType;
} }
public static SpellType<?> getSpellType(Entity entity) { public static SpellType<?> getSpellType(Entity entity, boolean remove) {
if (entity instanceof CastSpellEntity cast) { if (entity instanceof CastSpellEntity cast) {
return cast.getSpellSlot().get(c -> !SpellPredicate.IS_PLACED.test(c), true).map(Spell::getType).orElse(SpellType.empty()); return cast.getSpellSlot().get(c -> !SpellPredicate.IS_PLACED.test(c), true).map(Spell::getType).orElse(SpellType.empty());
} }
if (entity instanceof PlayerEntity player) { if (entity instanceof PlayerEntity player) {
if (remove) {
return Pony.of(player).getCharms().equipSpell(Hand.MAIN_HAND, SpellType.EMPTY_KEY.withTraits()).type(); return Pony.of(player).getCharms().equipSpell(Hand.MAIN_HAND, SpellType.EMPTY_KEY.withTraits()).type();
} }
return Pony.of(player).getCharms().getEquippedSpell(Hand.MAIN_HAND).type();
}
return ENTITY_TYPE_TO_SPELL.getOrDefault(entity.getType(), SpellType.empty()); return ENTITY_TYPE_TO_SPELL.getOrDefault(entity.getType(), SpellType.empty());
} }
@ -107,10 +111,13 @@ public class EnchantedStaffItem extends StaffItem implements EnchantableItem, Ch
super.useOnEntity(stack, player, target, hand); super.useOnEntity(stack, player, target, hand);
SpellType<?> type = getSpellType(target); boolean remove = EntityPredicates.EXCEPT_CREATIVE_OR_SPECTATOR.test(target);
SpellType<?> type = getSpellType(target, remove);
if (!type.isEmpty()) { if (!type.isEmpty()) {
if (remove) {
target.setHealth(1); target.setHealth(1);
target.setFrozenTicks(9000); target.setFrozenTicks(9000);
}
player.setStackInHand(hand, recharge(EnchantableItem.enchant(stack, type))); player.setStackInHand(hand, recharge(EnchantableItem.enchant(stack, type)));
} }
return ActionResult.SUCCESS; return ActionResult.SUCCESS;