Files
src
build.gradle
gradle.properties
manifest.kod
settings.gradle
Code
40 8,787
src/main/java/com/kodari/unbreakerite/UnbreakeriteMod.java
package com.kodari.unbreakerite;
import com.geckolib.animatable.GeoItem;
import com.geckolib.animatable.client.GeoRenderProvider;
import com.geckolib.animatable.instance.AnimatableInstanceCache;
import com.geckolib.animatable.manager.AnimatableManager;
import com.geckolib.model.DefaultedItemGeoModel;
import com.geckolib.renderer.GeoItemRenderer;
import com.geckolib.renderer.base.GeoRenderState;
import com.geckolib.util.GeckoLibUtil;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.creativetab.v1.CreativeModeTabEvents;
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.loot.v3.LootTableEvents;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.blocks.BlockStateArgument;
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.resources.Identifier;
import net.minecraft.resources.ResourceKey;
import net.minecraft.network.chat.Component;
import net.minecraft.tags.TagKey;
import net.minecraft.util.Unit;
import net.minecraft.world.effect.MobEffectCategory;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.item.crafting.SingleRecipeInput;
import net.minecraft.world.item.component.Tool;
import net.minecraft.world.item.equipment.ArmorMaterial;
import net.minecraft.world.item.equipment.ArmorMaterials;
import net.minecraft.world.item.equipment.ArmorType;
import net.minecraft.world.item.equipment.EquipmentAssets;
import net.minecraft.world.item.enchantment.Enchantments;
import net.minecraft.world.item.enchantment.ItemEnchantments;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
import net.minecraft.world.level.storage.LevelResource;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;
public class UnbreakeriteMod implements ModInitializer {
private static final Set<String> JCB_BLOCKS = new HashSet<>();
public static final ResourceKey<Item> UNBREAKERITE_KEY = ResourceKey.create(
Registries.ITEM, Identifier.fromNamespaceAndPath("unbreakerite", "unbreakerite"));
public static final Item UNBREAKERITE = Registry.register(BuiltInRegistries.ITEM, UNBREAKERITE_KEY,
new Item(new Item.Properties().setId(UNBREAKERITE_KEY)));
public static final ResourceKey<Item> JCB_PICKAXE_KEY = ResourceKey.create(
Registries.ITEM, Identifier.fromNamespaceAndPath("unbreakerite", "jcb_pickaxe"));
public static final Item JCB_PICKAXE = Registry.register(BuiltInRegistries.ITEM, JCB_PICKAXE_KEY,
new JcbPickaxe(new Item.Properties().setId(JCB_PICKAXE_KEY).stacksTo(1)
.component(DataComponents.UNBREAKABLE, Unit.INSTANCE)
.component(DataComponents.TOOL, new Tool(List.of(), 100000000.0F, 0, true))
.delayedComponent(DataComponents.ENCHANTMENTS, context -> {
ItemEnchantments.Mutable enchantments = new ItemEnchantments.Mutable(ItemEnchantments.EMPTY);
enchantments.set(context.getOrThrow(Enchantments.FORTUNE), 100);
return enchantments.toImmutable();
})));
private static final ArmorMaterial ULTIMATE_MATERIAL = new ArmorMaterial(
ArmorMaterials.NETHERITE.durability(), ArmorMaterials.NETHERITE.defense(),
ArmorMaterials.NETHERITE.enchantmentValue(), ArmorMaterials.NETHERITE.equipSound(),
ArmorMaterials.NETHERITE.toughness(), 1.0F, ArmorMaterials.NETHERITE.repairIngredient(),
ResourceKey.create(EquipmentAssets.ROOT_ID, Identifier.fromNamespaceAndPath("unbreakerite", "ultimate_miner")));
public static final Item HELMET = registerArmor("ultimate_miner_helmet", ArmorType.HELMET);
public static final Item CHESTPLATE = registerArmor("ultimate_miner_chestplate", ArmorType.CHESTPLATE);
public static final Item LEGGINGS = registerArmor("ultimate_miner_leggings", ArmorType.LEGGINGS);
public static final Item BOOTS = registerArmor("ultimate_miner_boots", ArmorType.BOOTS);
private static Item registerArmor(String name, ArmorType type) {
ResourceKey<Item> key = ResourceKey.create(Registries.ITEM, Identifier.fromNamespaceAndPath("unbreakerite", name));
return Registry.register(BuiltInRegistries.ITEM, key, new Item(new Item.Properties().setId(key)
.humanoidArmor(ULTIMATE_MATERIAL, type).component(DataComponents.UNBREAKABLE, Unit.INSTANCE)));
}
private static boolean wearsFullSet(LivingEntity player) {
return player.getItemBySlot(ArmorType.HELMET.getSlot()).getItem() == HELMET
&& player.getItemBySlot(ArmorType.CHESTPLATE.getSlot()).getItem() == CHESTPLATE
&& player.getItemBySlot(ArmorType.LEGGINGS.getSlot()).getItem() == LEGGINGS
&& player.getItemBySlot(ArmorType.BOOTS.getSlot()).getItem() == BOOTS;
}
@Override
public void onInitialize() {
ServerLifecycleEvents.SERVER_STARTED.register(server -> {
JCB_BLOCKS.clear();
Path path = server.getWorldPath(LevelResource.DATA).resolve("unbreakerite_jcb_blocks.txt");
if (Files.exists(path)) {
try {
JCB_BLOCKS.addAll(Files.readAllLines(path));
} catch (IOException exception) {
throw new IllegalStateException("Could not read JCB block list", exception);
}
}
});
CommandRegistrationCallback.EVENT.register((dispatcher, buildContext, selection) ->
dispatcher.register(Commands.literal("jcb")
.requires(Commands.hasPermission(Commands.LEVEL_ADMINS))
.then(Commands.literal("add")
.then(Commands.argument("block", BlockStateArgument.block(buildContext))
.executes(context -> {
var block = BlockStateArgument.getBlock(context, "block").getState().getBlock();
String id = BuiltInRegistries.BLOCK.getKey(block).toString();
Set<String> updated = new HashSet<>(JCB_BLOCKS);
updated.add(id);
Path path = context.getSource().getServer().getWorldPath(LevelResource.DATA)
.resolve("unbreakerite_jcb_blocks.txt");
try {
Files.createDirectories(path.getParent());
Files.write(path, updated.stream().sorted().toList());
} catch (IOException exception) {
context.getSource().sendFailure(Component.literal("Could not save the JCB block list: " + exception.getMessage()));
return 0;
}
JCB_BLOCKS.add(id);
context.getSource().sendSuccess(() -> Component.literal("Added " + id + " to JCB instant mining and automatic smelting."), true);
return 1;
}))));
LootTableEvents.MODIFY_DROPS.register((table, context, drops) -> {
var tool = context.getOptional(LootContextParams.TOOL);
var state = context.getOptional(LootContextParams.BLOCK_STATE);
var ores = TagKey.create(Registries.BLOCK, Identifier.fromNamespaceAndPath("c", "ores"));
if (tool == null || !tool.is(JCB_PICKAXE) || state == null
|| (!state.is(ores, block -> true) && !JCB_BLOCKS.contains(BuiltInRegistries.BLOCK.getKey(state.getBlock()).toString()))) {
return;
}
var level = context.getLevel();
var converted = new ArrayList<ItemStack>();
for (var drop : drops) {
var input = new SingleRecipeInput(drop.copyWithCount(1));
var recipe = level.getServer().getRecipeManager().getRecipeFor(RecipeType.SMELTING, input, level);
if (recipe.isEmpty()) {
converted.add(drop);
continue;
}
var result = recipe.get().value().assemble(input);
if (result.isEmpty()) {
converted.add(drop);
continue;
}
int remaining = result.getCount() * drop.getCount();
while (remaining > 0) {
int count = Math.min(remaining, result.getMaxStackSize());
converted.add(result.copyWithCount(count));
remaining -= count;
}
}
drops.clear();
drops.addAll(converted);
});
CreativeModeTabEvents.modifyOutputEvent(ResourceKey.create(Registries.CREATIVE_MODE_TAB,
Identifier.fromNamespaceAndPath("minecraft", "ingredients")))
.register(output -> output.accept(UNBREAKERITE));
CreativeModeTabEvents.modifyOutputEvent(ResourceKey.create(Registries.CREATIVE_MODE_TAB,
Identifier.fromNamespaceAndPath("minecraft", "tools_and_utilities")))
.register(output -> output.accept(JCB_PICKAXE));
CreativeModeTabEvents.modifyOutputEvent(ResourceKey.create(Registries.CREATIVE_MODE_TAB,
Identifier.fromNamespaceAndPath("minecraft", "combat")))
.register(output -> {
output.accept(HELMET);
output.accept(CHESTPLATE);
output.accept(LEGGINGS);
output.accept(BOOTS);
});
ServerTickEvents.END_SERVER_TICK.register(server -> {
for (var player : server.getPlayerList().getPlayers()) {
if (wearsFullSet(player)) {
BuiltInRegistries.MOB_EFFECT.listElements()
.filter(effect -> effect.value().getCategory() == MobEffectCategory.BENEFICIAL)
.filter(effect -> !effect.equals(MobEffects.JUMP_BOOST))
.forEach(effect -> player.addEffect(new MobEffectInstance(effect, 40, 9)));
}
}
});
ServerLivingEntityEvents.ALLOW_DAMAGE.register((entity, source, amount) ->
!(entity instanceof Player && wearsFullSet(entity)));
}
private static class JcbPickaxe extends Item implements GeoItem {
private final AnimatableInstanceCache geoCache = GeckoLibUtil.createInstanceCache(this);
private JcbPickaxe(Properties properties) {
super(properties);
}