Files
src
build.gradle
gradle.properties
settings.gradle
Code
24 826
src/main/java/com/kodari/permanenteffects/PermanentEffects.java
package com.kodari.permanenteffects;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.attachment.v1.AttachmentRegistry;
import net.fabricmc.fabric.api.attachment.v1.AttachmentType;
import net.fabricmc.fabric.api.creativetab.v1.CreativeModeTabEvents;
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;
import net.fabricmc.fabric.api.entity.event.v1.ServerPlayerEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.core.Holder;
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.MinecraftServer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.tags.DamageTypeTags;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.ai.attributes.Attribute;
import net.minecraft.world.entity.ai.attributes.AttributeInstance;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.food.FoodData;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import java.util.concurrent.ThreadLocalRandom;
public class PermanentEffects implements ModInitializer {
public static final String MOD_ID = "permanenteffects";
private static long serverTick;
public record PlayerEffectState(EffectType effect, int level) {
private static final Codec<EffectType> EFFECT_CODEC = Codec.STRING.xmap(EffectType::valueOf, type -> type.name());
public static final Codec<PlayerEffectState> CODEC = RecordCodecBuilder.create(instance -> instance.group(
EFFECT_CODEC.fieldOf("effect").forGetter(PlayerEffectState::effect),
Codec.INT.fieldOf("level").forGetter(PlayerEffectState::level)
).apply(instance, PlayerEffectState::new));
}
public enum EffectType {
HASTE("Haste", MobEffects.HASTE),
HERO_OF_THE_VILLAGE("Hero of the Village", MobEffects.HERO_OF_THE_VILLAGE),
STRENGTH("Strength", MobEffects.STRENGTH),
SPEED("Speed", MobEffects.SPEED),
LUCK("Luck", MobEffects.LUCK),
REGENERATION("Regeneration", MobEffects.REGENERATION),
SATURATION("Saturation", MobEffects.SATURATION),
FIRE_RESISTANCE("Fire Resistance", MobEffects.FIRE_RESISTANCE),
DOLPHINS_GRACE("Dolphin's Grace", MobEffects.DOLPHINS_GRACE),
HEALTH_BOOST("Health Boost", MobEffects.HEALTH_BOOST);
private final String displayName;
private final Holder<MobEffect> effect;
EffectType(String displayName, Holder<MobEffect> effect) {
this.displayName = displayName;
this.effect = effect;
}
public String displayName() {
return displayName;
}
public int color() {
return effect.value().getColor();
}
}
public static final AttachmentType<PlayerEffectState> PLAYER_EFFECT_STATE = AttachmentRegistry.create(
Identifier.fromNamespaceAndPath(MOD_ID, "player_effect_state"),
builder -> builder.persistent(PlayerEffectState.CODEC).copyOnDeath()
);
private static final ResourceKey<Item> REROLL_POTION_KEY = ResourceKey.create(
Registries.ITEM, Identifier.fromNamespaceAndPath(MOD_ID, "reroll_potion")
);
public static final Item REROLL_POTION = Registry.register(
BuiltInRegistries.ITEM,
REROLL_POTION_KEY,
new RerollPotionItem(new Item.Properties().setId(REROLL_POTION_KEY).stacksTo(1))
);
private static final ResourceKey<Item> POTION_SHARD_KEY = ResourceKey.create(
Registries.ITEM, Identifier.fromNamespaceAndPath(MOD_ID, "potion_shard")
);
public static final Item POTION_SHARD = Registry.register(
BuiltInRegistries.ITEM,
POTION_SHARD_KEY,
new LevelBoostItem(new Item.Properties().setId(POTION_SHARD_KEY).stacksTo(64), false)
);
private static final ResourceKey<Item> EFFECT_ESSENCE_KEY = ResourceKey.create(
Registries.ITEM, Identifier.fromNamespaceAndPath(MOD_ID, "effect_essence")
);
public static final Item EFFECT_ESSENCE = Registry.register(
BuiltInRegistries.ITEM,
EFFECT_ESSENCE_KEY,
new LevelBoostItem(new Item.Properties().setId(EFFECT_ESSENCE_KEY).stacksTo(64), true)
);
private static final ResourceKey<CreativeModeTab> TOOLS_AND_UTILITIES = ResourceKey.create(
Registries.CREATIVE_MODE_TAB, Identifier.fromNamespaceAndPath("minecraft", "tools_and_utilities")
);
private static final Identifier HASTE_ATTACK_SPEED = Identifier.fromNamespaceAndPath(MOD_ID, "haste_attack_speed");
private static final Identifier STRENGTH_DAMAGE = Identifier.fromNamespaceAndPath(MOD_ID, "strength_damage");
private static final Identifier SPEED_MOVEMENT = Identifier.fromNamespaceAndPath(MOD_ID, "speed_movement");
private static final Identifier LUCK_VALUE = Identifier.fromNamespaceAndPath(MOD_ID, "luck_value");
private static final Identifier HEALTH_BOOST_VALUE = Identifier.fromNamespaceAndPath(MOD_ID, "health_boost_value");
@Override
public void onInitialize() {
PayloadTypeRegistry.clientboundPlay().register(EffectStatePayload.TYPE, EffectStatePayload.CODEC);
WithdrawCommand.register();
CreativeModeTabEvents.modifyOutputEvent(TOOLS_AND_UTILITIES).register(output -> {
output.prepend(new ItemStack(REROLL_POTION));
output.prepend(new ItemStack(POTION_SHARD));
output.prepend(new ItemStack(EFFECT_ESSENCE));
});
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> {
ServerPlayer player = handler.getPlayer();
boolean firstAssignment = player.getAttached(PLAYER_EFFECT_STATE) == null;
PlayerEffectState state = ensureState(player);
refreshMechanics(player, state);
sendState(player, state, firstAssignment);
});
ServerPlayerEvents.COPY_FROM.register((oldPlayer, newPlayer, alive) -> {
PlayerEffectState state = oldPlayer.getAttached(PLAYER_EFFECT_STATE);
if (state == null) {
state = randomState();
}
oldPlayer.setAttached(PLAYER_EFFECT_STATE, state);
newPlayer.setAttached(PLAYER_EFFECT_STATE, state);
clearMechanics(oldPlayer, state.effect());
refreshMechanics(newPlayer, state);
sendState(newPlayer, state);
});
ServerLivingEntityEvents.AFTER_DEATH.register((entity, damageSource) -> {
if (!(entity instanceof ServerPlayer victim)) {
return;
}
PlayerEffectState victimState = ensureState(victim);
if (damageSource.getEntity() instanceof ServerPlayer killer && !killer.getUUID().equals(victim.getUUID())) {
PlayerEffectState killerState = ensureState(killer);
if (killerState.level() >= maximumLevel(killerState.effect())
&& victimState.level() > minimumLevel(victimState.effect())) {
ServerLevel serverLevel = victim.level();
serverLevel.addFreshEntity(new ItemEntity(serverLevel, victim.getX(), victim.getY(), victim.getZ(), new ItemStack(POTION_SHARD)));
}
}
applyLevelChange(victim, adjust(victimState, -1));
if (damageSource.getEntity() instanceof ServerPlayer killer && !killer.getUUID().equals(victim.getUUID())) {
changeLevel(killer, 1);
}
});
ServerLivingEntityEvents.ALLOW_DAMAGE.register((entity, source, amount) -> {
if (!(entity instanceof ServerPlayer player) || !source.is(DamageTypeTags.IS_FIRE)) {
return true;
}
PlayerEffectState state = player.getAttached(PLAYER_EFFECT_STATE);
if (state == null || state.effect() != EffectType.FIRE_RESISTANCE) {
return true;
}
double resistance = Math.min(1.0, ordinaryStrength(state));
return ThreadLocalRandom.current().nextDouble() >= resistance;
});
ServerTickEvents.END_SERVER_TICK.register(server -> {
serverTick++;
for (ServerPlayer player : server.getPlayerList().getPlayers()) {
if (!player.isAlive()) {
continue;
}
PlayerEffectState state = player.getAttached(PLAYER_EFFECT_STATE);
if (state != null) {
tickMechanics(player, state);
}
}
});
}
public static double strengthFor(Player player, EffectType effect) {