Files
src
build.gradle
gradle.properties
settings.gradle
Code
31 476
src/main/java/com/kodari/infinitygauntlet/InfinityGauntletMod.java
package com.kodari.infinitygauntlet;
import java.util.Map;
import java.util.function.Function;
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.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.ServerLevel;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.EntitySpawnReason;
import net.minecraft.world.entity.EntityTypes;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.monster.zombie.Zombie;
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.CreativeModeTab;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.Vec3;
public class InfinityGauntletMod implements ModInitializer {
public static final String MOD_ID = "infinitygauntlet";
public static final Item SPACE_STONE = registerStone("space_stone", StonePower.SPACE);
public static final Item MIND_STONE = registerStone("mind_stone", StonePower.MIND);
public static final Item REALITY_STONE = registerStone("reality_stone", StonePower.REALITY);
public static final Item POWER_STONE = registerStone("power_stone", StonePower.POWER);
public static final Item TIME_STONE = registerStone("time_stone", StonePower.TIME);
public static final Item SOUL_STONE = registerStone("soul_stone", StonePower.SOUL);
public static final Item COSMIC_GAUNTLET = registerItem("cosmic_gauntlet", CosmicGauntlet::new);
private static final Map<String, Item> BOSS_DROPS = Map.of(
"infinitygauntlet:space_warden", SPACE_STONE,
"infinitygauntlet:mind_warden", MIND_STONE,
"infinitygauntlet:reality_warden", REALITY_STONE,
"infinitygauntlet:power_warden", POWER_STONE,
"infinitygauntlet:time_warden", TIME_STONE,
"infinitygauntlet:soul_warden", SOUL_STONE
);
@Override
public void onInitialize() {
ResourceKey<CreativeModeTab> ingredientsTab = ResourceKey.create(
Registries.CREATIVE_MODE_TAB,
Identifier.fromNamespaceAndPath("minecraft", "ingredients")
);
CreativeModeTabEvents.modifyOutputEvent(ingredientsTab).register(output -> {
output.accept(SPACE_STONE);
output.accept(MIND_STONE);
output.accept(REALITY_STONE);
output.accept(POWER_STONE);
output.accept(TIME_STONE);
output.accept(SOUL_STONE);
output.accept(COSMIC_GAUNTLET);
});
ServerLivingEntityEvents.AFTER_DEATH.register((entity, damageSource) -> {
if (!(entity.level() instanceof ServerLevel serverLevel)) {
return;
}
for (Map.Entry<String, Item> bossDrop : BOSS_DROPS.entrySet()) {
if (entity.entityTags().contains(bossDrop.getKey())) {
entity.spawnAtLocation(serverLevel, new ItemStack(bossDrop.getValue()));
return;
}
}
});
}
private static Item registerStone(String path, StonePower power) {
return registerItem(path, properties -> new StoneItem(properties, power));
}
private static Item registerItem(String path, Function<Item.Properties, Item> factory) {
Identifier id = Identifier.fromNamespaceAndPath(MOD_ID, path);
ResourceKey<Item> itemKey = ResourceKey.create(Registries.ITEM, id);
return Registry.register(BuiltInRegistries.ITEM, id, factory.apply(new Item.Properties().setId(itemKey)));
}
private enum StonePower {
SPACE("space_warden", "space_warden"),
MIND("mind_warden", "mind_warden"),
REALITY("reality_warden", "reality_warden"),
POWER("power_warden", "power_warden"),
TIME("time_warden", "time_warden"),
SOUL("soul_warden", "soul_warden");
private final String nameKey;
private final String tag;
StonePower(String nameKey, String tag) {
this.nameKey = nameKey;
this.tag = MOD_ID + ":" + tag;
}
}
private static final class StoneItem extends Item {
private final StonePower power;
private StoneItem(Properties properties, StonePower power) {
super(properties);
this.power = power;
}
@Override
public InteractionResult use(Level level, Player player, InteractionHand hand) {
if (level.isClientSide()) {
return InteractionResult.SUCCESS;
}
if (!(level instanceof ServerLevel serverLevel)) {
return InteractionResult.FAIL;
}
Zombie boss = EntityTypes.ZOMBIE.create(level, EntitySpawnReason.SPAWN_ITEM_USE);
if (boss == null) {
return InteractionResult.FAIL;
}
boss.setPos(player.getX() + 2.5, player.getY(), player.getZ() + 2.5);
boss.setCustomName(Component.translatable("entity." + MOD_ID + "." + power.nameKey));
boss.setCustomNameVisible(true);
boss.addTag(power.tag);
boss.setPersistenceRequired();
var maxHealth = boss.getAttribute(Attributes.MAX_HEALTH);
if (maxHealth != null) {
maxHealth.setBaseValue(240.0);
}
boss.setHealth(240.0f);
applyBossPower(boss);
if (!serverLevel.addFreshEntity(boss)) {
return InteractionResult.FAIL;
}
if (!player.isCreative()) {
player.getItemInHand(hand).shrink(1);
}
return InteractionResult.SUCCESS;
}
private void applyBossPower(Zombie boss) {
switch (power) {
case SPACE -> boss.addEffect(new MobEffectInstance(MobEffects.SPEED, 12000, 2));
case MIND -> {
boss.addEffect(new MobEffectInstance(MobEffects.NIGHT_VISION, 12000));
boss.addEffect(new MobEffectInstance(MobEffects.STRENGTH, 12000, 1));
}
case REALITY -> {
boss.addEffect(new MobEffectInstance(MobEffects.FIRE_RESISTANCE, 12000));
boss.addEffect(new MobEffectInstance(MobEffects.REGENERATION, 12000, 1));
}
case POWER -> {
boss.addEffect(new MobEffectInstance(MobEffects.STRENGTH, 12000, 3));
boss.addEffect(new MobEffectInstance(MobEffects.RESISTANCE, 12000, 1));
}
case TIME -> {
boss.addEffect(new MobEffectInstance(MobEffects.HASTE, 12000, 2));
boss.addEffect(new MobEffectInstance(MobEffects.SPEED, 12000, 1));
}
case SOUL -> {
boss.addEffect(new MobEffectInstance(MobEffects.ABSORPTION, 12000, 2));
boss.addEffect(new MobEffectInstance(MobEffects.REGENERATION, 12000, 1));
}
}
}
}
private static final class CosmicGauntlet extends Item {
private CosmicGauntlet(Properties properties) {
super(properties);
}
@Override
public InteractionResult use(Level level, Player player, InteractionHand hand) {
if (level.isClientSide()) {
return InteractionResult.SUCCESS;
}
player.addEffect(new MobEffectInstance(MobEffects.STRENGTH, 600, 4));
player.addEffect(new MobEffectInstance(MobEffects.RESISTANCE, 600, 2));
player.addEffect(new MobEffectInstance(MobEffects.REGENERATION, 600, 2));
player.addEffect(new MobEffectInstance(MobEffects.SPEED, 600, 2));
player.addEffect(new MobEffectInstance(MobEffects.HASTE, 600, 2));
player.addEffect(new MobEffectInstance(MobEffects.NIGHT_VISION, 600));
player.addEffect(new MobEffectInstance(MobEffects.FIRE_RESISTANCE, 600));
player.addEffect(new MobEffectInstance(MobEffects.WATER_BREATHING, 600));
player.addEffect(new MobEffectInstance(MobEffects.ABSORPTION, 600, 3));
player.addEffect(new MobEffectInstance(MobEffects.SLOW_FALLING, 600));