Skip to content

Commit

Permalink
Add GlobalPos versions of IWandable methods with fallback if dimensio…
Browse files Browse the repository at this point in the history
…ns are the same (#1437)
  • Loading branch information
Vonr authored Nov 22, 2024
1 parent 57e46a5 commit 15e8c46
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// 1.21.1 2024-11-10T14:17:22.9532023 Languages: en_us for mod: ars_nouveau
6bda58086730da53e85d21674e15c61da8c5ab4f assets/ars_nouveau/lang/en_us.json
// 1.21.1 2024-11-22T17:42:19.4427397 Languages: en_us for mod: ars_nouveau
88075d83bbe635f207366f56b6caafce743838c7 assets/ars_nouveau/lang/en_us.json
1 change: 1 addition & 0 deletions src/generated/resources/assets/ars_nouveau/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@
"ars_nouveau.gathering_at": "Gathering at: %s",
"ars_nouveau.getting_started": "Getting Started",
"ars_nouveau.getting_started_desc": "An introduction to Ars Nouveau. It is recommended to follow each section in order.",
"ars_nouveau.global_position": "X: %1$d Y: %2$d Z: %3$d in %4$s",
"ars_nouveau.glyph_crafting": "Glyph Crafting",
"ars_nouveau.glyph_desc.glyph_accelerate": "Increases the speed of projectile spells.",
"ars_nouveau.glyph_desc.glyph_amplify": "Additively increases the power of most spell effects. Can increase the harvest level of Break and increases the damage of spells.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import com.hollingsworth.arsnouveau.client.particle.ColorPos;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.GlobalPos;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.Nullable;

import java.util.List;
Expand All @@ -14,23 +17,79 @@ public interface IWandable {
* When the wand has made 2 connections, block -> block, block -> entity, entity -> block, or entity -> entity.
* The FIRST IWandable in the chain is called.
*/
default void onFinishedConnectionFirst(@Nullable GlobalPos storedPos, @Nullable LivingEntity storedEntity, Player playerEntity) {
ResourceKey<Level> dim = getStoredDimension(storedPos, storedEntity);
if (dim == null) {
return;
}

if (playerEntity.level.dimension().equals(dim)) {
onFinishedConnectionFirst(storedPos != null ? storedPos.pos() : null, storedEntity, playerEntity);
}
}

/**
* When the wand has made 2 connections, block -> block, block -> entity, entity -> block, or entity -> entity.
* The LAST IWandable in the chain is called.
*/
default void onFinishedConnectionLast(@Nullable GlobalPos storedPos, @Nullable LivingEntity storedEntity, Player playerEntity) {
ResourceKey<Level> dim = getStoredDimension(storedPos, storedEntity);
if (dim == null) {
return;
}

if (playerEntity.level.dimension().equals(dim)) {
onFinishedConnectionLast(storedPos != null ? storedPos.pos() : null, storedEntity, playerEntity);
}
}

/**
* When the wand has made 2 connections, block -> block, block -> entity, entity -> block, or entity -> entity.
* The FIRST IWandable in the chain is called.
*/
@Deprecated
default void onFinishedConnectionFirst(@Nullable BlockPos storedPos, @Nullable LivingEntity storedEntity, Player playerEntity) {
}

/**
* When the wand has made 2 connections, block -> block, block -> entity, entity -> block, or entity -> entity.
* The LAST IWandable in the chain is called.
*/
@Deprecated
default void onFinishedConnectionLast(@Nullable BlockPos storedPos, @Nullable LivingEntity storedEntity, Player playerEntity) {
}


//Face-Sensitive versions

default void onFinishedConnectionFirst(@Nullable GlobalPos storedPos, @Nullable Direction face, @Nullable LivingEntity storedEntity, Player playerEntity) {
ResourceKey<Level> dim = getStoredDimension(storedPos, storedEntity);
if (dim == null) {
return;
}

if (playerEntity.level.dimension().equals(dim)) {
onFinishedConnectionFirst(storedPos != null ? storedPos.pos() : null, face, storedEntity, playerEntity);
}
}

default void onFinishedConnectionLast(@Nullable GlobalPos storedPos, @Nullable Direction face, @Nullable LivingEntity storedEntity, Player playerEntity) {
ResourceKey<Level> dim = getStoredDimension(storedPos, storedEntity);
if (dim == null) {
return;
}

if (playerEntity.level.dimension().equals(dim)) {
onFinishedConnectionLast(storedPos != null ? storedPos.pos() : null, face, storedEntity, playerEntity);
}
}

@Deprecated
default void onFinishedConnectionFirst(@Nullable BlockPos storedPos, @Nullable Direction face, @Nullable LivingEntity storedEntity, Player playerEntity) {
onFinishedConnectionFirst(storedPos, storedEntity, playerEntity);
}

@Deprecated
default void onFinishedConnectionLast(@Nullable BlockPos storedPos, @Nullable Direction face, @Nullable LivingEntity storedEntity, Player playerEntity) {
onFinishedConnectionLast(storedPos, storedEntity, playerEntity);
}
Expand All @@ -47,4 +106,16 @@ default void onWanded(Player playerEntity) {
default List<ColorPos> getWandHighlight(List<ColorPos> list) {
return list;
}
}

private ResourceKey<Level> getStoredDimension(@Nullable GlobalPos storedPos, @Nullable LivingEntity storedEntity) {
if (storedPos != null) {
return storedPos.dimension();
}

if (storedEntity != null) {
return storedEntity.level.dimension();
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ protected void addTranslations() {
add("ars_nouveau.dominion_wand.no_location", "No location set.");
add("ars_nouveau.dominion_wand.position_stored", "Stored: %s");
add("ars_nouveau.position", "X: %1$d Y: %2$d Z: %3$d");
add("ars_nouveau.global_position", "X: %1$d Y: %2$d Z: %3$d in %4$s");
add("ars_nouveau.spell_arrow.desc", "Augments spells when used with an Enchanter's Bow.");
add("ars_nouveau.spell_parchment.no_spell", "Set your spell book to a spell.");
add("ars_nouveau.spell_parchment.inscribed", "Spell inscribed.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.GlobalPos;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
Expand Down Expand Up @@ -50,7 +53,7 @@ public void inventoryTick(@NotNull ItemStack pStack, @NotNull Level pLevel, @Not
DominionWandData data = pStack.getOrDefault(DataComponentRegistry.DOMINION_WAND, new DominionWandData());

if (data.storedPos().isPresent()) {
if (pLevel.getBlockEntity(data.storedPos().get()) instanceof IWandable wandable) {
if (pLevel.getBlockEntity(data.storedPos().get().pos()) instanceof IWandable wandable) {
Networking.sendToPlayerClient(new HighlightAreaPacket(wandable.getWandHighlight(new ArrayList<>()), 10), (ServerPlayer) pEntity);
}
return;
Expand Down Expand Up @@ -79,9 +82,9 @@ public void inventoryTick(@NotNull ItemStack pStack, @NotNull Level pLevel, @Not
PortUtil.sendMessage(playerEntity, Component.translatable("ars_nouveau.dominion_wand.stored_entity"));
return InteractionResult.SUCCESS;
}
Level world = playerEntity.getCommandSenderWorld();
Level world = data.storedPos().isPresent() ? playerEntity.getServer().getLevel(data.storedPos().get().dimension()) : playerEntity.getCommandSenderWorld();

if (data.storedPos().isPresent() && world.getBlockEntity(data.storedPos().get()) instanceof IWandable wandable) {
if (data.storedPos().isPresent() && world.getBlockEntity(data.storedPos().get().pos()) instanceof IWandable wandable) {
wandable.onFinishedConnectionFirst(data.storedPos().orElse(null), data.face().orElse(null), target, playerEntity);
}

Expand Down Expand Up @@ -115,10 +118,10 @@ public void clear(ItemStack stack, Player player) {
return super.useOn(context);
BlockPos pos = context.getClickedPos();
Direction face = context.getClickedFace();
Level world = context.getLevel();
Player playerEntity = context.getPlayer();
ItemStack stack = context.getItemInHand();
DominionWandData data = stack.getOrDefault(DataComponentRegistry.DOMINION_WAND, new DominionWandData());
Level world = data.storedPos().isPresent() ? playerEntity.getServer().getLevel(data.storedPos().get().dimension()) : context.getLevel();

if (playerEntity.isShiftKeyDown() && world.getBlockEntity(pos) instanceof IWandable wandable && !data.hasStoredData()) {
wandable.onWanded(playerEntity);
Expand All @@ -127,23 +130,23 @@ public void clear(ItemStack stack, Player player) {
}

if (!data.hasStoredData()) {
data = data.storePos(pos.immutable());
data = data.storePos(new GlobalPos(context.getLevel().dimension(), pos));
if (data.strict()) data = data.setFace(face);
stack.set(DataComponentRegistry.DOMINION_WAND, data);
PortUtil.sendMessage(playerEntity, Component.translatable("ars_nouveau.dominion_wand.position_set"));
return InteractionResult.SUCCESS;
}

BlockPos storedPos = data.storedPos().orElse(null);
GlobalPos storedPos = data.storedPos().orElse(null);
Direction storedDirection = data.face().orElse(null);
if (storedPos != null && world.getBlockEntity(storedPos) instanceof IWandable wandable) {
wandable.onFinishedConnectionFirst(pos, storedDirection, (LivingEntity) world.getEntity(data.storedEntityId()), playerEntity);
if (storedPos != null && world.getBlockEntity(storedPos.pos()) instanceof IWandable wandable) {
wandable.onFinishedConnectionFirst(new GlobalPos(world.dimension(), pos), storedDirection, (LivingEntity) world.getEntity(data.storedEntityId()), playerEntity);
}
if (world.getBlockEntity(pos) instanceof IWandable wandable) {
wandable.onFinishedConnectionLast(storedPos, storedDirection, (LivingEntity) world.getEntity(data.storedEntityId()), playerEntity);
}
if (data.storedEntityId() != DominionWandData.NULL_ENTITY && world.getEntity(data.storedEntityId()) instanceof IWandable wandable) {
wandable.onFinishedConnectionFirst(pos, data.strict() ? face : null, null, playerEntity);
wandable.onFinishedConnectionFirst(new GlobalPos(world.dimension(), pos), data.strict() ? face : null, null, playerEntity);
}

clear(stack, playerEntity);
Expand All @@ -169,7 +172,7 @@ public void appendHoverText(@NotNull ItemStack stack, @NotNull TooltipContext wo
if (data.storedPos().isEmpty()) {
tooltip.add(Component.translatable("ars_nouveau.dominion_wand.no_location"));
} else {
tooltip.add(Component.translatable("ars_nouveau.dominion_wand.position_stored", getPosString(data.getValidPos())));
tooltip.add(Component.translatable("ars_nouveau.dominion_wand.position_stored", getGlobalPosString(data.getValidPos())));
}

if (data.strict()) tooltip.add(Component.literal("Side-Sensitive"));
Expand All @@ -179,6 +182,14 @@ public static String getPosString(BlockPos pos) {
return Component.translatable("ars_nouveau.position", pos.getX(), pos.getY(), pos.getZ()).getString();
}

public static String getGlobalPosString(GlobalPos globalPos) {
BlockPos pos = globalPos.pos();
ResourceLocation dimLoc = globalPos.dimension().location();
String translationKey = dimLoc.getPath() + "." + dimLoc.getNamespace() + ".name";
String translation = Component.translatable(translationKey).getString();
return Component.translatable("ars_nouveau.global_position", pos.getX(), pos.getY(), pos.getZ(), translation.equals(translationKey) ? dimLoc.toString() : translation).getString();
}

@OnlyIn(Dist.CLIENT)
@Override
public void onRadialKeyPressed(ItemStack stack, Player player) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.GlobalPos;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
Expand All @@ -12,7 +13,7 @@
import java.util.Objects;
import java.util.Optional;

public record DominionWandData(Optional<BlockPos> storedPos, Optional<Direction> face, boolean strict, int storedEntityId) {
public record DominionWandData(Optional<GlobalPos> storedPos, Optional<Direction> face, boolean strict, int storedEntityId) {

public DominionWandData(){
this(Optional.empty(), Optional.empty(), false, NULL_ENTITY);
Expand All @@ -21,14 +22,14 @@ public DominionWandData(){
public static final int NULL_ENTITY = -1;

public static Codec<DominionWandData> CODEC = RecordCodecBuilder.create(instance -> instance.group(
BlockPos.CODEC.optionalFieldOf("pos").forGetter(DominionWandData::storedPos),
GlobalPos.CODEC.optionalFieldOf("pos").forGetter(DominionWandData::storedPos),
Direction.CODEC.optionalFieldOf("face").forGetter(DominionWandData::face),
Codec.BOOL.fieldOf("strict").forGetter(DominionWandData::strict),
Codec.INT.fieldOf("entityId").forGetter(DominionWandData::storedEntityId)
).apply(instance, DominionWandData::new));

public static StreamCodec<RegistryFriendlyByteBuf, DominionWandData> STREAM = StreamCodec.composite(
BlockPos.STREAM_CODEC.apply(ByteBufCodecs::optional),
GlobalPos.STREAM_CODEC.apply(ByteBufCodecs::optional),
DominionWandData::storedPos,
Direction.STREAM_CODEC.apply(ByteBufCodecs::optional),
DominionWandData::face,
Expand All @@ -43,16 +44,16 @@ public boolean hasStoredData(){
return storedPos.isPresent() || storedEntityId != -1;
}

public @Nullable BlockPos getValidPos(){
public @Nullable GlobalPos getValidPos(){
return storedPos.orElse(null);
}

public int getStoredEntity(){
return storedEntityId == 0 || storedEntityId == NULL_ENTITY ? NULL_ENTITY : storedEntityId;
}

public DominionWandData storePos(@Nullable BlockPos pos){
return new DominionWandData(pos == null ? Optional.empty() : Optional.of(pos.immutable()), face, strict, storedEntityId);
public DominionWandData storePos(@Nullable GlobalPos pos){
return new DominionWandData(pos == null ? Optional.empty() : Optional.of(new GlobalPos(pos.dimension(), pos.pos().immutable())), face, strict, storedEntityId);
}

public DominionWandData storeEntity(int entityId){
Expand Down

0 comments on commit 15e8c46

Please sign in to comment.