Skip to content

Commit

Permalink
see desc
Browse files Browse the repository at this point in the history
- Internal changes to how item properties are registered
- Allowed users to define items for other modded ziglin variants in datamaps
  • Loading branch information
seymourimadeit committed Jul 11, 2024
1 parent dd4c915 commit 3cdedf4
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 43 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ jobs:
with:
curseforge-id: 657831
curseforge-token: ${{ secrets.CURSEFORGE_TOKEN }}
modrinth-id: iRE67WIn
modrinth-token: ${{ secrets.MODRINTH_TOKEN }}
files-primary: build/libs/!(*-@(javadoc|sources)).jar
version-type: release
loaders: neoforge
game-versions: 1.21
java: 21
name: ""
modrinth-id: iRE67WIn
modrinth-token: ${{ secrets.MODRINTH_TOKEN }}
changelog-file: changelog-1.21.*
4 changes: 2 additions & 2 deletions changelog-1.21.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
- Neoforge changes 2 electric boogalo
- Enabled buckler launch by default
- Internal changes to how item properties are registered
- Allowed users to define items for other modded ziglin variants in datamaps
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ minecraft_version=1.21
# as they do not follow standard versioning conventions.
minecraft_version_range=[1.21, 1.22)
# The Neo version must agree with the Minecraft version to get a valid artifact
neo_version=21.0.76-beta
neo_version=21.0.86-beta
# The Neo version range can use any version of Neo as bounds or match the loader version range
neo_version_range=[20.5.14,)
# The loader version range can only use the major version of Neo/FML as bounds
Expand All @@ -31,7 +31,7 @@ mod_name=Piglin Proliferation
# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
mod_license=MIT for code, CC BY-NC-SA 4.0 for assets
# The mod version. See https://semver.org/
mod_version=2.0.4
mod_version=2.0.5
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
# This should match the base package used for the mod sources.
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html
Expand Down
20 changes: 9 additions & 11 deletions src/main/java/tallestred/piglinproliferation/PPEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import net.minecraft.world.entity.monster.piglin.PiglinBrute;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.projectile.Arrow;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.alchemy.Potions;
Expand Down Expand Up @@ -307,11 +308,6 @@ 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");
float bruteChance = PPConfig.COMMON.zombifiedBruteChance.get().floatValue();
Expand All @@ -334,6 +330,13 @@ public static void finalizeSpawn(FinalizeSpawnEvent event) {
zombifiedPiglin.setItemSlot(EquipmentSlot.MAINHAND, new ItemStack(Items.CROSSBOW));
}
}
if (!ziglinVariants.isEmpty()) {
EntityType<?> variantType = WeightedRandom.getRandomItem(random, ziglinVariants).orElseThrow().type();
Item item = WeightedRandom.getRandomItem(random, ziglinVariants).orElseThrow().itemId();
ResourceLocation resourcelocation = BuiltInRegistries.ENTITY_TYPE.getKey(variantType);
zombifiedPiglin.setData(PPDataAttachments.TRANSFORMATION_TRACKER.get(), resourcelocation.getPath());
zombifiedPiglin.setItemSlot(EquipmentSlot.MAINHAND, new ItemStack(item));
}
}
if (spawnType == MobSpawnType.JOCKEY) {
event.setCanceled(true);
Expand All @@ -344,16 +347,11 @@ 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());
return new ZiglinVariantWeight(type, entry.getValue().weight(), entry.getValue().itemID());
}).toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ public PiglinProliferation(IEventBus modEventBus, Dist dist, ModContainer contai
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);
PPSounds.SOUNDS.register(modEventBus);
PPAttributes.ATTRIBUTES.register(modEventBus);
Expand Down Expand Up @@ -147,6 +145,8 @@ private void addCreativeTabs(final BuildCreativeModeTabContentsEvent event) {
);
} else if (CreativeModeTabs.COMBAT.equals(event.getTabKey())) {
addToCreativeTabAfter(event, Items.SHIELD, PPItems.BUCKLER.get());
} else if (CreativeModeTabs.TOOLS_AND_UTILITIES.equals(event.getTabKey())) {
addToCreativeTabAfter(event, Items.RECOVERY_COMPASS, PPItems.TRAVELERS_COMPASS.get());
}
}

Expand Down Expand Up @@ -179,26 +179,6 @@ private void enqueueIMC(final InterModEnqueueEvent event) {
private void processIMC(final InterModProcessEvent event) {
}


@OnlyIn(Dist.CLIENT)
private void doClientStuff(final FMLClientSetupEvent event) {
event.enqueueWork(() -> {
ItemProperties.register(PPItems.BUCKLER.get(), ResourceLocation.parse("blocking"),
(stack, clientWorld, livingEntity, useTime) -> {
boolean active = livingEntity != null && livingEntity.isUsingItem()
&& livingEntity.getUseItem() == stack
|| livingEntity != null && BucklerItem.isReady(stack);
return livingEntity != null && active ? 1.0F : 0.0F;
});
ItemProperties.register(PPItems.TRAVELERS_COMPASS.get(), ResourceLocation.parse("angle"), new CompassItemPropertyFunction((level, itemStack, player) -> {
TravelersCompassTracker tracker = itemStack.get(PPComponents.TRAVELERS_COMPASS_TRACKER);
if (tracker != null)
return tracker.target();
return null; //TODO not sure
}));
});
}

private void serverStart(final ServerAboutToStartEvent event) {
Registry<StructureTemplatePool> templatePoolRegistry = event.getServer().registryAccess().registry(Registries.TEMPLATE_POOL).orElseThrow();
Registry<StructureProcessorList> processorListRegistry = event.getServer().registryAccess().registry(Registries.PROCESSOR_LIST).orElseThrow();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package tallestred.piglinproliferation;

import net.minecraft.client.renderer.item.CompassItemPropertyFunction;
import net.minecraft.client.renderer.item.ItemProperties;
import net.minecraft.resources.ResourceLocation;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.fml.ModContainer;
import net.neoforged.fml.common.Mod;
import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent;
import tallestred.piglinproliferation.common.items.BucklerItem;
import tallestred.piglinproliferation.common.items.PPItems;
import tallestred.piglinproliferation.common.items.component.PPComponents;
import tallestred.piglinproliferation.common.items.component.TravelersCompassTracker;

@Mod(value = PiglinProliferation.MODID, dist = Dist.CLIENT)
public class PiglinProliferationClient {
public PiglinProliferationClient(IEventBus modEventBus, Dist dist, ModContainer container) {
modEventBus.addListener(this::doClientStuff);
}

private void doClientStuff(final FMLClientSetupEvent event) {
event.enqueueWork(() -> {
ItemProperties.register(PPItems.BUCKLER.get(), ResourceLocation.parse("blocking"),
(stack, clientWorld, livingEntity, useTime) -> {
boolean active = livingEntity != null && livingEntity.isUsingItem()
&& livingEntity.getUseItem() == stack
|| livingEntity != null && BucklerItem.isReady(stack);
return livingEntity != null && active ? 1.0F : 0.0F;
});
ItemProperties.register(PPItems.TRAVELERS_COMPASS.get(), ResourceLocation.parse("angle"), new CompassItemPropertyFunction((level, itemStack, player) -> {
TravelersCompassTracker tracker = itemStack.get(PPComponents.TRAVELERS_COMPASS_TRACKER);
if (tracker != null)
return tracker.target();
return null; //TODO not sure
}));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import net.minecraft.util.random.Weight;
import net.minecraft.util.random.WeightedEntry;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.item.Item;

public record ZiglinVariantWeight(EntityType<?> type, Weight weight) implements WeightedEntry {
public record ZiglinVariantWeight(EntityType<?> type, Weight weight, Item itemId) implements WeightedEntry {
@Override
public Weight getWeight() {
return weight;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.util.random.Weight;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.Items;

public record ZombifiedPiglinVariant(Weight weight) {
import java.util.function.Function;

public record ZombifiedPiglinVariant(Weight weight, Item itemID) {
public static final Codec<ZombifiedPiglinVariant> WEIGHT_CODEC = Weight.CODEC
.xmap(ZombifiedPiglinVariant::new, ZombifiedPiglinVariant::weight);
.xmap((Function<? super Weight, ? extends ZombifiedPiglinVariant>) 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.fieldOf("weight").forGetter(ZombifiedPiglinVariant::weight), BuiltInRegistries.ITEM.byNameCodec().fieldOf("item").forGetter(ZombifiedPiglinVariant::itemID)).apply(in, ZombifiedPiglinVariant::new)),
WEIGHT_CODEC);

public ZombifiedPiglinVariant(Weight weight) {
this(weight, Items.AIR);
}
}

0 comments on commit 3cdedf4

Please sign in to comment.