Skip to content

Commit

Permalink
when the data is mapped
Browse files Browse the repository at this point in the history
  • Loading branch information
seymourimadeit committed Jun 18, 2024
1 parent 1a47e8a commit 2e8c2a2
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 11 deletions.
44 changes: 34 additions & 10 deletions src/main/java/tallestred/piglinproliferation/PPEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
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;
import net.minecraft.server.level.ServerPlayer;
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;
Expand All @@ -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;
Expand All @@ -45,13 +46,15 @@
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;
import tallestred.piglinproliferation.common.blocks.PiglinSkullBlock;
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;
Expand All @@ -69,6 +72,8 @@
@SuppressWarnings("unused")
@EventBusSubscriber(modid = PiglinProliferation.MODID)
public class PPEvents {
private static List<ZiglinVariantWeight> ziglinVariants = List.of();

@SubscribeEvent
public static void onJump(LivingEvent.LivingJumpEvent event) {
if (BucklerItem.getChargeTicks(PPItems.checkEachHandForBuckler(event.getEntity())) > 0) {
Expand Down Expand Up @@ -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
}
}
}
}
}
Expand All @@ -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())));

}
}
}
Expand Down Expand Up @@ -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<? extends String> 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) {
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,17 @@
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;
import tallestred.piglinproliferation.common.attribute.PPAttributes;
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;
Expand All @@ -67,6 +71,8 @@
@Mod(PiglinProliferation.MODID)
public class PiglinProliferation {
public static final String MODID = "piglinproliferation";
public static final DataMapType<EntityType<?>, 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);
Expand All @@ -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);
Expand Down Expand Up @@ -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<ItemStack, CreativeModeTab.TabVisibility> creativeTab = event.getEntries();
if (CreativeModeTabs.SPAWN_EGGS.equals(event.getTabKey())) {
Expand Down Expand Up @@ -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(() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<ZombifiedPiglinVariant> WEIGHT_CODEC = Weight.CODEC
.xmap(ZombifiedPiglinVariant::new, ZombifiedPiglinVariant::weight);
public static final Codec<ZombifiedPiglinVariant> CODEC = Codec.withAlternative(
RecordCodecBuilder.create(in -> in.group(
Weight.CODEC.fieldOf("weight").forGetter(ZombifiedPiglinVariant::weight)).apply(in, ZombifiedPiglinVariant::new)),
WEIGHT_CODEC);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"values": {
}
}

0 comments on commit 2e8c2a2

Please sign in to comment.