Skip to content

Commit

Permalink
add ToolItem Item class
Browse files Browse the repository at this point in the history
wrench, toolkit
  • Loading branch information
LucasPlacentino committed Jan 2, 2023
1 parent 6cce75c commit 0e2ce31
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/main/java/com/obsilab/mcsc/item/ModItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -98,21 +99,23 @@ public class ModItems {
));

public static final RegistryObject<Item> 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<Item> 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<Item> TEST_ITEM_ITEM = ITEMS.register(
Expand Down
65 changes: 65 additions & 0 deletions src/main/java/com/obsilab/mcsc/item/custom/ToolItem.java
Original file line number Diff line number Diff line change
@@ -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<Component> 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<ItemStack> 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"));

}
}

0 comments on commit 0e2ce31

Please # to comment.