<commit_before>package net.wizardsoflua.lua.classes.entity;

import java.util.HashSet;
import java.util.Set;

import com.google.common.cache.Cache;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.scoreboard.Team;
import net.sandius.rembulan.ByteString;
import net.sandius.rembulan.Table;
import net.wizardsoflua.lua.Converters;
import net.wizardsoflua.lua.classes.DeclareLuaClass;
import net.wizardsoflua.lua.classes.ProxyCachingLuaClass;

@DeclareLuaClass(name = PlayerClass.METATABLE_NAME, superclassname = EntityClass.METATABLE_NAME)
public class PlayerClass
    extends ProxyCachingLuaClass<EntityPlayer, PlayerClass.Proxy<EntityPlayer>> {
  public static final String METATABLE_NAME = "Player";

  public PlayerClass() {
    add("putNbt", new UnsupportedFunction("putNbt", METATABLE_NAME));
  }

  @Override
  protected String getMetatableName() {
    return METATABLE_NAME;
  }

  @Override
  public Proxy<EntityPlayer> toLua(EntityPlayer delegate) {
    return new Proxy<>(getConverters(), getMetatable(), delegate);
  }

  public void replaceDelegate(EntityPlayerMP newPlayer) {
    Cache<EntityPlayer, Proxy<EntityPlayer>> cache = getCache();

    Set<EntityPlayer> found = new HashSet<>();
    for (EntityPlayer key : cache.asMap().keySet()) {
      if (key.getUniqueID().equals(newPlayer.getUniqueID())) {
        found.add(key);
      }
    }

    for (EntityPlayer entityPlayer : found) {
      Proxy<EntityPlayer> oldValue = cache.asMap().remove(entityPlayer);
      cache.put(newPlayer, oldValue);
      oldValue.setDelegate(newPlayer);
    }
  }

  public class Proxy<D extends EntityPlayer> extends EntityClass.EntityLivingBaseProxy<D> {
    public Proxy(Converters converters, Table metatable, D delegate) {
      super(converters, metatable, delegate);
      // Overwrite name, since player names can't be changed
      addReadOnly("name", this::getName);
      add("team", this::getTeam, this::setTeam);
    }

    @Override
    public void setRotationYaw(Object luaObj) {
      super.setRotationYaw(luaObj);
      if (delegate instanceof EntityPlayerMP) {
        ((EntityPlayerMP) delegate).connection.setPlayerLocation(delegate.posX, delegate.posY,
            delegate.posZ, delegate.rotationYaw, delegate.rotationPitch);
      }
    }

    @Override
    public void setRotationPitch(Object luaObj) {
      super.setRotationPitch(luaObj);
      if (delegate instanceof EntityPlayerMP) {
        ((EntityPlayerMP) delegate).connection.setPlayerLocation(delegate.posX, delegate.posY,
            delegate.posZ, delegate.rotationYaw, delegate.rotationPitch);
      }
    }

    public ByteString getTeam() {
      Team team = delegate.getTeam();
      if (team == null) {
        return null;
      }
      return ByteString.of(team.getRegisteredName());
    }

    public void setTeam(Object luaObj) {
      String teamName = getConverters().toJavaNullable(String.class, luaObj);
      if (teamName == null) {
        delegate.getWorldScoreboard().removePlayerFromTeams(delegate.getName());<commit_msg>Fix: Player.rotationYaw returns incorrect value<commit_after>package net.wizardsoflua.lua.classes.entity;

import java.util.HashSet;
import java.util.Set;

import com.google.common.cache.Cache;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.scoreboard.Team;
import net.minecraft.util.math.MathHelper;
import net.sandius.rembulan.ByteString;
import net.sandius.rembulan.Table;
import net.wizardsoflua.lua.Converters;
import net.wizardsoflua.lua.classes.DeclareLuaClass;
import net.wizardsoflua.lua.classes.ProxyCachingLuaClass;

@DeclareLuaClass(name = PlayerClass.METATABLE_NAME, superclassname = EntityClass.METATABLE_NAME)
public class PlayerClass
    extends ProxyCachingLuaClass<EntityPlayer, PlayerClass.Proxy<EntityPlayer>> {
  public static final String METATABLE_NAME = "Player";

  public PlayerClass() {
    add("putNbt", new UnsupportedFunction("putNbt", METATABLE_NAME));
  }

  @Override
  protected String getMetatableName() {
    return METATABLE_NAME;
  }

  @Override
  public Proxy<EntityPlayer> toLua(EntityPlayer delegate) {
    return new Proxy<>(getConverters(), getMetatable(), delegate);
  }

  public void replaceDelegate(EntityPlayerMP newPlayer) {
    Cache<EntityPlayer, Proxy<EntityPlayer>> cache = getCache();

    Set<EntityPlayer> found = new HashSet<>();
    for (EntityPlayer key : cache.asMap().keySet()) {
      if (key.getUniqueID().equals(newPlayer.getUniqueID())) {
        found.add(key);
      }
    }

    for (EntityPlayer entityPlayer : found) {
      Proxy<EntityPlayer> oldValue = cache.asMap().remove(entityPlayer);
      cache.put(newPlayer, oldValue);
      oldValue.setDelegate(newPlayer);
    }
  }

  public class Proxy<D extends EntityPlayer> extends EntityClass.EntityLivingBaseProxy<D> {
    public Proxy(Converters converters, Table metatable, D delegate) {
      super(converters, metatable, delegate);
      // Overwrite name, since player names can't be changed
      addReadOnly("name", this::getName);
      add("team", this::getTeam, this::setTeam);
    }

    @Override
    public float getRotationYaw() {
      float v = delegate.rotationYaw;
      return MathHelper.wrapDegrees(v);
    }
    
    @Override
    public void setRotationYaw(Object luaObj) {
      super.setRotationYaw(luaObj);
      if (delegate instanceof EntityPlayerMP) {
        ((EntityPlayerMP) delegate).connection.setPlayerLocation(delegate.posX, delegate.posY,
            delegate.posZ, delegate.rotationYaw, delegate.rotationPitch);
      }
    }

    @Override
    public void setRotationPitch(Object luaObj) {
      super.setRotationPitch(luaObj);
      if (delegate instanceof EntityPlayerMP) {
        ((EntityPlayerMP) delegate).connection.setPlayerLocation(delegate.posX, delegate.posY,
            delegate.posZ, delegate.rotationYaw, delegate.rotationPitch);
      }
    }
    
    @Override
    public void setRotationYawAndPitch(float yaw, float pitch) {
      super.setRotationYawAndPitch(yaw, pitch);
      if (delegate instanceof EntityPlayerMP) {
        ((EntityPlayerMP) delegate).connection.setPlayerLocation(delegate.posX, delegate.posY,
            delegate.posZ, delegate.rotationYaw, delegate.rotationPitch);
      }
    }

    public ByteString getTeam() {
      Team team = delegate.getTeam();
      if (team == null) {
        return null;
      }
      return ByteString.of(team.getRegisteredName());
    }

    public void setTeam(Object luaObj) {
      String teamName = getConverters().toJavaNullable(String.class, luaObj);
      if (teamName == null) {
        delegate.getWorldScoreboard().removePlayerFromTeams(delegate.getName());
{'fraction_non_alphanumeric': 0.10745387453874539, 'fraction_numerical': 0.0, 'mean_word_length': 3.9423778264040847, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2892657', 'n_tokens_mistral': 2044, 'n_tokens_neox': 2009, 'n_words': 389}