Skip to content

Commit

Permalink
added CustomEffects system
Browse files Browse the repository at this point in the history
  • Loading branch information
MEGATREX4 committed Jun 9, 2024
1 parent 5e327c9 commit 400affe
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 5 deletions.
6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ base {
}

repositories {
// maven { url "https://maven.shedaniel.me/" }

// Add repositories to retrieve artifacts from in here.
// You should only use this when depending on other mods because
// Loom adds the essential maven repositories to download Minecraft and libraries from automatically.
Expand All @@ -30,6 +32,10 @@ dependencies {

// Fabric API. This is technically optional, but you probably want it anyway.
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"

/* REI Integration */
// modCompileOnly "me.shedaniel:RoughlyEnoughItems-api-fabric:$rei_version"
// modCompileOnly "me.shedaniel:RoughlyEnoughItems-default-plugin-fabric:$rei_version"

}

Expand Down
4 changes: 4 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ minecraft_version=1.20
yarn_mappings=1.20+build.1
loader_version=0.15.11

# rei
# architectury_version=9.1.10
# rei_version=12.0.626

# Mod Properties
mod_version=1.0.0
maven_group=com.megatrex4.ukrainian_dlight
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public void onInitialize() {
ModItems.registerModItems();
ModItemGroups.registerItemGroups();


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

import java.util.List;

import com.mojang.datafixers.util.Pair;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.item.FoodComponent;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;

public class CustomFoodItem extends Item {

public CustomFoodItem(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();
int duration = effect.getDuration() / 20; // Convert ticks to seconds
int amplifier = effect.getAmplifier();
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(" (" + 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);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,50 @@
package com.megatrex4.ukrainian_dlight.item;

import net.minecraft.entity.effect.StatusEffect;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.item.FoodComponent;
import net.minecraft.registry.Registries;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.Nullable;

public class ModFoodComponents {
public static final FoodComponent VARENYK = new FoodComponent.Builder().hunger(3).saturationModifier(0.25f).snack().build();
// Define identifiers for effects from other mods
public static final Identifier COMFORT = new Identifier("farmersdelight", "comfort");
public static final Identifier NOURISHMENT = new Identifier("farmersdelight", "nourishment");
public static final Identifier SATURATION = new Identifier("minecraft", "saturation");

// Define food components with multiple effects
public static final FoodComponent VARENYK = createFoodComponent(3, 0.25f, COMFORT, 200, 0);
public static final FoodComponent BORSCHT = createFoodComponent(4, 0.25f, NOURISHMENT, 200, 0);
// public static final FoodComponent TOMATO = createFoodComponent(1, 0.1f);

// Method to create food component with multiple effects
public static FoodComponent createFoodComponent(int hunger, float saturation, @Nullable Identifier effectId, int duration, int amplifier) {
if (effectId == null) {
return createFoodComponent(hunger, saturation);
}

FoodComponent.Builder builder = new FoodComponent.Builder()
.hunger(hunger)
.saturationModifier(saturation);

StatusEffect effect = Registries.STATUS_EFFECT.getOrEmpty(effectId).orElse(null);

if (effect != null) {
builder.statusEffect(new StatusEffectInstance(effect, duration, amplifier), 1.0F);
} else {
throw new IllegalArgumentException("Invalid effectId: " + effectId);
}

return builder.build();
}

// Overloaded method to create food component without effects
public static FoodComponent createFoodComponent(int hunger, float saturation) {
return new FoodComponent.Builder()
.hunger(hunger)
.saturationModifier(saturation)
.build();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ public class ModItemGroups {
new Identifier(UkrainianDelight.MOD_ID, "ukrainian_delight"),
FabricItemGroup.builder()
.displayName(Text.translatable("itemgroup.ukrainian_delight"))
.icon(() -> new ItemStack(Items.CARROT))
.icon(() -> new ItemStack(ModItems.VARENYK))
.entries((displayContext, entries) -> {
entries.add(ModItems.VARENYK);
entries.add(ModItems.BORSCHT);

}).build());

public static void registerItemGroups() {
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/com/megatrex4/ukrainian_dlight/item/ModItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,25 @@
import com.megatrex4.ukrainian_dlight.UkrainianDelight;
import net.fabricmc.fabric.api.item.v1.FabricItemSettings;
import net.minecraft.item.FoodComponent;
import net.minecraft.item.FoodComponents;
import net.minecraft.item.Item;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.util.Identifier;
import com.megatrex4.ukrainian_dlight.item.ModFoodComponents;

public class ModItems {

public static final Item VARENYK = registerItem("Varenyk", new Item(new FabricItemSettings().food(ModFoodComponents.VARENYK)));
public static final Item VARENYK = registerItem("Varenyk", new CustomFoodItem(new FabricItemSettings().food(ModFoodComponents.VARENYK)));
public static final Item BORSCHT = registerItem("Borscht", new CustomFoodItem(new FabricItemSettings().food(ModFoodComponents.BORSCHT)));
// public static final Item TOMATO = registerItem("Tomato", new CustomFoodItem(new FabricItemSettings().food(ModFoodComponents.TOMATO)));



// 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 void registerModItems(){
UkrainianDelight.LOGGER.info("Registering Mod Items for " + UkrainianDelight.MOD_ID);
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/assets/minecraft/lang/uk_ua.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"item.minecraft.beetroot_soup": "Суп з буряка"
}
5 changes: 4 additions & 1 deletion src/main/resources/assets/ukrainian_delight/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"itemgroup.ukrainian_delight": "Ukrainian Delight",

"item.ukrainian_delight.varenyk": "Varenyk"
"item.ukrainian_delight.varenyk": "Varenyk",
"item.ukrainian_delight.borscht": "Borscht",

"ukranian_delight.rei": "Ukrainian Delight"
}
6 changes: 6 additions & 0 deletions src/main/resources/assets/ukrainian_delight/lang/uk_ua.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"itemgroup.ukrainian_delight": "Українська насолода",

"item.ukrainian_delight.varenyk": "Вареник",
"item.ukrainian_delight.borscht": "Борщ"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"type": "farmersdelight:cooking",
"cookingtime": 200,
"experience": 1.0,
"ingredients": [
{
"tag": "c:dough"
},
{
"item": "minecaft:carrot"
},
{
"item": "minecraft:potato"
},
{
"tag": "c:crops/onion"
},
[
{
"tag": "c:raw_beef"
}
]
],
"recipe_book_tab": "misc",
"result": {
"count": 2,
"item": "ukrainian_delight:varenyk"
}
}

0 comments on commit 400affe

Please sign in to comment.