From 7bf7a3c0855b456059722c48e4e3c49146ad8a67 Mon Sep 17 00:00:00 2001 From: direwolf20 <39863894+Direwolf20-MC@users.noreply.github.com> Date: Sat, 2 Nov 2024 11:36:53 -0400 Subject: [PATCH] Add previous destination capability to portal gun v2 --- .../common/items/PortalGunV2.java | 18 +++++++++++++++++- .../datacomponents/JustDireDataComponents.java | 1 + 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/direwolf20/justdirethings/common/items/PortalGunV2.java b/src/main/java/com/direwolf20/justdirethings/common/items/PortalGunV2.java index dbb5f00e..58e6740b 100644 --- a/src/main/java/com/direwolf20/justdirethings/common/items/PortalGunV2.java +++ b/src/main/java/com/direwolf20/justdirethings/common/items/PortalGunV2.java @@ -8,11 +8,14 @@ import com.direwolf20.justdirethings.setup.Config; import com.direwolf20.justdirethings.setup.Registration; import com.direwolf20.justdirethings.util.MagicHelpers; +import com.direwolf20.justdirethings.util.MiscHelpers; import com.direwolf20.justdirethings.util.NBTHelpers; import net.minecraft.ChatFormatting; import net.minecraft.advancements.CriteriaTriggers; import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; import net.minecraft.network.chat.Component; +import net.minecraft.resources.ResourceKey; import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; import net.minecraft.sounds.SoundEvents; @@ -102,7 +105,7 @@ public static boolean pickupFluid(Level level, Player player, ItemStack itemStac } public static void spawnProjectile(Level level, Player player, ItemStack itemStack, boolean isPrimaryType) { - NBTHelpers.PortalDestination portalDestination = getSelectedFavorite(itemStack); + NBTHelpers.PortalDestination portalDestination = player.isShiftKeyDown() ? getPrevious(itemStack) : getSelectedFavorite(itemStack); if (portalDestination == null || portalDestination.equals(NBTHelpers.PortalDestination.EMPTY)) return; int cost = calculateFluidCost((ServerLevel) level, player, portalDestination); if (!hasEnoughFluid(itemStack, cost)) { @@ -119,6 +122,7 @@ public static void spawnProjectile(Level level, Player player, ItemStack itemSta projectile.shootFromRotation(player, player.getXRot(), player.getYRot(), 0.0F, 1F, 1.0F); level.addFreshEntity(projectile); consumeFluid(itemStack, cost); + setPrevious(player, itemStack); } public static int calculateFluidCost(ServerLevel sourceLevel, Player player, NBTHelpers.PortalDestination portalDestination) { @@ -199,6 +203,18 @@ public static void setFavorites(ItemStack itemStack, List dimension = player.level().dimension(); + NBTHelpers.PortalDestination newDestination = new NBTHelpers.PortalDestination(new NBTHelpers.GlobalVec3(dimension, position), facing, "previous"); + itemStack.set(JustDireDataComponents.PORTAL_GUN_PREVIOUS, newDestination); + } + + public static NBTHelpers.PortalDestination getPrevious(ItemStack itemStack) { + return itemStack.getOrDefault(JustDireDataComponents.PORTAL_GUN_PREVIOUS, NBTHelpers.PortalDestination.EMPTY); + } + public static void addFavorite(ItemStack itemStack, int position, NBTHelpers.PortalDestination portalDestination) { if (!itemStack.has(JustDireDataComponents.PORTAL_GUN_FAVORITES)) { List list = new ArrayList<>(MAX_FAVORITES); diff --git a/src/main/java/com/direwolf20/justdirethings/common/items/datacomponents/JustDireDataComponents.java b/src/main/java/com/direwolf20/justdirethings/common/items/datacomponents/JustDireDataComponents.java index 008022ef..47071476 100644 --- a/src/main/java/com/direwolf20/justdirethings/common/items/datacomponents/JustDireDataComponents.java +++ b/src/main/java/com/direwolf20/justdirethings/common/items/datacomponents/JustDireDataComponents.java @@ -54,6 +54,7 @@ public class JustDireDataComponents { public static final DeferredHolder, DataComponentType> PORTALGUN_UUID = COMPONENTS.register("portalgun_uuid", () -> DataComponentType.builder().persistent(UUIDUtil.CODEC).networkSynchronized(UUIDUtil.STREAM_CODEC).build()); public static final DeferredHolder, DataComponentType> PORTALGUN_FAVORITE = COMPONENTS.register("portalgun_favorite", () -> DataComponentType.builder().persistent(Codec.INT).networkSynchronized(ByteBufCodecs.VAR_INT).build()); public static final DeferredHolder, DataComponentType>> PORTAL_GUN_FAVORITES = COMPONENTS.register("portal_gun_favorites", () -> DataComponentType.>builder().persistent(NBTHelpers.PortalDestination.CODEC.listOf()).networkSynchronized(NBTHelpers.PortalDestination.STREAM_CODEC.apply(ByteBufCodecs.list())).build()); + public static final DeferredHolder, DataComponentType> PORTAL_GUN_PREVIOUS = COMPONENTS.register("portal_gun_previous", () -> DataComponentType.builder().persistent(NBTHelpers.PortalDestination.CODEC).networkSynchronized(NBTHelpers.PortalDestination.STREAM_CODEC).build()); public static final DeferredHolder, DataComponentType> PORTAL_GUN_STAY_OPEN = COMPONENTS.register("portal_gun_stay_open", () -> DataComponentType.builder().persistent(Codec.BOOL).networkSynchronized(ByteBufCodecs.BOOL).build()); public static final DeferredHolder, DataComponentType> FLUID_CONTAINER = COMPONENTS.register("fluid_container", () -> DataComponentType.builder().persistent(SimpleFluidContent.CODEC).networkSynchronized(SimpleFluidContent.STREAM_CODEC).build());