Skip to content

Commit

Permalink
backport fixes for bugs found in 1.20.4 development to 1.19.2 (#444)
Browse files Browse the repository at this point in the history
  • Loading branch information
IchHabeHunger54 authored Apr 6, 2024
1 parent a93e3bd commit d5aaea5
Show file tree
Hide file tree
Showing 17 changed files with 77 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,14 @@ public interface ISpell {
boolean isEmpty();

/**
* @return Whether all parts of the spell are non-null. To check whether a spell is actually valid from a player perspective, use {@link ISpellHelper#isValidSpell(ISpell)} instead.
* @return Whether all parts of the spell are non-null.
*/
boolean isNonNull();

/**
* Validates the spell. This checks for non-emptiness of the spell stack, the shape groups being correct, and correct location of modifiers (if present).
*
* @return Whether the spell is valid or not.
*/
boolean isValid();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.HitResult;
Expand All @@ -32,14 +31,6 @@ public interface ISpellHelper {
*/
void setSpell(ItemStack stack, ISpell spell);

/**
* Validates the given spell. This checks for non-emptiness of the spell stack, the shape groups being correct, and correct location of modifiers (if present).
*
* @param spell The spell to validate.
* @return Whether the given spell is valid or not.
*/
boolean isValidSpell(ISpell spell);

/**
* Returns the item stack containing the spell the given entity is currently using, taking spell books into consideration.
* Returns empty if the entity is not currently holding anything that can be interpreted as a spell.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ protected void addTranslations() {
blockIdTranslation(AMBlocks.CELESTIAL_PRISM);
blockIdTranslation(AMBlocks.BLACK_AUREM);
addBlock(AMBlocks.WIZARDS_CHALK, "Wizard's Chalk");
blockIdTranslation(AMBlocks.SPELL_RUNE);
itemIdTranslation(AMItems.MAGITECH_GOGGLES);
itemIdTranslation(AMItems.CRYSTAL_WRENCH);
blockIdTranslation(AMBlocks.CHIMERITE_ORE);
Expand Down Expand Up @@ -502,6 +503,7 @@ protected void addTranslations() {
add(TranslationConstants.SPELL_RECIPE_UNKNOWN, "Unknown Item");
add(TranslationConstants.SPELL_UNKNOWN, "Unknown Item");
add(TranslationConstants.SPELL_UNNAMED, "Unnamed Spell");
add(TranslationConstants.INSCRIPTION_TABLE_DEFAULT_NAME, "Spell");
add(TranslationConstants.INSCRIPTION_TABLE_NAME, "Name");
add(TranslationConstants.INSCRIPTION_TABLE_SEARCH, "Search");
add(TranslationConstants.INSCRIPTION_TABLE_TITLE, "Inscription Table");
Expand Down
2 changes: 2 additions & 0 deletions src/main/generated/assets/arsmagicalegacy/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
"block.arsmagicalegacy.obelisk": "Obelisk",
"block.arsmagicalegacy.occulus": "Occulus",
"block.arsmagicalegacy.redstone_inlay": "Redstone Inlay",
"block.arsmagicalegacy.spell_rune": "Spell Rune",
"block.arsmagicalegacy.stripped_witchwood": "Stripped Witchwood",
"block.arsmagicalegacy.stripped_witchwood_log": "Stripped Witchwood Log",
"block.arsmagicalegacy.sunstone_block": "Block of Sunstone",
Expand Down Expand Up @@ -756,6 +757,7 @@
"screen.arsmagicalegacy.inscription_table.color_picker.done": "Done",
"screen.arsmagicalegacy.inscription_table.color_picker.title": "Choose Color",
"screen.arsmagicalegacy.inscription_table.create_spell": "Create Spell",
"screen.arsmagicalegacy.inscription_table.default_name": "Spell",
"screen.arsmagicalegacy.inscription_table.name": "Name",
"screen.arsmagicalegacy.inscription_table.search": "Search",
"screen.arsmagicalegacy.inscription_table.title": "Inscription Table",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,7 @@ protected void init() {
if (Objects.requireNonNull(getMinecraft().player).isCreative()) {
addRenderableWidget(new Button(leftPos + 72, topPos + 72, 100, 20, Component.translatable(TranslationConstants.INSCRIPTION_TABLE_CREATE_SPELL), button -> {
sync();
menu.getSpellRecipe().ifPresent(e -> {
if (ArsMagicaAPI.get().getSpellHelper().isValidSpell(e)) {
menu.createSpell();
}
});
menu.createSpell();
}));
}
sourceArea = new SpellPartSourceArea(leftPos + 42, topPos + 6, 136, 48);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
import java.util.Queue;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class AltarCoreBlockEntity extends BlockEntity implements IEtheriumConsumer {
Expand Down Expand Up @@ -431,7 +430,7 @@ public boolean isLeverActive() {
@Nullable
public Queue<ISpellIngredient> getRecipe() {
if (recipe == null || recipe.isEmpty()) {
getSpell().filter(ISpell::isValid).filter(((Predicate<ISpell>) ISpell::isEmpty).negate()).ifPresentOrElse(spell -> {
getSpell().filter(ISpell::isValid).ifPresentOrElse(spell -> {
this.recipe = new ArrayDeque<>(spell.recipe());
requiredPower = this.recipe.size();
}, () -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public Optional<ItemStack> saveRecipe(ItemStack stack) {
public void createSpell(ServerPlayer player) {
ItemStack spell = new ItemStack(AMItems.SPELL.get());
ArsMagicaAPI.get().getSpellHelper().setSpell(spell, Objects.requireNonNull(getSpellRecipe()));
ArsMagicaAPI.get().getSpellHelper().setSpellName(spell, spellName);
ArsMagicaAPI.get().getSpellHelper().setSpellName(spell, spellName != null ? spellName : Component.translatable(TranslationConstants.INSCRIPTION_TABLE_DEFAULT_NAME));
player.addItem(spell);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public Optional<ISpell> getSpellRecipe() {

public void createSpell() {
Optional<ISpell> recipe = getSpellRecipe();
if (recipe.isPresent() && !recipe.get().isEmpty() && recipe.get().isValid()) {
if (recipe.isPresent() && recipe.get().isValid()) {
ArsMagicaLegacy.NETWORK_HANDLER.sendToServer(new InscriptionTableCreateSpellPacket(table.getBlockPos()));
}
}
Expand All @@ -156,7 +156,7 @@ public boolean mayPlace(ItemStack stack) {

@Override
public Optional<ItemStack> tryRemove(int p_150642_, int p_150643_, Player player) {
if (table.getSpellRecipe() != null && !ArsMagicaAPI.get().getSpellHelper().isValidSpell(table.getSpellRecipe())) return super.tryRemove(p_150642_, p_150643_, player);
if (table.getSpellRecipe() != null && !table.getSpellRecipe().isValid()) return super.tryRemove(p_150642_, p_150643_, player);
player.getLevel().playSound(null, table.getBlockPos().getX(), table.getBlockPos().getY(), table.getBlockPos().getZ(), AMSounds.INSCRIPTION_TABLE_TAKE_BOOK.get(), SoundSource.BLOCKS, 1f, 1f);
return super.tryRemove(p_150642_, p_150643_, player).flatMap(stack -> stack.getItem() instanceof ISpellItem ? Optional.of(stack) : table.saveRecipe(stack));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtOps;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.Level;
Expand All @@ -20,12 +21,16 @@
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.EntityHitResult;

import java.util.UUID;

public class SpellRuneBlockEntity extends BlockEntity {
public static final String SPELL_KEY = ArsMagicaAPI.MOD_ID + ":spell";
public static final String INDEX_KEY = ArsMagicaAPI.MOD_ID + ":index";
public static final String CASTER_KEY = ArsMagicaAPI.MOD_ID + ":caster";
public static final String AWARD_XP_KEY = ArsMagicaAPI.MOD_ID + ":award_xp";
private ISpell spell;
private Integer index;
private UUID casterId;
private LivingEntity caster;
private Boolean awardXp;

Expand All @@ -39,6 +44,9 @@ protected void saveAdditional(CompoundTag pTag) {
if (index != null) {
pTag.putInt(INDEX_KEY, index);
}
if (casterId != null) {
pTag.putUUID(CASTER_KEY, casterId);
}
if (awardXp != null) {
pTag.putBoolean(AWARD_XP_KEY, awardXp);
}
Expand All @@ -53,6 +61,9 @@ public void load(CompoundTag pTag) {
if (pTag.contains(INDEX_KEY)) {
index = pTag.getInt(INDEX_KEY);
}
if (pTag.contains(CASTER_KEY)) {
casterId = pTag.getUUID(CASTER_KEY);
}
if (pTag.contains(AWARD_XP_KEY)) {
awardXp = pTag.getBoolean(AWARD_XP_KEY);
}
Expand All @@ -69,7 +80,10 @@ public void load(CompoundTag pTag) {
*/
public void collide(Level level, BlockPos pos, Entity entity, Direction direction) {
var helper = ArsMagicaAPI.get().getSpellHelper();
if (spell == null) return;
if (caster == null && casterId != null && level instanceof ServerLevel server && server.getEntity(casterId) instanceof LivingEntity living) {
caster = living;
}
if (spell == null || caster == null) return;
SpellCastResult r1 = helper.invoke(spell, caster, level, new EntityHitResult(entity), 0, index, awardXp);
SpellCastResult r2 = helper.invoke(spell, caster, level, new BlockHitResult(entity.position(), direction, pos, false), 0, index, awardXp);
if (r1.isSuccess() || r2.isSuccess()) {
Expand All @@ -88,6 +102,7 @@ public void collide(Level level, BlockPos pos, Entity entity, Direction directio
public void setSpell(ISpell spell, LivingEntity caster, int index, boolean awardXp) {
this.spell = spell;
this.index = index;
this.casterId = caster.getUUID();
this.caster = caster;
this.awardXp = awardXp;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ protected void registerGoals() {
@Override
public void aiStep() {
if (tickCount % 30 == 0) {
level.playSound(null, this, AMSounds.FIRE_GUARDIAN_NOVA.get(), SoundSource.HOSTILE, 1f, 0.5f + level.getRandom().nextFloat() * 0.5f);
if (level.getRandom().nextInt(10) == 0) {
level.playSound(null, this, AMSounds.FIRE_GUARDIAN_NOVA.get(), SoundSource.HOSTILE, 0.1f, 0.5f + level.getRandom().nextFloat() * 0.5f);
}
if (level.isClientSide()) {
// Particles
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void appendHoverText(ItemStack pStack, @Nullable Level pLevel, List<Compo
return;
}
ISpell spell = api.getSpellHelper().getSpell(pStack);
if (spell.isEmpty() || !spell.isValid()) {
if (!spell.isValid()) {
pTooltipComponents.add(Component.translatable(TranslationConstants.SPELL_INVALID_DESCRIPTION));
return;
}
Expand Down Expand Up @@ -97,7 +97,7 @@ public Component getName(ItemStack pStack) {
}
var helper = api.getSpellHelper();
ISpell spell = helper.getSpell(pStack);
if (spell.isEmpty() || !spell.isValid()) return Component.translatable(TranslationConstants.SPELL_INVALID);
if (!spell.isValid()) return Component.translatable(TranslationConstants.SPELL_INVALID);
return helper.getSpellName(pStack).orElseGet(() -> pStack.hasCustomHoverName() ? pStack.getHoverName() : Component.translatable(TranslationConstants.SPELL_UNNAMED));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public Component getName(ItemStack pStack) {
}
var helper = api.getSpellHelper();
ISpell spell = helper.getSpell(pStack);
if (spell.isEmpty() || !spell.isValid()) return Component.translatable(TranslationConstants.SPELL_RECIPE_INVALID);
if (!spell.isValid()) return Component.translatable(TranslationConstants.SPELL_RECIPE_INVALID);
return helper.getSpellName(pStack).orElse(super.getName(pStack));
}

Expand All @@ -85,7 +85,7 @@ public InteractionResultHolder<ItemStack> use(Level level, Player player, Intera
return InteractionResultHolder.fail(stack);
}
ISpell spell = api.getSpellHelper().getSpell(stack);
if (!spell.isValid() || spell.isEmpty()) {
if (!spell.isValid()) {
player.displayClientMessage(Component.translatable(TranslationConstants.SPELL_RECIPE_INVALID_DESCRIPTION), true);
return InteractionResultHolder.fail(stack);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public final class Spell implements ISpell {
private final CompoundTag additionalData;
private final Lazy<Boolean> continuous;
private final Lazy<Boolean> empty;
private final Lazy<Boolean> nonNull;
private final Lazy<Boolean> valid;

public Spell(List<ShapeGroup> shapeGroups, SpellStack spellStack, CompoundTag additionalData) {
Expand All @@ -61,9 +62,10 @@ public Spell(List<ShapeGroup> shapeGroups, SpellStack spellStack, CompoundTag ad
this.additionalData = additionalData;
continuous = Lazy.concurrentOf(() -> firstShape(currentShapeGroupIndex()).filter(ISpellShape::isContinuous).isPresent());
empty = Lazy.concurrentOf(() -> (shapeGroups().isEmpty() || shapeGroups().stream().allMatch(ShapeGroup::isEmpty)) && spellStack().isEmpty());
valid = Lazy.concurrentOf(() -> Stream.concat(shapeGroups().stream().map(ShapeGroup::parts).flatMap(Collection::stream), spellStack().parts().stream())
nonNull = Lazy.concurrentOf(() -> Stream.concat(shapeGroups().stream().map(ShapeGroup::parts).flatMap(Collection::stream), spellStack().parts().stream())
.map(ArsMagicaAPI.get().getSpellDataManager()::getDataForPart)
.allMatch(Objects::nonNull));
valid = Lazy.concurrentOf(this::validate);
}

public static Spell of(SpellStack spellStack, ShapeGroup... shapeGroups) {
Expand All @@ -80,6 +82,11 @@ public boolean isEmpty() {
return empty.get();
}

@Override
public boolean isNonNull() {
return nonNull.get();
}

@Override
public boolean isValid() {
return valid.get();
Expand Down Expand Up @@ -337,4 +344,29 @@ public int hashCode() {
public String toString() {
return "Spell[shapeGroups=" + shapeGroups + ", spellStack=" + spellStack + ", additionalData=" + additionalData + ']';
}

private boolean validate() {
if (isEmpty() || !isNonNull()) return false;
//check spell stack
if (spellStack().isEmpty()) return false;
if (spellStack().parts().get(0).getType() != ISpellPart.SpellPartType.COMPONENT) return false;
//find last non-empty shape group
List<ShapeGroup> groups = shapeGroups();
if (groups.stream().allMatch(ShapeGroup::isEmpty)) return false;
int last = -1;
for (int i = 0; i < groups.size(); i++) {
if (!groups.get(i).isEmpty()) {
last = i;
}
}
//check for empty shape groups between other non-empty shape groups
if (last == -1) return false;
groups = groups.stream().filter(e -> !e.isEmpty()).toList();
if (last != groups.size() - 1) return false;
//check shape groups themselves
for (ShapeGroup group : groups) {
if (group.parts().get(0).getType() != ISpellPart.SpellPartType.SHAPE) return false;
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import com.github.minecraftschurlimods.arsmagicalegacy.api.spell.ISpellPartStatModifier;
import com.github.minecraftschurlimods.arsmagicalegacy.api.spell.ISpellParticleSpawner;
import com.github.minecraftschurlimods.arsmagicalegacy.api.spell.ISpellShape;
import com.github.minecraftschurlimods.arsmagicalegacy.api.spell.ShapeGroup;
import com.github.minecraftschurlimods.arsmagicalegacy.api.spell.SpellCastResult;
import com.github.minecraftschurlimods.arsmagicalegacy.api.util.ItemFilter;
import com.github.minecraftschurlimods.arsmagicalegacy.common.item.spellbook.SpellBookItem;
Expand Down Expand Up @@ -79,32 +78,6 @@ public void setSpell(ItemStack stack, ISpell spell) {
stack.getOrCreateTag().put(SPELL_KEY, ISpell.CODEC.encodeStart(NbtOps.INSTANCE, spell).get().mapRight(DataResult.PartialResult::message).ifRight(ArsMagicaLegacy.LOGGER::warn).left().orElse(new CompoundTag()));
}

@Override
public boolean isValidSpell(ISpell spell) {
//check spell stack
if (spell.spellStack().isEmpty()) return false;
if (spell.spellStack().parts().get(0).getType() != ISpellPart.SpellPartType.COMPONENT) return false;
//find last non-empty shape group
List<ShapeGroup> groups = spell.shapeGroups();
if (groups.stream().allMatch(ShapeGroup::isEmpty)) return false;
int last = -1;
for (int i = 0; i < groups.size(); i++) {
ShapeGroup group = groups.get(i);
if (!group.isEmpty()) {
last = i;
}
}
//check for empty shape groups between other non-empty shape groups
if (last == -1) return false;
groups = groups.stream().filter(e -> !e.isEmpty()).toList();
if (last != groups.size() - 1) return false;
//check shape groups themselves
for (ShapeGroup group : groups) {
if (group.parts().get(0).getType() != ISpellPart.SpellPartType.SHAPE) return false;
}
return true;
}

@Override
public ItemStack getSpellItemStackFromEntity(LivingEntity entity) {
ItemStack stack = getSpellItemStackInHand(entity, InteractionHand.MAIN_HAND);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.github.minecraftschurlimods.arsmagicalegacy.api.spell.ISpellModifier;
import com.github.minecraftschurlimods.arsmagicalegacy.api.spell.SpellCastResult;
import com.github.minecraftschurlimods.arsmagicalegacy.common.spell.SpellPartStats;
import com.github.minecraftschurlimods.arsmagicalegacy.common.util.AMUtil;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.damagesource.DamageSource;
Expand Down Expand Up @@ -62,6 +61,7 @@ private static SpellCastResult performStorm(LivingEntity caster, Level level, Li
} else {
((ServerLevel) level).setWeatherParameters(0, (int) ArsMagicaAPI.get().getSpellHelper().getModifiedStat(200000, SpellPartStats.DURATION, modifiers, spell, caster, target, index), true, true);
}
return SpellCastResult.SUCCESS;
}
return SpellCastResult.EFFECT_FAILED;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public SpellCastResult invoke(ISpell spell, LivingEntity caster, Level level, Li
return SpellCastResult.EFFECT_FAILED;
Vec3 targetPos = target.getEntity().position();
Vec3 casterPos = caster.position();
target.getEntity().moveTo(casterPos);
caster.moveTo(targetPos);
target.getEntity().teleportTo(casterPos.x(), casterPos.y(), casterPos.z());
caster.teleportTo(targetPos.x(), targetPos.y(), targetPos.z());
return SpellCastResult.SUCCESS;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public interface TranslationConstants {
String SPELL_RECIPE_UNKNOWN = "item." + ArsMagicaAPI.MOD_ID + ".spell_recipe.unknown";
String SPELL_UNKNOWN = "item." + ArsMagicaAPI.MOD_ID + ".spell.unknown";
String SPELL_UNNAMED = "item." + ArsMagicaAPI.MOD_ID + ".spell.unnamed";
String INSCRIPTION_TABLE_DEFAULT_NAME = "screen." + ArsMagicaAPI.MOD_ID + ".inscription_table.default_name";
String INSCRIPTION_TABLE_NAME = "screen." + ArsMagicaAPI.MOD_ID + ".inscription_table.name";
String INSCRIPTION_TABLE_SEARCH = "screen." + ArsMagicaAPI.MOD_ID + ".inscription_table.search";
String INSCRIPTION_TABLE_TITLE = "screen." + ArsMagicaAPI.MOD_ID + ".inscription_table.title";
Expand Down

0 comments on commit d5aaea5

Please sign in to comment.