Skip to content

Commit

Permalink
fixed breathless effect crashing the server
Browse files Browse the repository at this point in the history
closes #367
  • Loading branch information
Ellpeck committed Nov 4, 2024
1 parent 7ac1c3d commit b3701fb
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/main/java/de/ellpeck/naturesaura/Helper.java
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ public static AABB aabb(Vec3 pos) {
}

// This is how @ObjectHolder SHOULD work...
public static <T> void populateObjectHolders(Class<?> clazz, Registry<T> registry) {
public static <T> void populateObjectHolders(Class<?> clazz, Registry<T> registry, boolean useHolders) {
for (var entry : clazz.getFields()) {
if (!Modifier.isStatic(entry.getModifiers()))
continue;
Expand All @@ -295,7 +295,7 @@ public static <T> void populateObjectHolders(Class<?> clazz, Registry<T> registr
continue;
}
try {
entry.set(null, registry.get(location));
entry.set(null, useHolders ? registry.getHolder(location).orElseThrow() : registry.get(location));
} catch (IllegalAccessException e) {
NaturesAura.LOGGER.error(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void update(Level level, LevelChunk chunk, IAuraChunk auraChunk, BlockPos
return;
var entities = level.getEntitiesOfClass(LivingEntity.class, this.bb);
for (var entity : entities)
entity.addEffect(new MobEffectInstance(Holder.direct(ModPotions.BREATHLESS), 300, this.amp));
entity.addEffect(new MobEffectInstance(ModPotions.BREATHLESS, 300, this.amp));
}

@Override
Expand Down
16 changes: 3 additions & 13 deletions src/main/java/de/ellpeck/naturesaura/enchant/ModEnchantments.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
package de.ellpeck.naturesaura.enchant;

import de.ellpeck.naturesaura.NaturesAura;
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
import net.minecraft.core.HolderSet;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.EquipmentSlotGroup;
import net.minecraft.world.item.ItemStack;
import net.minecraft.core.Holder;
import net.minecraft.server.ReloadableServerRegistries;
import net.minecraft.world.item.enchantment.Enchantment;

public final class ModEnchantments {

public static final Enchantment AURA_MENDING = Enchantment.enchantment(
Enchantment.definition(
HolderSet.direct(BuiltInRegistries.ITEM.holders().filter(i -> new ItemStack(i).getCapability(NaturesAuraAPI.AURA_RECHARGE_CAPABILITY) == null).toList()),
// same values as unbreaking, except for the max level
5, 1, Enchantment.dynamicCost(5, 8), Enchantment.dynamicCost(55, 8), 2, EquipmentSlotGroup.ANY)
).build(ResourceLocation.fromNamespaceAndPath(NaturesAura.MOD_ID, "aura_mending"));
public static Holder<Enchantment> AURA_MENDING;

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void inventoryTick(ItemStack stackIn, Level levelIn, Entity entityIn, int
if (recharge != null) {
if (recharge.rechargeFromContainer(container, itemSlot, i, player.getInventory().selected == i))
break;
} else if (stack.getEnchantmentLevel(Holder.direct(ModEnchantments.AURA_MENDING)) > 0) {
} else if (stack.getEnchantmentLevel(ModEnchantments.AURA_MENDING) > 0) {
var mainSize = player.getInventory().items.size();
var isArmor = i >= mainSize && i < mainSize + player.getInventory().armor.size();
if ((isArmor || player.getInventory().selected == i) && Helper.rechargeAuraItem(stack, container, 1000))
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/de/ellpeck/naturesaura/potion/ModPotions.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package de.ellpeck.naturesaura.potion;

import net.minecraft.core.Holder;
import net.minecraft.world.effect.MobEffect;

@SuppressWarnings("NonConstantFieldWithUpperCaseName")
public final class ModPotions {

public static MobEffect BREATHLESS;
public static Holder<MobEffect> BREATHLESS;

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public PotionBreathless() {

@SubscribeEvent
public void onHeal(LivingHealEvent event) {
var effect = event.getEntity().getEffect(Holder.direct(this));
var effect = event.getEntity().getEffect(ModPotions.BREATHLESS);
if (effect == null)
return;
var chance = (effect.getAmplifier() + 1) / 15F;
Expand Down
25 changes: 17 additions & 8 deletions src/main/java/de/ellpeck/naturesaura/reg/ModRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@
import de.ellpeck.naturesaura.renderers.PlayerLayerTrinkets;
import net.minecraft.client.renderer.entity.player.PlayerRenderer;
import net.minecraft.client.resources.PlayerSkin;
import net.minecraft.core.HolderSet;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.EquipmentSlotGroup;
import net.minecraft.world.entity.MobCategory;
import net.minecraft.world.item.*;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
Expand Down Expand Up @@ -150,7 +153,7 @@ public static void register(RegisterEvent event) {
new BlockImpl("sky_ingot_block", Block.Properties.of().sound(SoundType.METAL).strength(4F)),
new BlockImpl("depth_ingot_block", Block.Properties.of().sound(SoundType.METAL).strength(6F))
);
Helper.populateObjectHolders(ModBlocks.class, BuiltInRegistries.BLOCK);
Helper.populateObjectHolders(ModBlocks.class, event.getRegistry(), false);
});

event.register(Registries.ITEM, h -> {
Expand Down Expand Up @@ -235,7 +238,7 @@ public static void register(RegisterEvent event) {
new ItemArmor("depth_pants", ModArmorMaterial.DEPTH, ArmorItem.Type.LEGGINGS),
new ItemArmor("depth_shoes", ModArmorMaterial.DEPTH, ArmorItem.Type.BOOTS)
);
Helper.populateObjectHolders(ModItems.class, BuiltInRegistries.ITEM);
Helper.populateObjectHolders(ModItems.class, event.getRegistry(), false);
});

event.register(Registries.BLOCK_ENTITY_TYPE, h -> {
Expand All @@ -246,12 +249,12 @@ public static void register(RegisterEvent event) {
if (item instanceof ModTileType<?> type)
h.register(ResourceLocation.fromNamespaceAndPath(NaturesAura.MOD_ID, type.getBaseName()), type.type);
}
Helper.populateObjectHolders(ModBlockEntities.class, BuiltInRegistries.BLOCK_ENTITY_TYPE);
Helper.populateObjectHolders(ModBlockEntities.class, event.getRegistry(), false);
});

event.register(Registries.MOB_EFFECT, h -> {
h.register(ResourceLocation.fromNamespaceAndPath(NaturesAura.MOD_ID, "breathless"), new PotionBreathless());
Helper.populateObjectHolders(ModPotions.class, BuiltInRegistries.MOB_EFFECT);
Helper.populateObjectHolders(ModPotions.class, event.getRegistry(), true);
});

event.register(Registries.MENU, h -> {
Expand All @@ -267,11 +270,17 @@ public static void register(RegisterEvent event) {
IItemHandler handler = ILevelData.getOverworldData(inv.player.level()).getEnderStorage(data.readUtf());
return new ContainerEnderCrate(ModContainers.ENDER_ACCESS, windowId, inv.player, handler);
}));
Helper.populateObjectHolders(ModContainers.class, BuiltInRegistries.MENU);
Helper.populateObjectHolders(ModContainers.class, event.getRegistry(), false);
});

event.register(Registries.ENCHANTMENT, h -> {
h.register(ResourceLocation.fromNamespaceAndPath(NaturesAura.MOD_ID, "aura_mending"), ModEnchantments.AURA_MENDING);
h.register(ResourceLocation.fromNamespaceAndPath(NaturesAura.MOD_ID, "aura_mending"), Enchantment.enchantment(
Enchantment.definition(
HolderSet.direct(BuiltInRegistries.ITEM.holders().filter(i -> new ItemStack(i).getCapability(NaturesAuraAPI.AURA_RECHARGE_CAPABILITY) == null).toList()),
// same values as unbreaking, except for the max level
5, 1, Enchantment.dynamicCost(5, 8), Enchantment.dynamicCost(55, 8), 2, EquipmentSlotGroup.ANY)
).build(ResourceLocation.fromNamespaceAndPath(NaturesAura.MOD_ID, "aura_mending")));
Helper.populateObjectHolders(ModEnchantments.class, event.getRegistry(), true);
});

event.register(Registries.ENTITY_TYPE, h -> {
Expand All @@ -291,7 +300,7 @@ public static void register(RegisterEvent event) {
.of(EntityStructureFinder::new, MobCategory.MISC)
.sized(0.5F, 0.5F).setShouldReceiveVelocityUpdates(true)
.setTrackingRange(64).setUpdateInterval(2).fireImmune().build(NaturesAura.MOD_ID + ":structure_finder"));
Helper.populateObjectHolders(ModEntities.class, BuiltInRegistries.ENTITY_TYPE);
Helper.populateObjectHolders(ModEntities.class, event.getRegistry(), false);
});

event.register(Registries.FEATURE, h -> {
Expand All @@ -302,7 +311,7 @@ public static void register(RegisterEvent event) {
h.register(ResourceLocation.fromNamespaceAndPath(NaturesAura.MOD_ID, "aura_mushroom"), new LevelGenAuraBloom(ModBlocks.AURA_MUSHROOM, 20, false));
h.register(ResourceLocation.fromNamespaceAndPath(NaturesAura.MOD_ID, "ancient_tree"), new LevelGenAncientTree());
h.register(ResourceLocation.fromNamespaceAndPath(NaturesAura.MOD_ID, "nether_wart_mushroom"), new LevelGenNetherWartMushroom());
Helper.populateObjectHolders(ModFeatures.class, BuiltInRegistries.FEATURE);
Helper.populateObjectHolders(ModFeatures.class, event.getRegistry(), false);
});

event.register(Registries.RECIPE_TYPE, h -> {
Expand Down

0 comments on commit b3701fb

Please sign in to comment.