Skip to content

Commit

Permalink
Add previous destination capability to portal gun v2
Browse files Browse the repository at this point in the history
  • Loading branch information
Direwolf20-MC committed Nov 2, 2024
1 parent cd40526 commit 7bf7a3c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand All @@ -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) {
Expand Down Expand Up @@ -199,6 +203,18 @@ public static void setFavorites(ItemStack itemStack, List<NBTHelpers.PortalDesti
itemStack.set(JustDireDataComponents.PORTAL_GUN_FAVORITES, favorites);
}

public static void setPrevious(Player player, ItemStack itemStack) {
Vec3 position = player.position();
Direction facing = MiscHelpers.getFacingDirection(player);
ResourceKey<Level> 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<NBTHelpers.PortalDestination> list = new ArrayList<>(MAX_FAVORITES);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class JustDireDataComponents {
public static final DeferredHolder<DataComponentType<?>, DataComponentType<UUID>> PORTALGUN_UUID = COMPONENTS.register("portalgun_uuid", () -> DataComponentType.<UUID>builder().persistent(UUIDUtil.CODEC).networkSynchronized(UUIDUtil.STREAM_CODEC).build());
public static final DeferredHolder<DataComponentType<?>, DataComponentType<Integer>> PORTALGUN_FAVORITE = COMPONENTS.register("portalgun_favorite", () -> DataComponentType.<Integer>builder().persistent(Codec.INT).networkSynchronized(ByteBufCodecs.VAR_INT).build());
public static final DeferredHolder<DataComponentType<?>, DataComponentType<List<NBTHelpers.PortalDestination>>> PORTAL_GUN_FAVORITES = COMPONENTS.register("portal_gun_favorites", () -> DataComponentType.<List<NBTHelpers.PortalDestination>>builder().persistent(NBTHelpers.PortalDestination.CODEC.listOf()).networkSynchronized(NBTHelpers.PortalDestination.STREAM_CODEC.apply(ByteBufCodecs.list())).build());
public static final DeferredHolder<DataComponentType<?>, DataComponentType<NBTHelpers.PortalDestination>> PORTAL_GUN_PREVIOUS = COMPONENTS.register("portal_gun_previous", () -> DataComponentType.<NBTHelpers.PortalDestination>builder().persistent(NBTHelpers.PortalDestination.CODEC).networkSynchronized(NBTHelpers.PortalDestination.STREAM_CODEC).build());
public static final DeferredHolder<DataComponentType<?>, DataComponentType<Boolean>> PORTAL_GUN_STAY_OPEN = COMPONENTS.register("portal_gun_stay_open", () -> DataComponentType.<Boolean>builder().persistent(Codec.BOOL).networkSynchronized(ByteBufCodecs.BOOL).build());
public static final DeferredHolder<DataComponentType<?>, DataComponentType<SimpleFluidContent>> FLUID_CONTAINER = COMPONENTS.register("fluid_container", () -> DataComponentType.<SimpleFluidContent>builder().persistent(SimpleFluidContent.CODEC).networkSynchronized(SimpleFluidContent.STREAM_CODEC).build());

Expand Down

0 comments on commit 7bf7a3c

Please sign in to comment.