New-Soviet-Era/src/main/java/su/a71/new_soviet/items/CigaretteItem.java

126 lines
5.6 KiB
Java

package su.a71.new_soviet.items;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.*;
import net.minecraft.particle.DustParticleEffect;
import net.minecraft.particle.ParticleEffect;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.sound.SoundCategory;
import net.minecraft.stat.Stats;
import net.minecraft.text.Text;
import net.minecraft.util.*;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.random.Random;
import net.minecraft.world.World;
import net.minecraft.entity.LivingEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import org.jetbrains.annotations.Nullable;
import su.a71.new_soviet.NewSoviet;
import su.a71.new_soviet.registration.NSE_Items;
import su.a71.new_soviet.registration.NSE_Sounds;
import java.util.List;
import java.util.Objects;
// FIXME: 26.08.2023 This whole class is making my head hurt, fix!!!
public class CigaretteItem extends Item {
private final int durationInTicks;
private final Item returnedItem;
private final String local_tooltip;
public CigaretteItem(int durationInTicks, Item returnedItem, String localTooltip, Item.Settings settings) {
super(settings.maxDamageIfAbsent(durationInTicks));
this.durationInTicks = durationInTicks;
this.returnedItem = returnedItem;
this.local_tooltip = localTooltip;
}
@Override
public int getMaxUseTime(ItemStack stack) {
return durationInTicks - stack.getDamage() - 1;
}
@Override
public UseAction getUseAction(ItemStack stack) {
return UseAction.SPYGLASS;
}
@Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
ItemStack itemStack = user.getStackInHand(hand);
if (user.getInventory().getStack(40).getItem() == Items.FLINT_AND_STEEL && user.getInventory().getMainHandStack().getDamage() == 0) {
world.playSound(user, BlockPos.ofFloored(user.getPos()), NSE_Sounds.CIGARETTE_START, SoundCategory.PLAYERS, 1.0f, 1.0f);
user.incrementStat(Stats.USED.getOrCreateStat(this));
itemStack.damage(1, user, e -> e.sendEquipmentBreakStatus(EquipmentSlot.OFFHAND));
return ItemUsage.consumeHeldItem(world, user, hand);
} else if (user.getInventory().getMainHandStack().getDamage() != 0) {
world.playSound(user, BlockPos.ofFloored(user.getPos()), NSE_Sounds.CIGARETTE_RESTART, SoundCategory.PLAYERS, 1.0f, 1.0f);
user.incrementStat(Stats.USED.getOrCreateStat(this));
return ItemUsage.consumeHeldItem(world, user, hand);
} else if (user.isCreative()) {
world.playSound(user, BlockPos.ofFloored(user.getPos()), NSE_Sounds.CIGARETTE_RESTART, SoundCategory.PLAYERS, 1.0f, 1.0f);
user.incrementStat(Stats.USED.getOrCreateStat(this));
return ItemUsage.consumeHeldItem(world, user, hand);
}
return TypedActionResult.fail(itemStack);
}
@Override
public ItemStack finishUsing(ItemStack stack, World world, LivingEntity user) {
stack.onStoppedUsing(world, user, durationInTicks);
return stack;
}
@Override
public void usageTick(World world, LivingEntity user, ItemStack stack, int remainingUseTicks) {
user.setMovementSpeed(1.1f);
if (stack.getDamage() < durationInTicks - 1)
stack.damage(1, user, e -> e.sendEquipmentBreakStatus(EquipmentSlot.MAINHAND));
double d = user.getX();
double e = user.getY() + user.getHeight();
double f = user.getZ();
if (NewSoviet.RANDOM.nextBetween(1, 2) == 1) {
world.addParticle(ParticleTypes.SMOKE, d, e, f, 0, 0, 0);
float pitch = NewSoviet.RANDOM.nextBetween(8, 10) * 0.1f;
world.playSound(user, BlockPos.ofFloored(user.getPos()), NSE_Sounds.SMOKING, SoundCategory.PLAYERS, 1.0f, pitch);
} else if (NewSoviet.RANDOM.nextBetween(1, 6) == 1) {
world.addParticle(ParticleTypes.LARGE_SMOKE, d, e, f, 0, 0, 0);
float pitch = NewSoviet.RANDOM.nextBetween(9, 10) * 0.1f;
world.playSound(user, BlockPos.ofFloored(user.getPos()), NSE_Sounds.CIGARETTE_RANDOM, SoundCategory.PLAYERS, 1.0f, pitch);
}
}
@Override
public void onStoppedUsing(ItemStack stack, World world, LivingEntity user, int remainingUseTicks) {
if (stack.getDamage() < (durationInTicks - 1)) {
stack.damage(1, user, e -> e.sendEquipmentBreakStatus(EquipmentSlot.MAINHAND));
world.playSound(user, BlockPos.ofFloored(user.getPos()), NSE_Sounds.CIGARETTE_PAUSE, SoundCategory.PLAYERS, 1.0f, 1.0f);
} else if (stack.getDamage() >= (durationInTicks - 2)) {
user.equipStack(EquipmentSlot.MAINHAND, new ItemStack(returnedItem));
world.playSound(user, BlockPos.ofFloored(user.getPos()), NSE_Sounds.CIGARETTE_STOPPED, SoundCategory.PLAYERS, 1.0f, 1.0f);
}
double d = user.getX();
double e = user.getY() + 1.5;
double f = user.getZ();
world.addParticle(ParticleTypes.LARGE_SMOKE, d, e, f, 0, -0.03, 0);
}
@Override
public void appendTooltip(ItemStack stack, @Nullable World world, List<Text> tooltip, TooltipContext context) {
if (!local_tooltip.isEmpty()) {
tooltip.add(Text.translatable(local_tooltip));
super.appendTooltip(stack, world, tooltip, context);
}
}
}