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

192 lines
7.5 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
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 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
2018-08-10 05:04:07 +02:00
import java.util.List;
2018-05-10 11:38:58 +02:00
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
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;
2018-08-10 05:04:07 +02:00
import de.ellpeck.actuallyadditions.mod.util.ItemStackHandlerAA.IAcceptor;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
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;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.math.AxisAlignedBB;
2017-07-30 02:53:30 +02:00
import net.minecraft.util.math.MathHelper;
public class TileEntityXPSolidifier extends TileEntityInventoryBase implements IButtonReactor {
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++) {
2016-06-17 23:50:38 +02:00
XP_MAP[i] = getExperienceForLevelImpl(i);
}
}
private final int[] buttonAmounts = new int[] { 1, 5, 10, 20, 30, 40, 50, 64, -999 };
public int amount;
private int lastAmount;
private int singlePointAmount;
2015-07-13 23:44:33 +02:00
public TileEntityXPSolidifier() {
super(2, "xpSolidifier");
2015-07-13 23:44:33 +02:00
}
/*
* The below methods were excerpted from EnderIO by SleepyTrousers with permission, thanks!
*/
public static int getExperienceForLevel(int level) {
if (level >= 0 && level < XP_MAP.length) { return XP_MAP[level]; }
if (level >= 21863) { return Integer.MAX_VALUE; }
2016-06-17 23:50:38 +02:00
return getExperienceForLevelImpl(level);
}
private static int getExperienceForLevelImpl(int level) {
2016-06-17 23:50:38 +02:00
int res = 0;
for (int i = 0; i < level; i++) {
2016-06-17 23:50:38 +02:00
res += getXpBarCapacity(i);
if (res < 0) { return Integer.MAX_VALUE; }
2016-06-17 23:50:38 +02:00
}
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;
2016-06-17 23:50:38 +02:00
}
public static int getLevelForExperience(int experience) {
for (int i = 0; i < XP_MAP.length; i++) {
if (XP_MAP[i] > experience) { return i - 1; }
2016-06-17 23:50:38 +02:00
}
int i = XP_MAP.length;
while (getExperienceForLevel(i) <= experience) {
2016-06-17 23:50:38 +02:00
i++;
}
return i - 1;
2016-06-17 23:50:38 +02:00
}
public static int getPlayerXP(EntityPlayer player) {
2019-02-27 19:53:05 +01:00
return (int) (getExperienceForLevel(player.experienceLevel) + player.experience * player.xpBarCap());
2016-06-17 23:50:38 +02:00
}
public static void addPlayerXP(EntityPlayer player, int amount) {
int experience = Math.max(0, getPlayerXP(player) + amount);
2016-06-17 23:50:38 +02:00
player.experienceTotal = experience;
player.experienceLevel = getLevelForExperience(experience);
int expForLevel = getExperienceForLevel(player.experienceLevel);
player.experience = (float) (experience - expForLevel) / (float) player.xpBarCap();
2016-06-17 23:50:38 +02:00
}
@Override
public void writeSyncableNBT(NBTTagCompound compound, NBTType type) {
super.writeSyncableNBT(compound, type);
compound.setInteger("Amount", this.amount);
compound.setInteger("SinglePointAmount", this.singlePointAmount);
}
@Override
public void readSyncableNBT(NBTTagCompound compound, NBTType type) {
super.readSyncableNBT(compound, type);
this.amount = compound.getInteger("Amount");
this.singlePointAmount = compound.getInteger("SinglePointAmount");
}
2015-07-13 23:44:33 +02:00
@Override
public void updateEntity() {
super.updateEntity();
if (!this.world.isRemote) {
if (this.amount > 0) {
ItemStack stack = this.inv.getStackInSlot(0);
if (stack.isEmpty()) {
int toSet = this.amount > 64 ? 64 : this.amount;
this.inv.setStackInSlot(0, new ItemStack(InitItems.itemSolidifiedExperience, toSet));
this.amount -= toSet;
2016-11-20 15:47:33 +01:00
this.markDirty();
} else if (stack.getCount() < 64) {
int needed = 64 - stack.getCount();
2019-02-27 19:53:05 +01:00
int toAdd = Math.min(needed, this.amount);
stack.grow(toAdd);
this.amount -= toAdd;
2016-11-20 15:47:33 +01:00
this.markDirty();
}
}
if (!this.isRedstonePowered) {
int range = 5;
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));
if (orbs != null && !orbs.isEmpty()) {
for (EntityXPOrb orb : orbs) {
if (orb != null && !orb.isDead && !orb.getEntityData().getBoolean(ActuallyAdditions.MODID + "FromSolidified")) {
2016-09-12 20:45:29 +02:00
this.singlePointAmount += orb.getXpValue();
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-09-12 20:45:29 +02:00
ItemStack stack = this.inv.getStackInSlot(1);
if (StackUtil.isValid(stack) && stack.getItem() instanceof ItemSolidifiedExperience) {
2019-02-27 19:53:05 +01:00
int remainingSpace = MathHelper.clamp(Integer.MAX_VALUE - this.amount, 0, stack.getCount());
if (stack.getCount() >= remainingSpace && remainingSpace != 0) {
this.amount += remainingSpace;
stack.shrink(remainingSpace);
this.markDirty();
2017-07-30 02:53:30 +02:00
}
}
if (this.lastAmount != this.amount && this.sendUpdateWithInterval()) {
this.lastAmount = this.amount;
}
}
}
2015-07-13 23:44:33 +02:00
@Override
2018-08-10 05:04:07 +02:00
public IAcceptor getAcceptor() {
return (slot, stack, automation) -> slot == 1 && stack.getItem() == InitItems.itemSolidifiedExperience;
2015-07-13 23:44:33 +02:00
}
2017-07-30 02:53:30 +02:00
@Override
public void markDirty() {
2019-02-27 19:53:05 +01:00
if (this.amount < 0) this.amount = Integer.MAX_VALUE; //don't u go negative on me weird number
super.markDirty();
2017-07-30 02:53:30 +02:00
}
2015-07-13 23:44:33 +02:00
@Override
public void onButtonPressed(int buttonID, EntityPlayer player) {
if (buttonID < this.buttonAmounts.length) {
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) {
addPlayerXP(player, -(ItemSolidifiedExperience.SOLID_XP_AMOUNT * xp));
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-13 23:44:33 +02:00
}
}
}