-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e528da4
commit 63c9abf
Showing
12 changed files
with
169 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
src/main/java/dev/enjarai/trickster/cca/WardComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package dev.enjarai.trickster.cca; | ||
|
||
import dev.enjarai.trickster.spell.*; | ||
import dev.enjarai.trickster.spell.fragment.EntityFragment; | ||
import dev.enjarai.trickster.spell.fragment.FragmentType; | ||
import dev.enjarai.trickster.spell.fragment.ListFragment; | ||
import dev.enjarai.trickster.spell.tricks.Trick; | ||
import dev.enjarai.trickster.spell.tricks.blunder.BlunderException; | ||
import dev.enjarai.trickster.spell.tricks.blunder.WardModifiedSelfBlunder; | ||
import dev.enjarai.trickster.spell.tricks.blunder.WardReturnBlunder; | ||
import io.wispforest.endec.impl.KeyedEndec; | ||
import net.minecraft.entity.EquipmentSlot; | ||
import net.minecraft.entity.player.PlayerEntity; | ||
import net.minecraft.nbt.NbtCompound; | ||
import net.minecraft.registry.RegistryWrapper; | ||
import net.minecraft.server.network.ServerPlayerEntity; | ||
import net.minecraft.text.Text; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.ladysnake.cca.api.v3.component.Component; | ||
|
||
import java.util.List; | ||
|
||
public class WardComponent implements Component { | ||
private static final KeyedEndec<SpellPart> SPELL = SpellPart.ENDEC.keyed("handler", () -> null); | ||
|
||
private final PlayerEntity player; | ||
@Nullable | ||
private SpellPart spell; | ||
|
||
public WardComponent(PlayerEntity player) { | ||
this.player = player; | ||
} | ||
|
||
@Override | ||
public void writeToNbt(NbtCompound tag, RegistryWrapper.WrapperLookup registryLookup) { | ||
if (spell != null) | ||
tag.put(SPELL, spell); | ||
} | ||
|
||
@Override | ||
public void readFromNbt(NbtCompound tag, RegistryWrapper.WrapperLookup registryLookup) { | ||
spell = tag.get(SPELL); | ||
} | ||
|
||
public void register(SpellPart spell) { | ||
this.spell = spell; | ||
} | ||
|
||
public List<Fragment> run(SpellContext triggerCtx, Trick source, SpellPart triggerSpell, List<Fragment> inputs) throws BlunderException { | ||
if (spell == null) | ||
return inputs; | ||
|
||
var manaPool = ModEntityCumponents.MANA.get(player); | ||
var finalResult = inputs; | ||
boolean isModified = false; | ||
boolean applyBacklashIfModified = true; | ||
|
||
try { | ||
var ctx = new PlayerSpellContext((ServerPlayerEntity)this.player, EquipmentSlot.MAINHAND); | ||
ctx.pushPartGlyph(List.of(triggerSpell, new ListFragment(inputs))); | ||
|
||
var result = spell.run(ctx); | ||
ctx.popPartGlyph(); | ||
|
||
if (result.type() == FragmentType.LIST) { | ||
var newInputs = ((ListFragment)result).fragments(); | ||
int index = 0; | ||
|
||
if (newInputs.size() != inputs.size()) | ||
throw new WardReturnBlunder(); | ||
|
||
for (var input : inputs) { | ||
var inputType = input.type(); | ||
var newInput = newInputs.get(index); | ||
|
||
if (!isModified && !newInput.equals(input)) { | ||
isModified = true; | ||
} | ||
|
||
if (newInput.type() != inputType) | ||
throw new WardReturnBlunder(); | ||
|
||
if (inputType == FragmentType.ENTITY) { | ||
var entity = (EntityFragment)input; | ||
var newEntity = (EntityFragment)newInput; | ||
|
||
if (entity.uuid() == player.getUuid() && newEntity.uuid() != entity.uuid()) | ||
throw new WardModifiedSelfBlunder(); | ||
} | ||
|
||
index++; | ||
} | ||
|
||
finalResult = newInputs; | ||
} else throw new WardReturnBlunder(); | ||
} catch (BlunderException e) { | ||
isModified = true; | ||
applyBacklashIfModified = false; | ||
player.sendMessage(Text.literal("Ward failure: ").append(e.createMessage())); | ||
} | ||
|
||
if (isModified) { | ||
manaPool.decrease(14); | ||
|
||
if (applyBacklashIfModified) | ||
triggerCtx.useMana(source, 9); | ||
} | ||
|
||
return finalResult; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/main/java/dev/enjarai/trickster/spell/tricks/blunder/WardModifiedSelfBlunder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package dev.enjarai.trickster.spell.tricks.blunder; | ||
|
||
import net.minecraft.text.MutableText; | ||
import net.minecraft.text.Text; | ||
|
||
public class WardModifiedSelfBlunder extends BlunderException { | ||
@Override | ||
public MutableText createMessage() { | ||
return Text.literal("Ward handler replaced owner reference"); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/dev/enjarai/trickster/spell/tricks/blunder/WardReturnBlunder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package dev.enjarai.trickster.spell.tricks.blunder; | ||
|
||
import net.minecraft.text.MutableText; | ||
import net.minecraft.text.Text; | ||
|
||
public class WardReturnBlunder extends BlunderException { | ||
@Override | ||
public MutableText createMessage() { | ||
return Text.literal("Ward handler return invalid"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters