|
| 1 | +package errorcraft.entitymodifiers.entity.modifier.modifiers; |
| 2 | + |
| 3 | +import com.google.gson.JsonDeserializationContext; |
| 4 | +import com.google.gson.JsonObject; |
| 5 | +import com.google.gson.JsonSerializationContext; |
| 6 | +import errorcraft.entitymodifiers.entity.modifier.EntityModifier; |
| 7 | +import errorcraft.entitymodifiers.entity.modifier.EntityModifierType; |
| 8 | +import errorcraft.entitymodifiers.entity.modifier.EntityModifierTypes; |
| 9 | +import net.minecraft.entity.Entity; |
| 10 | +import net.minecraft.entity.LivingEntity; |
| 11 | +import net.minecraft.loot.context.LootContext; |
| 12 | +import net.minecraft.loot.provider.number.LootNumberProvider; |
| 13 | +import net.minecraft.util.JsonHelper; |
| 14 | + |
| 15 | +public class SetAbsorptionEntityModifier implements EntityModifier { |
| 16 | + private final LootNumberProvider absorptionProvider; |
| 17 | + private final boolean add; |
| 18 | + |
| 19 | + public SetAbsorptionEntityModifier(LootNumberProvider absorptionProvider, boolean add) { |
| 20 | + this.absorptionProvider = absorptionProvider; |
| 21 | + this.add = add; |
| 22 | + } |
| 23 | + |
| 24 | + @Override |
| 25 | + public EntityModifierType getType() { |
| 26 | + return EntityModifierTypes.SET_ABSORPTION; |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public Entity apply(Entity entity, LootContext lootContext) { |
| 31 | + if (entity instanceof LivingEntity livingEntity) { |
| 32 | + setAbsorption(livingEntity, lootContext); |
| 33 | + } |
| 34 | + return entity; |
| 35 | + } |
| 36 | + |
| 37 | + private void setAbsorption(LivingEntity livingEntity, LootContext lootContext) { |
| 38 | + float newAbsorption = this.add ? livingEntity.getAbsorptionAmount() : 0.0f; |
| 39 | + livingEntity.setAbsorptionAmount(newAbsorption + this.absorptionProvider.nextFloat(lootContext)); |
| 40 | + } |
| 41 | + |
| 42 | + public static class Serialiser implements EntityModifier.Serialiser<SetAbsorptionEntityModifier> { |
| 43 | + @Override |
| 44 | + public void toJson(JsonObject json, SetAbsorptionEntityModifier object, JsonSerializationContext context) { |
| 45 | + json.add("absorption", context.serialize(object.absorptionProvider)); |
| 46 | + json.addProperty("add", object.add); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public SetAbsorptionEntityModifier fromJson(JsonObject json, JsonDeserializationContext context) { |
| 51 | + LootNumberProvider absorptionProvider = JsonHelper.deserialize(json, "absorption", context, LootNumberProvider.class); |
| 52 | + boolean add = JsonHelper.getBoolean(json, "add", false); |
| 53 | + return new SetAbsorptionEntityModifier(absorptionProvider, add); |
| 54 | + } |
| 55 | + } |
| 56 | +} |
0 commit comments