Files
src
build.gradle
gradle.properties
settings.gradle
Code
19 274
src/main/java/com/kodari/godspear/GodSpearMod.java
package com.kodari.godspear;
import com.mojang.serialization.Codec;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.creativetab.v1.CreativeModeTabEvents;
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;
import net.minecraft.core.Registry;
import net.minecraft.core.component.DataComponentType;
import net.minecraft.core.component.DataComponents;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.Identifier;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.EquipmentSlotGroup;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.ai.attributes.Attributes;
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.component.ItemAttributeModifiers;
import net.minecraft.world.item.component.Weapon;
public final class GodSpearMod implements ModInitializer {
public static final String MOD_ID = "godspear";
private static final int KILLS_PER_TIER = 10;
private static final String[] TIER_NAMES = {"", "God Spear I", "God Spear II", "God Spear III", "God Spear IV"};
private static final DataComponentType<Integer> KILL_COUNT = Registry.register(
BuiltInRegistries.DATA_COMPONENT_TYPE,
Identifier.fromNamespaceAndPath(MOD_ID, "kill_count"),
DataComponentType.<Integer>builder().persistent(Codec.INT).build()
);
private static final Item GOD_SPEAR_I = registerSpear("god_spear_1", 4.0, 800);
private static final Item GOD_SPEAR_II = registerSpear("god_spear_2", 6.0, 1200);
private static final Item GOD_SPEAR_III = registerSpear("god_spear_3", 8.0, 1600);
private static final Item GOD_SPEAR_IV = registerSpear("god_spear_4", 11.0, 2031);
@Override
public void onInitialize() {
ResourceKey<CreativeModeTab> combatTab = ResourceKey.create(
Registries.CREATIVE_MODE_TAB,
Identifier.fromNamespaceAndPath("minecraft", "combat")
);
CreativeModeTabEvents.modifyOutputEvent(combatTab).register(output -> {
output.accept(new ItemStack(GOD_SPEAR_I));
output.accept(new ItemStack(GOD_SPEAR_II));
output.accept(new ItemStack(GOD_SPEAR_III));
output.accept(new ItemStack(GOD_SPEAR_IV));
});
ServerLivingEntityEvents.AFTER_DEATH.register(this::onLivingEntityDeath);
}
private static Item registerSpear(String path, double attackDamage, int durability) {
ResourceKey<Item> itemKey = ResourceKey.create(
Registries.ITEM,
Identifier.fromNamespaceAndPath(MOD_ID, path)
);
Item.Properties properties = new Item.Properties()
.setId(itemKey)
.durability(durability)
.attributes(ItemAttributeModifiers.builder()
.add(Attributes.ATTACK_DAMAGE, new AttributeModifier(
Identifier.fromNamespaceAndPath(MOD_ID, path + "_attack_damage"),
attackDamage,
AttributeModifier.Operation.ADD_VALUE
), EquipmentSlotGroup.MAINHAND)
.add(Attributes.ATTACK_SPEED, new AttributeModifier(
Identifier.fromNamespaceAndPath(MOD_ID, path + "_attack_speed"),
-2.8,
AttributeModifier.Operation.ADD_VALUE
), EquipmentSlotGroup.MAINHAND)
.build())
.component(DataComponents.WEAPON, new Weapon(1));
return Registry.register(BuiltInRegistries.ITEM, itemKey, new Item(properties));
}
private void onLivingEntityDeath(LivingEntity victim, DamageSource damageSource) {
if (victim instanceof Player) {
return;
}
if (!(damageSource.getEntity() instanceof Player killer)) {
return;
}
ItemStack weapon = killer.getMainHandItem();
int tier = getTier(weapon);
if (tier == 0 || tier == 4) {
return;
}
Integer previousKills = weapon.get(KILL_COUNT);
int kills = (previousKills == null ? 0 : previousKills) + 1;
if (kills < KILLS_PER_TIER) {
weapon.set(KILL_COUNT, kills);
return;
}
Item nextTier = switch (tier) {
case 1 -> GOD_SPEAR_II;
case 2 -> GOD_SPEAR_III;
default -> GOD_SPEAR_IV;
};
killer.setItemInHand(InteractionHand.MAIN_HAND, new ItemStack(nextTier));
if (killer instanceof ServerPlayer serverPlayer) {
serverPlayer.sendOverlayMessage(Component.literal(TIER_NAMES[tier + 1] + " awakened!"));
}
}
private static int getTier(ItemStack stack) {
if (stack.is(GOD_SPEAR_I)) {
return 1;
}
if (stack.is(GOD_SPEAR_II)) {
return 2;
}
if (stack.is(GOD_SPEAR_III)) {
return 3;
}
if (stack.is(GOD_SPEAR_IV)) {
return 4;
}
return 0;
}
}