Skip to content

Commit

Permalink
Tooltips not showing problem fixed
Browse files Browse the repository at this point in the history
Crash while using Cactus Pickaxe Fixed
Attack Damage and Attack Speed Balanced
  • Loading branch information
rafaelmehdiyev committed Dec 24, 2024
1 parent 40ee6f2 commit 5b33e6e
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 93 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package net.rafael.usefulcactus.client;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.item.ItemStack;
import net.minecraft.registry.tag.BiomeTags;
import net.minecraft.client.MinecraftClient;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.text.Text;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.biome.BiomeKeys;

import java.util.List;

@Environment(EnvType.CLIENT)
public class CactusItemTooltips {
public static void addCactusSwordTooltip(ItemStack stack, World world, List<Text> tooltip) {
if (world != null && world.getBiome(world.getPlayerByUuid(world.getPlayers().get(0).getUuid()).getBlockPos())
.isIn(BiomeTags.VILLAGE_DESERT_HAS_STRUCTURE)) {
public static void addCactusSwordTooltip(List<Text> tooltip) {
if (getBiomeAtPlayerPosition() != null && getBiomeAtPlayerPosition().matchesKey(BiomeKeys.DESERT))
{
World world = getWorld();
tooltip.add(Text.translatable("item.rafaels-useful-cactus.cactus_sword.tooltip.desert"));

long timeOfDay = world.getTimeOfDay() % 24000;
if (timeOfDay >= 6000 && timeOfDay <= 6500) {
tooltip.add(Text.translatable("item.rafaels-useful-cactus.cactus_sword.tooltip.noon"));
Expand All @@ -26,18 +27,29 @@ public static void addCactusSwordTooltip(ItemStack stack, World world, List<Text
tooltip.add(Text.translatable("item.rafaels-useful-cactus.cactus_sword.tooltip.thorn"));
}

public static void addCactusPickaxeTooltip(ItemStack stack, World world, List<Text> tooltip) {
if (world != null && world.getBiome(world.getPlayerByUuid(world.getPlayers().get(0).getUuid()).getBlockPos())
.isIn(BiomeTags.VILLAGE_DESERT_HAS_STRUCTURE)) {
tooltip.add(Text.translatable("item.rafaels-useful-cactus.cactus_pickaxe.tooltip.desert"));
public static void addCactusPickaxeTooltip(List<Text> tooltip) {
if (getBiomeAtPlayerPosition() != null && getBiomeAtPlayerPosition().matchesKey(BiomeKeys.DESERT)) {
World world = getWorld();
tooltip.add(Text.translatable("item.rafaels-useful-cactus.cactus_pickaxe.tooltip.durability"));

long timeOfDay = world.getTimeOfDay() % 24000;
if (timeOfDay >= 6000 && timeOfDay <= 6500) {
tooltip.add(Text.translatable("item.rafaels-useful-cactus.cactus_pickaxe.tooltip.noon"));
} else if (timeOfDay >= 0 && timeOfDay < 13000) {
tooltip.add(Text.translatable("item.rafaels-useful-cactus.cactus_pickaxe.tooltip.daytime"));
}
}
}

// For client-side using MinecraftClient instance
private static RegistryEntry<Biome> getBiomeAtPlayerPosition() {
MinecraftClient client = MinecraftClient.getInstance();
BlockPos playerPos = client.player != null ? client.player.getBlockPos() : null;
return getWorld().getBiome(playerPos);
}

private static World getWorld() {
MinecraftClient client = MinecraftClient.getInstance();
return client.world;
}


}
43 changes: 26 additions & 17 deletions src/main/java/net/rafael/usefulcactus/item/ModItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,59 +22,70 @@ public class ModItems {

// Basic Items
public static final Item CACTUS_SKIN = registerItem("cactus_skin", new Item(new Item.Settings()
.registryKey(RegistryKey.of(RegistryKeys.ITEM, Identifier.of(RafaelsUsefulCactus.MOD_ID, "cactus_skin")))));
.registryKey(RegistryKey.of(RegistryKeys.ITEM, Identifier.of(RafaelsUsefulCactus.MOD_ID, "cactus_skin")))));

// Cactus Tool Items

// Damange 0 -> 3
// | Attack Speed | Value |
// |--------------|-------|
// | 0 | 4 |
// | -1 | 3 |
// | -2 | 2 |
// | -3 | 1 |
// | -4 | 0 |


// Cactus Sword: 6 attack damage, 1.6 attack speed
public static final Item CACTUS_SWORD = registerItem("cactus_sword",
new CactusSwordItem(ModToolMaterial.CACTUS_SKIN,3f, 2f,new Item.Settings()
new CactusSwordItem(ModToolMaterial.CACTUS_SKIN,3f, -2.6f,new Item.Settings()
.registryKey(RegistryKey.of(RegistryKeys.ITEM, Identifier.of(RafaelsUsefulCactus.MOD_ID, "cactus_sword")))));

// Cactus Pickaxe: 4 attack damage, 1.2 attack speed
public static final Item CACTUS_PICKAXE = registerItem("cactus_pickaxe", new CactusPickaxeItem(
ModToolMaterial.CACTUS_SKIN,1, -2.8f, new Item.Settings()
.registryKey(RegistryKey.of(RegistryKeys.ITEM, Identifier.of(RafaelsUsefulCactus.MOD_ID, "cactus_pickaxe")))));
ModToolMaterial.CACTUS_SKIN,0.5f, -2.8f, new Item.Settings()
.registryKey(RegistryKey.of(RegistryKeys.ITEM, Identifier.of(RafaelsUsefulCactus.MOD_ID, "cactus_pickaxe")))));

// Cactus Axe: 9 attack damage, 0.9 attack speed
public static final Item CACTUS_AXE = registerItem("cactus_axe", new AxeItem(
ModToolMaterial.CACTUS_SKIN,6, -3.1f, new Item.Settings()
.registryKey(RegistryKey.of(RegistryKeys.ITEM, Identifier.of(RafaelsUsefulCactus.MOD_ID, "cactus_axe")))));

// Cactus Shovel: 4.5 attack damage, 1.0 attack speed
// Cactus Shovel: 4 attack damage, 1.0 attack speed
public static final Item CACTUS_SHOVEL = registerItem("cactus_shovel", new ShovelItem(
ModToolMaterial.CACTUS_SKIN,1.5f, -3.0f, new Item.Settings()
ModToolMaterial.CACTUS_SKIN,1f, -3.0f, new Item.Settings()
.registryKey(RegistryKey.of(RegistryKeys.ITEM, Identifier.of(RafaelsUsefulCactus.MOD_ID, "cactus_shovel")))));

// Cactus Hoe: 1 attack damage, 1.0 attack speed
public static final Item CACTUS_HOE = registerItem("cactus_hoe", new HoeItem(
ModToolMaterial.CACTUS_SKIN,-2, -3.0f, new Item.Settings()
.registryKey(RegistryKey.of(RegistryKeys.ITEM, Identifier.of(RafaelsUsefulCactus.MOD_ID, "cactus_hoe")))));

// Cactus Hammer: 12 attack damage, 0.9 attack speed
// Cactus Hammer: 7 attack damage, 0.6 attack speed
public static final Item CACTUS_HAMMER = registerItem("cactus_hammer", new CactusHammerItem(
ModToolMaterial.CACTUS_SKIN,9, -3.1f, new Item.Settings()
ModToolMaterial.CACTUS_SKIN,4, -3.4f, new Item.Settings()
.registryKey(RegistryKey.of(RegistryKeys.ITEM, Identifier.of(RafaelsUsefulCactus.MOD_ID, "cactus_hammer")))));

// Cactus Armor Items
public static final Item CACTUS_HELMET = registerItem("cactus_helmet",
new ModArmorItem(ModArmorMaterials.CACTUS_SKIN_ARMOR_MATERIAL, EquipmentType.HELMET,
new Item.Settings()
.registryKey(RegistryKey.of(RegistryKeys.ITEM, Identifier.of(RafaelsUsefulCactus.MOD_ID, "cactus_helmet")))));
.registryKey(RegistryKey.of(RegistryKeys.ITEM, Identifier.of(RafaelsUsefulCactus.MOD_ID, "cactus_helmet")))));

public static final Item CACTUS_CHESTPLATE = registerItem("cactus_chestplate",
new ArmorItem(ModArmorMaterials.CACTUS_SKIN_ARMOR_MATERIAL, EquipmentType.CHESTPLATE,
new Item.Settings()
.registryKey(RegistryKey.of(RegistryKeys.ITEM, Identifier.of(RafaelsUsefulCactus.MOD_ID, "cactus_chestplate")))));
.registryKey(RegistryKey.of(RegistryKeys.ITEM, Identifier.of(RafaelsUsefulCactus.MOD_ID, "cactus_chestplate")))));

public static final Item CACTUS_LEGGINGS = registerItem("cactus_leggings",
new ArmorItem(ModArmorMaterials.CACTUS_SKIN_ARMOR_MATERIAL, EquipmentType.LEGGINGS,
new Item.Settings()
.registryKey(RegistryKey.of(RegistryKeys.ITEM, Identifier.of(RafaelsUsefulCactus.MOD_ID, "cactus_leggings")))));
.registryKey(RegistryKey.of(RegistryKeys.ITEM, Identifier.of(RafaelsUsefulCactus.MOD_ID, "cactus_leggings")))));

public static final Item CACTUS_BOOTS = registerItem("cactus_boots",
new ArmorItem(ModArmorMaterials.CACTUS_SKIN_ARMOR_MATERIAL, EquipmentType.BOOTS,
new Item.Settings()
.registryKey(RegistryKey.of(RegistryKeys.ITEM, Identifier.of(RafaelsUsefulCactus.MOD_ID, "cactus_boots")))));
.registryKey(RegistryKey.of(RegistryKeys.ITEM, Identifier.of(RafaelsUsefulCactus.MOD_ID, "cactus_boots")))));

/**
* Registers an item with the game's item registry.
Expand All @@ -92,9 +103,7 @@ private static Item registerItem(String name, Item item) {
public static void registerModItems() {
RafaelsUsefulCactus.LOGGER.info("Registering Mod Items for " + RafaelsUsefulCactus.MOD_ID);

ItemGroupEvents.modifyEntriesEvent(ItemGroups.INGREDIENTS).register(entries -> {
entries.add(CACTUS_SKIN);
});
ItemGroupEvents.modifyEntriesEvent(ItemGroups.INGREDIENTS).register(entries -> entries.add(CACTUS_SKIN));

ItemGroupEvents.modifyEntriesEvent(ItemGroups.COMBAT).register(entries -> {
entries.add(CACTUS_SWORD);
Expand All @@ -112,4 +121,4 @@ public static void registerModItems() {
entries.add(CACTUS_HAMMER);
});
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package net.rafael.usefulcactus.item.custom;

import java.util.List;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.block.BlockState;
Expand All @@ -12,37 +10,39 @@
import net.minecraft.item.PickaxeItem;
import net.minecraft.item.ToolMaterial;
import net.minecraft.item.tooltip.TooltipType;
import net.minecraft.registry.tag.BiomeTags;
import net.minecraft.text.Text;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.biome.BiomeKeys;
import net.rafael.usefulcactus.client.CactusItemTooltips;

import java.util.List;


public class CactusPickaxeItem extends PickaxeItem {
public CactusPickaxeItem(ToolMaterial material,float attackDamage, float attackSpeed, Item.Settings settings) {
public CactusPickaxeItem(ToolMaterial material, float attackDamage, float attackSpeed, Item.Settings settings) {
super(material, attackDamage, attackSpeed, settings);
}

@Override
public boolean postMine(ItemStack stack, World world, BlockState state, BlockPos pos, LivingEntity miner) {
if (miner instanceof PlayerEntity player) {
if (world.getBiome(player.getBlockPos()).isIn(BiomeTags.VILLAGE_DESERT_HAS_STRUCTURE)) {
// 20% chance to not consume durability
if (world.random.nextFloat() < 0.20f) {
return false;
@Override
public boolean postMine(ItemStack stack, World world, BlockState state, BlockPos pos, LivingEntity miner) {
if (miner instanceof PlayerEntity player) {
if (world.getBiome(player.getBlockPos()).matchesKey(BiomeKeys.DESERT)) {
// 20% chance to not consume durability
if (world.random.nextFloat() < 0.20f) {
return false;
}
}
}
return super.postMine(stack, world, state, pos, miner);
}
return super.postMine(stack, world, state, pos, miner);
}

@Override
@Override
@Environment(EnvType.CLIENT)
public void appendTooltip(ItemStack stack, Item.TooltipContext context, List<Text> tooltip, TooltipType type) {
if (type == TooltipType.ADVANCED) {
CactusItemTooltips.addCactusPickaxeTooltip(stack, null, tooltip);
CactusItemTooltips.addCactusPickaxeTooltip(tooltip);
}
super.appendTooltip(stack, context, tooltip, type);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public CactusSwordItem(ToolMaterial toolMaterial,float attackDamage, float attac
@Environment(EnvType.CLIENT)
public void appendTooltip(ItemStack stack, Item.TooltipContext context, List<Text> tooltip, TooltipType type) {
if (type == TooltipType.ADVANCED) {
CactusItemTooltips.addCactusSwordTooltip(stack, null, tooltip);
CactusItemTooltips.addCactusSwordTooltip(tooltip);
}
super.appendTooltip(stack, context, tooltip, type);
}
Expand Down

This file was deleted.

20 changes: 13 additions & 7 deletions src/main/java/net/rafael/usefulcactus/mixin/CactusSwordMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@
import net.minecraft.entity.mob.HuskEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.SwordItem;
import net.minecraft.registry.tag.BiomeTags;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.biome.BiomeKeys;
import net.rafael.usefulcactus.effect.ModEffects;
import net.rafael.usefulcactus.item.ModItems;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(SwordItem.class)
public class CactusSwordMixin {

@Inject(method = "postHit", at = @At("HEAD"))
private void onPostHit(ItemStack stack, LivingEntity target, LivingEntity attacker, CallbackInfoReturnable<Boolean> cir) {
if (!(stack.getItem() instanceof SwordItem) || stack.getItem() != ModItems.CACTUS_SWORD) {
Expand All @@ -27,8 +28,8 @@ private void onPostHit(ItemStack stack, LivingEntity target, LivingEntity attack

World world = target.getWorld();
BlockPos pos = target.getBlockPos();
if (world.getBiome(pos).isIn(BiomeTags.VILLAGE_DESERT_HAS_STRUCTURE)) {

if (world.getBiome(pos).matchesKey(BiomeKeys.DESERT)) {
applyTimeDamageBonus(world, target);
applyDesertThornEffect(world, target);
applyExtraDamageToHusks(target);
Expand All @@ -37,44 +38,49 @@ private void onPostHit(ItemStack stack, LivingEntity target, LivingEntity attack
}
}

@Unique
private void applyTimeDamageBonus(World world, LivingEntity target) {
long timeOfDay = world.getTimeOfDay() % 24000;
float bonus = 0;

if (timeOfDay >= 0 && timeOfDay < 13000) {
bonus = 1.0f;
if (timeOfDay >= 6000 && timeOfDay <= 6500) {
bonus = 2.0f;
}
}

if (bonus > 0) {
target.damage((ServerWorld)world,target.getDamageSources().magic(), bonus);
}
}

@Unique
private void applyDesertThornEffect(World world, LivingEntity target) {
if (world.getRandom().nextFloat() < 0.2f) {
target.addStatusEffect(new StatusEffectInstance(ModEffects.DESERT_THORN, 200, 0, false, true, true));
}
}

@Unique
private void applyExtraDamageToHusks(LivingEntity target) {
World world = target.getWorld();
if (target instanceof HuskEntity) {
target.damage((ServerWorld)world,target.getDamageSources().magic(), 3.0f);
}
}

@Unique
private void applyBonusDamageOnSand(World world, BlockPos pos, LivingEntity target) {
if (world.getBlockState(pos.down()).isIn(net.minecraft.registry.tag.BlockTags.SAND)) {
target.damage((ServerWorld) world,target.getDamageSources().magic(), 1.5f);
}
}

@Unique
private void reduceDurabilityLoss(World world, ItemStack stack) {
if (world.getRandom().nextFloat() < 0.5f && stack.getDamage() > 0) {
stack.setDamage(stack.getDamage() - 1);
}
}
}
}
3 changes: 1 addition & 2 deletions src/main/resources/rafaels-useful-cactus.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
"compatibilityLevel": "JAVA_21",
"mixins": [
"CactusBlockMixin",
"CactusSwordMixin",
"CactusPickaxeMixin"
"CactusSwordMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down

0 comments on commit 5b33e6e

Please sign in to comment.