diff --git a/src/main/java/tallestred/piglinproliferation/PPEvents.java b/src/main/java/tallestred/piglinproliferation/PPEvents.java index 3cc9c2a..ea4a382 100644 --- a/src/main/java/tallestred/piglinproliferation/PPEvents.java +++ b/src/main/java/tallestred/piglinproliferation/PPEvents.java @@ -4,6 +4,7 @@ import net.minecraft.core.Holder; import net.minecraft.core.Registry; import net.minecraft.core.particles.ParticleTypes; +import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.core.registries.Registries; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.level.ServerLevel; @@ -11,6 +12,7 @@ import net.minecraft.sounds.SoundEvents; import net.minecraft.sounds.SoundSource; import net.minecraft.util.RandomSource; +import net.minecraft.util.random.WeightedRandom; import net.minecraft.world.damagesource.DamageTypes; import net.minecraft.world.effect.MobEffect; import net.minecraft.world.effect.MobEffectInstance; @@ -33,7 +35,6 @@ import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.pathfinder.PathType; import net.minecraft.world.phys.Vec3; -import net.neoforged.bus.api.Event; import net.neoforged.bus.api.SubscribeEvent; import net.neoforged.fml.common.EventBusSubscriber; import net.neoforged.neoforge.event.entity.EntityJoinLevelEvent; @@ -45,6 +46,7 @@ import net.neoforged.neoforge.event.tick.EntityTickEvent; import net.neoforged.neoforge.event.tick.LevelTickEvent; import net.neoforged.neoforge.network.PacketDistributor; +import net.neoforged.neoforge.registries.datamaps.DataMapsUpdatedEvent; import tallestred.piglinproliferation.capablities.PPDataAttachments; import tallestred.piglinproliferation.client.PPSounds; import tallestred.piglinproliferation.common.blockentities.PPBlockEntities; @@ -52,6 +54,7 @@ import tallestred.piglinproliferation.common.enchantments.PPEnchantments; import tallestred.piglinproliferation.common.entities.PPEntityTypes; import tallestred.piglinproliferation.common.entities.PiglinTraveler; +import tallestred.piglinproliferation.common.entities.ZiglinVariantWeight; import tallestred.piglinproliferation.common.entities.ai.goals.*; import tallestred.piglinproliferation.common.entities.spawns.TravelerSpawner; import tallestred.piglinproliferation.common.items.BucklerItem; @@ -69,6 +72,8 @@ @SuppressWarnings("unused") @EventBusSubscriber(modid = PiglinProliferation.MODID) public class PPEvents { + private static List ziglinVariants = List.of(); + @SubscribeEvent public static void onJump(LivingEvent.LivingJumpEvent event) { if (BucklerItem.getChargeTicks(PPItems.checkEachHandForBuckler(event.getEntity())) > 0) { @@ -105,9 +110,16 @@ public static void entityJoin(EntityJoinLevelEvent event) { for (int z = centreZ - radius; z < centreZ + radius; z++) { mutable.setX(x); mutable.setZ(z); - event.getLevel().getBlockEntity(mutable, PPBlockEntities.FIRE_RING.get()).ifPresent(fireRing -> { - fireRing.addEffects(castOrNull(lingeringCloud.getOwner(), Player.class), null, null, lingeringCloud.potionContents.getAllEffects()); - }); + if (!lingeringCloud.potionContents.hasEffects()) { + try { + event.getLevel().getBlockEntity(mutable, PPBlockEntities.FIRE_RING.get()).ifPresent(fireRing -> { + fireRing.addEffects(castOrNull(lingeringCloud.getOwner(), Player.class), null, null, lingeringCloud.potionContents.getAllEffects()); + }); + } catch (ArrayIndexOutOfBoundsException execption) { + lingeringCloud.remove(Entity.RemovalReason.DISCARDED); + // I hope this fixes this issue, if not I will have to investigate further and see exactly how to recreate it + } + } } } } @@ -117,7 +129,6 @@ public static void startTracking(PlayerEvent.StartTracking event) { if (event.getTarget() instanceof ZombifiedPiglin ziglin) { if (!event.getTarget().level().isClientSide) { PacketDistributor.sendToPlayersTrackingEntity(ziglin, new ZiglinCapabilitySyncPacket(ziglin.getId(), ziglin.getData(PPDataAttachments.TRANSFORMATION_TRACKER.get()))); - } } } @@ -296,13 +307,13 @@ public static void finalizeSpawn(FinalizeSpawnEvent event) { if (event.getEntity().getType() == EntityType.ZOMBIFIED_PIGLIN) { // Some mods have entities that extend zombified piglins in order to make their own ziglins have custom textures ZombifiedPiglin zombifiedPiglin = (ZombifiedPiglin) event.getEntity(); if (spawnType != MobSpawnType.CONVERSION) { + if (!ziglinVariants.isEmpty()) { + EntityType variantType = getRandomCustomVariant(random); + ResourceLocation resourcelocation = BuiltInRegistries.ENTITY_TYPE.getKey(variantType); + zombifiedPiglin.setData(PPDataAttachments.TRANSFORMATION_TRACKER.get(), resourcelocation.getPath()); + } if (random.nextFloat() < PPConfig.COMMON.zombifiedPiglinDefaultChance.get().floatValue()) zombifiedPiglin.setData(PPDataAttachments.TRANSFORMATION_TRACKER.get(), "piglin"); - if (random.nextFloat() < PPConfig.COMMON.piglinVariantChances.get().floatValue()) { - List piglinTypes = PPConfig.COMMON.zombifiedPiglinTypeList.get(); - if (!piglinTypes.isEmpty()) - zombifiedPiglin.setData(PPDataAttachments.TRANSFORMATION_TRACKER.get(), piglinTypes.get(random.nextInt(piglinTypes.size()))); - } float bruteChance = PPConfig.COMMON.zombifiedBruteChance.get().floatValue(); if (zombifiedPiglin.getData(PPDataAttachments.TRANSFORMATION_TRACKER.get()).equalsIgnoreCase("piglin")) { if (random.nextFloat() < bruteChance) { @@ -333,6 +344,19 @@ public static void finalizeSpawn(FinalizeSpawnEvent event) { } } + public static EntityType getRandomCustomVariant(RandomSource rand) { + ZiglinVariantWeight mob = WeightedRandom.getRandomItem(rand, ziglinVariants).orElseThrow(); + return mob.type(); + } + + @SubscribeEvent + public static void onDataMapsUpdated(DataMapsUpdatedEvent event) { + event.ifRegistry(Registries.ENTITY_TYPE, registry -> ziglinVariants = registry.getDataMap(PiglinProliferation.ZOMBIFIED_PIGLIN_VARIANT_DATA_MAP).entrySet().stream().map((entry) -> { + EntityType type = Objects.requireNonNull(registry.get(entry.getKey()), "Nonexistent entity " + entry.getKey() + " in modded ziglin variant datamap!"); + return new ZiglinVariantWeight(type, entry.getValue().weight()); + }).toList()); + } + @SubscribeEvent public static void visionPercent(LivingEvent.LivingVisibilityEvent event) { if (event.getLookingEntity() != null) { diff --git a/src/main/java/tallestred/piglinproliferation/PiglinProliferation.java b/src/main/java/tallestred/piglinproliferation/PiglinProliferation.java index bedb46a..fd251ba 100644 --- a/src/main/java/tallestred/piglinproliferation/PiglinProliferation.java +++ b/src/main/java/tallestred/piglinproliferation/PiglinProliferation.java @@ -39,6 +39,9 @@ import net.neoforged.neoforge.event.server.ServerAboutToStartEvent; import net.neoforged.neoforge.network.event.RegisterPayloadHandlersEvent; import net.neoforged.neoforge.network.registration.PayloadRegistrar; +import net.neoforged.neoforge.registries.datamaps.DataMapType; +import net.neoforged.neoforge.registries.datamaps.RegisterDataMapTypesEvent; +import net.neoforged.neoforge.registries.datamaps.builtin.Compostable; import tallestred.piglinproliferation.capablities.PPDataAttachments; import tallestred.piglinproliferation.client.PPSounds; import tallestred.piglinproliferation.common.advancement.PPCriteriaTriggers; @@ -46,6 +49,7 @@ import tallestred.piglinproliferation.common.blockentities.FireRingBlockEntity; import tallestred.piglinproliferation.common.enchantments.PPEnchantments; import tallestred.piglinproliferation.common.entities.PiglinTraveler; +import tallestred.piglinproliferation.common.entities.ZombifiedPiglinVariant; import tallestred.piglinproliferation.common.items.BucklerItem; import tallestred.piglinproliferation.common.items.PPItems; import tallestred.piglinproliferation.common.blockentities.PPBlockEntities; @@ -67,6 +71,8 @@ @Mod(PiglinProliferation.MODID) public class PiglinProliferation { public static final String MODID = "piglinproliferation"; + public static final DataMapType, ZombifiedPiglinVariant> ZOMBIFIED_PIGLIN_VARIANT_DATA_MAP = DataMapType.builder( + ResourceLocation.fromNamespaceAndPath(MODID, "zombified_piglin_variants"), Registries.ENTITY_TYPE, ZombifiedPiglinVariant.CODEC).synced(ZombifiedPiglinVariant.WEIGHT_CODEC, false).build(); public PiglinProliferation(IEventBus modEventBus, Dist dist, ModContainer container) { modEventBus.addListener(this::setup); @@ -77,6 +83,7 @@ public PiglinProliferation(IEventBus modEventBus, Dist dist, ModContainer contai modEventBus.addListener(this::addSpawn); modEventBus.addListener(this::addCreativeTabs); modEventBus.addListener(this::registerPackets); + modEventBus.addListener(this::addDataMaps); if (dist == Dist.CLIENT) modEventBus.addListener(this::doClientStuff); NeoForge.EVENT_BUS.addListener(this::serverStart); @@ -111,6 +118,10 @@ private void addCustomAttributes(EntityAttributeModificationEvent event) { event.add(type, PPAttributes.TURNING_SPEED); } + private void addDataMaps(RegisterDataMapTypesEvent event) { + event.register(ZOMBIFIED_PIGLIN_VARIANT_DATA_MAP); + } + private void addCreativeTabs(final BuildCreativeModeTabContentsEvent event) { MutableHashedLinkedMap creativeTab = event.getEntries(); if (CreativeModeTabs.SPAWN_EGGS.equals(event.getTabKey())) { @@ -168,6 +179,7 @@ private void enqueueIMC(final InterModEnqueueEvent event) { private void processIMC(final InterModProcessEvent event) { } + @OnlyIn(Dist.CLIENT) private void doClientStuff(final FMLClientSetupEvent event) { event.enqueueWork(() -> { diff --git a/src/main/java/tallestred/piglinproliferation/client/particles/AfterImageParticle.java b/src/main/java/tallestred/piglinproliferation/client/particles/AfterImageParticle.java index cd34000..d0c2d33 100644 --- a/src/main/java/tallestred/piglinproliferation/client/particles/AfterImageParticle.java +++ b/src/main/java/tallestred/piglinproliferation/client/particles/AfterImageParticle.java @@ -115,7 +115,6 @@ public void render(@NotNull VertexConsumer vertex, Camera camera, float tick) { if (rendertype != null) { VertexConsumer ivertexbuilder = multibuffersource$buffersource.getBuffer(rendertype); int overlay = LivingEntityRenderer.getOverlayCoords(this.entity, 0.0F); - int color = FastColor.ARGB32.colorFromFloat(0.5F / Math.abs((float) life + 1), 1.0F, 1.0F, 1.0F); model.renderToBuffer(stack, ivertexbuilder, getLightColor(tick), overlay, flag1 ? 654311423 : -1); } stack.popPose(); diff --git a/src/main/java/tallestred/piglinproliferation/common/entities/ZiglinVariantWeight.java b/src/main/java/tallestred/piglinproliferation/common/entities/ZiglinVariantWeight.java new file mode 100644 index 0000000..5cdad0d --- /dev/null +++ b/src/main/java/tallestred/piglinproliferation/common/entities/ZiglinVariantWeight.java @@ -0,0 +1,12 @@ +package tallestred.piglinproliferation.common.entities; + +import net.minecraft.util.random.Weight; +import net.minecraft.util.random.WeightedEntry; +import net.minecraft.world.entity.EntityType; + +public record ZiglinVariantWeight(EntityType type, Weight weight) implements WeightedEntry { + @Override + public Weight getWeight() { + return weight; + } +} diff --git a/src/main/java/tallestred/piglinproliferation/common/entities/ZombifiedPiglinVariant.java b/src/main/java/tallestred/piglinproliferation/common/entities/ZombifiedPiglinVariant.java new file mode 100644 index 0000000..9e31126 --- /dev/null +++ b/src/main/java/tallestred/piglinproliferation/common/entities/ZombifiedPiglinVariant.java @@ -0,0 +1,14 @@ +package tallestred.piglinproliferation.common.entities; + +import com.mojang.serialization.Codec; +import com.mojang.serialization.codecs.RecordCodecBuilder; +import net.minecraft.util.random.Weight; + +public record ZombifiedPiglinVariant(Weight weight) { + public static final Codec WEIGHT_CODEC = Weight.CODEC + .xmap(ZombifiedPiglinVariant::new, ZombifiedPiglinVariant::weight); + public static final Codec CODEC = Codec.withAlternative( + RecordCodecBuilder.create(in -> in.group( + Weight.CODEC.fieldOf("weight").forGetter(ZombifiedPiglinVariant::weight)).apply(in, ZombifiedPiglinVariant::new)), + WEIGHT_CODEC); +} diff --git a/src/main/resources/data/piglinproliferation/data_maps/entity_type/zombified_piglin_variants.json b/src/main/resources/data/piglinproliferation/data_maps/entity_type/zombified_piglin_variants.json new file mode 100644 index 0000000..6850ea1 --- /dev/null +++ b/src/main/resources/data/piglinproliferation/data_maps/entity_type/zombified_piglin_variants.json @@ -0,0 +1,4 @@ +{ + "values": { + } +} \ No newline at end of file