Skip to content

Commit

Permalink
Inline Patterns feature and pattern rendering refactor-ish (#764)
Browse files Browse the repository at this point in the history
  • Loading branch information
gamma-delta authored Oct 25, 2024
2 parents 5f583f3 + cab0f70 commit 2f0509d
Show file tree
Hide file tree
Showing 44 changed files with 1,570 additions and 858 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ eclipse
run
.DS_Store

# Idk what prompted gradle to create this folder
Fabric/Fabric


# MacOS moment
.DS_Store

Expand Down
4 changes: 4 additions & 0 deletions Common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ repositories {
url = "https://modmaven.dev"
}

maven { url "https://maven.shedaniel.me/" }

}

dependencies {
Expand All @@ -37,6 +39,8 @@ dependencies {
compileOnly "at.petra-k.paucal:paucal-common-$minecraftVersion:$paucalVersion"
compileOnly "vazkii.patchouli:Patchouli-xplat:$minecraftVersion-$patchouliVersion-SNAPSHOT"

compileOnly "com.samsthenerd.inline:inline-forge:$minecraftVersion-$inlineVersion"

compileOnly "org.jetbrains:annotations:$jetbrainsAnnotationsVersion"
testCompileOnly "org.jetbrains:annotations:$jetbrainsAnnotationsVersion"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

import at.petrak.hexcasting.api.utils.HexUtils;
import at.petrak.hexcasting.common.lib.hex.HexIotaTypes;
import com.samsthenerd.inline.api.InlineAPI;
import com.samsthenerd.inline.api.data.EntityInlineData;
import com.samsthenerd.inline.api.data.PlayerHeadData;
import net.minecraft.ChatFormatting;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtUtils;
import net.minecraft.nbt.Tag;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -38,13 +43,29 @@ public boolean isTruthy() {
Tag serialize() {
var out = new CompoundTag();
out.putUUID("uuid", this.getEntity().getUUID());
out.putString("name", Component.Serializer.toJson(this.getEntity().getName()));
out.putString("name", Component.Serializer.toJson(getEntityNameWithInline(true)));
return out;
}

@Override
public Component display() {
return this.getEntity().getName().copy().withStyle(ChatFormatting.AQUA);
return getEntityNameWithInline(false).copy().withStyle(ChatFormatting.AQUA);
}

private Component getEntityNameWithInline(boolean fearSerializer){
MutableComponent baseName = this.getEntity().getName().copy();
Component inlineEnt = null;
if(this.getEntity() instanceof Player player){
inlineEnt = new PlayerHeadData(player.getGameProfile()).asText(!fearSerializer);
inlineEnt = inlineEnt.plainCopy().withStyle(InlineAPI.INSTANCE.withSizeModifier(inlineEnt.getStyle(), 1.5));
} else{
if(fearSerializer){ // we don't want to have to serialize an entity just to display it
inlineEnt = EntityInlineData.fromType(this.getEntity().getType()).asText(!fearSerializer);
} else {
inlineEnt = EntityInlineData.fromEntity(this.getEntity()).asText(!fearSerializer);
}
}
return baseName.append(Component.literal(": ")).append(inlineEnt);
}

public static IotaType<EntityIota> TYPE = new IotaType<>() {
Expand Down Expand Up @@ -73,6 +94,7 @@ public Component display(Tag tag) {
return Component.translatable("hexcasting.spelldata.entity.whoknows");
}
var nameJson = ctag.getString("name");
// return Component.literal(nameJson);
return Component.Serializer.fromJsonLenient(nameJson).withStyle(ChatFormatting.AQUA);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ public Component display(Tag tag) {

out.append(IotaType.getDisplay(csub));

if (i < list.size() - 1) {
// only add a comma between 2 non-patterns (commas don't look good with Inline patterns)
// TODO: maybe add a config? maybe add a method on IotaType to allow it to opt out of commas
if (i < list.size() - 1 && (IotaType.getTypeFromTag(csub) != PatternIota.TYPE
|| IotaType.getTypeFromTag(HexUtils.downcast(list.get(i+1), CompoundTag.TYPE)) != PatternIota.TYPE)) {
out.append(", ");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@
import at.petrak.hexcasting.api.casting.mishaps.MishapEvalTooMuch;
import at.petrak.hexcasting.api.casting.mishaps.MishapInvalidPattern;
import at.petrak.hexcasting.api.casting.mishaps.MishapUnenlightened;
import at.petrak.hexcasting.api.mod.HexConfig;
import at.petrak.hexcasting.api.mod.HexTags;
import at.petrak.hexcasting.api.utils.HexUtils;
import at.petrak.hexcasting.common.casting.PatternRegistryManifest;
import at.petrak.hexcasting.common.lib.hex.HexEvalSounds;
import at.petrak.hexcasting.common.lib.hex.HexIotaTypes;
import at.petrak.hexcasting.interop.inline.InlinePatternData;
import at.petrak.hexcasting.xplat.IXplatAbstractions;
import net.minecraft.ChatFormatting;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.Tag;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.Style;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.level.ServerLevel;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -163,6 +164,12 @@ public static PatternIota deserialize(Tag tag) throws IllegalArgumentException {
}

public static Component display(HexPattern pat) {
Component text = (new InlinePatternData(pat)).asText(true);
return text.copy().withStyle(text.getStyle().applyTo(Style.EMPTY.withColor(ChatFormatting.WHITE)));
}

// keep around just in case it's needed.
public static Component displayNonInline(HexPattern pat){
var bob = new StringBuilder();
bob.append(pat.getStartDir());

Expand All @@ -172,7 +179,7 @@ public static Component display(HexPattern pat) {
bob.append(sig);
}
return Component.translatable("hexcasting.tooltip.pattern_iota",
Component.literal(bob.toString()).withStyle(ChatFormatting.WHITE))
.withStyle(ChatFormatting.GOLD);
Component.literal(bob.toString()).withStyle(ChatFormatting.WHITE))
.withStyle(ChatFormatting.GOLD);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package at.petrak.hexcasting.api.casting.math

import at.petrak.hexcasting.api.utils.getSafe
import com.mojang.serialization.Codec

enum class HexDir {
NORTH_EAST, EAST, SOUTH_EAST, SOUTH_WEST, WEST, NORTH_WEST;
Expand All @@ -26,6 +27,11 @@ enum class HexDir {
}

companion object {
val CODEC: Codec<HexDir> = Codec.STRING.xmap(
HexDir::fromString,
HexDir::name
)

@JvmStatic
fun fromString(key: String): HexDir {
return values().getSafe(key, WEST)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import at.petrak.hexcasting.api.utils.NBTBuilder
import at.petrak.hexcasting.api.utils.coordToPx
import at.petrak.hexcasting.api.utils.findCenter
import at.petrak.hexcasting.api.utils.getSafe
import com.mojang.serialization.Codec
import com.mojang.serialization.codecs.RecordCodecBuilder
import net.minecraft.nbt.CompoundTag
import net.minecraft.nbt.Tag
import net.minecraft.world.phys.Vec2
Expand Down Expand Up @@ -127,6 +129,13 @@ data class HexPattern(val startDir: HexDir, val angles: MutableList<HexAngle> =
const val TAG_START_DIR = "start_dir"
const val TAG_ANGLES = "angles"

@JvmField
val CODEC: Codec<HexPattern> = RecordCodecBuilder.create({instance -> instance.group(
Codec.STRING.fieldOf(TAG_START_DIR).forGetter(HexPattern::anglesSignature),
HexDir.CODEC.fieldOf(TAG_ANGLES).forGetter(HexPattern::startDir)
).apply(instance, HexPattern::fromAngles)
})

@JvmStatic
fun isPattern(tag: CompoundTag): Boolean {
return tag.contains(TAG_START_DIR, Tag.TAG_ANY_NUMERIC.toInt())
Expand Down
Loading

0 comments on commit 2f0509d

Please sign in to comment.