Files
src
build.gradle
gradle.properties
settings.gradle
Code
32 1,801
src/main/java/com/kodari/combatinjuries/CombatInjuries.java
package com.kodari.combatinjuries;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.entity.event.v1.EntitySleepEvents;
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerEntityEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.event.player.AttackEntityCallback;
import net.fabricmc.fabric.api.event.player.UseItemCallback;
import net.fabricmc.fabric.api.creativetab.v1.CreativeModeTabEvents;
import net.fabricmc.fabric.api.loot.v3.LootTableEvents;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Registry;
import net.minecraft.core.Holder;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.Identifier;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.damagesource.DamageType;
import net.minecraft.world.damagesource.DamageTypes;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectCategory;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntitySpawnReason;
import net.minecraft.world.entity.EntityTypes;
import net.minecraft.world.entity.EquipmentSlot;
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.monster.zombie.Zombie;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.item.enchantment.Enchantments;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.component.CustomData;
import net.minecraft.core.component.DataComponents;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.server.permissions.Permissions;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.world.level.storage.loot.BuiltInLootTables;
import net.minecraft.world.level.storage.loot.LootPool;
import net.minecraft.world.level.storage.loot.entries.LootItem;
import net.minecraft.world.level.storage.loot.predicates.LootItemRandomChanceCondition;
import net.minecraft.network.chat.Component;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
public final class CombatInjuries implements ModInitializer {
public static final String MOD_ID = "combatinjuries";
private static final String RUSTY_MARKER = "combatinjuries_rusty";
private static final ResourceKey<Item> ADRENALINE_KEY = ResourceKey.create(
Registries.ITEM,
Identifier.fromNamespaceAndPath(MOD_ID, "adrenaline_shot")
);
public static final Item ADRENALINE_SHOT = Registry.register(
BuiltInRegistries.ITEM,
ADRENALINE_KEY,
new Item(new Item.Properties().setId(ADRENALINE_KEY))
);
public static final Holder<MobEffect> HEMORRHAGE_EFFECT = registerEffect("hemorrhage", MobEffectCategory.HARMFUL, 0x7D1020);
public static final Holder<MobEffect> TETANUS_EFFECT = registerEffect("tetanus", MobEffectCategory.HARMFUL, 0xA65E32);
public static final Holder<MobEffect> FRACTURE_EFFECT = registerEffect("fracture", MobEffectCategory.HARMFUL, 0xC9C5B8);
public static final Holder<MobEffect> CONCUSSION_EFFECT = registerEffect("concussion", MobEffectCategory.HARMFUL, 0xC5C02A);
public static final Holder<MobEffect> ASPHYXIA_EFFECT = registerEffect("asphyxia", MobEffectCategory.HARMFUL, 0x263C51);
public static final Holder<MobEffect> WINDED_EFFECT = registerEffect("winded", MobEffectCategory.HARMFUL, 0x758899);
public static final Holder<MobEffect> HYSTERIA_EFFECT = registerEffect("hysteria", MobEffectCategory.NEUTRAL, 0xE8E8E8);
public static final Holder<MobEffect> ADRENALINE_RUSH_EFFECT = registerEffect("adrenaline_rush", MobEffectCategory.BENEFICIAL, 0xD71935);
public static final Holder<MobEffect> ADRENALINE_CRASH_EFFECT = registerEffect("adrenaline_crash", MobEffectCategory.HARMFUL, 0x33364A);
private static final Identifier FRACTURE_SPEED_ID = Identifier.fromNamespaceAndPath(MOD_ID, "fracture_speed");
private static final Identifier RUSH_SPEED_ID = Identifier.fromNamespaceAndPath(MOD_ID, "adrenaline_rush_speed");
private static final Identifier CRASH_SPEED_ID = Identifier.fromNamespaceAndPath(MOD_ID, "adrenaline_crash_speed");
private static final Identifier RUSH_DAMAGE_ID = Identifier.fromNamespaceAndPath(MOD_ID, "adrenaline_rush_damage");
private static final Identifier HYSTERIA_DAMAGE_ID = Identifier.fromNamespaceAndPath(MOD_ID, "hysteria_damage");
private static final Identifier HYSTERIA_KNOCKBACK_ID = Identifier.fromNamespaceAndPath(MOD_ID, "hysteria_knockback_resistance");
private static final ResourceKey<DamageType> HEMORRHAGE_DAMAGE_TYPE = damageTypeKey("hemorrhage");
private static final ResourceKey<DamageType> ASPHYXIA_DAMAGE_TYPE = damageTypeKey("asphyxia");
private static final ResourceKey<DamageType> ADRENALINE_CRASH_DAMAGE_TYPE = damageTypeKey("adrenaline_crash");
private static final Identifier ADRENALINE_MUSIC_ID = Identifier.fromNamespaceAndPath(MOD_ID, "adrenaline_music");
private static final Identifier CONCUSSION_TINNITUS_ID = Identifier.fromNamespaceAndPath(MOD_ID, "concussion_tinnitus");
public static final SoundEvent ADRENALINE_MUSIC = Registry.register(
BuiltInRegistries.SOUND_EVENT,
ADRENALINE_MUSIC_ID,
SoundEvent.createVariableRangeEvent(ADRENALINE_MUSIC_ID)
);
public static final SoundEvent CONCUSSION_TINNITUS = Registry.register(
BuiltInRegistries.SOUND_EVENT,
CONCUSSION_TINNITUS_ID,
SoundEvent.createVariableRangeEvent(CONCUSSION_TINNITUS_ID)
);
private static final Map<UUID, InjuryState> STATES = new HashMap<>();
@Override
public void onInitialize() {
CreativeModeTabEvents.modifyOutputEvent(ResourceKey.create(
Registries.CREATIVE_MODE_TAB,
Identifier.withDefaultNamespace("tools_and_utilities")
)).register(output -> {
output.accept(ADRENALINE_SHOT);
output.accept(rustyCreativeStack(Items.IRON_SWORD, "item.combatinjuries.rusty_iron_sword"));
output.accept(rustyCreativeStack(Items.IRON_AXE, "item.combatinjuries.rusty_iron_axe"));
output.accept(rustyCreativeStack(Items.IRON_PICKAXE, "item.combatinjuries.rusty_iron_pickaxe"));
output.accept(rustyCreativeStack(Items.IRON_SHOVEL, "item.combatinjuries.rusty_iron_shovel"));
output.accept(rustyCreativeStack(Items.IRON_HOE, "item.combatinjuries.rusty_iron_hoe"));
output.accept(rustyCreativeStack(Items.COPPER_SWORD, "item.combatinjuries.rusty_copper_sword"));
output.accept(rustyCreativeStack(Items.COPPER_AXE, "item.combatinjuries.rusty_copper_axe"));
output.accept(rustyCreativeStack(Items.COPPER_PICKAXE, "item.combatinjuries.rusty_copper_pickaxe"));
output.accept(rustyCreativeStack(Items.COPPER_SHOVEL, "item.combatinjuries.rusty_copper_shovel"));
output.accept(rustyCreativeStack(Items.COPPER_HOE, "item.combatinjuries.rusty_copper_hoe"));
});
LootTableEvents.MODIFY.register((key, tableBuilder, source, registries) -> {
if (key.equals(BuiltInLootTables.STRONGHOLD_LIBRARY)
|| key.equals(BuiltInLootTables.STRONGHOLD_CROSSING)
|| key.equals(BuiltInLootTables.STRONGHOLD_CORRIDOR)
|| key.equals(BuiltInLootTables.ANCIENT_CITY)
|| key.equals(BuiltInLootTables.ANCIENT_CITY_ICE_BOX)) {
tableBuilder.withPool(LootPool.lootPool()
.add(LootItem.lootTableItem(ADRENALINE_SHOT))
.when(LootItemRandomChanceCondition.randomChance(0.01F)));
}
});
ServerEntityEvents.ENTITY_LOAD.register(CombatInjuries::markNaturallyHeldIronItems);
AttackEntityCallback.EVENT.register(CombatInjuries::onAttack);
UseItemCallback.EVENT.register(CombatInjuries::onUseItem);
ServerLivingEntityEvents.AFTER_DAMAGE.register(CombatInjuries::afterDamage);
CommandRegistrationCallback.EVENT.register((dispatcher, buildContext, selection) -> dispatcher.register(
Commands.literal("injurytest")
.requires(source -> source.permissions().hasPermission(Permissions.COMMANDS_MODERATOR))
.then(Commands.literal("hemorrhage").executes(context -> runInjuryTest(context.getSource(), "hemorrhage")))
.then(Commands.literal("tetanus").executes(context -> runInjuryTest(context.getSource(), "tetanus")))
.then(Commands.literal("fracture").executes(context -> runInjuryTest(context.getSource(), "fracture")))
.then(Commands.literal("concussion").executes(context -> runInjuryTest(context.getSource(), "concussion")))
.then(Commands.literal("asphyxia").executes(context -> runInjuryTest(context.getSource(), "asphyxia")))
.then(Commands.literal("winded").executes(context -> runInjuryTest(context.getSource(), "winded")))
.then(Commands.literal("hysteria").executes(context -> runInjuryTest(context.getSource(), "hysteria")))
.then(Commands.literal("adrenaline_rush").executes(context -> runInjuryTest(context.getSource(), "adrenaline_rush")))
.then(Commands.literal("adrenaline_crash").executes(context -> runInjuryTest(context.getSource(), "adrenaline_crash")))
.then(Commands.literal("rusty_zombie").executes(context -> spawnRustyZombie(context.getSource())))
.then(Commands.literal("clear").executes(context -> runInjuryTest(context.getSource(), "clear")))
));
EntitySleepEvents.START_SLEEPING.register((entity, sleepingPos) -> {
if (entity instanceof ServerPlayer player) {
state(player).deepSleepQualified = false;
}
});
EntitySleepEvents.ALLOW_RESETTING_TIME.register(player -> {
if (player instanceof ServerPlayer serverPlayer) {
state(serverPlayer).deepSleepQualified = true;
}
return true;
});
EntitySleepEvents.STOP_SLEEPING.register((entity, sleepingPos) -> {
if (entity instanceof ServerPlayer player) {
InjuryState state = state(player);
if (!state.deepSleepQualified) {
return;
}
state.deepSleepQualified = false;
ServerLevel level = (ServerLevel) player.level();
var defaultClock = level.dimensionType().defaultClock();
if (defaultClock.isEmpty() || !level.clockManager().isAtTimeMarker(defaultClock.get(), net.minecraft.world.clock.ClockTimeMarkers.WAKE_UP_FROM_SLEEP)) {
return;
}
state.hemorrhage = false;
state.tetanus = false;
state.fracture = false;
state.asphyxia = false;
state.asphyxiaSawLowAir = false;
state.asphyxiaDamageTicks = 0;
state.buriedSuffocationTicks = 0;
state.lastBuriedSuffocationTick = -1;
state.concussionTicks = 0;
state.winded = false;
state.windedTicks = 0;
state.hysteria = false;
state.adrenalineRushTicks = 0;
state.adrenalineCrashTicks = 0;
state.storedAdrenalineDamage = 0.0F;
state.bleedTicks = 0;
state.tetanusCooldown = 0;
state.tetanusDropOffHand = false;
state.berryDamageTicks = 0;