Change bullet visuals

Add reloading and firing cooldowns
This commit is contained in:
2024-07-25 21:08:08 +02:00
parent 96d369b2f7
commit 4df69c27a1
6 changed files with 152 additions and 77 deletions

View File

@@ -3,12 +3,17 @@ package systems.brn.plasticgun.lib;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.text.Text;
import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import net.minecraft.world.World;
import org.slf4j.LoggerFactory;
import systems.brn.plasticgun.guns.Gun;
import static systems.brn.plasticgun.PlasticGun.guns;
import static systems.brn.plasticgun.PlasticGun.*;
import static systems.brn.plasticgun.lib.GunComponents.*;
public class EventHandler {
public static TypedActionResult<ItemStack> onItemUse(PlayerEntity playerEntity, World world, Hand hand) {
@@ -29,12 +34,18 @@ public class EventHandler {
public static void onWorldTick(World world) {
// Iterate through all players to detect hand swings or item interactions
for (PlayerEntity player : world.getPlayers()) {
if (!world.isClient && player.handSwinging && player.handSwingTicks == 1) {
if (!world.isClient) {
Hand hand = player.getActiveHand();
Item stackInHand = player.getStackInHand(hand).getItem();
ItemStack stackInHand = player.getStackInHand(hand);
Item itemInHand = stackInHand.getItem();
for (Gun gun : guns) {
if (gun == stackInHand) {
gun.shoot(world, player, hand);
if (gun == itemInHand) {
decrementComponent(GUN_COOLDOWN_COMPONENT, stackInHand);
decrementComponent(GUN_RELOAD_COOLDOWN_COMPONENT, stackInHand);
if (player.handSwinging && player.handSwingTicks == -1) {
gun.shoot(world, player, hand);
}
break;
}
}

View File

@@ -11,8 +11,10 @@ import java.util.function.UnaryOperator;
public class GunComponents {
public static final ComponentType<ItemStack> GUN_AMMO_COMPONENT = register("gunammo", builder -> builder.codec(ItemStack.CODEC));
public static final ComponentType<Integer> GUN_LOADING_COMPONENT = register("gunload", builder -> builder.codec(Codec.INT));
public static final ComponentType<ItemStack> GUN_AMMO_COMPONENT = register("gun_ammo", builder -> builder.codec(ItemStack.CODEC));
public static final ComponentType<Integer> GUN_LOADING_COMPONENT = register("gun_load", builder -> builder.codec(Codec.INT));
public static final ComponentType<Integer> GUN_COOLDOWN_COMPONENT = register("gun_cooldown", builder -> builder.codec(Codec.INT));
public static final ComponentType<Integer> GUN_RELOAD_COOLDOWN_COMPONENT = register("gun_reload_cooldown", builder -> builder.codec(Codec.INT));
private static <T> ComponentType<T> register(String id, UnaryOperator<ComponentType.Builder<T>> builderOperator) {
ComponentType<T> componentType = Registry.register(
@@ -23,4 +25,12 @@ public class GunComponents {
PolymerComponent.registerDataComponent(componentType);
return componentType;
}
public static void decrementComponent(ComponentType<Integer> componentType, ItemStack stack) {
int component = stack.getOrDefault(componentType, 0);
if (component > 0) {
component--;
stack.set(componentType, component);
}
}
}