|
| 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.loot.context.LootContext; |
| 11 | +import net.minecraft.loot.function.SetNameLootFunction; |
| 12 | +import net.minecraft.text.Text; |
| 13 | + |
| 14 | +public class SetCustomNameEntityModifier implements EntityModifier { |
| 15 | + private final Text customName; |
| 16 | + |
| 17 | + public SetCustomNameEntityModifier(Text customName) { |
| 18 | + this.customName = customName; |
| 19 | + } |
| 20 | + |
| 21 | + @Override |
| 22 | + public EntityModifierType getType() { |
| 23 | + return EntityModifierTypes.SET_CUSTOM_NAME; |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + public Entity apply(Entity entity, LootContext lootContext) { |
| 28 | + if (this.customName != null) { |
| 29 | + entity.setCustomName(SetNameLootFunction.applySourceEntity(lootContext, LootContext.EntityTarget.THIS).apply(this.customName)); |
| 30 | + } |
| 31 | + return entity; |
| 32 | + } |
| 33 | + |
| 34 | + public static class Serialiser implements EntityModifier.Serialiser<SetCustomNameEntityModifier> { |
| 35 | + @Override |
| 36 | + public void toJson(JsonObject json, SetCustomNameEntityModifier object, JsonSerializationContext context) { |
| 37 | + if (object.customName != null) { |
| 38 | + json.add("name", Text.Serializer.toJsonTree(object.customName)); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public SetCustomNameEntityModifier fromJson(JsonObject json, JsonDeserializationContext context) { |
| 44 | + Text customName = Text.Serializer.fromJson(json.get("name")); |
| 45 | + return new SetCustomNameEntityModifier(customName); |
| 46 | + } |
| 47 | + } |
| 48 | +} |
0 commit comments