ActuallyAdditions/src/main/java/ellpeck/actuallyadditions/tile/TileEntityXPSolidifier.java

152 lines
5.1 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
* This file ("TileEntityXPSolidifier.java") is part of the Actually Additions Mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
* http://github.com/Ellpeck/ActuallyAdditions/blob/master/README.md
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2015-11-02 20:55:19 +01:00
* © 2015 Ellpeck
2015-08-29 14:33:25 +02:00
*/
2015-07-13 23:44:33 +02:00
package ellpeck.actuallyadditions.tile;
import ellpeck.actuallyadditions.items.InitItems;
import ellpeck.actuallyadditions.items.ItemSpecialDrop;
import ellpeck.actuallyadditions.items.metalists.TheSpecialDrops;
import ellpeck.actuallyadditions.network.gui.IButtonReactor;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
2015-07-13 23:44:33 +02:00
public class TileEntityXPSolidifier extends TileEntityInventoryBase implements IButtonReactor{
public short amount;
private short lastAmount;
2015-10-03 10:16:18 +02:00
private int[] buttonAmounts = new int[]{1, 5, 10, 20, 30, 40, 50, 64, -999};
2015-07-13 23:44:33 +02:00
public TileEntityXPSolidifier(){
super(1, "xpSolidifier");
2015-07-13 23:44:33 +02:00
}
2015-12-01 19:48:09 +01:00
@Override
public void writeSyncableNBT(NBTTagCompound compound, boolean sync){
super.writeSyncableNBT(compound, sync);
compound.setShort("Amount", this.amount);
}
@Override
public void readSyncableNBT(NBTTagCompound compound, boolean sync){
super.readSyncableNBT(compound, sync);
this.amount = compound.getShort("Amount");
}
2015-07-13 23:44:33 +02:00
@Override
public void updateEntity(){
super.updateEntity();
if(!worldObj.isRemote){
if(this.amount > 0){
if(this.slots[0] == null){
int toSet = this.amount > 64 ? 64 : this.amount;
this.slots[0] = new ItemStack(InitItems.itemSpecialDrop, toSet, TheSpecialDrops.SOLIDIFIED_EXPERIENCE.ordinal());
this.amount -= toSet;
}
else if(this.slots[0].stackSize < 64){
int needed = 64-this.slots[0].stackSize;
int toAdd = this.amount > needed ? needed : this.amount;
this.slots[0].stackSize += toAdd;
this.amount -= toAdd;
}
}
if(this.lastAmount != this.amount && this.sendUpdateWithInterval()){
this.lastAmount = this.amount;
}
}
}
2015-07-13 23:44:33 +02:00
@Override
public boolean isItemValidForSlot(int i, ItemStack stack){
return false;
}
@Override
public boolean canInsertItem(int slot, ItemStack stack, int side){
return this.isItemValidForSlot(slot, stack);
}
@Override
public boolean canExtractItem(int slot, ItemStack stack, int side){
return true;
}
@Override
public void onButtonPressed(int buttonID, EntityPlayer player){
if(buttonID < this.buttonAmounts.length){
if(this.getPlayerXP(player) > 0){
2015-07-15 09:28:01 +02:00
int xp = this.buttonAmounts[buttonID] == -999 ? this.getPlayerXP(player)/ItemSpecialDrop.SOLID_XP_AMOUNT : this.buttonAmounts[buttonID];
if(this.amount < Short.MAX_VALUE-xp && this.getPlayerXP(player) >= ItemSpecialDrop.SOLID_XP_AMOUNT*xp){
this.addPlayerXP(player, -(ItemSpecialDrop.SOLID_XP_AMOUNT*xp));
2015-10-03 10:16:18 +02:00
if(!worldObj.isRemote){
this.amount += xp;
}
2015-07-15 01:53:04 +02:00
}
}
2015-07-13 23:44:33 +02:00
}
}
/**
* Gets the Player's XP
* (Excerpted from OpenBlocks' XP system with permission, thanks guys!)
2015-10-02 16:48:01 +02:00
*
* @param player The Player
* @return The XP
*/
2015-07-13 23:44:33 +02:00
private int getPlayerXP(EntityPlayer player){
return (int)(this.getExperienceForLevel(player.experienceLevel)+(player.experience*player.xpBarCap()));
}
/**
* Adds (or removes, if negative) a certain amount of XP from a player
* (Excerpted from OpenBlocks' XP system with permission, thanks guys!)
2015-10-02 16:48:01 +02:00
*
* @param player The Player
* @param amount The Amount
*/
2015-07-13 23:44:33 +02:00
private void addPlayerXP(EntityPlayer player, int amount){
int experience = getPlayerXP(player)+amount;
player.experienceTotal = experience;
int level = 0;
while(getExperienceForLevel(level) <= experience){
level++;
}
player.experienceLevel = level-1;
int expForLevel = this.getExperienceForLevel(player.experienceLevel);
player.experience = (float)(experience-expForLevel)/(float)player.xpBarCap();
}
/**
* Gets the amount of experience a certain level contains
* (Excerpted from OpenBlocks' XP system with permission, thanks guys!)
2015-10-02 16:48:01 +02:00
*
* @param level The Level in question
* @return The total XP the level has
*/
2015-07-13 23:44:33 +02:00
private int getExperienceForLevel(int level){
if(level > 0){
2015-10-02 16:48:01 +02:00
if(level > 0 && level < 16){
return level*17;
}
else if(level > 15 && level < 31){
return (int)(1.5*Math.pow(level, 2)-29.5*level+360);
}
else{
return (int)(3.5*Math.pow(level, 2)-151.5*level+2220);
}
2015-07-13 23:44:33 +02:00
}
return 0;
}
}