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

156 lines
5.2 KiB
Java
Raw Normal View History

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;
2015-07-13 23:44:33 +02:00
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing;
2015-07-13 23:44:33 +02:00
2016-05-19 20:05:12 +02:00
import javax.annotation.Nonnull;
public class TileEntityXPSolidifier extends TileEntityInventoryBase implements IButtonReactor{
public short amount;
private short lastAmount;
2016-05-19 20:05:12 +02:00
private final 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
}
@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();
2016-05-02 17:46:53 +02:00
if(!this.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.itemSolidifiedExperience, toSet);
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
2016-05-19 20:05:12 +02:00
public boolean isItemValidForSlot(int i, @Nonnull 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-05-19 20:05:12 +02:00
public boolean canInsertItem(int slot, @Nonnull ItemStack stack, @Nonnull EnumFacing side){
2016-02-01 17:49:55 +01:00
return this.isItemValidForSlot(slot, stack);
2015-12-19 10:30:39 +01:00
}
2015-07-13 23:44:33 +02:00
@Override
2016-05-19 20:05:12 +02:00
public boolean canExtractItem(int slot, @Nonnull ItemStack stack, @Nonnull EnumFacing side){
2015-07-13 23:44:33 +02:00
return true;
}
@Override
public void onButtonPressed(int buttonID, EntityPlayer player){
if(buttonID < this.buttonAmounts.length){
if(this.getPlayerXP(player) > 0){
int xp = this.buttonAmounts[buttonID] == -999 ? this.getPlayerXP(player)/ItemSolidifiedExperience.SOLID_XP_AMOUNT : this.buttonAmounts[buttonID];
if(this.amount < Short.MAX_VALUE-xp && this.getPlayerXP(player) >= ItemSolidifiedExperience.SOLID_XP_AMOUNT*xp){
this.addPlayerXP(player, -(ItemSolidifiedExperience.SOLID_XP_AMOUNT*xp));
2016-05-02 17:46:53 +02:00
if(!this.worldObj.isRemote){
2015-10-03 10:16:18 +02:00
this.amount += xp;
}
2015-07-15 01:53:04 +02:00
}
}
2015-07-13 23:44:33 +02:00
}
}
//TODO Fix XP System to fit points needed in 1.8 (OpenBlocks?)
2016-02-01 17:49:55 +01: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){
2016-05-02 17:46:53 +02:00
int experience = this.getPlayerXP(player)+amount;
2015-07-13 23:44:33 +02:00
player.experienceTotal = experience;
int level = 0;
2016-05-02 17:46:53 +02:00
while(this.getExperienceForLevel(level) <= experience){
2015-07-13 23:44:33 +02:00
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){
2016-05-17 15:59:12 +02:00
return (int)(1.5*(level*level)-29.5*level+360);
2015-10-02 16:48:01 +02:00
}
else{
2016-05-17 15:59:12 +02:00
return (int)(3.5*(level*level)-151.5*level+2220);
2015-10-02 16:48:01 +02:00
}
2015-07-13 23:44:33 +02:00
}
return 0;
}
}