Skip to content

Commit

Permalink
blaster ADS toggling and attribute modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
parzivail committed Oct 21, 2024
1 parent 6cc7425 commit c860a84
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}
}
}
4 changes: 2 additions & 2 deletions projects/pswg_blasters/src/main/java/dev/pswg/Blasters.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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-
Expand Down Expand Up @@ -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)
{
Expand All @@ -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);
}
Expand All @@ -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;
}
}
2 changes: 1 addition & 1 deletion projects/pswg_core/src/main/java/dev/pswg/Galaxies.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions projects/pswg_core/src/main/java/dev/pswg/world/TickConstants.java
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit c860a84

Please sign in to comment.