Files
src
build.gradle
gradle.properties
settings.gradle
Code
33 418
src/main/java/com/kodari/myspacemod/MySpaceMod.java
package com.kodari.myspacemod;
import java.util.function.Function;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.creativetab.v1.CreativeModeTabEvents;
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.sounds.SoundSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
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.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.material.MapColor;
public class MySpaceMod implements ModInitializer {
public static final String MOD_ID = "myspacemod";
public static final Block LUNAR_REGOLITH = registerBlock("lunar_regolith", MapColor.SAND, 0.7F, false);
public static final Block MOONSTONE = registerBlock("moonstone", MapColor.DEEPSLATE, 2.5F, true);
public static final Item LUNAR_REGOLITH_ITEM = registerBlockItem("lunar_regolith", LUNAR_REGOLITH);
public static final Item MOONSTONE_ITEM = registerBlockItem("moonstone", MOONSTONE);
public static final Item SPACE_ALLOY = registerItem("space_alloy", Item::new);
public static final Item OXYGEN_CANISTER = registerItem("oxygen_canister", Item::new);
public static final Item THRUSTER_PACK = registerItem("thruster_pack", ThrusterPackItem::new);
@Override
public void onInitialize() {
CreativeModeTabEvents.modifyOutputEvent(creativeTabKey("building_blocks")).register(entries -> {
entries.accept(LUNAR_REGOLITH_ITEM);
entries.accept(MOONSTONE_ITEM);
});
CreativeModeTabEvents.modifyOutputEvent(creativeTabKey("ingredients")).register(entries -> {
entries.accept(SPACE_ALLOY);
entries.accept(OXYGEN_CANISTER);
entries.accept(THRUSTER_PACK);
});
}
private static Block registerBlock(String path, MapColor mapColor, float strength, boolean requiresCorrectTool) {
Identifier identifier = id(path);
BlockBehaviour.Properties properties = BlockBehaviour.Properties.of()
.mapColor(mapColor)
.strength(strength)
.setId(ResourceKey.create(Registries.BLOCK, identifier));
if (requiresCorrectTool) {
properties.requiresCorrectToolForDrops();
}
return Registry.register(BuiltInRegistries.BLOCK, identifier, new Block(properties));
}
private static Item registerBlockItem(String path, Block block) {
Identifier identifier = id(path);
Item.Properties properties = new Item.Properties().setId(ResourceKey.create(Registries.ITEM, identifier));
return Registry.register(BuiltInRegistries.ITEM, identifier, new BlockItem(block, properties));
}
private static Item registerItem(String path, Function<Item.Properties, Item> factory) {
Identifier identifier = id(path);
Item.Properties properties = new Item.Properties().setId(ResourceKey.create(Registries.ITEM, identifier));
return Registry.register(BuiltInRegistries.ITEM, identifier, factory.apply(properties));
}
private static Identifier id(String path) {
return Identifier.fromNamespaceAndPath(MOD_ID, path);
}
private static ResourceKey<CreativeModeTab> creativeTabKey(String path) {
return ResourceKey.create(Registries.CREATIVE_MODE_TAB, Identifier.fromNamespaceAndPath("minecraft", path));
}
private static final class ThrusterPackItem extends Item {
private ThrusterPackItem(Properties properties) {
super(properties);
}
@Override
public InteractionResult use(Level level, Player player, InteractionHand hand) {
if (!level.isClientSide()) {
player.push(0.0D, 1.35D, 0.0D);
player.addEffect(new MobEffectInstance(MobEffects.SLOW_FALLING, 100, 0));
player.getCooldowns().addCooldown(player.getItemInHand(hand), 100);
level.playSound(null, player.getX(), player.getY(), player.getZ(), SoundEvents.FIREWORK_ROCKET_LAUNCH, SoundSource.PLAYERS, 0.8F, 0.9F);
}
return InteractionResult.SUCCESS;
}
}
}