/*
 * This file is part of Sponge, licensed under the MIT License (MIT).
 *
 * Copyright (c) SpongePowered <https://www.spongepowered.org>
 * Copyright (c) contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.spongepowered.common.data.processor.value.entity;

import net.minecraft.entity.player.EntityPlayer;
import org.spongepowered.api.data.DataTransactionResult;
import org.spongepowered.api.data.key.Keys;
import org.spongepowered.api.data.value.ValueContainer;
import org.spongepowered.api.data.value.immutable.ImmutableValue;
import org.spongepowered.api.data.value.mutable.MutableBoundedValue;
import org.spongepowered.common.data.processor.common.AbstractSpongeValueProcessor;
import org.spongepowered.common.data.processor.common.ExperienceHolderUtils;
import org.spongepowered.common.data.value.SpongeValueFactory;

import java.util.Optional;

public class TotalExperienceValueProcessor extends AbstractSpongeValueProcessor<EntityPlayer, Integer, MutableBoundedValue<Integer>> {

    public TotalExperienceValueProcessor() {
        super(EntityPlayer.class, Keys.TOTAL_EXPERIENCE);
    }

    @Override
    public DataTransactionResult removeFrom(ValueContainer<?> container) {
        return DataTransactionResult.failNoData();
    }

    @Override
    public MutableBoundedValue<Integer> constructValue(Integer defaultValue) {
        return SpongeValueFactory.boundedBuilder(Keys.TOTAL_EXPERIENCE)
            .defaultValue(0)
            .minimum(0)
            .maximum(Integer.MAX_VALUE)
            .actualValue(defaultValue)
            .build();
    }

    @Override
    protected boolean set(EntityPlayer container, Integer value) {
        int level = 0;
        for (int i = value; true; i -= ExperienceHolderUtils.getExpBetweenLevels(level)) {
            if (i - ExperienceHolderUtils.getExpBetweenLevels(level) <= 0) {
                container.experience = (float) i / ExperienceHolderUtils.getExpBetweenLevels(level);
                container.experienceLevel = level;
                break;
            }
            level++;
        }
        container.experienceTotal = value;
        return false;
    }

    @Override
    protected Optional<Integer> getVal(EntityPlayer container) {
        return Optional.of(container.experienceTotal);
    }

    @Override
    protected ImmutableValue<Integer> constructImmutableValue(Integer value) {
        return constructValue(value).asImmutable();
    }

}

@@ -2722,10 +2722,12 @@
 l = 
-0;
+-1;%0A
 %0A   
@@ -2735,167 +2735,963 @@
     
-for (int i = value; true; i -= ExperienceHolderUtils.getExpBetweenLevels(level)) %7B%0A            if (i - ExperienceHolderUtils.getExpBetweenLevels(level) %3C=
+int experienceForCurrentLevel;%0A        int experienceAtNextLevel = -1;%0A%0A        // We work iteratively to get the level. Remember, the level variable contains the CURRENT level and the method%0A        // calculates what we need to get to the NEXT level, so we work our way up, summing up all these intervals, until%0A        // we get an experience value that is larger than the value. This gives us our level.%0A        //%0A        // If the cumulative experience required for level+1 is still below that (or in the edge case, equal to) our%0A        // value, we need to go up a level. So, if the boundary is at 7 exp, and we have 7 exp, we need one more loop%0A        // to increment the level as we are at 100%25 and therefore should be at level+1.%0A        do %7B%0A            // We need this later.%0A            experienceForCurrentLevel = experienceAtNextLevel;%0A%0A            // Increment level, as we know we are at least that level (in the first instance -1 -%3E
  0)
- %7B
 %0A   
@@ -3703,46 +3703,102 @@
     
-    container.experience = (float) i /
+// and add the next amount of experience to the variable.%0A            experienceAtNextLevel +=
  Exp
@@ -3836,16 +3836,18 @@
 nLevels(
+++
 level);%0A
@@ -3858,118 +3858,518 @@
     
-        container.experienceLevel = level;%0A                break;%0A            %7D%0A            level++;%0A        %7D
+%7D while (experienceAtNextLevel %3C= value);%0A%0A        // Once we're here, we have the correct level. The experience is the decimal fraction that we are through the%0A        // current level. This is why we require the experienceForCurrentLevel variable, we need the difference between%0A        // the current value and the beginning of the level.%0A        container.experience = (float)(value - experienceForCurrentLevel) / ExperienceHolderUtils.getExpBetweenLevels(level);%0A        container.experienceLevel = level;
 %0A   
@@ -4427,12 +4427,11 @@
 urn 
-fals
+tru
 e;%0A 

Update the Total Experience value processor.