2016-04-02 16:02:01 -04:00
|
|
|
package WayofTime.bloodmagic.item;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
2016-04-03 08:54:15 -04:00
|
|
|
import net.minecraft.init.SoundEvents;
|
2016-04-02 16:02:01 -04:00
|
|
|
import net.minecraft.item.Item;
|
|
|
|
import net.minecraft.item.ItemStack;
|
2016-04-02 16:42:04 -04:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2016-04-02 20:09:23 -04:00
|
|
|
import net.minecraft.util.ActionResult;
|
2016-04-02 16:02:01 -04:00
|
|
|
import net.minecraft.util.EnumActionResult;
|
|
|
|
import net.minecraft.util.EnumHand;
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
import net.minecraftforge.fml.relauncher.Side;
|
|
|
|
import net.minecraftforge.fml.relauncher.SideOnly;
|
|
|
|
|
|
|
|
import org.apache.commons.lang3.tuple.ImmutablePair;
|
|
|
|
import org.apache.commons.lang3.tuple.Pair;
|
|
|
|
|
|
|
|
import WayofTime.bloodmagic.BloodMagic;
|
|
|
|
import WayofTime.bloodmagic.api.Constants;
|
2016-04-02 16:42:04 -04:00
|
|
|
import WayofTime.bloodmagic.api.util.helper.NBTHelper;
|
2016-04-02 16:02:01 -04:00
|
|
|
import WayofTime.bloodmagic.client.IVariantProvider;
|
|
|
|
import WayofTime.bloodmagic.util.helper.TextHelper;
|
|
|
|
|
|
|
|
public class ItemExperienceBook extends Item implements IVariantProvider
|
|
|
|
{
|
|
|
|
public ItemExperienceBook()
|
|
|
|
{
|
|
|
|
setUnlocalizedName(Constants.Mod.MODID + ".experienceTome");
|
|
|
|
setMaxStackSize(1);
|
|
|
|
setCreativeTab(BloodMagic.tabBloodMagic);
|
|
|
|
}
|
|
|
|
|
2016-04-29 19:55:23 -04:00
|
|
|
@Override
|
|
|
|
@SideOnly(Side.CLIENT)
|
|
|
|
public boolean hasEffect(ItemStack stack)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-02 16:02:01 -04:00
|
|
|
@Override
|
|
|
|
@SideOnly(Side.CLIENT)
|
|
|
|
public void addInformation(ItemStack stack, EntityPlayer player, List<String> tooltip, boolean advanced)
|
|
|
|
{
|
|
|
|
tooltip.add(TextHelper.localizeEffect("tooltip.BloodMagic.experienceTome"));
|
2016-04-02 16:42:04 -04:00
|
|
|
|
2016-04-03 08:54:15 -04:00
|
|
|
double storedExp = getStoredExperience(stack);
|
|
|
|
|
|
|
|
tooltip.add(TextHelper.localizeEffect("tooltip.BloodMagic.experienceTome.exp", (int) storedExp));
|
|
|
|
|
|
|
|
tooltip.add(TextHelper.localizeEffect("tooltip.BloodMagic.experienceTome.expLevel", (int) getLevelForExperience(storedExp)));
|
2016-04-02 16:02:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-04-02 20:09:23 -04:00
|
|
|
public ActionResult<ItemStack> onItemRightClick(ItemStack stack, World world, EntityPlayer player, EnumHand hand)
|
2016-04-02 16:02:01 -04:00
|
|
|
{
|
2016-04-02 20:09:23 -04:00
|
|
|
if (!world.isRemote)
|
|
|
|
{
|
|
|
|
if (player.isSneaking())
|
|
|
|
absorbOneLevelExpFromPlayer(stack, player);
|
2016-04-03 08:54:15 -04:00
|
|
|
else
|
|
|
|
giveOneLevelExpToPlayer(stack, player);
|
2016-04-02 20:09:23 -04:00
|
|
|
}
|
2016-04-02 16:02:01 -04:00
|
|
|
|
2016-04-02 20:09:23 -04:00
|
|
|
return new ActionResult<ItemStack>(EnumActionResult.SUCCESS, stack);
|
2016-04-02 16:02:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<Pair<Integer, String>> getVariants()
|
|
|
|
{
|
|
|
|
List<Pair<Integer, String>> ret = new ArrayList<Pair<Integer, String>>();
|
|
|
|
ret.add(new ImmutablePair<Integer, String>(0, "type=experiencetome"));
|
|
|
|
return ret;
|
|
|
|
}
|
2016-04-02 16:42:04 -04:00
|
|
|
|
2016-04-03 08:54:15 -04:00
|
|
|
public void giveOneLevelExpToPlayer(ItemStack stack, EntityPlayer player)
|
|
|
|
{
|
|
|
|
float progress = player.experience;
|
|
|
|
int expToNext = getExperienceForNextLevel(player.experienceLevel);
|
|
|
|
|
2016-06-10 19:36:21 -04:00
|
|
|
int neededExp = (int) Math.ceil((1 - progress) * expToNext);
|
2016-04-03 08:54:15 -04:00
|
|
|
float containedExp = (float) getStoredExperience(stack);
|
|
|
|
|
2016-06-13 14:40:27 -04:00
|
|
|
System.out.println("Needed: " + neededExp + ", contained: " + containedExp + ", exp to next: " + expToNext);
|
|
|
|
|
2016-04-03 08:54:15 -04:00
|
|
|
if (containedExp >= neededExp)
|
|
|
|
{
|
|
|
|
setStoredExperience(stack, containedExp - neededExp);
|
2016-06-10 19:36:21 -04:00
|
|
|
addPlayerXP(player, neededExp);
|
2016-04-03 08:54:15 -04:00
|
|
|
|
|
|
|
if (player.experienceLevel % 5 == 0)
|
|
|
|
{
|
|
|
|
float f = player.experienceLevel > 30 ? 1.0F : (float) player.experienceLevel / 30.0F;
|
2016-04-24 10:06:28 -07:00
|
|
|
player.worldObj.playSound((EntityPlayer) null, player.posX, player.posY, player.posZ, SoundEvents.ENTITY_PLAYER_LEVELUP, player.getSoundCategory(), f * 0.75F, 1.0F);
|
2016-04-03 08:54:15 -04:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
setStoredExperience(stack, 0);
|
2016-06-10 19:36:21 -04:00
|
|
|
addPlayerXP(player, (int) containedExp);
|
2016-04-03 08:54:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-02 18:45:52 -04:00
|
|
|
public void absorbOneLevelExpFromPlayer(ItemStack stack, EntityPlayer player)
|
|
|
|
{
|
|
|
|
float progress = player.experience;
|
2016-06-10 19:36:21 -04:00
|
|
|
|
2016-04-02 18:45:52 -04:00
|
|
|
if (progress > 0)
|
|
|
|
{
|
2016-06-10 19:36:21 -04:00
|
|
|
int expDeduction = (int) getExperienceAcquiredToNext(player);
|
|
|
|
if (expDeduction > 0)
|
|
|
|
{
|
|
|
|
addPlayerXP(player, -expDeduction);
|
|
|
|
addExperience(stack, expDeduction);
|
|
|
|
}
|
|
|
|
} else if (progress == 0 && player.experienceLevel > 0)
|
2016-04-02 18:45:52 -04:00
|
|
|
{
|
2016-06-10 19:36:21 -04:00
|
|
|
int expDeduction = getExperienceForNextLevel(player.experienceLevel - 1);
|
|
|
|
addPlayerXP(player, -expDeduction);
|
2016-04-02 18:45:52 -04:00
|
|
|
addExperience(stack, expDeduction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-10 19:36:21 -04:00
|
|
|
// Credits to Ender IO for some of the experience code, although now modified slightly for my convenience.
|
|
|
|
public static int getPlayerXP(EntityPlayer player)
|
|
|
|
{
|
|
|
|
return (int) (getExperienceForLevel(player.experienceLevel) + (player.experience * player.xpBarCap()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void addPlayerXP(EntityPlayer player, int amount)
|
|
|
|
{
|
|
|
|
int experience = Math.max(0, getPlayerXP(player) + amount);
|
|
|
|
player.experienceTotal = experience;
|
|
|
|
player.experienceLevel = getLevelForExperience(experience);
|
|
|
|
int expForLevel = getExperienceForLevel(player.experienceLevel);
|
|
|
|
player.experience = (float) (experience - expForLevel) / (float) player.xpBarCap();
|
|
|
|
}
|
|
|
|
|
2016-04-02 16:42:04 -04:00
|
|
|
public static void setStoredExperience(ItemStack stack, double exp)
|
|
|
|
{
|
|
|
|
NBTHelper.checkNBT(stack);
|
|
|
|
|
|
|
|
NBTTagCompound tag = stack.getTagCompound();
|
|
|
|
|
|
|
|
tag.setDouble("experience", exp);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static double getStoredExperience(ItemStack stack)
|
|
|
|
{
|
|
|
|
NBTHelper.checkNBT(stack);
|
|
|
|
|
|
|
|
NBTTagCompound tag = stack.getTagCompound();
|
|
|
|
|
|
|
|
return tag.getDouble("experience");
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void addExperience(ItemStack stack, double exp)
|
|
|
|
{
|
|
|
|
setStoredExperience(stack, getStoredExperience(stack) + exp);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int getExperienceForNextLevel(int currentLevel)
|
|
|
|
{
|
2016-06-13 14:40:27 -04:00
|
|
|
if (currentLevel < 16)
|
2016-04-02 16:42:04 -04:00
|
|
|
{
|
|
|
|
return 2 * currentLevel + 7;
|
2016-06-13 14:40:27 -04:00
|
|
|
} else if (currentLevel < 31)
|
2016-04-02 16:42:04 -04:00
|
|
|
{
|
|
|
|
return 5 * currentLevel - 38;
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
return 9 * currentLevel - 158;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-10 19:36:21 -04:00
|
|
|
//TODO: Change to calculation form.
|
|
|
|
public static int getExperienceForLevel(int level)
|
|
|
|
{
|
|
|
|
if (level >= 21863)
|
|
|
|
{
|
|
|
|
return Integer.MAX_VALUE;
|
|
|
|
}
|
|
|
|
if (level == 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int res = 0;
|
|
|
|
for (int i = 0; i < level; i++)
|
|
|
|
{
|
|
|
|
res += getExperienceForNextLevel(i);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static double getExperienceAcquiredToNext(EntityPlayer player)
|
2016-04-02 16:42:04 -04:00
|
|
|
{
|
2016-06-10 19:36:21 -04:00
|
|
|
return player.experience * player.xpBarCap();
|
2016-04-02 16:42:04 -04:00
|
|
|
}
|
2016-04-03 08:54:15 -04:00
|
|
|
|
|
|
|
public static int getLevelForExperience(double exp)
|
|
|
|
{
|
|
|
|
if (exp <= 352)
|
|
|
|
{
|
|
|
|
return (int) Math.floor(solveParabola(1, 6, -exp));
|
|
|
|
} else if (exp <= 1507)
|
|
|
|
{
|
|
|
|
return (int) Math.floor(solveParabola(2.5, -40.5, 360 - exp));
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
return (int) Math.floor(solveParabola(4.5, -162.5, 2220 - exp));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static double solveParabola(double a, double b, double c)
|
|
|
|
{
|
|
|
|
return (-b + Math.sqrt(b * b - 4 * a * c)) / (2 * a);
|
|
|
|
}
|
2016-04-02 16:02:01 -04:00
|
|
|
}
|