Files
src
build.gradle
gradle.properties
settings.gradle
Code
21 207
src/main/java/com/kodari/grandfruits/GrandFruits.java
package com.kodari.grandfruits;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.player.UseItemCallback;
import net.minecraft.core.Holder;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.Identifier;
import net.minecraft.resources.ResourceKey;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.Level;
import net.minecraft.network.chat.Component;
public class GrandFruits implements ModInitializer {
public static final String MOD_ID = "grandfruits";
private static final Item ELASTIC_FRUIT = registerFruit("elastic_fruit", 1.1F,
new Power(MobEffects.SPEED, 1), new Power(MobEffects.JUMP_BOOST, 1));
private static final Item FLAME_FRUIT = registerFruit("flame_fruit", 1.4F,
new Power(MobEffects.FIRE_RESISTANCE, 0), new Power(MobEffects.STRENGTH, 0));
private static final Item FROST_FRUIT = registerFruit("frost_fruit", 0.9F,
new Power(MobEffects.RESISTANCE, 0), new Power(MobEffects.SLOW_FALLING, 0));
private static final Item STORM_FRUIT = registerFruit("storm_fruit", 1.7F,
new Power(MobEffects.SPEED, 2), new Power(MobEffects.NIGHT_VISION, 0));
private static final Item QUAKE_FRUIT = registerFruit("quake_fruit", 0.7F,
new Power(MobEffects.STRENGTH, 2), new Power(MobEffects.RESISTANCE, 1));
private static final List<Item> FRUITS = List.of(
ELASTIC_FRUIT, FLAME_FRUIT, FROST_FRUIT, STORM_FRUIT, QUAKE_FRUIT);
@SuppressWarnings("unused")
private static final CreativeModeTab FRUIT_TAB = registerFruitTab();
@Override
public void onInitialize() {
UseItemCallback.EVENT.register(GrandFruits::convertAppleToFruit);
}
private static InteractionResult convertAppleToFruit(Player player, Level level, InteractionHand hand) {
if (level.isClientSide() || player.isSpectator()) {
return InteractionResult.PASS;
}
ItemStack heldStack = player.getItemInHand(hand);
if (heldStack.getItem() != Items.APPLE) {
return InteractionResult.PASS;
}
ItemStack fruitStack = new ItemStack(FRUITS.get(ThreadLocalRandom.current().nextInt(FRUITS.size())));
if (!player.getInventory().add(fruitStack)) {
return InteractionResult.FAIL;
}
heldStack.shrink(1);
player.playSound(SoundEvents.AMETHYST_BLOCK_CHIME, 0.8F, 1.5F);
return InteractionResult.SUCCESS;
}
private static Item registerFruit(String name, float soundPitch, Power... powers) {
Identifier identifier = Identifier.fromNamespaceAndPath(MOD_ID, name);
ResourceKey<Item> itemKey = ResourceKey.create(Registries.ITEM, identifier);
Item item = new FruitItem(new Item.Properties().setId(itemKey).stacksTo(1), soundPitch, powers);
return Registry.register(BuiltInRegistries.ITEM, identifier, item);
}
private static CreativeModeTab registerFruitTab() {
Identifier identifier = Identifier.fromNamespaceAndPath(MOD_ID, "fruits");
ResourceKey<CreativeModeTab> tabKey = ResourceKey.create(Registries.CREATIVE_MODE_TAB, identifier);
CreativeModeTab tab = CreativeModeTab.builder(CreativeModeTab.Row.TOP, 0)
.title(Component.translatable("itemGroup.grandfruits.fruits"))
.icon(() -> new ItemStack(ELASTIC_FRUIT))
.displayItems((parameters, output) -> {
for (Item fruit : FRUITS) {
output.accept(fruit);
}
})
.build();
return Registry.register(BuiltInRegistries.CREATIVE_MODE_TAB, tabKey, tab);
}
private record Power(Holder<MobEffect> effect, int amplifier) {
}
private static final class FruitItem extends Item {
private final float soundPitch;
private final Power[] powers;
private FruitItem(Properties properties, float soundPitch, Power... powers) {
super(properties);
this.soundPitch = soundPitch;
this.powers = powers;
}
@Override
public InteractionResult use(Level level, Player player, InteractionHand hand) {
if (!level.isClientSide()) {
for (Power power : powers) {
player.addEffect(new MobEffectInstance(power.effect(), 20 * 30, power.amplifier()));
}
player.playSound(SoundEvents.AMETHYST_BLOCK_CHIME, 1.0F, soundPitch);
player.getCooldowns().addCooldown(player.getItemInHand(hand), 20 * 40);
}
return InteractionResult.SUCCESS;
}
}
}