Files
src
build.gradle
gradle.properties
settings.gradle
Code
54 646
src/main/java/dev/kodari/pantrycraft/Pantrycraft.java
package dev.kodari.pantrycraft;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.creativetab.v1.CreativeModeTabEvents;
import net.minecraft.core.Registry;
import net.minecraft.core.component.DataComponents;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.references.BlockItemId;
import net.minecraft.resources.Identifier;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.BlockItem;
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.item.component.CustomData;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.food.FoodProperties;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockBehaviour;
public class Pantrycraft implements ModInitializer {
public static final String MOD_ID = "pantrycraft";
private static final String AGE_TAG = "pantrycraft_age";
private static final String PRESERVED_TAG = "pantrycraft_preserved";
private static final int SPOIL_TICKS = 24_000;
private static final Item TOMATO = registerFood("tomato", 2, 0.2F);
private static final Item CORN = registerFood("corn", 3, 0.3F);
private static final Item STRAWBERRY = registerFood("strawberry", 2, 0.2F);
private static final Item LETTUCE = registerFood("lettuce", 1, 0.1F);
private static final Item VEGETABLE_SOUP = registerFood("vegetable_soup", 8, 0.8F);
private static final Item ROASTED_VEGETABLES = registerFood("roasted_vegetables", 6, 0.6F);
private static final Item CERAMIC_PLATE = registerItem("ceramic_plate");
private static final Block REFRIGERATOR = registerBlock("refrigerator", 2.5F);
private static final Block STOVE = registerBlock("stove", 3.5F);
private static final Block COUNTERTOP = registerBlock("countertop", 1.5F);
private static final Block CUPBOARD = registerBlock("cupboard", 1.5F);
@Override
public void onInitialize() {
ResourceKey<CreativeModeTab> foodTab = ResourceKey.create(
Registries.CREATIVE_MODE_TAB,
Identifier.fromNamespaceAndPath("minecraft", "food_and_drinks")
);
CreativeModeTabEvents.modifyOutputEvent(foodTab).register(entries -> {
entries.accept(TOMATO);
entries.accept(CORN);
entries.accept(STRAWBERRY);
entries.accept(LETTUCE);
entries.accept(VEGETABLE_SOUP);
entries.accept(ROASTED_VEGETABLES);
entries.accept(CERAMIC_PLATE);
});
ResourceKey<CreativeModeTab> buildingTab = ResourceKey.create(
Registries.CREATIVE_MODE_TAB,
Identifier.fromNamespaceAndPath("minecraft", "building_blocks")
);
CreativeModeTabEvents.modifyOutputEvent(buildingTab).register(entries -> {
entries.accept(REFRIGERATOR);
entries.accept(STOVE);
entries.accept(COUNTERTOP);
entries.accept(CUPBOARD);
});
}
private static Item registerFood(String name, int nutrition, float saturation) {
Identifier id = Identifier.fromNamespaceAndPath(MOD_ID, name);
ResourceKey<Item> key = ResourceKey.create(Registries.ITEM, id);
FoodProperties food = new FoodProperties.Builder().nutrition(nutrition).saturationModifier(saturation).build();
Item.Properties properties = new Item.Properties().setId(key).food(food);
return Registry.register(BuiltInRegistries.ITEM, key, new PantryFood(properties));
}
private static Item registerItem(String name) {
Identifier id = Identifier.fromNamespaceAndPath(MOD_ID, name);
ResourceKey<Item> key = ResourceKey.create(Registries.ITEM, id);
return Registry.register(BuiltInRegistries.ITEM, key, new Item(new Item.Properties().setId(key)));
}
private static Block registerBlock(String name, float strength) {
Identifier id = Identifier.fromNamespaceAndPath(MOD_ID, name);
BlockItemId blockItemId = BlockItemId.create(id, id);
Block block = Registry.register(
BuiltInRegistries.BLOCK,
blockItemId.block(),
new Block(BlockBehaviour.Properties.of().strength(strength).setId(blockItemId.block()))
);
Registry.register(
BuiltInRegistries.ITEM,
blockItemId.item(),
new BlockItem(block, new Item.Properties().setId(blockItemId.item()))
);
return block;
}
private static final class PantryFood extends Item {
private PantryFood(Properties properties) {
super(properties);
}
@Override
public void inventoryTick(ItemStack stack, ServerLevel level, Entity owner, EquipmentSlot slot) {
CustomData data = stack.getOrDefault(DataComponents.CUSTOM_DATA, CustomData.EMPTY);
CompoundTag tag = data.copyTag();
if (tag.getBooleanOr(PRESERVED_TAG, false)) {
return;
}
int age = tag.getIntOr(AGE_TAG, 0);
if (age < SPOIL_TICKS) {
CustomData.update(DataComponents.CUSTOM_DATA, stack, updated -> updated.putInt(AGE_TAG, age + 1));
}
}
@Override
public InteractionResult use(Level level, Player player, net.minecraft.world.InteractionHand hand) {
if (isSpoiled(player.getItemInHand(hand))) {
if (!level.isClientSide()) {
player.sendOverlayMessage(Component.literal("Эта еда испортилась."));
}
return InteractionResult.FAIL;
}
return super.use(level, player, hand);
}
@Override
public InteractionResult useOn(UseOnContext context) {
Level level = context.getLevel();
Block clickedBlock = level.getBlockState(context.getClickedPos()).getBlock();
ItemStack heldStack = context.getItemInHand();
if (clickedBlock == REFRIGERATOR) {
if (!level.isClientSide()) {
CustomData.update(DataComponents.CUSTOM_DATA, heldStack, tag ->
tag.putBoolean(PRESERVED_TAG, !tag.getBooleanOr(PRESERVED_TAG, false))
);
}
return InteractionResult.SUCCESS;
}
if (clickedBlock == STOVE && isCookable(heldStack.getItem())) {
if (level.isClientSide()) {
return InteractionResult.SUCCESS;
}
Player player = context.getPlayer();
if (player == null) {
return InteractionResult.PASS;
}
if (!player.addItem(new ItemStack(ROASTED_VEGETABLES))) {
return InteractionResult.FAIL;
}
heldStack.shrink(1);
return InteractionResult.SUCCESS;
}
return super.useOn(context);
}
private static boolean isSpoiled(ItemStack stack) {
return stack.getOrDefault(DataComponents.CUSTOM_DATA, CustomData.EMPTY)
.copyTag()
.getIntOr(AGE_TAG, 0) >= SPOIL_TICKS;
}
private static boolean isCookable(Item item) {
return item == TOMATO || item == CORN || item == LETTUCE;
}
}
}