2015-08-29 14:33:25 +02:00
|
|
|
/*
|
2016-05-16 22:52:27 +02:00
|
|
|
* This file ("TileEntityXPSolidifier.java") is part of the Actually Additions mod for Minecraft.
|
2015-08-29 14:33:25 +02:00
|
|
|
* It is created and owned by Ellpeck and distributed
|
|
|
|
* under the Actually Additions License to be found at
|
2016-05-16 22:52:27 +02:00
|
|
|
* http://ellpeck.de/actaddlicense
|
2015-08-29 14:33:25 +02:00
|
|
|
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
|
|
|
|
*
|
2016-05-16 22:54:42 +02:00
|
|
|
* © 2015-2016 Ellpeck
|
2015-08-29 14:33:25 +02:00
|
|
|
*/
|
|
|
|
|
2016-01-05 04:47:35 +01:00
|
|
|
package de.ellpeck.actuallyadditions.mod.tile;
|
2015-07-13 23:44:33 +02:00
|
|
|
|
|
|
|
|
2016-01-05 04:47:35 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.items.InitItems;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.items.ItemSolidifiedExperience;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.network.gui.IButtonReactor;
|
2016-11-07 17:48:02 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.ModUtil;
|
2016-11-16 16:59:00 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
|
2016-08-12 22:02:20 +02:00
|
|
|
import net.minecraft.entity.item.EntityXPOrb;
|
2015-07-13 23:44:33 +02:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.item.ItemStack;
|
2015-07-15 00:17:21 +02:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2016-08-12 22:02:20 +02:00
|
|
|
import net.minecraft.util.math.AxisAlignedBB;
|
|
|
|
|
|
|
|
import java.util.List;
|
2015-07-13 23:44:33 +02:00
|
|
|
|
2015-10-18 15:28:06 +02:00
|
|
|
public class TileEntityXPSolidifier extends TileEntityInventoryBase implements IButtonReactor{
|
2015-07-15 00:17:21 +02:00
|
|
|
|
2016-08-12 18:44:09 +02:00
|
|
|
private static final int[] XP_MAP = new int[256];
|
2016-06-17 23:50:38 +02:00
|
|
|
|
|
|
|
static{
|
|
|
|
for(int i = 0; i < XP_MAP.length; i++){
|
|
|
|
XP_MAP[i] = getExperienceForLevelImpl(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-01 00:39:35 +02:00
|
|
|
private final int[] buttonAmounts = new int[]{1, 5, 10, 20, 30, 40, 50, 64, -999};
|
2016-08-12 18:44:09 +02:00
|
|
|
public int amount;
|
|
|
|
private int lastAmount;
|
2016-08-12 22:02:20 +02:00
|
|
|
private int singlePointAmount;
|
2015-07-13 23:44:33 +02:00
|
|
|
|
|
|
|
public TileEntityXPSolidifier(){
|
2016-08-12 18:44:09 +02:00
|
|
|
super(2, "xpSolidifier");
|
2015-07-13 23:44:33 +02:00
|
|
|
}
|
|
|
|
|
2016-08-12 18:44:09 +02:00
|
|
|
/*
|
|
|
|
* The below methods were excerpted from EnderIO by SleepyTrousers with permission, thanks!
|
|
|
|
*/
|
|
|
|
|
2016-06-17 23:50:38 +02:00
|
|
|
public static int getExperienceForLevel(int level){
|
|
|
|
if(level >= 0 && level < XP_MAP.length){
|
|
|
|
return XP_MAP[level];
|
|
|
|
}
|
|
|
|
if(level >= 21863){
|
|
|
|
return Integer.MAX_VALUE;
|
|
|
|
}
|
|
|
|
return getExperienceForLevelImpl(level);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static int getExperienceForLevelImpl(int level){
|
|
|
|
int res = 0;
|
|
|
|
for(int i = 0; i < level; i++){
|
|
|
|
res += getXpBarCapacity(i);
|
|
|
|
if(res < 0){
|
|
|
|
return Integer.MAX_VALUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int getXpBarCapacity(int level){
|
|
|
|
if(level >= 30){
|
|
|
|
return 112+(level-30)*9;
|
|
|
|
}
|
|
|
|
else if(level >= 15){
|
|
|
|
return 37+(level-15)*5;
|
|
|
|
}
|
|
|
|
return 7+level*2;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int getLevelForExperience(int experience){
|
|
|
|
for(int i = 0; i < XP_MAP.length; i++){
|
|
|
|
if(XP_MAP[i] > experience){
|
|
|
|
return i-1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int i = XP_MAP.length;
|
|
|
|
while(getExperienceForLevel(i) <= experience){
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return i-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-02-01 20:32:49 +01:00
|
|
|
@Override
|
2016-07-02 15:01:46 +02:00
|
|
|
public void writeSyncableNBT(NBTTagCompound compound, NBTType type){
|
|
|
|
super.writeSyncableNBT(compound, type);
|
2016-08-12 18:44:09 +02:00
|
|
|
compound.setInteger("Amount", this.amount);
|
2016-08-12 22:02:20 +02:00
|
|
|
compound.setInteger("SinglePointAmount", this.singlePointAmount);
|
2016-02-01 20:32:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-07-02 15:01:46 +02:00
|
|
|
public void readSyncableNBT(NBTTagCompound compound, NBTType type){
|
|
|
|
super.readSyncableNBT(compound, type);
|
2016-08-12 18:44:09 +02:00
|
|
|
this.amount = compound.getInteger("Amount");
|
2016-08-12 22:02:20 +02:00
|
|
|
this.singlePointAmount = compound.getInteger("SinglePointAmount");
|
2016-02-01 20:32:49 +01:00
|
|
|
}
|
|
|
|
|
2015-07-13 23:44:33 +02:00
|
|
|
@Override
|
2015-07-15 00:17:21 +02:00
|
|
|
public void updateEntity(){
|
2015-11-18 23:11:24 +01:00
|
|
|
super.updateEntity();
|
2016-11-26 21:32:27 +01:00
|
|
|
if(!this.world.isRemote){
|
2015-07-15 00:17:21 +02:00
|
|
|
if(this.amount > 0){
|
2016-12-04 00:10:52 +01:00
|
|
|
if(!StackUtil.isValid(this.slots.getStackInSlot(0))){
|
2015-07-15 00:17:21 +02:00
|
|
|
int toSet = this.amount > 64 ? 64 : this.amount;
|
2016-12-04 00:10:52 +01:00
|
|
|
this.slots.setStackInSlot(0, new ItemStack(InitItems.itemSolidifiedExperience, toSet));
|
2015-07-15 00:17:21 +02:00
|
|
|
this.amount -= toSet;
|
2016-11-20 15:47:33 +01:00
|
|
|
this.markDirty();
|
2015-07-15 00:17:21 +02:00
|
|
|
}
|
2016-12-04 00:10:52 +01:00
|
|
|
else if(StackUtil.getStackSize(this.slots.getStackInSlot(0)) < 64){
|
|
|
|
int needed = 64-StackUtil.getStackSize(this.slots.getStackInSlot(0));
|
2015-07-15 00:17:21 +02:00
|
|
|
int toAdd = this.amount > needed ? needed : this.amount;
|
2016-12-04 00:10:52 +01:00
|
|
|
this.slots.setStackInSlot(0, StackUtil.addStackSize(this.slots.getStackInSlot(0), toAdd));
|
2015-07-15 00:17:21 +02:00
|
|
|
this.amount -= toAdd;
|
2016-11-20 15:47:33 +01:00
|
|
|
this.markDirty();
|
2015-07-15 00:17:21 +02:00
|
|
|
}
|
|
|
|
}
|
2016-08-12 22:02:20 +02:00
|
|
|
|
|
|
|
if(!this.isRedstonePowered){
|
|
|
|
int range = 5;
|
2016-11-26 21:32:27 +01:00
|
|
|
List<EntityXPOrb> orbs = this.world.getEntitiesWithinAABB(EntityXPOrb.class, new AxisAlignedBB(this.pos.getX()-range, this.pos.getY()-range, this.pos.getZ()-range, this.pos.getX()+1+range, this.pos.getY()+1+range, this.pos.getZ()+1+range));
|
2016-08-12 22:02:20 +02:00
|
|
|
if(orbs != null && !orbs.isEmpty()){
|
|
|
|
for(EntityXPOrb orb : orbs){
|
2016-11-07 17:48:02 +01:00
|
|
|
if(orb != null && !orb.isDead && !orb.getEntityData().getBoolean(ModUtil.MOD_ID+"FromSolidified")){
|
2016-09-12 20:45:29 +02:00
|
|
|
this.singlePointAmount += orb.getXpValue();
|
2016-08-12 22:02:20 +02:00
|
|
|
orb.setDead();
|
|
|
|
|
|
|
|
if(this.singlePointAmount >= ItemSolidifiedExperience.SOLID_XP_AMOUNT){
|
|
|
|
this.amount += this.singlePointAmount/ItemSolidifiedExperience.SOLID_XP_AMOUNT;
|
|
|
|
this.singlePointAmount = 0;
|
2016-11-20 15:47:33 +01:00
|
|
|
this.markDirty();
|
2016-08-12 22:02:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-12 20:45:29 +02:00
|
|
|
|
2016-12-04 00:10:52 +01:00
|
|
|
if(StackUtil.isValid(this.slots.getStackInSlot(1)) && this.slots.getStackInSlot(1).getItem() instanceof ItemSolidifiedExperience){
|
|
|
|
this.amount += StackUtil.getStackSize(this.slots.getStackInSlot(1));
|
|
|
|
this.slots.setStackInSlot(1, StackUtil.getNull());
|
2016-11-20 15:47:33 +01:00
|
|
|
this.markDirty();
|
2016-08-12 18:44:09 +02:00
|
|
|
}
|
2015-07-15 00:17:21 +02:00
|
|
|
|
2015-12-01 17:28:50 +01:00
|
|
|
if(this.lastAmount != this.amount && this.sendUpdateWithInterval()){
|
2015-07-15 00:17:21 +02:00
|
|
|
this.lastAmount = this.amount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-13 23:44:33 +02:00
|
|
|
@Override
|
2016-05-29 23:49:35 +02:00
|
|
|
public boolean isItemValidForSlot(int i, ItemStack stack){
|
2016-02-01 17:49:55 +01:00
|
|
|
return false;
|
2015-07-13 23:44:33 +02:00
|
|
|
}
|
|
|
|
|
2015-12-19 10:30:39 +01:00
|
|
|
@Override
|
2016-12-04 00:10:52 +01:00
|
|
|
public boolean canExtractItem(int slot, ItemStack stack){
|
2015-07-13 23:44:33 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onButtonPressed(int buttonID, EntityPlayer player){
|
2015-07-15 00:17:21 +02:00
|
|
|
if(buttonID < this.buttonAmounts.length){
|
2016-08-12 22:02:20 +02:00
|
|
|
int playerXP = getPlayerXP(player);
|
|
|
|
if(playerXP > 0){
|
|
|
|
int xp = this.buttonAmounts[buttonID] == -999 ? playerXP/ItemSolidifiedExperience.SOLID_XP_AMOUNT : this.buttonAmounts[buttonID];
|
|
|
|
if(this.amount < Integer.MAX_VALUE-xp && playerXP >= ItemSolidifiedExperience.SOLID_XP_AMOUNT*xp){
|
2016-06-15 18:37:54 +02:00
|
|
|
addPlayerXP(player, -(ItemSolidifiedExperience.SOLID_XP_AMOUNT*xp));
|
2016-11-26 21:32:27 +01:00
|
|
|
if(!this.world.isRemote){
|
2015-10-03 10:16:18 +02:00
|
|
|
this.amount += xp;
|
|
|
|
}
|
2015-07-15 01:53:04 +02:00
|
|
|
}
|
2015-07-15 00:17:21 +02:00
|
|
|
}
|
2015-07-13 23:44:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|