-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
87 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 18 additions & 19 deletions
37
common/src/main/kotlin/juuxel/adorn/criterion/BoughtFromTradingStationCriterion.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,38 @@ | ||
package juuxel.adorn.criterion | ||
|
||
import com.google.gson.JsonNull | ||
import com.google.gson.JsonObject | ||
import com.mojang.serialization.Codec | ||
import com.mojang.serialization.codecs.RecordCodecBuilder | ||
import net.minecraft.advancement.criterion.AbstractCriterion | ||
import net.minecraft.advancement.criterion.AbstractCriterionConditions | ||
import net.minecraft.item.ItemStack | ||
import net.minecraft.predicate.entity.AdvancementEntityPredicateDeserializer | ||
import net.minecraft.predicate.entity.EntityPredicate | ||
import net.minecraft.predicate.entity.LootContextPredicate | ||
import net.minecraft.predicate.item.ItemPredicate | ||
import net.minecraft.server.network.ServerPlayerEntity | ||
import net.minecraft.util.dynamic.Codecs | ||
import java.util.Optional | ||
|
||
class BoughtFromTradingStationCriterion : AbstractCriterion<BoughtFromTradingStationCriterion.Conditions>() { | ||
override fun conditionsFromJson( | ||
json: JsonObject, | ||
playerPredicate: Optional<LootContextPredicate>, | ||
predicateDeserializer: AdvancementEntityPredicateDeserializer | ||
): Conditions = Conditions( | ||
playerPredicate, | ||
ItemPredicate.fromJson(json["item"]).orElse(null) | ||
) | ||
override fun getConditionsCodec(): Codec<Conditions> = Conditions.CODEC | ||
|
||
fun trigger(player: ServerPlayerEntity, soldItem: ItemStack) { | ||
trigger(player) { it.matches(soldItem) } | ||
} | ||
|
||
class Conditions(playerPredicate: Optional<LootContextPredicate>, private val soldItem: ItemPredicate?) : | ||
AbstractCriterionConditions(playerPredicate) { | ||
data class Conditions(val playerPredicate: Optional<LootContextPredicate>, val soldItem: Optional<ItemPredicate>) : AbstractCriterion.Conditions { | ||
override fun player(): Optional<LootContextPredicate> = playerPredicate | ||
|
||
fun matches(stack: ItemStack): Boolean = | ||
soldItem == null || soldItem.test(stack) | ||
soldItem.map { it.test(stack) }.orElse(true) | ||
|
||
override fun toJson(): JsonObject { | ||
val json = super.toJson() | ||
json.add("sold_item", soldItem?.toJson() ?: JsonNull.INSTANCE) | ||
return json | ||
companion object { | ||
val CODEC: Codec<Conditions> = RecordCodecBuilder.create { instance -> | ||
instance.group( | ||
Codecs.createStrictOptionalFieldCodec(EntityPredicate.LOOT_CONTEXT_PREDICATE_CODEC, "player") | ||
.forGetter(Conditions::playerPredicate), | ||
Codecs.createStrictOptionalFieldCodec(ItemPredicate.CODEC, "item") | ||
.forGetter(Conditions::soldItem) | ||
).apply(instance, ::Conditions) | ||
} | ||
} | ||
} | ||
} |
41 changes: 15 additions & 26 deletions
41
common/src/main/kotlin/juuxel/adorn/criterion/SitOnBlockCriterion.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,37 @@ | ||
package juuxel.adorn.criterion | ||
|
||
import com.google.gson.JsonObject | ||
import com.google.gson.JsonParseException | ||
import com.mojang.serialization.JsonOps | ||
import juuxel.adorn.util.logger | ||
import com.mojang.serialization.Codec | ||
import com.mojang.serialization.codecs.RecordCodecBuilder | ||
import net.minecraft.advancement.criterion.AbstractCriterion | ||
import net.minecraft.advancement.criterion.AbstractCriterionConditions | ||
import net.minecraft.predicate.BlockPredicate | ||
import net.minecraft.predicate.entity.AdvancementEntityPredicateDeserializer | ||
import net.minecraft.predicate.entity.EntityPredicate | ||
import net.minecraft.predicate.entity.LootContextPredicate | ||
import net.minecraft.server.network.ServerPlayerEntity | ||
import net.minecraft.util.Util | ||
import net.minecraft.util.dynamic.Codecs | ||
import net.minecraft.util.math.BlockPos | ||
import java.util.Optional | ||
|
||
class SitOnBlockCriterion : AbstractCriterion<SitOnBlockCriterion.Conditions>() { | ||
override fun conditionsFromJson( | ||
json: JsonObject, | ||
playerPredicate: Optional<LootContextPredicate>, | ||
predicateDeserializer: AdvancementEntityPredicateDeserializer | ||
): Conditions = Conditions(playerPredicate, Util.getResult(BlockPredicate.CODEC.parse(JsonOps.INSTANCE, json["block"]), ::JsonParseException)) | ||
override fun getConditionsCodec(): Codec<Conditions> = Conditions.CODEC | ||
|
||
fun trigger(player: ServerPlayerEntity, pos: BlockPos) { | ||
trigger(player) { it.matches(player, pos) } | ||
} | ||
|
||
companion object { | ||
private val LOGGER = logger() | ||
} | ||
data class Conditions(val playerPredicate: Optional<LootContextPredicate>, val block: BlockPredicate) : AbstractCriterion.Conditions { | ||
override fun player(): Optional<LootContextPredicate> = playerPredicate | ||
|
||
class Conditions(playerPredicate: Optional<LootContextPredicate>, private val block: BlockPredicate) : | ||
AbstractCriterionConditions(playerPredicate) { | ||
fun matches(player: ServerPlayerEntity, pos: BlockPos): Boolean = | ||
block.test(player.serverWorld, pos) | ||
|
||
override fun toJson(): JsonObject { | ||
val json = super.toJson() | ||
json.add( | ||
"block", | ||
BlockPredicate.CODEC.encodeStart(JsonOps.INSTANCE, block).getOrThrow(false) { | ||
LOGGER.error("Could not encode block predicate {}: {}", block, it) | ||
} | ||
) | ||
return json | ||
companion object { | ||
val CODEC: Codec<Conditions> = RecordCodecBuilder.create { instance -> | ||
instance.group( | ||
Codecs.createStrictOptionalFieldCodec(EntityPredicate.LOOT_CONTEXT_PREDICATE_CODEC, "player") | ||
.forGetter(Conditions::playerPredicate), | ||
BlockPredicate.CODEC.fieldOf("block").forGetter(Conditions::block) | ||
).apply(instance, ::Conditions) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.