Skip to content

Commit

Permalink
to make sure that the BOWL is returned to the player after being cons…
Browse files Browse the repository at this point in the history
…umed
  • Loading branch information
MEGATREX4 committed Jun 12, 2024
1 parent fe2ec6b commit 7365965
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void onInitialize() {
ModBlock.registerModBlock();
ModItems.registerModItems();
ModItemGroups.registerItemGroups();
ToolTipHelper.RegisterTooltip();
ToolTipHelper.registerTooltip();


LOGGER.info("Hello Fabric world it`s " + MOD_ID + "!");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.megatrex4.ukrainian_dlight.item;

import com.megatrex4.ukrainian_dlight.UkrainianDelight;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.world.World;

public class BowlReturningFoodItem extends ToolTipHelper {

public BowlReturningFoodItem(Settings settings) {
super(settings);
}

@Override
public ItemStack finishUsing(ItemStack stack, World world, LivingEntity user) {
// Use the item as usual
ItemStack resultStack = super.finishUsing(stack, world, user);

if (user instanceof PlayerEntity) {
PlayerEntity player = (PlayerEntity) user;
ItemStack bowlStack = new ItemStack(Items.BOWL);

// Check if the player's inventory can accept the bowl
boolean addedToInventory = player.getInventory().insertStack(bowlStack);

// Log inventory addition attempt
// UkrainianDelight.LOGGER.info("Attempting to add bowl to inventory: " + addedToInventory);

// If not added to inventory, drop the bowl near the player
if (!addedToInventory) {
player.dropItem(bowlStack, false);
// UkrainianDelight.LOGGER.info("Dropping bowl near player");
}
}

return stack.isEmpty() ? ItemStack.EMPTY : stack;
}
}
43 changes: 43 additions & 0 deletions src/main/java/com/megatrex4/ukrainian_dlight/item/ItemBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.megatrex4.ukrainian_dlight.item;

import net.fabricmc.fabric.api.item.v1.FabricItemSettings;
import net.minecraft.item.FoodComponent;
import net.minecraft.item.Item;

public class ItemBuilder {
private FoodComponent foodComponent;
private int maxCount = 64;
private Boolean returnsBowl = null;

public ItemBuilder food(FoodComponent foodComponent) {
this.foodComponent = foodComponent;
return this;
}

public ItemBuilder maxCount(int maxCount) {
this.maxCount = maxCount;
return this;
}

public ItemBuilder returnsBowl() {
this.returnsBowl = true;
return this;
}

public ItemBuilder returnsBowl(boolean returnsBowl) {
this.returnsBowl = returnsBowl;
return this;
}

public Item build() {
FabricItemSettings settings = new FabricItemSettings().maxCount(maxCount);
if (foodComponent != null) {
settings.food(foodComponent);
}
if (returnsBowl == null || returnsBowl) {
return new BowlReturningFoodItem(settings);
} else {
return new ToolTipHelper(settings);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.nhoryzon.mc.farmersdelight.registry.EffectsRegistry;
import net.minecraft.entity.effect.StatusEffect;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.item.FoodComponent;

public class ModFoodComponents {
Expand Down
35 changes: 9 additions & 26 deletions src/main/java/com/megatrex4/ukrainian_dlight/item/ModItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,23 @@

public class ModItems {

// Define items without specifying maxCount, defaults to 64
public static final Item VARENYK = createFoodItem("varenyk", ModFoodComponents.VARENYK);
public static final Item BORSCHT = createFoodItem("borscht", ModFoodComponents.BORSCHT, 16);
public static final Item LEAN_BORSCHT = createFoodItem("lean_borscht", ModFoodComponents.LEAN_BORSCHT, 16);
// Define items using the ItemBuilder
public static final Item VARENYK = registerItem("varenyk", new ItemBuilder().food(ModFoodComponents.VARENYK).build());
public static final Item BORSCHT = registerItem("borscht", new ItemBuilder().food(ModFoodComponents.BORSCHT).maxCount(16).returnsBowl().build());
public static final Item LEAN_BORSCHT = registerItem("lean_borscht", new ItemBuilder().food(ModFoodComponents.LEAN_BORSCHT).maxCount(16).returnsBowl().build());

public static final Item HORSERADISH = createFoodItem("horseradish", ModFoodComponents.HORSERADISH);
public static final Item CUCUMBER = createFoodItem("cucumber", ModFoodComponents.CUCUMBER);
public static final Item CUTTED_CUCUMBER = createFoodItem("cutted_cucumber", ModFoodComponents.CUTTED_CUCUMBER);
public static final Item HORSERADISH = registerItem("horseradish", new ItemBuilder().food(ModFoodComponents.HORSERADISH).build());
public static final Item CUCUMBER = registerItem("cucumber", new ItemBuilder().food(ModFoodComponents.CUCUMBER).build());
public static final Item CUTTED_CUCUMBER = registerItem("cutted_cucumber", new ItemBuilder().food(ModFoodComponents.CUTTED_CUCUMBER).build());

// Simple items without specifying maxCount, defaults to 64
public static final Item SALT = registerItem("salt", new Item(new FabricItemSettings()));
public static final Item SALT = registerItem("salt", new ItemBuilder().build());

// Register items with optional maxCount
// Register items
public static Item registerItem(String name, Item item) {
return Registry.register(Registries.ITEM, new Identifier(UkrainianDelight.MOD_ID, name.toLowerCase()), item);
}

public static Item registerItem(String name, Item item, int maxCount) {
return Registry.register(Registries.ITEM, new Identifier(UkrainianDelight.MOD_ID, name.toLowerCase()), item);
}

// Create food item with specified food component, optional maxCount
private static Item createFoodItem(String name, FoodComponent foodComponent) {
return createFoodItem(name, foodComponent, 64);
}

private static Item createFoodItem(String name, FoodComponent foodComponent, int maxCount) {
// Ensure that ModFoodComponents is initialized before accessing its components
ModFoodComponents.init();

// Create the food item with the specified max count
return registerItem(name, new ToolTipHelper(new FabricItemSettings().food(foodComponent).maxCount(maxCount)), maxCount);
}

public static void registerModItems() {
UkrainianDelight.LOGGER.info("Registering Mod Items for " + UkrainianDelight.MOD_ID);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.megatrex4.ukrainian_dlight.item;

import java.util.List;

import com.megatrex4.ukrainian_dlight.UkrainianDelight;
import com.megatrex4.ukrainian_dlight.util.StatusEffectUtil;
import com.mojang.datafixers.util.Pair;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.item.TooltipContext;
Expand All @@ -17,35 +15,30 @@
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class ToolTipHelper extends Item {

public ToolTipHelper(Settings settings) {
super(settings);
}

// This method adds tooltip information to the item when hovered over in the inventory
@Override
@Environment(EnvType.CLIENT)
public void appendTooltip(ItemStack stack, @Nullable World world, List<Text> tooltip, TooltipContext context) {
super.appendTooltip(stack, world, tooltip, context);

// Retrieve the FoodComponent of the item
FoodComponent foodComponent = this.getFoodComponent();

// Check if the FoodComponent exists and if it has any status effects
if (foodComponent != null && !foodComponent.getStatusEffects().isEmpty()) {
// Call a method to add the status effects to the tooltip
addFoodEffectTooltip(stack, tooltip, foodComponent.getStatusEffects());
}
}

// This method adds status effect information to the tooltip
public static void addFoodEffectTooltip(ItemStack itemStack, List<Text> tooltip, List<Pair<StatusEffectInstance, Float>> effects) {
// If there are no effects, add a message indicating that there are no effects
if (effects.isEmpty()) {
tooltip.add(Text.translatable("tooltip.ukrainian_delight.no_effects").formatted(Formatting.GRAY));
} else {
// Iterate through each effect and add its information to the tooltip
for (Pair<StatusEffectInstance, Float> pair : effects) {
StatusEffectInstance effect = pair.getFirst();
String name = effect.getEffectType().getTranslationKey();
Expand All @@ -54,28 +47,22 @@ public static void addFoodEffectTooltip(ItemStack itemStack, List<Text> tooltip,
String amplifierString = StatusEffectUtil.formatAmplifier(amplifier);
String durationString = formatDuration(duration);

// Combine the effect name and duration, formatting them as blue text
Text tooltipText = Text.translatable(name).formatted(Formatting.BLUE).append(Text.of(amplifierString))
.append(Text.of(" (" + durationString + ") "));

// Add the tooltip text to the list
tooltip.add(tooltipText);
}
}
}


// This method formats the duration of an effect
private static String formatDuration(int duration) {
int minutes = duration / 60;
int seconds = duration % 60;

return String.format("%d:%02d", minutes, seconds);
}

public static void RegisterTooltip() {
public static void registerTooltip() {
UkrainianDelight.LOGGER.info("Registering ToolTip for " + UkrainianDelight.MOD_ID + " items");
}


}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.megatrex4.ukrainian_dlight.item;
package com.megatrex4.ukrainian_dlight.util;

import net.minecraft.util.Language;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"display": {
"icon": {
"item": "ukrainian_delight:borscht"
},
"title": {
"translate": "ukrainian_delight.advancement.borscht"
},
"description": {
"translate": "ukrainian_delight.advancement.borscht.desc"
},
"frame": "task",
"show_toast": true,
"announce_to_chat": true
},
"parent": "ukrainian_delight:ukrainian_delight",
"criteria": {
"requirement": {
"trigger": "minecraft:inventory_changed",
"conditions": {
"items": [
{
"items": [
"ukrainian_delight:borscht"
]
}
]
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@
{
"tag": "c:salad_ingredients"
},
{
"items": [
"minecraft:red_mushroom",
"minecraft:brown_mushroom"
]
}
[
{
"tag": "c:mushrooms"
}
]
],
"recipe_book_tab": "meals",
"result": {
"item": "ukrainian_delight:borscht"
"item": "ukrainian_delight:lean_borscht"
}
}

0 comments on commit 7365965

Please sign in to comment.