-
Notifications
You must be signed in to change notification settings - Fork 754
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement conditional damage boost modifier serializer
- Loading branch information
1 parent
2c97314
commit 388adf7
Showing
16 changed files
with
240 additions
and
153 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
src/generated/resources/data/tconstruct/tinkering/modifiers/antiaquatic.json
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"type": "tconstruct:conditional_damage", | ||
"predicate": { | ||
"type": "tconstruct:mob_type", | ||
"mobs": "minecraft:water" | ||
}, | ||
"damage": 2.0 | ||
} |
21 changes: 21 additions & 0 deletions
21
src/generated/resources/data/tconstruct/tinkering/modifiers/bane_of_sssss.json
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"type": "tconstruct:conditional_damage", | ||
"predicate": { | ||
"type": "tconstruct:or", | ||
"predicates": [ | ||
{ | ||
"type": "tconstruct:mob_type", | ||
"mobs": "minecraft:arthropod" | ||
}, | ||
{ | ||
"type": "tconstruct:tag", | ||
"tag": "forge:creepers" | ||
} | ||
] | ||
}, | ||
"damage": 2.0, | ||
"effect": { | ||
"name": "minecraft:slowness", | ||
"level": 4 | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/generated/resources/data/tconstruct/tinkering/modifiers/cooling.json
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "tconstruct:conditional_damage", | ||
"predicate": { | ||
"type": "tconstruct:fire_immune" | ||
}, | ||
"damage": 1.6 | ||
} |
17 changes: 17 additions & 0 deletions
17
src/generated/resources/data/tconstruct/tinkering/modifiers/killager.json
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"type": "tconstruct:conditional_damage", | ||
"predicate": { | ||
"type": "tconstruct:or", | ||
"predicates": [ | ||
{ | ||
"type": "tconstruct:mob_type", | ||
"mobs": "minecraft:illager" | ||
}, | ||
{ | ||
"type": "tconstruct:tag", | ||
"tag": "forge:villagers" | ||
} | ||
] | ||
}, | ||
"damage": 2.0 | ||
} |
8 changes: 8 additions & 0 deletions
8
src/generated/resources/data/tconstruct/tinkering/modifiers/smite.json
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"type": "tconstruct:conditional_damage", | ||
"predicate": { | ||
"type": "tconstruct:mob_type", | ||
"mobs": "minecraft:undead" | ||
}, | ||
"damage": 2.0 | ||
} |
133 changes: 133 additions & 0 deletions
133
...ain/java/slimeknights/tconstruct/library/modifiers/dynamic/ConditionalDamageModifier.java
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 |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package slimeknights.tconstruct.library.modifiers.dynamic; | ||
|
||
import com.google.gson.JsonObject; | ||
import lombok.RequiredArgsConstructor; | ||
import net.minecraft.network.FriendlyByteBuf; | ||
import net.minecraft.network.chat.Component; | ||
import net.minecraft.util.GsonHelper; | ||
import net.minecraft.world.effect.MobEffect; | ||
import net.minecraft.world.effect.MobEffectInstance; | ||
import net.minecraft.world.entity.LivingEntity; | ||
import net.minecraft.world.entity.player.Player; | ||
import net.minecraft.world.item.TooltipFlag; | ||
import net.minecraftforge.registries.ForgeRegistries; | ||
import slimeknights.mantle.data.GenericLoaderRegistry.IGenericLoader; | ||
import slimeknights.tconstruct.library.json.predicate.IJsonPredicate; | ||
import slimeknights.tconstruct.library.json.predicate.entity.LivingEntityPredicate; | ||
import slimeknights.tconstruct.library.modifiers.Modifier; | ||
import slimeknights.tconstruct.library.modifiers.impl.IncrementalModifier; | ||
import slimeknights.tconstruct.library.tools.context.ToolAttackContext; | ||
import slimeknights.tconstruct.library.tools.nbt.IToolStackView; | ||
import slimeknights.tconstruct.library.tools.stat.ToolStats; | ||
import slimeknights.tconstruct.library.utils.JsonUtils; | ||
import slimeknights.tconstruct.library.utils.TooltipKey; | ||
|
||
import javax.annotation.Nullable; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
/** Modifier that boosts damage against a conditional mob, applying an optional potion effect */ | ||
@RequiredArgsConstructor | ||
public class ConditionalDamageModifier extends IncrementalModifier { | ||
/** Requirement for entities to match */ | ||
private final IJsonPredicate<LivingEntity> predicate; | ||
/** Damage bonus */ | ||
private final float damage; | ||
/** Optional effect to add */ | ||
@Nullable | ||
private final MobEffect effect; | ||
/** Optional effect level */ | ||
private final int effectLevel; | ||
|
||
public ConditionalDamageModifier(IJsonPredicate<LivingEntity> predicate, float damage) { | ||
this(predicate, damage, null, 0); | ||
} | ||
|
||
@Override | ||
public float getEntityDamage(IToolStackView tool, int level, ToolAttackContext context, float baseDamage, float damage) { | ||
LivingEntity target = context.getLivingTarget(); | ||
if (target != null && predicate.matches(target)) { | ||
damage += getScaledLevel(tool, level) * damage * tool.getMultiplier(ToolStats.ATTACK_DAMAGE); | ||
} | ||
return damage; | ||
} | ||
|
||
@Override | ||
public void addInformation(IToolStackView tool, int level, @Nullable Player player, List<Component> tooltip, TooltipKey tooltipKey, TooltipFlag tooltipFlag) { | ||
addDamageTooltip(tool, level, damage, tooltip); | ||
} | ||
|
||
@Override | ||
public int afterEntityHit(IToolStackView tool, int level, ToolAttackContext context, float damageDealt) { | ||
if (effect != null) { | ||
LivingEntity target = context.getLivingTarget(); | ||
if (target != null && predicate.matches(target)) { | ||
int duration = 20; | ||
int maxBonus = (int)(10 * getScaledLevel(tool, level)); | ||
if (maxBonus > 0) { | ||
duration += context.getAttacker().getRandom().nextInt(maxBonus); | ||
} | ||
target.addEffect(new MobEffectInstance(effect, duration, effectLevel - 1)); | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
@Override | ||
public IGenericLoader<? extends Modifier> getLoader() { | ||
return LOADER; | ||
} | ||
|
||
/** Loader for this modifier */ | ||
public static final IGenericLoader<ConditionalDamageModifier> LOADER = new IGenericLoader<>() { | ||
@Override | ||
public ConditionalDamageModifier deserialize(JsonObject json) { | ||
IJsonPredicate<LivingEntity> predicate = LivingEntityPredicate.LOADER.getAndDeserialize(json, "predicate"); | ||
float damage = GsonHelper.getAsFloat(json, "damage"); | ||
MobEffect effect = null; | ||
int level = 0; | ||
if (json.has("effect")) { | ||
JsonObject effectJson = GsonHelper.getAsJsonObject(json, "effect"); | ||
effect = JsonUtils.getAsEntry(ForgeRegistries.MOB_EFFECTS, effectJson, "name"); | ||
level = JsonUtils.getIntMin(effectJson, "level", 1); | ||
} | ||
return new ConditionalDamageModifier(predicate, damage, effect, level); | ||
} | ||
|
||
@Override | ||
public void serialize(ConditionalDamageModifier object, JsonObject json) { | ||
json.add("predicate", LivingEntityPredicate.LOADER.serialize(object.predicate)); | ||
json.addProperty("damage", object.damage); | ||
if (object.effect != null && object.effectLevel > 0) { | ||
JsonObject effectJson = new JsonObject(); | ||
effectJson.addProperty("name", Objects.requireNonNull(object.effect.getRegistryName()).toString()); | ||
effectJson.addProperty("level", object.effectLevel); | ||
json.add("effect", effectJson); | ||
} | ||
} | ||
|
||
@Override | ||
public ConditionalDamageModifier fromNetwork(FriendlyByteBuf buffer) { | ||
IJsonPredicate<LivingEntity> predicate = LivingEntityPredicate.LOADER.fromNetwork(buffer); | ||
float damage = buffer.readFloat(); | ||
MobEffect effect = null; | ||
int level = buffer.readVarInt(); | ||
if (level > 0) { | ||
effect = buffer.readRegistryIdUnsafe(ForgeRegistries.MOB_EFFECTS); | ||
} | ||
return new ConditionalDamageModifier(predicate, damage, effect, level); | ||
} | ||
|
||
@Override | ||
public void toNetwork(ConditionalDamageModifier object, FriendlyByteBuf buffer) { | ||
LivingEntityPredicate.LOADER.toNetwork(object.predicate, buffer); | ||
buffer.writeFloat(object.damage); | ||
if (object.effectLevel > 0 && object.effect != null) { | ||
buffer.writeVarInt(object.effectLevel); | ||
buffer.writeRegistryIdUnsafe(ForgeRegistries.MOB_EFFECTS, object.effect); | ||
} else { | ||
buffer.writeVarInt(0); | ||
} | ||
} | ||
}; | ||
} |
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
Oops, something went wrong.