Skip to content

Commit

Permalink
Add possessor loot condition and assorted predicate
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyrofab committed Sep 25, 2022
1 parent 093efe2 commit bac7cb8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
25 changes: 21 additions & 4 deletions src/main/java/ladysnake/requiem/common/loot/HostLootCondition.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,27 @@
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import ladysnake.requiem.api.v1.possession.Possessable;
import ladysnake.requiem.api.v1.possession.PossessionComponent;
import net.minecraft.entity.LivingEntity;
import net.minecraft.loot.condition.LootCondition;
import net.minecraft.loot.condition.LootConditionType;
import net.minecraft.loot.context.LootContext;
import net.minecraft.loot.context.LootContextParameter;
import net.minecraft.loot.context.LootContextParameters;
import net.minecraft.predicate.entity.EntityPredicate;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.JsonHelper;
import net.minecraft.util.JsonSerializer;

import java.util.Set;

public class HostLootCondition implements LootCondition {
private final CheckedEntity checkedEntity;
private final LootContext.EntityTarget entity;
private final EntityPredicate predicate;

public HostLootCondition(LootContext.EntityTarget entity, EntityPredicate predicate) {
public HostLootCondition(CheckedEntity checkedEntity, LootContext.EntityTarget entity, EntityPredicate predicate) {
this.checkedEntity = checkedEntity;
this.entity = entity;
this.predicate = predicate;
}
Expand All @@ -72,14 +75,23 @@ public Set<LootContextParameter<?>> getRequiredParameters() {

@Override
public boolean test(LootContext lootContext) {
return lootContext.get(this.entity.getParameter()) instanceof ServerPlayerEntity player && predicate.test(
return lootContext.get(this.entity.getParameter()) instanceof LivingEntity e && predicate.test(
lootContext.getWorld(),
lootContext.get(LootContextParameters.ORIGIN),
PossessionComponent.getHost(player)
switch (checkedEntity) {
case HOST -> PossessionComponent.getHost(e);
case POSSESSOR -> ((Possessable) e).getPossessor();
}
);
}

public static class Serializer implements JsonSerializer<HostLootCondition> {
private final CheckedEntity checkedEntity;

public Serializer(CheckedEntity checkedEntity) {
this.checkedEntity = checkedEntity;
}

@Override
public void toJson(JsonObject jsonObject, HostLootCondition condition, JsonSerializationContext ctx) {
jsonObject.add("entity", ctx.serialize(condition.entity));
Expand All @@ -89,9 +101,14 @@ public void toJson(JsonObject jsonObject, HostLootCondition condition, JsonSeria
@Override
public HostLootCondition fromJson(JsonObject jsonObject, JsonDeserializationContext ctx) {
return new HostLootCondition(
checkedEntity,
JsonHelper.deserialize(jsonObject, "entity", ctx, LootContext.EntityTarget.class),
EntityPredicate.fromJson(jsonObject.get("predicate"))
);
}
}

public enum CheckedEntity {
HOST, POSSESSOR
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public final class RequiemLootTables {
builder -> builder.require(LootContextParameters.THIS_ENTITY).require(LootContextParameters.ORIGIN)
);
public static final LootConditionType BOUND_SHELL_CONDITION = new LootConditionType(new BoundShellLootCondition.Serializer());
public static final LootConditionType HOST_CONDITION = new LootConditionType(new HostLootCondition.Serializer());
public static final LootConditionType HOST_CONDITION = new LootConditionType(new HostLootCondition.Serializer(HostLootCondition.CheckedEntity.HOST));
public static final LootConditionType POSSESSOR_CONDITION = new LootConditionType(new HostLootCondition.Serializer(HostLootCondition.CheckedEntity.POSSESSOR));
public static final LootConditionType RIFT_MORTICIAN_CONDITION = new LootConditionType(new RiftMorticianLootCondition.Serializer());

private static final Pattern NETHER_CHEST = Pattern.compile("chests/.*nether.*");
Expand All @@ -75,6 +76,7 @@ public static void init() {
Registry.register(Registry.LOOT_CONDITION_TYPE, Requiem.id("rift_mortician"), RIFT_MORTICIAN_CONDITION);
Registry.register(Registry.LOOT_CONDITION_TYPE, Requiem.id("bound_shell"), BOUND_SHELL_CONDITION);
Registry.register(Registry.LOOT_CONDITION_TYPE, Requiem.id("host"), HOST_CONDITION);
Registry.register(Registry.LOOT_CONDITION_TYPE, Requiem.id("possessor"), POSSESSOR_CONDITION);

LootTableEvents.MODIFY.register((resourceManager, lootManager, identifier, fabricLootSupplierBuilder, lootTableSetter) -> {
if (NETHER_CHEST.matcher(identifier.getPath()).matches()) {
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/data/requiem/predicates/possessed.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"condition": "requiem:possessor",
"entity": "this",
"predicate": {}
}

0 comments on commit bac7cb8

Please sign in to comment.