Skip to content

Commit

Permalink
Baller
Browse files Browse the repository at this point in the history
  • Loading branch information
enjarai committed Jun 11, 2024
1 parent 4f2d300 commit efb2b67
Show file tree
Hide file tree
Showing 27 changed files with 478 additions and 23 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ dependencies {

modImplementation "io.wispforest:lavender:${project.lavender_version}"
// implementation "io.wispforest:endec:0.1.0-pre.8"
modLocalRuntime "io.wispforest.lavender-md:owo-ui:0.1.2+1.21"
}

processResources {
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/dev/enjarai/trickster/spell/Fragment.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.enjarai.trickster.spell;

import com.mojang.serialization.MapCodec;
import dev.enjarai.trickster.spell.fragment.BooleanFragment;
import dev.enjarai.trickster.spell.fragment.FragmentType;

public interface Fragment {
Expand All @@ -9,4 +10,6 @@ public interface Fragment {
FragmentType<?> type();

String asString();

BooleanFragment asBoolean();
}
6 changes: 6 additions & 0 deletions src/main/java/dev/enjarai/trickster/spell/PatternGlyph.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.MapCodec;
import dev.enjarai.trickster.spell.fragment.BooleanFragment;
import dev.enjarai.trickster.spell.fragment.FragmentType;
import dev.enjarai.trickster.spell.tricks.Tricks;
import dev.enjarai.trickster.spell.tricks.blunder.BlunderException;
Expand Down Expand Up @@ -43,4 +44,9 @@ public FragmentType<?> type() {
public String asString() {
return "TODO"; // TODO
}

@Override
public BooleanFragment asBoolean() {
return BooleanFragment.TRUE;
}
}
12 changes: 11 additions & 1 deletion src/main/java/dev/enjarai/trickster/spell/SpellContext.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
package dev.enjarai.trickster.spell;

import dev.enjarai.trickster.spell.tricks.blunder.BlunderException;
import dev.enjarai.trickster.spell.tricks.blunder.RecursionLimitReachedBlunder;
import net.minecraft.server.network.ServerPlayerEntity;
import org.jetbrains.annotations.Nullable;

import java.util.*;

public class SpellContext {
public static final int MAX_RECURSION_DEPTH = 1000;

@Nullable
private final ServerPlayerEntity player;
private final Deque<List<Fragment>> partGlyphStack = new ArrayDeque<>();
private int recursions = 0;

public SpellContext(@Nullable ServerPlayerEntity player) {
this.player = player;
}

public void pushPartGlyph(List<Fragment> fragments) {
public void pushPartGlyph(List<Fragment> fragments) throws BlunderException {
partGlyphStack.push(fragments);
recursions++;
if (recursions > MAX_RECURSION_DEPTH) {
throw new RecursionLimitReachedBlunder();
}
}

public void popPartGlyph() {
partGlyphStack.pop();
recursions--;
}

public List<Fragment> peekPartGlyph() {
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/dev/enjarai/trickster/spell/SpellPart.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import dev.enjarai.trickster.spell.fragment.BooleanFragment;
import dev.enjarai.trickster.spell.fragment.FragmentType;
import dev.enjarai.trickster.spell.tricks.blunder.BlunderException;
import io.wispforest.endec.Endec;
Expand Down Expand Up @@ -113,4 +114,9 @@ public FragmentType<?> type() {
public String asString() {
return "TODO"; // TODO
}

@Override
public BooleanFragment asBoolean() {
return BooleanFragment.TRUE; // TODO
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package dev.enjarai.trickster.spell.fragment;

import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
import dev.enjarai.trickster.spell.Fragment;

public record BooleanFragment(boolean bool) implements Fragment {
public static final MapCodec<BooleanFragment> CODEC = Codec.BOOL
.fieldOf("bool").xmap(BooleanFragment::new, BooleanFragment::bool);
public static final BooleanFragment TRUE = new BooleanFragment(true);
public static final BooleanFragment FALSE = new BooleanFragment(false);

@Override
public FragmentType<?> type() {
return FragmentType.BOOLEAN;
}

@Override
public String asString() {
return "" + bool;
}

@Override
public BooleanFragment asBoolean() {
return bool ? TRUE : FALSE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package dev.enjarai.trickster.spell.fragment;

import com.mojang.serialization.MapCodec;
import dev.enjarai.trickster.spell.Fragment;
import net.minecraft.text.Text;
import net.minecraft.util.Uuids;

import java.util.UUID;

public record EntityFragment(UUID uuid, Text name) implements Fragment {
public static final MapCodec<EntityFragment> CODEC = Uuids.CODEC
.xmap(uuid -> new EntityFragment(uuid, Text.of(uuid)), EntityFragment::uuid).fieldOf("uuid");

@Override
public FragmentType<?> type() {
return FragmentType.ENTITY;
}

@Override
public String asString() {
return name.getString();
}

@Override
public BooleanFragment asBoolean() {
return BooleanFragment.TRUE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ public record FragmentType<T extends Fragment>(MapCodec<T> codec) {
public static final Registry<FragmentType<?>> REGISTRY = new SimpleRegistry<>(REGISTRY_KEY, Lifecycle.stable());

public static final FragmentType<NumberFragment> NUMBER = register("number", NumberFragment.CODEC);
public static final FragmentType<BooleanFragment> BOOLEAN = register("boolean", BooleanFragment.CODEC);
public static final FragmentType<VectorFragment> VECTOR = register("vector", VectorFragment.CODEC);
public static final FragmentType<ListFragment> LIST = register("list", ListFragment.CODEC);
public static final FragmentType<VoidFragment> VOID = register("void", VoidFragment.CODEC);
public static final FragmentType<PatternGlyph> PATTERN = register("pattern", PatternGlyph.MAP_CODEC);
public static final FragmentType<SpellPart> SPELL_PART = register("spell_part", SpellPart.MAP_CODEC);
public static final FragmentType<EntityFragment> ENTITY = register("entity", EntityFragment.CODEC);

private static <T extends Fragment> FragmentType<T> register(String name, MapCodec<T> codec) {
return Registry.register(REGISTRY, Trickster.id(name), new FragmentType<>(codec));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@ public FragmentType<?> type() {
public String asString() {
return "[" + fragments.stream().map(Fragment::asString).collect(Collectors.joining(", ")) + "]";
}

@Override
public BooleanFragment asBoolean() {
return new BooleanFragment(!fragments.isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import dev.enjarai.trickster.spell.tricks.blunder.DivideByZeroBlunder;
import dev.enjarai.trickster.spell.tricks.blunder.IncompatibleTypesBlunder;

public record NumberFragment(double number) implements Fragment, AddableFragment, SubtractableFragment, MultiplicableFragment, DivisibleFragment {
public record NumberFragment(double number) implements Fragment, AddableFragment, SubtractableFragment, MultiplicableFragment, DivisibleFragment, RoundableFragment {
public static final MapCodec<NumberFragment> CODEC =
Codec.DOUBLE.fieldOf("number").xmap(NumberFragment::new, NumberFragment::number);

Expand All @@ -22,6 +22,11 @@ public String asString() {
return String.valueOf(number);
}

@Override
public BooleanFragment asBoolean() {
return new BooleanFragment(number == 0);
}

@Override
public AddableFragment add(Fragment other) throws BlunderException {
if (other instanceof NumberFragment num) {
Expand Down Expand Up @@ -57,4 +62,19 @@ public DivisibleFragment divide(Fragment other) throws BlunderException {
}
throw new IncompatibleTypesBlunder(Tricks.DIVIDE);
}

@Override
public RoundableFragment floor() throws BlunderException {
return new NumberFragment(Math.floor(number));
}

@Override
public RoundableFragment ceil() throws BlunderException {
return new NumberFragment(Math.ceil(number));
}

@Override
public RoundableFragment round() throws BlunderException {
return new NumberFragment(Math.round(number));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package dev.enjarai.trickster.spell.fragment;

import dev.enjarai.trickster.spell.Fragment;
import dev.enjarai.trickster.spell.tricks.blunder.BlunderException;

public interface RoundableFragment extends Fragment {
RoundableFragment floor() throws BlunderException;

RoundableFragment ceil() throws BlunderException;

RoundableFragment round() throws BlunderException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package dev.enjarai.trickster.spell.fragment;

import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.MapCodec;
import dev.enjarai.trickster.spell.Fragment;
import dev.enjarai.trickster.spell.tricks.Tricks;
import dev.enjarai.trickster.spell.tricks.blunder.BlunderException;
import dev.enjarai.trickster.spell.tricks.blunder.DivideByZeroBlunder;
import dev.enjarai.trickster.spell.tricks.blunder.IncompatibleTypesBlunder;
import org.joml.Vector3d;
import org.joml.Vector3dc;

import java.util.List;

public record VectorFragment(Vector3dc vector) implements Fragment, AddableFragment, SubtractableFragment, MultiplicableFragment, DivisibleFragment, RoundableFragment {
public static final MapCodec<VectorFragment> CODEC = Codec.DOUBLE.listOf(3, 3)
.<Vector3dc>xmap(list -> new Vector3d(list.get(0), list.get(1), list.get(2)), vec -> List.of(vec.x(), vec.y(), vec.z()))
.xmap(VectorFragment::new, VectorFragment::vector)
.fieldOf("vector");

@Override
public FragmentType<?> type() {
return FragmentType.VECTOR;
}

@Override
public String asString() {
return "(" + vector.x() + ", " + vector.y() + ", " + vector.z() + ")";
}

@Override
public BooleanFragment asBoolean() {
return new BooleanFragment(!new Vector3d().equals(vector));
}

@Override
public AddableFragment add(Fragment other) throws BlunderException {
if (other instanceof VectorFragment vec) {
return new VectorFragment(vector.add(vec.vector, new Vector3d()));
}
throw new IncompatibleTypesBlunder(Tricks.ADD);
}

@Override
public SubtractableFragment subtract(Fragment other) throws BlunderException {
if (other instanceof VectorFragment vec) {
return new VectorFragment(vector.sub(vec.vector, new Vector3d()));
}
throw new IncompatibleTypesBlunder(Tricks.SUBTRACT);
}

@Override
public MultiplicableFragment multiply(Fragment other) throws BlunderException {
if (other instanceof NumberFragment num) {
return new VectorFragment(vector.mul(num.number(), new Vector3d()));
} else if (other instanceof VectorFragment vec) {
return new VectorFragment(vector.mul(vec.vector, new Vector3d()));
}
throw new IncompatibleTypesBlunder(Tricks.MULTIPLY);
}

@Override
public DivisibleFragment divide(Fragment other) throws BlunderException {
if (other instanceof VectorFragment vec) {
if (vec.vector.x() == 0 || vec.vector.y() == 0 || vec.vector.z() == 0) {
throw new DivideByZeroBlunder(Tricks.DIVIDE);
}

return new VectorFragment(vector.div(vec.vector, new Vector3d()));
} else if (other instanceof NumberFragment num) {
if (num.number() == 0) {
throw new DivideByZeroBlunder(Tricks.DIVIDE);
}

return new VectorFragment(vector.div(num.number(), new Vector3d()));
}
throw new IncompatibleTypesBlunder(Tricks.DIVIDE);
}

@Override
public RoundableFragment floor() throws BlunderException {
return new VectorFragment(vector.floor(new Vector3d()));
}

@Override
public RoundableFragment ceil() throws BlunderException {
return new VectorFragment(vector.ceil(new Vector3d()));
}

@Override
public RoundableFragment round() throws BlunderException {
return new VectorFragment(vector.round(new Vector3d()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ public FragmentType<?> type() {
public String asString() {
return "void";
}

@Override
public BooleanFragment asBoolean() {
return BooleanFragment.FALSE;
}
}
22 changes: 22 additions & 0 deletions src/main/java/dev/enjarai/trickster/spell/tricks/CeilTrick.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package dev.enjarai.trickster.spell.tricks;

import dev.enjarai.trickster.spell.Fragment;
import dev.enjarai.trickster.spell.Pattern;
import dev.enjarai.trickster.spell.SpellContext;
import dev.enjarai.trickster.spell.fragment.RoundableFragment;
import dev.enjarai.trickster.spell.tricks.blunder.BlunderException;

import java.util.List;

public class CeilTrick extends Trick {
protected CeilTrick() {
super(Pattern.of(6, 7, 4, 5));
}

@Override
public Fragment activate(SpellContext ctx, List<Fragment> fragments) throws BlunderException {
var param = expectInput(fragments, RoundableFragment.class, 0);

return param.ceil();
}
}
24 changes: 24 additions & 0 deletions src/main/java/dev/enjarai/trickster/spell/tricks/FloorTrick.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package dev.enjarai.trickster.spell.tricks;

import dev.enjarai.trickster.spell.Fragment;
import dev.enjarai.trickster.spell.Pattern;
import dev.enjarai.trickster.spell.SpellContext;
import dev.enjarai.trickster.spell.fragment.FragmentType;
import dev.enjarai.trickster.spell.fragment.NumberFragment;
import dev.enjarai.trickster.spell.fragment.RoundableFragment;
import dev.enjarai.trickster.spell.tricks.blunder.BlunderException;

import java.util.List;

public class FloorTrick extends Trick {
protected FloorTrick() {
super(Pattern.of(0, 1, 4, 5));
}

@Override
public Fragment activate(SpellContext ctx, List<Fragment> fragments) throws BlunderException {
var param = expectInput(fragments, RoundableFragment.class, 0);

return param.floor();
}
}
Loading

0 comments on commit efb2b67

Please sign in to comment.