From a43d92c6a1f7538b02a29d9d8e7155f209694040 Mon Sep 17 00:00:00 2001 From: "yrsegal@gmail.com" Date: Fri, 8 Apr 2022 20:17:07 -0400 Subject: [PATCH] sentinels are now dimension-bound --- .../client/HexAdditionalRenderers.java | 2 +- .../common/casting/CastingContext.kt | 1 + .../mishaps/MishapLocationInWrongDimension.kt | 22 +++++++++++++++++++ .../spells/sentinel/CapSentinel.java | 11 +++++++++- .../spells/sentinel/OpCreateSentinel.kt | 3 ++- .../spells/sentinel/OpDestroySentinel.kt | 9 ++++++-- .../spells/sentinel/OpGetSentinelPos.kt | 5 ++++- .../spells/sentinel/OpGetSentinelWayfind.kt | 6 ++++- .../common/lib/HexCapabilities.java | 4 +++- .../network/MsgSentinelStatusUpdateAck.java | 4 +++- .../assets/hexcasting/lang/en_us.json | 1 + 11 files changed, 59 insertions(+), 9 deletions(-) create mode 100644 src/main/java/at/petrak/hexcasting/common/casting/mishaps/MishapLocationInWrongDimension.kt diff --git a/src/main/java/at/petrak/hexcasting/client/HexAdditionalRenderers.java b/src/main/java/at/petrak/hexcasting/client/HexAdditionalRenderers.java index f2e40e33..184d5819 100644 --- a/src/main/java/at/petrak/hexcasting/client/HexAdditionalRenderers.java +++ b/src/main/java/at/petrak/hexcasting/client/HexAdditionalRenderers.java @@ -34,7 +34,7 @@ public class HexAdditionalRenderers { var maybeSentinelCap = player.getCapability(HexCapabilities.SENTINEL).resolve(); if (maybeSentinelCap.isPresent()) { var cap = maybeSentinelCap.get(); - if (cap.hasSentinel) { + if (cap.hasSentinel && player.getLevel().dimension().equals(cap.dimension)) { renderSentinel(cap, player, evt.getPoseStack(), evt.getPartialTick()); } } diff --git a/src/main/java/at/petrak/hexcasting/common/casting/CastingContext.kt b/src/main/java/at/petrak/hexcasting/common/casting/CastingContext.kt index b37f496d..d7a5d623 100644 --- a/src/main/java/at/petrak/hexcasting/common/casting/CastingContext.kt +++ b/src/main/java/at/petrak/hexcasting/common/casting/CastingContext.kt @@ -83,6 +83,7 @@ data class CastingContext( val sentinel = maybeSentinel.get() if (sentinel.hasSentinel && sentinel.extendsRange + && world.dimension() == sentinel.dimension && vec.distanceToSqr(sentinel.position) < Operator.MAX_DISTANCE_FROM_SENTINEL * Operator.MAX_DISTANCE_FROM_SENTINEL ) return true diff --git a/src/main/java/at/petrak/hexcasting/common/casting/mishaps/MishapLocationInWrongDimension.kt b/src/main/java/at/petrak/hexcasting/common/casting/mishaps/MishapLocationInWrongDimension.kt new file mode 100644 index 00000000..2ad11c76 --- /dev/null +++ b/src/main/java/at/petrak/hexcasting/common/casting/mishaps/MishapLocationInWrongDimension.kt @@ -0,0 +1,22 @@ +package at.petrak.hexcasting.common.casting.mishaps + +import at.petrak.hexcasting.api.spell.SpellDatum +import at.petrak.hexcasting.common.casting.CastingContext +import at.petrak.hexcasting.common.casting.Widget +import at.petrak.hexcasting.common.casting.colors.FrozenColorizer +import net.minecraft.network.chat.Component +import net.minecraft.resources.ResourceLocation +import net.minecraft.world.item.DyeColor + +class MishapLocationInWrongDimension(val properDimension: ResourceLocation) : Mishap() { + override fun accentColor(ctx: CastingContext, errorCtx: Context): FrozenColorizer = + dyeColor(DyeColor.MAGENTA) + + override fun execute(ctx: CastingContext, errorCtx: Context, stack: MutableList>) { + stack.add(SpellDatum.make(Widget.GARBAGE)) + } + + override fun errorMessage(ctx: CastingContext, errorCtx: Context): Component = + error("wrong_dimension", actionName(errorCtx.action!!), properDimension.toString(), + ctx.world.dimension().registryName.toString()) +} diff --git a/src/main/java/at/petrak/hexcasting/common/casting/operators/spells/sentinel/CapSentinel.java b/src/main/java/at/petrak/hexcasting/common/casting/operators/spells/sentinel/CapSentinel.java index 762f6b1a..538bcee9 100644 --- a/src/main/java/at/petrak/hexcasting/common/casting/operators/spells/sentinel/CapSentinel.java +++ b/src/main/java/at/petrak/hexcasting/common/casting/operators/spells/sentinel/CapSentinel.java @@ -3,7 +3,11 @@ package at.petrak.hexcasting.common.casting.operators.spells.sentinel; import at.petrak.hexcasting.HexUtils; import at.petrak.hexcasting.common.lib.HexCapabilities; import net.minecraft.core.Direction; +import net.minecraft.core.Registry; import net.minecraft.nbt.CompoundTag; +import net.minecraft.resources.ResourceKey; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.level.Level; import net.minecraft.world.phys.Vec3; import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.ICapabilitySerializable; @@ -16,15 +20,18 @@ public class CapSentinel implements ICapabilitySerializable { public static final String TAG_EXISTS = "exists"; public static final String TAG_EXTENDS_RANGE = "extends_range"; public static final String TAG_POSITION = "position"; + public static final String TAG_DIMENSION = "dimension"; public boolean hasSentinel; public boolean extendsRange; public Vec3 position; + public ResourceKey dimension; - public CapSentinel(boolean hasSentinel, boolean extendsRange, Vec3 position) { + public CapSentinel(boolean hasSentinel, boolean extendsRange, Vec3 position, ResourceKey dimension) { this.hasSentinel = hasSentinel; this.extendsRange = extendsRange; this.position = position; + this.dimension = dimension; } @NotNull @@ -39,6 +46,7 @@ public class CapSentinel implements ICapabilitySerializable { tag.putBoolean(TAG_EXISTS, this.hasSentinel); tag.putBoolean(TAG_EXTENDS_RANGE, this.extendsRange); tag.put(TAG_POSITION, HexUtils.serializeToNBT(this.position)); + tag.putString(TAG_DIMENSION, dimension.getRegistryName().toString()); return tag; } @@ -47,6 +55,7 @@ public class CapSentinel implements ICapabilitySerializable { public void deserializeNBT(CompoundTag tag) { this.hasSentinel = tag.getBoolean(TAG_EXISTS); this.extendsRange = tag.getBoolean(TAG_EXTENDS_RANGE); + this.dimension = ResourceKey.create(Registry.DIMENSION_REGISTRY, new ResourceLocation(tag.getString(TAG_DIMENSION))); this.position = HexUtils.DeserializeVec3FromNBT(tag.getLongArray(TAG_POSITION)); } } diff --git a/src/main/java/at/petrak/hexcasting/common/casting/operators/spells/sentinel/OpCreateSentinel.kt b/src/main/java/at/petrak/hexcasting/common/casting/operators/spells/sentinel/OpCreateSentinel.kt index c0234cb7..da9e4d50 100644 --- a/src/main/java/at/petrak/hexcasting/common/casting/operators/spells/sentinel/OpCreateSentinel.kt +++ b/src/main/java/at/petrak/hexcasting/common/casting/operators/spells/sentinel/OpCreateSentinel.kt @@ -40,8 +40,9 @@ class OpCreateSentinel(val extendsRange: Boolean) : SpellOperator { cap.hasSentinel = true cap.extendsRange = extendsRange cap.position = target + cap.dimension = ctx.world.dimension() HexMessages.getNetwork().send(PacketDistributor.PLAYER.with { ctx.caster }, MsgSentinelStatusUpdateAck(cap)) } } -} \ No newline at end of file +} diff --git a/src/main/java/at/petrak/hexcasting/common/casting/operators/spells/sentinel/OpDestroySentinel.kt b/src/main/java/at/petrak/hexcasting/common/casting/operators/spells/sentinel/OpDestroySentinel.kt index 8b3930cc..fc71aa7f 100644 --- a/src/main/java/at/petrak/hexcasting/common/casting/operators/spells/sentinel/OpDestroySentinel.kt +++ b/src/main/java/at/petrak/hexcasting/common/casting/operators/spells/sentinel/OpDestroySentinel.kt @@ -5,6 +5,7 @@ import at.petrak.hexcasting.api.spell.RenderedSpell import at.petrak.hexcasting.api.spell.SpellDatum import at.petrak.hexcasting.api.spell.SpellOperator import at.petrak.hexcasting.common.casting.CastingContext +import at.petrak.hexcasting.common.casting.mishaps.MishapLocationInWrongDimension import at.petrak.hexcasting.common.lib.HexCapabilities import at.petrak.hexcasting.common.network.HexMessages import at.petrak.hexcasting.common.network.MsgSentinelStatusUpdateAck @@ -18,7 +19,11 @@ object OpDestroySentinel : SpellOperator { ): Triple> { val particles = mutableListOf() val maybeCap = ctx.caster.getCapability(HexCapabilities.SENTINEL).resolve() - maybeCap.ifPresent { particles.add(ParticleSpray.Cloud(it.position, 2.0)) } + maybeCap.ifPresent { + if (it.dimension != ctx.world.dimension()) + throw MishapLocationInWrongDimension(it.dimension.registryName) + particles.add(ParticleSpray.Cloud(it.position, 2.0)) + } return Triple( Spell, @@ -39,4 +44,4 @@ object OpDestroySentinel : SpellOperator { HexMessages.getNetwork().send(PacketDistributor.PLAYER.with { ctx.caster }, MsgSentinelStatusUpdateAck(cap)) } } -} \ No newline at end of file +} diff --git a/src/main/java/at/petrak/hexcasting/common/casting/operators/spells/sentinel/OpGetSentinelPos.kt b/src/main/java/at/petrak/hexcasting/common/casting/operators/spells/sentinel/OpGetSentinelPos.kt index 9a2dd056..1bafb3bf 100644 --- a/src/main/java/at/petrak/hexcasting/common/casting/operators/spells/sentinel/OpGetSentinelPos.kt +++ b/src/main/java/at/petrak/hexcasting/common/casting/operators/spells/sentinel/OpGetSentinelPos.kt @@ -5,6 +5,7 @@ import at.petrak.hexcasting.api.spell.Operator.Companion.spellListOf import at.petrak.hexcasting.api.spell.SpellDatum import at.petrak.hexcasting.common.casting.CastingContext import at.petrak.hexcasting.common.casting.Widget +import at.petrak.hexcasting.common.casting.mishaps.MishapLocationInWrongDimension import at.petrak.hexcasting.common.lib.HexCapabilities object OpGetSentinelPos : ConstManaOperator { @@ -16,6 +17,8 @@ object OpGetSentinelPos : ConstManaOperator { return spellListOf(Widget.NULL) val cap = maybeCap.get() + if (cap.dimension != ctx.world.dimension()) + throw MishapLocationInWrongDimension(cap.dimension.registryName) return spellListOf( if (cap.hasSentinel) cap.position @@ -23,4 +26,4 @@ object OpGetSentinelPos : ConstManaOperator { Widget.NULL ) } -} \ No newline at end of file +} diff --git a/src/main/java/at/petrak/hexcasting/common/casting/operators/spells/sentinel/OpGetSentinelWayfind.kt b/src/main/java/at/petrak/hexcasting/common/casting/operators/spells/sentinel/OpGetSentinelWayfind.kt index 5f63ca05..87c562dc 100644 --- a/src/main/java/at/petrak/hexcasting/common/casting/operators/spells/sentinel/OpGetSentinelWayfind.kt +++ b/src/main/java/at/petrak/hexcasting/common/casting/operators/spells/sentinel/OpGetSentinelWayfind.kt @@ -6,6 +6,7 @@ import at.petrak.hexcasting.api.spell.Operator.Companion.spellListOf import at.petrak.hexcasting.api.spell.SpellDatum import at.petrak.hexcasting.common.casting.CastingContext import at.petrak.hexcasting.common.casting.Widget +import at.petrak.hexcasting.common.casting.mishaps.MishapLocationInWrongDimension import at.petrak.hexcasting.common.lib.HexCapabilities import net.minecraft.world.phys.Vec3 @@ -20,6 +21,9 @@ object OpGetSentinelWayfind : ConstManaOperator { return spellListOf(Widget.NULL) val cap = maybeCap.get() + if (cap.dimension != ctx.world.dimension()) + throw MishapLocationInWrongDimension(cap.dimension.registryName) + val sentinelPos = if (!cap.hasSentinel) return spellListOf(Widget.NULL) else @@ -27,4 +31,4 @@ object OpGetSentinelWayfind : ConstManaOperator { return spellListOf(sentinelPos.subtract(from).normalize()) } -} \ No newline at end of file +} diff --git a/src/main/java/at/petrak/hexcasting/common/lib/HexCapabilities.java b/src/main/java/at/petrak/hexcasting/common/lib/HexCapabilities.java index 8640599a..723da547 100644 --- a/src/main/java/at/petrak/hexcasting/common/lib/HexCapabilities.java +++ b/src/main/java/at/petrak/hexcasting/common/lib/HexCapabilities.java @@ -16,6 +16,7 @@ import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.npc.Villager; import net.minecraft.world.entity.player.Player; +import net.minecraft.world.level.Level; import net.minecraft.world.phys.Vec3; import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.CapabilityManager; @@ -53,7 +54,7 @@ public class HexCapabilities { // generate a new instance of the capability new OpFlight.CapFlight(false, 0, Vec3.ZERO, 0.0)); evt.addCapability(new ResourceLocation(HexMod.MOD_ID, CapSentinel.CAP_NAME), - new CapSentinel(false, false, Vec3.ZERO)); + new CapSentinel(false, false, Vec3.ZERO, Level.OVERWORLD)); evt.addCapability(new ResourceLocation(HexMod.MOD_ID, CapPreferredColorizer.CAP_NAME), new CapPreferredColorizer(FrozenColorizer.DEFAULT)); } else if (evt.getObject() instanceof Villager) { @@ -89,6 +90,7 @@ public class HexCapabilities { sentinel.hasSentinel = protoSentinel.hasSentinel; sentinel.position = protoSentinel.position; sentinel.extendsRange = protoSentinel.extendsRange; + sentinel.dimension = protoSentinel.dimension; }); }); var protoCapColor = proto.getCapability(PREFERRED_COLORIZER).resolve(); diff --git a/src/main/java/at/petrak/hexcasting/common/network/MsgSentinelStatusUpdateAck.java b/src/main/java/at/petrak/hexcasting/common/network/MsgSentinelStatusUpdateAck.java index b9507a06..a53f1bd3 100644 --- a/src/main/java/at/petrak/hexcasting/common/network/MsgSentinelStatusUpdateAck.java +++ b/src/main/java/at/petrak/hexcasting/common/network/MsgSentinelStatusUpdateAck.java @@ -5,6 +5,7 @@ import at.petrak.hexcasting.common.lib.HexCapabilities; import io.netty.buffer.ByteBuf; import net.minecraft.client.Minecraft; import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.world.level.Level; import net.minecraft.world.phys.Vec3; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.fml.DistExecutor; @@ -20,7 +21,7 @@ public record MsgSentinelStatusUpdateAck(CapSentinel update) { var buf = new FriendlyByteBuf(buffer); var tag = buf.readAnySizeNbt(); - var sentinel = new CapSentinel(false, false, Vec3.ZERO); + var sentinel = new CapSentinel(false, false, Vec3.ZERO, Level.OVERWORLD); sentinel.deserializeNBT(tag); return new MsgSentinelStatusUpdateAck(sentinel); } @@ -43,6 +44,7 @@ public record MsgSentinelStatusUpdateAck(CapSentinel update) { cap.hasSentinel = update().hasSentinel; cap.extendsRange = update().extendsRange; cap.position = update().position; + cap.dimension = update().dimension; }) ); ctx.get().setPacketHandled(true); diff --git a/src/main/resources/assets/hexcasting/lang/en_us.json b/src/main/resources/assets/hexcasting/lang/en_us.json index d18a6e5c..0dc61a00 100644 --- a/src/main/resources/assets/hexcasting/lang/en_us.json +++ b/src/main/resources/assets/hexcasting/lang/en_us.json @@ -273,6 +273,7 @@ "hexcasting.mishap.not_enough_args": "%s expected %s or more arguments but the stack was only %s tall", "hexcasting.mishap.too_many_close_parens": "Used Retrospection without first using Introspection", "hexcasting.mishap.location_too_far": "%s is out of range for %s", + "hexcasting.mishap.wrong_dimension": "%s cannot see %s from %s", "hexcasting.mishap.entity_too_far": "%s is out of range for %s", "hexcasting.mishap.eval_too_deep": "Recursively evaluated too deep", "hexcasting.mishap.bad_item": "%s needs %s but got %s",