Fix server crashes due to late initialisation of registered values

This commit is contained in:
Sollace 2022-08-27 16:37:37 +02:00
parent 03bb4f654c
commit cf07f5d341
18 changed files with 81 additions and 53 deletions

View file

@ -11,17 +11,22 @@ import java.util.Optional;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.minelittlepony.unicopia.ability.Abilities;
import com.minelittlepony.unicopia.ability.data.tree.TreeTypeLoader;
import com.minelittlepony.unicopia.ability.magic.spell.effect.SpellType;
import com.minelittlepony.unicopia.ability.magic.spell.trait.TraitLoader;
import com.minelittlepony.unicopia.advancement.UCriteria;
import com.minelittlepony.unicopia.block.UBlocks;
import com.minelittlepony.unicopia.block.state.StateMaps;
import com.minelittlepony.unicopia.command.Commands;
import com.minelittlepony.unicopia.container.UScreenHandlers;
import com.minelittlepony.unicopia.entity.UEntities;
import com.minelittlepony.unicopia.entity.effect.UPotions;
import com.minelittlepony.unicopia.entity.player.Pony;
import com.minelittlepony.unicopia.item.UItems;
import com.minelittlepony.unicopia.item.enchantment.UEnchantments;
import com.minelittlepony.unicopia.network.Channel;
import com.minelittlepony.unicopia.particle.UParticles;
public class Unicopia implements ModInitializer {
public static final String DEFAULT_NAMESPACE = "unicopia";
@ -64,6 +69,13 @@ public class Unicopia implements ModInitializer {
UBlocks.bootstrap();
UItems.bootstrap();
UPotions.bootstrap();
UParticles.bootstrap();
USounds.bootstrap();
Race.bootstrap();
SpellType.bootstrap();
Abilities.bootstrap();
StateMaps.bootstrap();
UScreenHandlers.bootstrap();
}
public interface SidedAccess {

View file

@ -58,4 +58,5 @@ public interface Abilities {
return Registry.register(REGISTRY, id, power);
}
static void bootstrap() {}
}

View file

@ -2,6 +2,7 @@ package com.minelittlepony.unicopia.ability;
import com.minelittlepony.unicopia.Race;
import com.minelittlepony.unicopia.ability.data.Multi;
import com.minelittlepony.unicopia.entity.UEntityAttributes;
import com.minelittlepony.unicopia.entity.player.PlayerAttributes;
import com.minelittlepony.unicopia.entity.player.Pony;
import com.minelittlepony.unicopia.util.RayTraceHelper;
@ -67,7 +68,7 @@ public class BatPonyHangAbility implements Ability<Multi> {
@Override
public void apply(Pony player, Multi data) {
EntityAttributeInstance attr = player.getMaster().getAttributeInstance(PlayerAttributes.ENTITY_GRAVTY_MODIFIER);
EntityAttributeInstance attr = player.getMaster().getAttributeInstance(UEntityAttributes.ENTITY_GRAVTY_MODIFIER);
if (data.hitType == 0 && attr.hasModifier(PlayerAttributes.BAT_HANGING)) {
attr.removeModifier(PlayerAttributes.BAT_HANGING);

View file

@ -62,6 +62,8 @@ public final class SpellType<T extends Spell> implements Affine, SpellPredicate<
public static final SpellType<FireBoltSpell> FIRE_BOLT = register("fire_bolt", Affinity.GOOD, 0xFF8811, true, FireBoltSpell.DEFAULT_TRAITS, FireBoltSpell::new);
public static final SpellType<LightSpell> LIGHT = register("light", Affinity.GOOD, 0xEEFFAA, true, LightSpell.DEFAULT_TRAITS, LightSpell::new);
public static void bootstrap() {}
private final Identifier id;
private final Affinity affinity;
private final int color;

View file

@ -94,4 +94,6 @@ public class StateMaps {
static boolean isOre(BlockState s) {
return s.getBlock() instanceof OreBlock;
}
public static void bootstrap() {}
}

View file

@ -11,7 +11,7 @@ import org.jetbrains.annotations.Nullable;
import com.google.common.collect.Multimap;
import com.minelittlepony.unicopia.entity.Equine;
import com.minelittlepony.unicopia.entity.player.PlayerAttributes;
import com.minelittlepony.unicopia.entity.UEntityAttributes;
import com.minelittlepony.unicopia.entity.player.Pony;
import com.minelittlepony.unicopia.item.enchantment.AttributedEnchantment;
import net.fabricmc.fabric.api.client.item.v1.ItemTooltipCallback;
@ -55,7 +55,7 @@ public class ModifierTooltipRenderer implements ItemTooltipCallback {
List<Text> newLines = new ArrayList<>();
modifs.entries().stream()
.filter(entry -> entry.getKey().equals(EntityAttributes.GENERIC_MOVEMENT_SPEED) || PlayerAttributes.REGISTRY.contains(entry.getKey()))
.filter(entry -> entry.getKey().equals(EntityAttributes.GENERIC_MOVEMENT_SPEED) || UEntityAttributes.REGISTRY.contains(entry.getKey()))
.forEach(entry -> describeModifiers(entry.getKey(), entry.getValue(), null, newLines));
if (!newLines.isEmpty()) {
@ -133,9 +133,9 @@ public class ModifierTooltipRenderer implements ItemTooltipCallback {
if (baseAdjusted) {
lines.add(Text.literal(" ").append(getModifierLineBase("equals", displayValue, op, attribute, Formatting.DARK_GREEN)));
} else if (value > 0) {
lines.add(getModifierLineBase("plus", displayValue, op, attribute, attribute == PlayerAttributes.ENTITY_GRAVTY_MODIFIER ? Formatting.RED : Formatting.BLUE));
lines.add(getModifierLineBase("plus", displayValue, op, attribute, attribute == UEntityAttributes.ENTITY_GRAVTY_MODIFIER ? Formatting.RED : Formatting.BLUE));
} else if (value < 0) {
lines.add(getModifierLineBase("take", -displayValue, op, attribute, attribute == PlayerAttributes.ENTITY_GRAVTY_MODIFIER ? Formatting.BLUE : Formatting.RED));
lines.add(getModifierLineBase("take", -displayValue, op, attribute, attribute == UEntityAttributes.ENTITY_GRAVTY_MODIFIER ? Formatting.BLUE : Formatting.RED));
}
}

View file

@ -17,7 +17,6 @@ import com.minelittlepony.unicopia.entity.ai.BreakHeartGoal;
import com.minelittlepony.unicopia.entity.ai.DynamicTargetGoal;
import com.minelittlepony.unicopia.entity.ai.EatMuffinGoal;
import com.minelittlepony.unicopia.entity.ai.WantItTakeItGoal;
import com.minelittlepony.unicopia.entity.player.PlayerAttributes;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
@ -151,7 +150,7 @@ public class Creature extends Living<LivingEntity> implements WeaklyOwned<Living
public static void registerAttributes(DefaultAttributeContainer.Builder builder) {
builder.add(EntityAttributes.GENERIC_ATTACK_DAMAGE);
builder.add(EntityAttributes.GENERIC_ATTACK_KNOCKBACK);
builder.add(PlayerAttributes.ENTITY_GRAVTY_MODIFIER);
builder.add(UEntityAttributes.ENTITY_GRAVTY_MODIFIER);
}
@Override

View file

@ -1,6 +1,5 @@
package com.minelittlepony.unicopia.entity;
import com.minelittlepony.unicopia.entity.player.PlayerAttributes;
import com.minelittlepony.unicopia.util.Copieable;
import com.minelittlepony.unicopia.util.Tickable;
@ -145,7 +144,7 @@ public class EntityPhysics<T extends Entity> implements Physics, Copieable<Entit
return 1;
}
return getBaseGravityModifier() * (float)((LivingEntity)entity).getAttributeValue(PlayerAttributes.ENTITY_GRAVTY_MODIFIER);
return getBaseGravityModifier() * (float)((LivingEntity)entity).getAttributeValue(UEntityAttributes.ENTITY_GRAVTY_MODIFIER);
}
return getBaseGravityModifier();

View file

@ -3,6 +3,7 @@ package com.minelittlepony.unicopia.entity;
import java.util.function.Predicate;
import com.minelittlepony.unicopia.Unicopia;
import com.minelittlepony.unicopia.entity.behaviour.EntityBehaviour;
import com.minelittlepony.unicopia.projectile.MagicProjectileEntity;
import net.fabricmc.fabric.api.biome.v1.BiomeModifications;
@ -70,5 +71,7 @@ public interface UEntities {
), SpawnGroup.AMBIENT, BUTTERFLY, 7, 5, 19);
UTradeOffers.bootstrap();
EntityBehaviour.bootstrap();
UEntityAttributes.bootstrap();
}
}

View file

@ -0,0 +1,26 @@
package com.minelittlepony.unicopia.entity;
import java.util.ArrayList;
import java.util.List;
import com.minelittlepony.unicopia.Unicopia;
import net.minecraft.entity.attribute.ClampedEntityAttribute;
import net.minecraft.entity.attribute.EntityAttribute;
import net.minecraft.util.registry.Registry;
public interface UEntityAttributes {
List<EntityAttribute> REGISTRY = new ArrayList<>();
EntityAttribute EXTENDED_REACH_DISTANCE = register("pegasus.reach", new ClampedEntityAttribute("player.reachDistance", 0, 0, 10).setTracked(true));
EntityAttribute EXTRA_MINING_SPEED = register("earth.mining_speed", new ClampedEntityAttribute("player.miningSpeed", 1, 0, 5).setTracked(true));
EntityAttribute ENTITY_GRAVTY_MODIFIER = register("player.gravity", (new EntityAttribute("player.gravityModifier", 1) {}).setTracked(true));
private static EntityAttribute register(String name, EntityAttribute attribute) {
REGISTRY.add(attribute);
return Registry.register(Registry.ATTRIBUTE, Unicopia.id(name), attribute);
}
static void bootstrap() {}
}

View file

@ -15,7 +15,7 @@ import com.minelittlepony.unicopia.InteractionManager;
import com.minelittlepony.unicopia.Owned;
import com.minelittlepony.unicopia.ability.magic.Caster;
import com.minelittlepony.unicopia.ability.magic.SpellPredicate;
import com.minelittlepony.unicopia.entity.player.PlayerAttributes;
import com.minelittlepony.unicopia.entity.UEntityAttributes;
import com.minelittlepony.unicopia.entity.player.PlayerDimensions;
import com.minelittlepony.unicopia.entity.player.Pony;
import com.minelittlepony.unicopia.projectile.ProjectileUtil;
@ -198,7 +198,7 @@ public class EntityAppearance implements NbtSerialisable, PlayerDimensions.Provi
Caster.of(entity).ifPresent(c -> c.getSpellSlot().clear());
if (entity instanceof LivingEntity) {
((LivingEntity) entity).getAttributeInstance(PlayerAttributes.ENTITY_GRAVTY_MODIFIER).clearModifiers();
((LivingEntity) entity).getAttributeInstance(UEntityAttributes.ENTITY_GRAVTY_MODIFIER).clearModifiers();
}
if (source.isClient()) {

View file

@ -290,4 +290,6 @@ public class EntityBehaviour<T extends Entity> {
register(BlazeBehaviour::new, EntityType.BLAZE);
register(MinecartBehaviour::new, EntityType.CHEST_MINECART, EntityType.COMMAND_BLOCK_MINECART, EntityType.FURNACE_MINECART, EntityType.HOPPER_MINECART, EntityType.MINECART, EntityType.SPAWNER_MINECART, EntityType.TNT_MINECART);
}
public static void bootstrap() {}
}

View file

@ -86,11 +86,7 @@ public class RaceChangeStatusEffect extends StatusEffect {
((PlayerEntity)entity).sendMessage(stage.getMessage(race), true);
}
float hitAmount = entity.getHealth() / 2;
if (hitAmount > 1) {
entity.damage(DamageSource.MAGIC, hitAmount);
}
entity.damage(DamageSource.MAGIC, 0);
}
if (entity instanceof PlayerEntity) {
@ -112,9 +108,12 @@ public class RaceChangeStatusEffect extends StatusEffect {
((Caster<?>)eq).getSpellSlot().clear();
}
if (eq instanceof Pony) {
((Pony)eq).setDirty();
if (eq instanceof Pony pony) {
MagicReserves magic = pony.getMagicalReserves();
magic.getExertion().set(0.2F);
pony.setDirty();
}
entity.damage(MagicalDamageSource.TRIBE_SWAP, Float.MAX_VALUE);
}
}

View file

@ -1,29 +1,19 @@
package com.minelittlepony.unicopia.entity.player;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import com.minelittlepony.unicopia.Race;
import com.minelittlepony.unicopia.entity.UEntityAttributes;
import com.minelittlepony.unicopia.util.Tickable;
import net.minecraft.entity.attribute.ClampedEntityAttribute;
import net.minecraft.entity.attribute.EntityAttribute;
import net.minecraft.entity.attribute.EntityAttributeInstance;
import net.minecraft.entity.attribute.EntityAttributeModifier;
import net.minecraft.entity.attribute.EntityAttributeModifier.Operation;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.registry.Registry;
public class PlayerAttributes implements Tickable {
public static final List<EntityAttribute> REGISTRY = new ArrayList<>();
public static final EntityAttribute EXTENDED_REACH_DISTANCE = register("unicopia.pegasus.reach", new ClampedEntityAttribute("player.reachDistance", 0, 0, 10).setTracked(true));
public static final EntityAttribute EXTRA_MINING_SPEED = register("unicopia.earth.mining_speed", new ClampedEntityAttribute("player.miningSpeed", 1, 0, 5).setTracked(true));
public static final EntityAttribute ENTITY_GRAVTY_MODIFIER = register("unicopia.player.gravity", (new EntityAttribute("player.gravityModifier", 1) {}).setTracked(true));
private static final EntityAttributeModifier EARTH_PONY_STRENGTH =
new EntityAttributeModifier(UUID.fromString("777a5505-521e-480b-b9d5-6ea54f259564"), "Earth Pony Strength", 0.6, Operation.MULTIPLY_TOTAL);
private static final EntityAttributeModifier EARTH_PONY_MINING_SPEED =
@ -55,12 +45,11 @@ public class PlayerAttributes implements Tickable {
toggleAttribute(entity, EntityAttributes.GENERIC_KNOCKBACK_RESISTANCE, EARTH_PONY_KNOCKBACK_RESISTANCE, race.canUseEarth() && entity.isSneaking());
toggleAttribute(entity, EntityAttributes.GENERIC_MOVEMENT_SPEED, PEGASUS_SPEED, race.canFly());
toggleAttribute(entity, EntityAttributes.GENERIC_ATTACK_SPEED, PEGASUS_SPEED, race.canFly());
toggleAttribute(entity, EXTENDED_REACH_DISTANCE, PEGASUS_REACH, race.canFly());
toggleAttribute(entity, EXTRA_MINING_SPEED, EARTH_PONY_MINING_SPEED, race.canUseEarth());
toggleAttribute(entity, UEntityAttributes.EXTENDED_REACH_DISTANCE, PEGASUS_REACH, race.canFly());
toggleAttribute(entity, UEntityAttributes.EXTRA_MINING_SPEED, EARTH_PONY_MINING_SPEED, race.canUseEarth());
}
private void toggleAttribute(PlayerEntity entity, EntityAttribute attribute, EntityAttributeModifier modifier, boolean enable) {
EntityAttributeInstance instance = entity.getAttributeInstance(attribute);
if (enable) {
@ -72,8 +61,4 @@ public class PlayerAttributes implements Tickable {
}
}
private static EntityAttribute register(String id, EntityAttribute attribute) {
REGISTRY.add(attribute);
return Registry.register(Registry.ATTRIBUTE, id, attribute);
}
}

View file

@ -8,10 +8,7 @@ import com.minelittlepony.unicopia.ability.magic.SpellPredicate;
import com.minelittlepony.unicopia.ability.magic.spell.effect.SpellType;
import com.minelittlepony.unicopia.advancement.UCriteria;
import com.minelittlepony.unicopia.client.render.PlayerPoser.Animation;
import com.minelittlepony.unicopia.entity.Creature;
import com.minelittlepony.unicopia.entity.EntityPhysics;
import com.minelittlepony.unicopia.entity.Jumper;
import com.minelittlepony.unicopia.entity.Leaner;
import com.minelittlepony.unicopia.entity.*;
import com.minelittlepony.unicopia.entity.player.MagicReserves.Bar;
import com.minelittlepony.unicopia.item.AmuletItem;
import com.minelittlepony.unicopia.item.UItems;
@ -250,7 +247,7 @@ public class PlayerPhysics extends EntityPhysics<PlayerEntity> implements Tickab
if (type.canFly()) {
if (isFlying()) {
if (pony.getSpecies() == Race.BAT && entity.verticalCollision && pony.canHangAt(pony.getOrigin().up(2))) {
EntityAttributeInstance attr = entity.getAttributeInstance(PlayerAttributes.ENTITY_GRAVTY_MODIFIER);
EntityAttributeInstance attr = entity.getAttributeInstance(UEntityAttributes.ENTITY_GRAVTY_MODIFIER);
if (!attr.hasModifier(PlayerAttributes.BAT_HANGING)) {
attr.addPersistentModifier(PlayerAttributes.BAT_HANGING);

View file

@ -24,9 +24,7 @@ import com.minelittlepony.unicopia.ability.magic.spell.Spell;
import com.minelittlepony.unicopia.ability.magic.spell.effect.SpellType;
import com.minelittlepony.unicopia.ability.magic.spell.trait.TraitDiscovery;
import com.minelittlepony.unicopia.advancement.UCriteria;
import com.minelittlepony.unicopia.entity.PonyContainer;
import com.minelittlepony.unicopia.entity.Living;
import com.minelittlepony.unicopia.entity.Trap;
import com.minelittlepony.unicopia.entity.*;
import com.minelittlepony.unicopia.entity.effect.SunBlindnessStatusEffect;
import com.minelittlepony.unicopia.entity.effect.UEffects;
import com.minelittlepony.unicopia.item.UItems;
@ -125,9 +123,9 @@ public class Pony extends Living<PlayerEntity> implements Transmittable, Copieab
}
public static void registerAttributes(DefaultAttributeContainer.Builder builder) {
builder.add(PlayerAttributes.EXTENDED_REACH_DISTANCE);
builder.add(PlayerAttributes.EXTRA_MINING_SPEED);
builder.add(PlayerAttributes.ENTITY_GRAVTY_MODIFIER);
builder.add(UEntityAttributes.EXTENDED_REACH_DISTANCE);
builder.add(UEntityAttributes.EXTRA_MINING_SPEED);
builder.add(UEntityAttributes.ENTITY_GRAVTY_MODIFIER);
}
public void setAnimation(Animation animation) {
@ -248,11 +246,11 @@ public class Pony extends Living<PlayerEntity> implements Transmittable, Copieab
}
public float getExtendedReach() {
return (float)entity.getAttributeInstance(PlayerAttributes.EXTENDED_REACH_DISTANCE).getValue();
return (float)entity.getAttributeInstance(UEntityAttributes.EXTENDED_REACH_DISTANCE).getValue();
}
public float getBlockBreakingSpeed() {
return (float)entity.getAttributeInstance(PlayerAttributes.EXTRA_MINING_SPEED).getValue();
return (float)entity.getAttributeInstance(UEntityAttributes.EXTRA_MINING_SPEED).getValue();
}
public Motion getMotion() {
@ -316,7 +314,7 @@ public class Pony extends Living<PlayerEntity> implements Transmittable, Copieab
}
public boolean isHanging() {
return entity.getAttributeInstance(PlayerAttributes.ENTITY_GRAVTY_MODIFIER).hasModifier(PlayerAttributes.BAT_HANGING);
return entity.getAttributeInstance(UEntityAttributes.ENTITY_GRAVTY_MODIFIER).hasModifier(PlayerAttributes.BAT_HANGING);
}
public boolean canHangAt(BlockPos pos) {
@ -345,7 +343,7 @@ public class Pony extends Living<PlayerEntity> implements Transmittable, Copieab
|| !canHangAt(getHangingPos())) {
entity.getAttributes().getCustomInstance(PlayerAttributes.ENTITY_GRAVTY_MODIFIER).removeModifier(PlayerAttributes.BAT_HANGING);
entity.getAttributes().getCustomInstance(UEntityAttributes.ENTITY_GRAVTY_MODIFIER).removeModifier(PlayerAttributes.BAT_HANGING);
entity.calculateDimensions();
}
}

View file

@ -5,7 +5,7 @@ import java.util.List;
import java.util.UUID;
import com.minelittlepony.unicopia.Unicopia;
import com.minelittlepony.unicopia.entity.player.PlayerAttributes;
import com.minelittlepony.unicopia.entity.UEntityAttributes;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.Enchantment.Rarity;
@ -49,7 +49,7 @@ public interface UEnchantments {
* Alters gravity
*/
Enchantment REPULSION = register("repulsion", new AttributedEnchantment(Rarity.VERY_RARE, EnchantmentTarget.ARMOR_FEET, false, 3, EquipmentSlot.FEET))
.addModifier(PlayerAttributes.ENTITY_GRAVTY_MODIFIER, (user, level) -> {
.addModifier(UEntityAttributes.ENTITY_GRAVTY_MODIFIER, (user, level) -> {
return new EntityAttributeModifier(UUID.fromString("1734bbd6-1916-4124-b710-5450ea70fbdb"), "Anti Grav", (0.5F - (0.375 * (level - 1))) - 1, Operation.MULTIPLY_TOTAL);
});

View file

@ -32,4 +32,6 @@ public interface UParticles {
static <T extends ParticleType<?>> T register(String name, T type) {
return Registry.register(Registry.PARTICLE_TYPE, Unicopia.id(name), type);
}
static void bootstrap() {}
}