|
| 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 errorcraft.entitymodifiers.world.rotation.RotationProvider; |
| 10 | +import net.minecraft.entity.Entity; |
| 11 | +import net.minecraft.loot.context.LootContext; |
| 12 | +import net.minecraft.server.network.ServerPlayerEntity; |
| 13 | +import net.minecraft.util.JsonHelper; |
| 14 | +import net.minecraft.util.math.Vec2f; |
| 15 | + |
| 16 | +public class SetRotationEntityModifier implements EntityModifier { |
| 17 | + private final RotationProvider rotation; |
| 18 | + |
| 19 | + public SetRotationEntityModifier(RotationProvider rotation) { |
| 20 | + this.rotation = rotation; |
| 21 | + } |
| 22 | + |
| 23 | + @Override |
| 24 | + public EntityModifierType getType() { |
| 25 | + return EntityModifierTypes.SET_ROTATION; |
| 26 | + } |
| 27 | + |
| 28 | + @Override |
| 29 | + public Entity apply(Entity entity, LootContext lootContext) { |
| 30 | + Vec2f newRotation = this.rotation.getRotation(entity.getRotationClient(), lootContext); |
| 31 | + entity.refreshPositionAndAngles(entity.getX(), entity.getY(), entity.getZ(), newRotation.y, newRotation.x); |
| 32 | + entity.setHeadYaw(newRotation.y); |
| 33 | + if (entity instanceof ServerPlayerEntity player) { |
| 34 | + setPlayerRotation(player, newRotation); |
| 35 | + } |
| 36 | + return entity; |
| 37 | + } |
| 38 | + |
| 39 | + private static void setPlayerRotation(ServerPlayerEntity player, Vec2f rotation) { |
| 40 | + player.networkHandler.requestTeleport(player.getX(), player.getY(), player.getZ(), rotation.y, rotation.x); |
| 41 | + } |
| 42 | + |
| 43 | + public static class Serialiser implements EntityModifier.Serialiser<SetRotationEntityModifier> { |
| 44 | + @Override |
| 45 | + public void toJson(JsonObject json, SetRotationEntityModifier object, JsonSerializationContext context) { |
| 46 | + json.add("rotation", context.serialize(object.rotation)); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public SetRotationEntityModifier fromJson(JsonObject json, JsonDeserializationContext context) { |
| 51 | + RotationProvider rotation = JsonHelper.deserialize(json, "rotation", context, RotationProvider.class); |
| 52 | + return new SetRotationEntityModifier(rotation); |
| 53 | + } |
| 54 | + } |
| 55 | +} |
0 commit comments