From 0e2ce31297f45ab15ff1433864f5bf61da44e906 Mon Sep 17 00:00:00 2001 From: ticccco <23436953+LucasPlacentino@users.noreply.github.com> Date: Tue, 3 Jan 2023 00:02:05 +0100 Subject: [PATCH] add ToolItem Item class wrench, toolkit --- .../java/com/obsilab/mcsc/item/ModItems.java | 13 ++-- .../obsilab/mcsc/item/custom/ToolItem.java | 65 +++++++++++++++++++ 2 files changed, 73 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/obsilab/mcsc/item/custom/ToolItem.java diff --git a/src/main/java/com/obsilab/mcsc/item/ModItems.java b/src/main/java/com/obsilab/mcsc/item/ModItems.java index 5681d44..8e6d8e2 100644 --- a/src/main/java/com/obsilab/mcsc/item/ModItems.java +++ b/src/main/java/com/obsilab/mcsc/item/ModItems.java @@ -3,6 +3,7 @@ import com.obsilab.mcsc.MCSC; import com.obsilab.mcsc.fluid.ModFluids; import com.obsilab.mcsc.item.custom.TestItem; +import com.obsilab.mcsc.item.custom.ToolItem; import net.minecraft.world.item.BucketItem; import net.minecraft.world.item.Item; import net.minecraft.world.item.Items; @@ -98,21 +99,23 @@ public class ModItems { )); public static final RegistryObject WRENCH_ITEM = ITEMS.register( - "wrench", () -> new Item( - new Item.Properties() + "wrench", () -> new ToolItem( + new ToolItem.Properties() .stacksTo(1) .durability(100) //.group(MCSC.MCSC_GROUP) //.tab(ModCreativeModeTab.MCSC_TAB) + , true )); public static final RegistryObject TOOLKIT_ITEM = ITEMS.register( - "toolkit", () -> new Item( - new Item.Properties() + "toolkit", () -> new ToolItem( + new ToolItem.Properties() .stacksTo(1) - .durability(100) + .durability(1000) //.group(MCSC.MCSC_GROUP) //.tab(ModCreativeModeTab.MCSC_TAB) + , false )); public static final RegistryObject TEST_ITEM_ITEM = ITEMS.register( diff --git a/src/main/java/com/obsilab/mcsc/item/custom/ToolItem.java b/src/main/java/com/obsilab/mcsc/item/custom/ToolItem.java new file mode 100644 index 0000000..61a7800 --- /dev/null +++ b/src/main/java/com/obsilab/mcsc/item/custom/ToolItem.java @@ -0,0 +1,65 @@ +package com.obsilab.mcsc.item.custom; + +import net.minecraft.ChatFormatting; +import net.minecraft.client.gui.screens.Screen; +import net.minecraft.network.chat.Component; +import net.minecraft.world.InteractionHand; +import net.minecraft.world.InteractionResult; +import net.minecraft.world.InteractionResultHolder; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.TooltipFlag; +import net.minecraft.world.level.Level; +import org.jetbrains.annotations.Nullable; + +import java.util.List; + +public class ToolItem extends Item { + private boolean HAS_COOLDOWN = true; + private int COOLDOWN = 20; //20 ticks = 1s of cooldown + + public ToolItem(Properties properties, boolean hasCooldown) { + super(properties); + this.HAS_COOLDOWN = hasCooldown; + } + + @Override + public void appendHoverText(ItemStack itemStack, @Nullable Level level, List components, TooltipFlag tooltipFlag) { + + if(Screen.hasShiftDown()) { + components.add(Component.literal("This is a test item").withStyle(ChatFormatting.UNDERLINE, ChatFormatting.YELLOW)); + components.add(Component.literal("Hold SHIFT for more info").withStyle(ChatFormatting.WHITE)); + components.add(Component.literal("TestItem tooltip and description").withStyle(ChatFormatting.BOLD)); + } else { + components.add(Component.literal("This is a test item").withStyle(ChatFormatting.UNDERLINE, ChatFormatting.YELLOW)); + components.add(Component.literal("Hold SHIFT for more info").withStyle(ChatFormatting.WHITE)); + + } + + super.appendHoverText(itemStack, level, components, tooltipFlag); + } + + + @Override + public InteractionResultHolder use(Level level, Player player, InteractionHand hand) { + + if(!level.isClientSide() && hand==InteractionHand.MAIN_HAND) { + player.sendSystemMessage(Component.literal("ToolItem used")); + outputMessage(player); + if(this.HAS_COOLDOWN) { + player.getCooldowns().addCooldown(this, COOLDOWN); + //player.sendSystemMessage(Component.literal());// get the bloc kthe player is looking at + } + //player.getCooldowns().addCooldown(this, COOLDOWN); //20 ticks = 1s of cooldown + //return InteractionResultHolder.success(player.getItemInHand(hand)); + } + + return super.use(level, player, hand); + } + + private void outputMessage(Player player) { + player.sendSystemMessage(Component.literal(player.getName().getString() + " interacted with TestItem")); + + } +}