From 9cb6ccf8cdfc58df3d90a808e83129bcfe35d25a Mon Sep 17 00:00:00 2001 From: Micle Date: Wed, 12 Jan 2022 17:17:18 +0000 Subject: [PATCH] Created new StrawTotemItem. --- .../totemofreviving/item/StrawTotemItem.java | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 src/main/java/dev/micle/totemofreviving/item/StrawTotemItem.java diff --git a/src/main/java/dev/micle/totemofreviving/item/StrawTotemItem.java b/src/main/java/dev/micle/totemofreviving/item/StrawTotemItem.java new file mode 100644 index 0000000..2fa5107 --- /dev/null +++ b/src/main/java/dev/micle/totemofreviving/item/StrawTotemItem.java @@ -0,0 +1,109 @@ +package dev.micle.totemofreviving.item; + +import dev.micle.totemofreviving.TotemOfReviving; +import dev.micle.totemofreviving.config.Config; +import net.minecraft.client.Minecraft; +import net.minecraft.client.util.ITooltipFlag; +import net.minecraft.client.util.InputMappings; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.item.Rarity; +import net.minecraft.item.crafting.IRecipe; +import net.minecraft.util.ActionResult; +import net.minecraft.util.Hand; +import net.minecraft.util.text.ITextComponent; +import net.minecraft.util.text.StringTextComponent; +import net.minecraft.util.text.TextFormatting; +import net.minecraft.world.World; +import net.minecraftforge.api.distmarker.Dist; +import net.minecraftforge.api.distmarker.OnlyIn; +import org.lwjgl.glfw.GLFW; + +import javax.annotation.Nullable; +import javax.annotation.ParametersAreNonnullByDefault; +import java.util.List; + +public class StrawTotemItem extends Item { + private static final String NAME = "straw_totem"; + + private static final String TAG_TARGET_NAME = "target_uuid"; + private static final String TAG_TARGET_CHARGE = "target_cost"; + private static final String TAG_CHARGE= "charge"; + + private static IRecipe recipe; + + public StrawTotemItem() { + super(new Item.Properties().tab(TotemOfReviving.ITEM_GROUP).stacksTo(1).rarity(Rarity.UNCOMMON).defaultDurability(Config.Server.getStrawTotemConfig().getDurability())); + } + + public static IRecipe getRecipe() { + return recipe; + } + + public static void setRecipe(IRecipe recipeIn) { + recipe = recipeIn; + } + + @Override + @OnlyIn(Dist.CLIENT) + @ParametersAreNonnullByDefault + public void appendHoverText(ItemStack itemStack, @Nullable World world, List tooltip, ITooltipFlag tooltipFlag) { + super.appendHoverText(itemStack, world, tooltip, tooltipFlag); + if (world == null) { return; } + String target_uuid = itemStack.getOrCreateTag().getString(TAG_TARGET_NAME); + int target_charge = itemStack.getOrCreateTag().getInt(TAG_TARGET_CHARGE); + int charge = itemStack.getOrCreateTag().getInt(TAG_CHARGE); + + if (target_charge > getMaxCharge()) { + tooltip.add(new StringTextComponent(TextFormatting.RED + "Target: " + TextFormatting.DARK_RED + target_uuid)); + tooltip.add(new StringTextComponent(TextFormatting.RED + "Required charges: + " + TextFormatting.DARK_RED + target_charge)); + } else { + tooltip.add(new StringTextComponent(TextFormatting.WHITE + "Target: " + TextFormatting.GRAY + target_uuid)); + tooltip.add(new StringTextComponent(TextFormatting.WHITE + "Required charges: " + TextFormatting.GRAY + target_charge)); + } + tooltip.add(new StringTextComponent("")); + tooltip.add(new StringTextComponent(TextFormatting.WHITE + "Charges: " + TextFormatting.GRAY + "(" + charge + "/" + getMaxCharge() + ")")); + tooltip.add(new StringTextComponent("")); + if (InputMappings.isKeyDown(Minecraft.getInstance().getWindow().getWindow(), GLFW.GLFW_KEY_LEFT_SHIFT)) { + tooltip.add(new StringTextComponent(TextFormatting.GRAY + "Showing advanced tooltip.")); + tooltip.add(new StringTextComponent(TextFormatting.WHITE + "[" + TextFormatting.GRAY + "R-CLICK" + TextFormatting.WHITE + "]")); + tooltip.add(new StringTextComponent(TextFormatting.WHITE + "When second hand is empty: revive target.")); + tooltip.add(new StringTextComponent(TextFormatting.WHITE + "When second hand is holding a straw reviving charge: charge totem.")); + tooltip.add(new StringTextComponent("")); + tooltip.add(new StringTextComponent(TextFormatting.WHITE + "[" + TextFormatting.GRAY + "L-SHIFT + R-CLICK" + TextFormatting.WHITE + "]")); + tooltip.add(new StringTextComponent(TextFormatting.WHITE + "Change target.")); + } else { + tooltip.add(new StringTextComponent(TextFormatting.GRAY + "Hold [" + TextFormatting.DARK_GRAY + "L-SHIFT" + TextFormatting.GRAY + "] for advanced tooltip.")); + } + } + + @Override + @OnlyIn(Dist.CLIENT) + @ParametersAreNonnullByDefault + public ActionResult use(World world, PlayerEntity playerEntity, Hand hand) { + if (world.isClientSide) return super.use(world, playerEntity, hand); + if (playerEntity.isCrouching()) { + ItemStack itemStack; + if (hand.equals(Hand.MAIN_HAND)) { + itemStack = playerEntity.getMainHandItem(); + } else { + itemStack = playerEntity.getOffhandItem(); + } + itemStack.hurtAndBreak(1, playerEntity, e -> e.broadcastBreakEvent(hand)); + } else { + + } + return super.use(world, playerEntity, hand); + } + + @OnlyIn(Dist.CLIENT) + private int getMaxCharge() { + if (Config.Server.getStrawTotemConfig().getChargeCost() == -1) { + return Config.Server.getStrawTotemConfig().getChargeCostLimit(); + } + return Config.Server.getStrawTotemConfig().getChargeCost(); + } + + public static String getName() { return NAME; } +}