Created new StrawTotemItem.

This commit is contained in:
2022-01-12 17:17:18 +00:00
parent 517162fa4b
commit 9cb6ccf8cd

View File

@ -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<ITextComponent> 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<ItemStack> 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; }
}