Skip to content

Commit

Permalink
Add advancement triggers and some new advancements (#96)
Browse files Browse the repository at this point in the history
* Start custom advancement triggers

* Add advancement triggers and some new advancements
  • Loading branch information
legobmw99 authored Jan 13, 2024
1 parent 6988235 commit aa89c06
Show file tree
Hide file tree
Showing 13 changed files with 291 additions and 32 deletions.
2 changes: 2 additions & 0 deletions src/generated/resources/assets/allomancy/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"advancements.become_mistborn.title": "Become Mistborn!",
"advancements.coinshot.desc": "Kill a mob with the bag of coins.",
"advancements.coinshot.title": "Coinshot",
"advancements.consequences.desc": "Learn what happens when you push on a much heavier target.",
"advancements.consequences.title": "Consequences, Vin",
"advancements.dna_entangled.desc": "Your DNA is too entangled with the spiritual realm to use Lerasium",
"advancements.dna_entangled.title": "Spiritual DNA Entanglement",
"advancements.local_metallurgist.desc": "Craft a grinder to begin mixing metals",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"parent": "allomancy:main/metallurgist",
"criteria": {
"pulled_iron_golem": {
"conditions": {
"entity": [
{
"condition": "minecraft:entity_properties",
"entity": "this",
"predicate": {
"type": "minecraft:iron_golem"
}
}
],
"metal": "iron"
},
"trigger": "allomancy:metal_used_on_entity"
},
"pushed_iron_golem": {
"conditions": {
"entity": [
{
"condition": "minecraft:entity_properties",
"entity": "this",
"predicate": {
"type": "minecraft:iron_golem"
}
}
],
"metal": "steel"
},
"trigger": "allomancy:metal_used_on_entity"
}
},
"display": {
"announce_to_chat": false,
"description": {
"translate": "advancements.consequences.desc"
},
"hidden": true,
"icon": {
"item": "minecraft:iron_block"
},
"title": {
"translate": "advancements.consequences.title"
}
},
"requirements": [
[
"pushed_iron_golem",
"pulled_iron_golem"
]
],
"sends_telemetry_event": true
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,49 @@
{
"parent": "allomancy:main/metallurgist",
"criteria": {
"tin_foil_hat": {
"attempted_chromium_manipulation": {
"conditions": {
"items": [
"enhanced": false,
"metal": "chromium",
"player": [
{
"items": [
"allomancy:aluminum_helmet"
]
"condition": "minecraft:entity_properties",
"entity": "this",
"predicate": {
"equipment": {
"head": {
"items": [
"allomancy:aluminum_helmet"
]
}
}
}
}
]
},
"trigger": "minecraft:inventory_changed"
"trigger": "allomancy:metal_used_on_player"
},
"attempted_nicrosil_manipulation": {
"conditions": {
"enhanced": false,
"metal": "nicrosil",
"player": [
{
"condition": "minecraft:entity_properties",
"entity": "this",
"predicate": {
"equipment": {
"head": {
"items": [
"allomancy:aluminum_helmet"
]
}
}
}
}
]
},
"trigger": "allomancy:metal_used_on_player"
}
},
"display": {
Expand All @@ -30,7 +62,8 @@
},
"requirements": [
[
"tin_foil_hat"
"attempted_nicrosil_manipulation",
"attempted_chromium_manipulation"
]
],
"sends_telemetry_event": true
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/legobmw99/allomancy/api/enums/Metal.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.legobmw99.allomancy.api.enums;

import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;

import java.util.Locale;

public enum Metal {
Expand Down Expand Up @@ -61,5 +64,13 @@ public int getIndex() {
return ordinal();
}

public static final Codec<Metal> CODEC = Codec.STRING.comapFlatMap(s -> {
for (Metal mt : Metal.values()) {
if (mt.getName().equals(s)) {
return DataResult.success(mt);
}
}
return DataResult.error(() -> s + " is not a valid Metal");
}, Metal::getName);

}
25 changes: 23 additions & 2 deletions src/main/java/com/legobmw99/allomancy/datagen/Advancements.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package com.legobmw99.allomancy.datagen;

import com.legobmw99.allomancy.Allomancy;
import com.legobmw99.allomancy.api.enums.Metal;
import com.legobmw99.allomancy.modules.combat.CombatSetup;
import com.legobmw99.allomancy.modules.consumables.ConsumeSetup;
import com.legobmw99.allomancy.modules.extras.advancement.MetalUsedOnEntityTrigger;
import com.legobmw99.allomancy.modules.extras.advancement.MetalUsedOnPlayerTrigger;
import com.legobmw99.allomancy.modules.materials.MaterialsSetup;
import net.minecraft.advancements.*;
import net.minecraft.advancements.critereon.*;
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.Blocks;
import net.neoforged.neoforge.common.data.AdvancementProvider;
import net.neoforged.neoforge.common.data.ExistingFileHelper;

Expand Down Expand Up @@ -74,13 +79,29 @@ public void generate(HolderLookup.Provider registries, Consumer<AdvancementHolde
.save(saver, "allomancy:main/coinshot");


var tinFoilPredicate = EntityPredicate.wrap(
EntityPredicate.Builder.entity().equipment(EntityEquipmentPredicate.Builder.equipment().head(ItemPredicate.Builder.item().of(CombatSetup.ALUMINUM_HELMET))));

Advancement.Builder
.advancement()
.parent(Advancement.Builder.advancement().build(new ResourceLocation(Allomancy.MODID, "main/metallurgist")))
.display(CombatSetup.ALUMINUM_HELMET.get(), Component.translatable("advancements.tin_foil_hat.title"), Component.translatable("advancements.tin_foil_hat.desc"),
null, AdvancementType.TASK, true, false, true)
.addCriterion("tin_foil_hat", InventoryChangeTrigger.TriggerInstance.hasItems(CombatSetup.ALUMINUM_HELMET.get()))
// TODO require that someone actually use allomancy on you
.addCriterion("attempted_nicrosil_manipulation", MetalUsedOnPlayerTrigger.TriggerInstance.instance(Optional.of(tinFoilPredicate), Metal.NICROSIL, false))
.addCriterion("attempted_chromium_manipulation", MetalUsedOnPlayerTrigger.TriggerInstance.instance(Optional.of(tinFoilPredicate), Metal.CHROMIUM, false))
.requirements(AdvancementRequirements.Strategy.OR)
.save(saver, "allomancy:main/tin_foil_hat");

var ironGolemPredicate = EntityPredicate.wrap(EntityPredicate.Builder.entity().of(EntityType.IRON_GOLEM));

Advancement.Builder
.advancement()
.parent(Advancement.Builder.advancement().build(new ResourceLocation(Allomancy.MODID, "main/metallurgist")))
.display(Blocks.IRON_BLOCK, Component.translatable("advancements.consequences.title"), Component.translatable("advancements.consequences.desc"), null,
AdvancementType.TASK, true, false, true)
.addCriterion("pushed_iron_golem", MetalUsedOnEntityTrigger.TriggerInstance.instance(Optional.empty(), Optional.of(ironGolemPredicate), Metal.STEEL))
.addCriterion("pulled_iron_golem", MetalUsedOnEntityTrigger.TriggerInstance.instance(Optional.empty(), Optional.of(ironGolemPredicate), Metal.IRON))
.requirements(AdvancementRequirements.Strategy.OR)
.save(saver, "allomancy:main/consequences");
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/legobmw99/allomancy/datagen/Languages.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ protected void addTranslations() {
add("advancements.coinshot.desc", "Kill a mob with the bag of coins.");
add("advancements.tin_foil_hat.title", "Tin foil hat");
add("advancements.tin_foil_hat.desc", "Protect yourself, and be a bit paranoid too");
add("advancements.consequences.title", "Consequences, Vin");
add("advancements.consequences.desc", "Learn what happens when you push on a much heavier target.");

add("key.categories.allomancy", "Allomancy");
add("key.burn", "Burn Metals");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ private enum Ammo {
MAGIC(5.5F, 2.0F, 4.0F, 1.0F),
LIGHT(4.0F, 2.0F, 4.0F, 1.0F);

// TODO consider if should be more granular

final float damage;
final float arg1;
final float arg2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import com.legobmw99.allomancy.Allomancy;
import com.legobmw99.allomancy.api.enums.Metal;
import com.legobmw99.allomancy.modules.extras.advancement.MetalUsedOnEntityTrigger;
import com.legobmw99.allomancy.modules.extras.advancement.MetalUsedOnPlayerTrigger;
import com.legobmw99.allomancy.modules.extras.block.IronButtonBlock;
import com.legobmw99.allomancy.modules.extras.block.IronLeverBlock;
import net.minecraft.advancements.CriterionTrigger;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
Expand All @@ -19,6 +22,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

public class ExtrasSetup {
public static final DeferredRegister.Blocks BLOCKS = DeferredRegister.createBlocks(Allomancy.MODID);
Expand Down Expand Up @@ -51,9 +55,15 @@ public class ExtrasSetup {
}
}

private static final DeferredRegister<CriterionTrigger<?>> CT = DeferredRegister.create(Registries.TRIGGER_TYPE, Allomancy.MODID);
public static final Supplier<MetalUsedOnEntityTrigger> METAL_USED_ON_ENTITY_TRIGGER = CT.register("metal_used_on_entity", MetalUsedOnEntityTrigger::new);
public static final Supplier<MetalUsedOnPlayerTrigger> METAL_USED_ON_PLAYER_TRIGGER = CT.register("metal_used_on_player", MetalUsedOnPlayerTrigger::new);

public static void register(IEventBus bus) {
BLOCKS.register(bus);
ITEMS.register(bus);
BP.register(bus);
CT.register(bus);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.legobmw99.allomancy.modules.extras.advancement;

import com.legobmw99.allomancy.api.enums.Metal;
import com.legobmw99.allomancy.modules.extras.ExtrasSetup;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.advancements.Criterion;
import net.minecraft.advancements.critereon.ContextAwarePredicate;
import net.minecraft.advancements.critereon.EntityPredicate;
import net.minecraft.advancements.critereon.SimpleCriterionTrigger;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.ExtraCodecs;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.storage.loot.LootContext;

import java.util.Optional;

/**
* Triggered when a player affects a mob with Allomancy
* This currently only means: zinc, brass, iron, steel
*/
public class MetalUsedOnEntityTrigger extends SimpleCriterionTrigger<MetalUsedOnEntityTrigger.TriggerInstance> {
@Override
public Codec codec() {
return TriggerInstance.CODEC;
}

public void trigger(ServerPlayer player, Entity entity, Metal mt, boolean enhanced) {
LootContext lootcontext = EntityPredicate.createContext(player, entity);
this.trigger(player, p_48112_ -> p_48112_.matches(lootcontext, mt, enhanced));
}

public record TriggerInstance(Optional<ContextAwarePredicate> player, Optional<ContextAwarePredicate> entityPredicate, Metal mt,
Optional<Boolean> enhanced) implements SimpleCriterionTrigger.SimpleInstance {
public static final Codec<TriggerInstance> CODEC = RecordCodecBuilder.create(builder -> builder
.group(ExtraCodecs.strictOptionalField(EntityPredicate.ADVANCEMENT_CODEC, "player").forGetter(TriggerInstance::player),
ExtraCodecs.strictOptionalField(EntityPredicate.ADVANCEMENT_CODEC, "entity").forGetter(TriggerInstance::entityPredicate),
Metal.CODEC.fieldOf("metal").forGetter(TriggerInstance::mt), Codec.BOOL.optionalFieldOf("enhanced").forGetter(TriggerInstance::enhanced))
.apply(builder, TriggerInstance::new));

public static Criterion<TriggerInstance> instance(Optional<ContextAwarePredicate> player, Optional<ContextAwarePredicate> entityPredicate, Metal mt) {
return ExtrasSetup.METAL_USED_ON_ENTITY_TRIGGER.get().createCriterion(new TriggerInstance(player, entityPredicate, mt, Optional.empty()));
}

public static Criterion<TriggerInstance> instance(Optional<ContextAwarePredicate> player, Optional<ContextAwarePredicate> entityPredicate, Metal mt, boolean enhanced) {
return ExtrasSetup.METAL_USED_ON_ENTITY_TRIGGER.get().createCriterion(new TriggerInstance(player, entityPredicate, mt, Optional.of(enhanced)));
}

boolean matches(LootContext entity, Metal mt, boolean enhanced) {
return this.mt == mt && (this.entityPredicate.isEmpty() || this.entityPredicate.get().matches(entity)) &&
(this.enhanced().isEmpty() || this.enhanced().get() == enhanced);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.legobmw99.allomancy.modules.extras.advancement;

import com.legobmw99.allomancy.api.enums.Metal;
import com.legobmw99.allomancy.modules.extras.ExtrasSetup;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.advancements.Criterion;
import net.minecraft.advancements.critereon.ContextAwarePredicate;
import net.minecraft.advancements.critereon.EntityPredicate;
import net.minecraft.advancements.critereon.SimpleCriterionTrigger;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.ExtraCodecs;

import java.util.Optional;

/**
* Triggered when a player is affected by Allomancy from another player
* This currently only means: chromium, nicrosil, iron, steel
*/
public class MetalUsedOnPlayerTrigger extends SimpleCriterionTrigger<MetalUsedOnPlayerTrigger.TriggerInstance> {
@Override
public Codec codec() {
return TriggerInstance.CODEC;
}

public void trigger(ServerPlayer player, Metal mt, boolean enhanced) {
this.trigger(player, p_48112_ -> p_48112_.matches(mt, enhanced));
}

public record TriggerInstance(Optional<ContextAwarePredicate> player, Metal mt, Optional<Boolean> enhanced) implements SimpleInstance {
public static final Codec<TriggerInstance> CODEC = RecordCodecBuilder.create(builder -> builder
.group(ExtraCodecs.strictOptionalField(EntityPredicate.ADVANCEMENT_CODEC, "player").forGetter(TriggerInstance::player),
Metal.CODEC.fieldOf("metal").forGetter(TriggerInstance::mt), Codec.BOOL.optionalFieldOf("enhanced").forGetter(TriggerInstance::enhanced))
.apply(builder, TriggerInstance::new));


public static Criterion<TriggerInstance> instance(Optional<ContextAwarePredicate> player, Metal mt) {
return ExtrasSetup.METAL_USED_ON_PLAYER_TRIGGER.get().createCriterion(new TriggerInstance(player, mt, Optional.empty()));
}

public static Criterion<TriggerInstance> instance(Optional<ContextAwarePredicate> player, Metal mt, boolean enhanced) {
return ExtrasSetup.METAL_USED_ON_PLAYER_TRIGGER.get().createCriterion(new TriggerInstance(player, mt, Optional.of(enhanced)));
}


boolean matches(Metal mt, boolean enhanced) {
return this.mt == mt && (this.enhanced().isEmpty() || this.enhanced().get() == enhanced);
}


}
}
Loading

0 comments on commit aa89c06

Please sign in to comment.