From c860a843f015731bea894f7f5aaf4ec3dc02d314 Mon Sep 17 00:00:00 2001 From: parzivail Date: Sun, 20 Oct 2024 22:36:44 -0400 Subject: [PATCH] blaster ADS toggling and attribute modifiers --- .../pswg/mixin/client/DrawContextMixin.java | 15 +--- .../src/main/java/dev/pswg/Blasters.java | 4 +- .../main/java/dev/pswg/item/BlasterItem.java | 71 +++++++++++++++++-- .../src/main/java/dev/pswg/Galaxies.java | 2 +- .../java/dev/pswg/world/TickConstants.java | 22 ++++++ 5 files changed, 91 insertions(+), 23 deletions(-) create mode 100644 projects/pswg_core/src/main/java/dev/pswg/world/TickConstants.java diff --git a/projects/pswg_blasters/src/client/java/dev/pswg/mixin/client/DrawContextMixin.java b/projects/pswg_blasters/src/client/java/dev/pswg/mixin/client/DrawContextMixin.java index cabc835b7..2a475c134 100644 --- a/projects/pswg_blasters/src/client/java/dev/pswg/mixin/client/DrawContextMixin.java +++ b/projects/pswg_blasters/src/client/java/dev/pswg/mixin/client/DrawContextMixin.java @@ -1,14 +1,10 @@ package dev.pswg.mixin.client; import dev.pswg.Blasters; -import dev.pswg.GalaxiesClient; -import dev.pswg.item.BlasterItem; -import dev.pswg.rendering.Drawables; import net.minecraft.client.MinecraftClient; import net.minecraft.client.font.TextRenderer; import net.minecraft.client.gui.DrawContext; import net.minecraft.item.ItemStack; -import net.minecraft.util.math.MathHelper; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; @@ -28,15 +24,8 @@ public void drawStackOverlay(TextRenderer textRenderer, ItemStack stack, int x, if (stack.isOf(Blasters.BLASTER)) { - var nextTimestamp = stack.get(BlasterItem.NEXT_TIMESTAMP_COMPONENT); - if (nextTimestamp == null) - return; - - var currentTimestamp = client.world.getTime() + GalaxiesClient.getTickDelta(); - - var value = MathHelper.clamp((nextTimestamp - currentTimestamp) / 10f, 0, 1); - - Drawables.itemDurability(self, value, x, y - 13, 13, 0x0000FF); + // TODO: implement cooldown heat bar + // Drawables.itemDurability(self, value, x, y - 13, 13, 0x0000FF); } } } diff --git a/projects/pswg_blasters/src/main/java/dev/pswg/Blasters.java b/projects/pswg_blasters/src/main/java/dev/pswg/Blasters.java index a4c0ca21b..bf591a3ba 100644 --- a/projects/pswg_blasters/src/main/java/dev/pswg/Blasters.java +++ b/projects/pswg_blasters/src/main/java/dev/pswg/Blasters.java @@ -38,9 +38,9 @@ public static Identifier id(String path) } /** - * A logger available only to PSWG core + * A logger available only to PSWG module and addon blasters */ - static final Logger LOGGER = Galaxies.createSubLogger("blasters"); + public static final Logger LOGGER = Galaxies.createSubLogger("blasters"); /** * The configuration file that controls the behavior of PSWG core diff --git a/projects/pswg_blasters/src/main/java/dev/pswg/item/BlasterItem.java b/projects/pswg_blasters/src/main/java/dev/pswg/item/BlasterItem.java index 523b10920..9cb3275e3 100644 --- a/projects/pswg_blasters/src/main/java/dev/pswg/item/BlasterItem.java +++ b/projects/pswg_blasters/src/main/java/dev/pswg/item/BlasterItem.java @@ -2,27 +2,54 @@ import com.mojang.serialization.Codec; import dev.pswg.Blasters; -import dev.pswg.world.TickSpan; +import dev.pswg.world.TickConstants; import net.minecraft.block.BlockState; import net.minecraft.component.ComponentType; +import net.minecraft.component.DataComponentTypes; +import net.minecraft.component.type.AttributeModifierSlot; +import net.minecraft.component.type.AttributeModifiersComponent; import net.minecraft.entity.LivingEntity; +import net.minecraft.entity.attribute.EntityAttributeModifier; +import net.minecraft.entity.attribute.EntityAttributes; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.item.consume.UseAction; import net.minecraft.registry.Registries; import net.minecraft.registry.Registry; +import net.minecraft.util.ActionResult; +import net.minecraft.util.Hand; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; public class BlasterItem extends Item { /** - * If a blaster us "used" for longer than this time, in seconds, then + * If a blaster us "used" for longer than this time, in ticks, then * the "use" interaction will be considered a "hold to aim" instead of * a "toggle aim", and aiming will cease when the "using" stops. */ - public static final float TOGGLE_AIMING_USE_TIME_SECONDS = 0.15f; + protected static final int TOGGLE_AIMING_USE_TIME_TICKS = 3; + + /** + * The attribute modifier that is applied to the {@link EntityAttributes#MOVEMENT_SPEED} + * attribute in players when they are aiming-down-sights. + */ + protected static final EntityAttributeModifier ATTR_MODIFIER_AIMING_SPEED_PENALTY_ENABLED = new EntityAttributeModifier( + Blasters.id("aiming_speed_penalty"), + -0.5F, + EntityAttributeModifier.Operation.ADD_MULTIPLIED_TOTAL + ); + + /** + * The attribute modifier that is applied to the {@link EntityAttributes#MOVEMENT_SPEED} + * attribute in players when they are not aiming-down-sights. + */ + protected static final EntityAttributeModifier ATTR_MODIFIER_AIMING_SPEED_PENALTY_DISABLED = new EntityAttributeModifier( + Blasters.id("aiming_speed_penalty"), + 0, + EntityAttributeModifier.Operation.ADD_MULTIPLIED_TOTAL + ); /** * The component that determines if the blaster is currently aiming- @@ -51,6 +78,23 @@ public static boolean isAiming(ItemStack stack) return stack.getOrDefault(IS_AIMING, false); } + /** + * Sets the aiming-down-sights status of the given blaster + * + * @param stack The stack to modify + * @param aiming True if the blaster should be aiming-down-sights, false otherwise + */ + public static void setAiming(ItemStack stack, boolean aiming) + { + stack.set(IS_AIMING, aiming); + var attrs = stack.getOrDefault(DataComponentTypes.ATTRIBUTE_MODIFIERS, AttributeModifiersComponent.DEFAULT); + stack.set(DataComponentTypes.ATTRIBUTE_MODIFIERS, attrs.with( + EntityAttributes.MOVEMENT_SPEED, + aiming ? ATTR_MODIFIER_AIMING_SPEED_PENALTY_ENABLED : ATTR_MODIFIER_AIMING_SPEED_PENALTY_DISABLED, + AttributeModifierSlot.HAND) + ); + } + @Override public boolean canMine(BlockState state, World world, BlockPos pos, PlayerEntity miner) { @@ -61,7 +105,7 @@ public boolean canMine(BlockState state, World world, BlockPos pos, PlayerEntity public int getMaxUseTime(ItemStack stack, LivingEntity user) { if (isAiming(stack)) - return TickSpan.fromSeconds(user.getWorld(), 3600); + return TickConstants.ONE_HOUR; return super.getMaxUseTime(stack, user); } @@ -75,10 +119,23 @@ public UseAction getUseAction(ItemStack stack) @Override public boolean onStoppedUsing(ItemStack stack, World world, LivingEntity user, int remainingUseTicks) { - var toggleAimingUseTimeTicks = TickSpan.fromSeconds(world, TOGGLE_AIMING_USE_TIME_SECONDS); - if (!world.isClient && user.getItemUseTime() > toggleAimingUseTimeTicks && isAiming(stack)) - stack.set(IS_AIMING, false); + if (!world.isClient && user.getItemUseTime() > TOGGLE_AIMING_USE_TIME_TICKS && isAiming(stack)) + setAiming(stack, false); return super.onStoppedUsing(stack, world, user, remainingUseTicks); } + + @Override + public ActionResult use(World world, PlayerEntity user, Hand hand) + { + var stack = user.getStackInHand(hand); + + // TODO: hold-ADS not working + if (!world.isClient) + { + setAiming(stack, !isAiming(stack)); + } + + return ActionResult.FAIL; + } } diff --git a/projects/pswg_core/src/main/java/dev/pswg/Galaxies.java b/projects/pswg_core/src/main/java/dev/pswg/Galaxies.java index bcf3cfb48..ffb969b05 100644 --- a/projects/pswg_core/src/main/java/dev/pswg/Galaxies.java +++ b/projects/pswg_core/src/main/java/dev/pswg/Galaxies.java @@ -45,7 +45,7 @@ public static Identifier id(String path) /** * A logger available only to PSWG core */ - static final Logger LOGGER = LoggerFactory.getLogger(MODID); + public static final Logger LOGGER = LoggerFactory.getLogger(MODID); /** * The configuration file that controls the behavior of PSWG core diff --git a/projects/pswg_core/src/main/java/dev/pswg/world/TickConstants.java b/projects/pswg_core/src/main/java/dev/pswg/world/TickConstants.java new file mode 100644 index 000000000..16ecff7a0 --- /dev/null +++ b/projects/pswg_core/src/main/java/dev/pswg/world/TickConstants.java @@ -0,0 +1,22 @@ +package dev.pswg.world; + +/** + * A collection of common constant tick lengths, assuming 20 ticks per second + */ +public final class TickConstants +{ + /** + * One second, or 20 ticks + */ + public static final int ONE_SECOND = 20; + + /** + * One minute, or 1200 ticks + */ + public static final int ONE_MINUTE = 60 * ONE_SECOND; + + /** + * One hour, or 72000 ticks + */ + public static final int ONE_HOUR = 60 * ONE_MINUTE; +}