Skip to content

Commit

Permalink
sequence temperature effect
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDeathlyCow committed Mar 18, 2024
1 parent 73cc9a7 commit 7f3f9a3
Showing 7 changed files with 124 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.github.thedeathlycow.thermoo.api.temperature.effects;

import com.google.gson.*;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.server.world.ServerWorld;

import java.util.ArrayList;
import java.util.List;

/**
* Applies multiple child temperature effects at once. Useful for when you want to apply several different temperature
* effects under the same base set of conditions, without the overhead of checking those conditions multiple times.
*/
public class SequenceTemperatureEffect extends TemperatureEffect<SequenceTemperatureEffect.Config> {

@Override
public void apply(LivingEntity victim, ServerWorld serverWorld, Config config) {
for (ConfiguredTemperatureEffect<?> child : config.children()) {
EntityType<?> childType = child.getEntityType();
if (childType == null || victim.getType() == childType) {
child.applyIfPossible(victim);
}
}
}

@Override
public boolean shouldApply(LivingEntity victim, Config config) {
return true;
}

@Override
public Config configFromJson(JsonElement json, JsonDeserializationContext context) throws JsonSyntaxException {
return Config.fromJson(json, context);
}

public record Config(List<ConfiguredTemperatureEffect<?>> children) {

public static Config fromJson(JsonElement json, JsonDeserializationContext context) throws JsonSyntaxException {
JsonObject object = json.getAsJsonObject();

JsonArray jsonChildren = object.get("children").getAsJsonArray();
List<ConfiguredTemperatureEffect<?>> children = new ArrayList<>(jsonChildren.size());
for (JsonElement jsonChild : jsonChildren) {
ConfiguredTemperatureEffect<?> child = context.deserialize(jsonChild, ConfiguredTemperatureEffect.class);
children.add(child);
}

return new Config(children);
}

}

}
Original file line number Diff line number Diff line change
@@ -20,14 +20,20 @@ public final class TemperatureEffects {
public static final TemperatureEffect<?> EMPTY = new EmptyTemperatureEffect();

/**
* Applies {@link net.minecraft.entity.effect.StatusEffect}s to entities based on their temperature
* A meta temperature effect that allows multiple child effects to be applied under the same conditions.
*/
public static final TemperatureEffect<?> STATUS_EFFECT = new StatusEffectTemperatureEffect();
public static final TemperatureEffect<?> SEQUENCE = new SequenceTemperatureEffect();

/**
* Applies scaled {@link net.minecraft.entity.attribute.EntityAttributeModifier}s to entities based on their
* Applies {@linkplain net.minecraft.entity.effect.StatusEffect status effects} to entities based on their
* temperature
*/
public static final TemperatureEffect<?> STATUS_EFFECT = new StatusEffectTemperatureEffect();

/**
* Applies scaled {@linkplain net.minecraft.entity.attribute.EntityAttributeModifier attribute modifiers} to
* entities based on their temperature
*/
public static final TemperatureEffect<?> SCALING_ATTRIBUTE_MODIFIER = new ScalingAttributeModifierTemperatureEffect();

/**
@@ -49,6 +55,7 @@ public final class TemperatureEffects {

/**
* Returns all currently loaded {@link ConfiguredTemperatureEffect}s that are mapped to the {@code entity}'s type.
*
* @param entity The entity to fetch the effects for
* @return Returns the effects loaded for the entity type
*/
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
import com.google.common.collect.Multimap;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.event.registry.DynamicRegistryView;
import net.fabricmc.fabric.api.item.v1.ModifyItemAttributeModifiersCallback;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.attribute.EntityAttribute;
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@ public class ThermooCommonRegisters {
@SuppressWarnings("deprecation")
public static void registerTemperatureEffects() {
registerTemperatureEffect("empty", TemperatureEffects.EMPTY);
registerTemperatureEffect("sequence", TemperatureEffects.SEQUENCE);
registerTemperatureEffect("status_effect", TemperatureEffects.STATUS_EFFECT);
registerTemperatureEffect("scaling_attribute_modifier", TemperatureEffects.SCALING_ATTRIBUTE_MODIFIER);
registerTemperatureEffect("damage", TemperatureEffects.DAMAGE);
11 changes: 11 additions & 0 deletions src/testmod/resources/data/thermoo-test/predicates/is_cold.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"condition": "minecraft:entity_properties",
"entity": "this",
"predicate": {
"thermoo.temperature": {
"scale": {
"max": 0
}
}
}
}
11 changes: 11 additions & 0 deletions src/testmod/resources/data/thermoo-test/predicates/is_warm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"condition": "minecraft:entity_properties",
"entity": "this",
"predicate": {
"thermoo.temperature": {
"scale": {
"min": 0
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"type": "thermoo:sequence",
"entity": {
"condition": "minecraft:reference",
"name": "thermoo-test:is_warm"
},
"config": {
"children": [
{
"type": "thermoo:damage",
"entity_type": "minecraft:player",
"config": {
"amount": 1,
"damage_interval": 20,
"damage_type": "minecraft:in_fire"
}
},
{
"type": "thermoo:status_effect",
"entity": {
"condition": "minecraft:reference",
"name": "thermoo-test:is_cold"
},
"config": {
"effects": [
{
"amplifier": 0,
"duration": 200,
"effect": "minecraft:nausea"
}
]
}
}
]
}
}

0 comments on commit 7f3f9a3

Please sign in to comment.