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-05-10 11:38:58 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
|
2021-08-30 22:51:41 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.blocks.ActuallyBlocks;
|
2021-03-01 18:46:12 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.inventory.ContainerXPSolidifier;
|
2021-03-01 21:23:52 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.items.ActuallyItems;
|
2016-01-05 04:47:35 +01:00
|
|
|
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;
|
2016-11-16 16:59:00 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
|
2024-03-02 21:23:08 +01:00
|
|
|
import net.minecraft.core.BlockPos;
|
|
|
|
import net.minecraft.nbt.CompoundTag;
|
|
|
|
import net.minecraft.network.chat.Component;
|
|
|
|
import net.minecraft.network.chat.TextComponent;
|
|
|
|
import net.minecraft.util.Mth;
|
|
|
|
import net.minecraft.world.MenuProvider;
|
|
|
|
import net.minecraft.world.entity.ExperienceOrb;
|
|
|
|
import net.minecraft.world.entity.player.Inventory;
|
|
|
|
import net.minecraft.world.entity.player.Player;
|
|
|
|
import net.minecraft.world.inventory.AbstractContainerMenu;
|
|
|
|
import net.minecraft.world.item.ItemStack;
|
|
|
|
import net.minecraft.world.level.Level;
|
|
|
|
import net.minecraft.world.level.block.entity.BlockEntity;
|
|
|
|
import net.minecraft.world.level.block.state.BlockState;
|
|
|
|
import net.minecraft.world.phys.AABB;
|
2016-08-12 22:02:20 +02:00
|
|
|
|
2021-03-01 18:46:12 +01:00
|
|
|
import javax.annotation.Nullable;
|
2021-02-26 22:15:48 +01:00
|
|
|
import java.util.List;
|
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
public class TileEntityXPSolidifier extends TileEntityInventoryBase implements IButtonReactor, MenuProvider {
|
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
|
|
|
|
2018-06-23 01:39:30 +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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-26 22:15:48 +01: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
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
public TileEntityXPSolidifier(BlockPos pos, BlockState state) {
|
|
|
|
super(ActuallyBlocks.XP_SOLIDIFIER.getTileEntityType(), pos, state, 2);
|
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!
|
|
|
|
*/
|
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
public static int getExperienceForLevel(int level) {
|
2021-02-26 22:15:48 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
private static int getExperienceForLevelImpl(int level) {
|
2016-06-17 23:50:38 +02:00
|
|
|
int res = 0;
|
2018-06-23 01:39:30 +02:00
|
|
|
for (int i = 0; i < level; i++) {
|
2016-06-17 23:50:38 +02:00
|
|
|
res += getXpBarCapacity(i);
|
2021-02-26 22:15:48 +01:00
|
|
|
if (res < 0) {
|
|
|
|
return Integer.MAX_VALUE;
|
|
|
|
}
|
2016-06-17 23:50:38 +02:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
public static int getXpBarCapacity(int level) {
|
|
|
|
if (level >= 30) {
|
|
|
|
return 112 + (level - 30) * 9;
|
2021-02-26 22:15:48 +01:00
|
|
|
} else if (level >= 15) {
|
|
|
|
return 37 + (level - 15) * 5;
|
|
|
|
}
|
2018-06-23 01:39:30 +02:00
|
|
|
return 7 + level * 2;
|
2016-06-17 23:50:38 +02:00
|
|
|
}
|
|
|
|
|
2018-06-23 01:39:30 +02:00
|
|
|
public static int getLevelForExperience(int experience) {
|
|
|
|
for (int i = 0; i < XP_MAP.length; i++) {
|
2021-02-26 22:15:48 +01:00
|
|
|
if (XP_MAP[i] > experience) {
|
|
|
|
return i - 1;
|
|
|
|
}
|
2016-06-17 23:50:38 +02:00
|
|
|
}
|
|
|
|
int i = XP_MAP.length;
|
2018-06-23 01:39:30 +02:00
|
|
|
while (getExperienceForLevel(i) <= experience) {
|
2016-06-17 23:50:38 +02:00
|
|
|
i++;
|
|
|
|
}
|
2018-06-23 01:39:30 +02:00
|
|
|
return i - 1;
|
2016-06-17 23:50:38 +02:00
|
|
|
}
|
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
public static int getPlayerXP(Player player) {
|
2021-08-22 17:09:06 +02:00
|
|
|
return (int) (getExperienceForLevel(player.experienceLevel) + player.experienceProgress * player.getXpNeededForNextLevel());
|
2016-06-17 23:50:38 +02:00
|
|
|
}
|
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
public static void addPlayerXP(Player player, int amount) {
|
2018-06-23 01:39:30 +02:00
|
|
|
int experience = Math.max(0, getPlayerXP(player) + amount);
|
2021-08-22 17:09:06 +02:00
|
|
|
player.totalExperience = experience;
|
2016-06-17 23:50:38 +02:00
|
|
|
player.experienceLevel = getLevelForExperience(experience);
|
|
|
|
int expForLevel = getExperienceForLevel(player.experienceLevel);
|
2021-08-22 17:09:06 +02:00
|
|
|
player.experienceProgress = (float) (experience - expForLevel) / (float) player.getXpNeededForNextLevel();
|
2016-06-17 23:50:38 +02:00
|
|
|
}
|
|
|
|
|
2016-02-01 20:32:49 +01:00
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public void writeSyncableNBT(CompoundTag compound, NBTType type) {
|
2016-07-02 15:01:46 +02:00
|
|
|
super.writeSyncableNBT(compound, type);
|
2021-02-27 16:33:00 +01:00
|
|
|
compound.putInt("Amount", this.amount);
|
|
|
|
compound.putInt("SinglePointAmount", this.singlePointAmount);
|
2016-02-01 20:32:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public void readSyncableNBT(CompoundTag compound, NBTType type) {
|
2016-07-02 15:01:46 +02:00
|
|
|
super.readSyncableNBT(compound, type);
|
2021-02-27 16:33:00 +01:00
|
|
|
this.amount = compound.getInt("Amount");
|
|
|
|
this.singlePointAmount = compound.getInt("SinglePointAmount");
|
2016-02-01 20:32:49 +01:00
|
|
|
}
|
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
public static <T extends BlockEntity> void clientTick(Level level, BlockPos pos, BlockState state, T t) {
|
|
|
|
if (t instanceof TileEntityXPSolidifier tile) {
|
|
|
|
tile.clientTick();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static <T extends BlockEntity> void serverTick(Level level, BlockPos pos, BlockState state, T t) {
|
|
|
|
if (t instanceof TileEntityXPSolidifier tile) {
|
|
|
|
tile.serverTick();
|
|
|
|
|
|
|
|
if (tile.amount > 0) {
|
|
|
|
ItemStack stack = tile.inv.getStackInSlot(0);
|
2018-06-23 01:39:30 +02:00
|
|
|
if (stack.isEmpty()) {
|
2024-03-02 21:23:08 +01:00
|
|
|
int toSet = Math.min(tile.amount, 64);
|
|
|
|
tile.inv.setStackInSlot(0, new ItemStack(ActuallyItems.SOLIDIFIED_EXPERIENCE.get(), toSet));
|
|
|
|
tile.amount -= toSet;
|
|
|
|
tile.setChanged();
|
2018-06-23 01:39:30 +02:00
|
|
|
} else if (stack.getCount() < 64) {
|
|
|
|
int needed = 64 - stack.getCount();
|
2024-03-02 21:23:08 +01:00
|
|
|
int toAdd = Math.min(needed, tile.amount);
|
2018-06-23 01:39:30 +02:00
|
|
|
stack.grow(toAdd);
|
2024-03-02 21:23:08 +01:00
|
|
|
tile.amount -= toAdd;
|
|
|
|
tile.setChanged();
|
2015-07-15 00:17:21 +02:00
|
|
|
}
|
|
|
|
}
|
2016-08-12 22:02:20 +02:00
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
if (!tile.isRedstonePowered) {
|
2016-08-12 22:02:20 +02:00
|
|
|
int range = 5;
|
2024-03-02 21:23:08 +01:00
|
|
|
List<ExperienceOrb> orbs = level.getEntitiesOfClass(ExperienceOrb.class, new AABB(pos.getX() - range, pos.getY() - range, pos.getZ() - range, pos.getX() + 1 + range, pos.getY() + 1 + range, pos.getZ() + 1 + range));
|
2018-06-23 01:39:30 +02:00
|
|
|
if (orbs != null && !orbs.isEmpty()) {
|
2024-03-02 21:23:08 +01:00
|
|
|
for (ExperienceOrb orb : orbs) {
|
2021-02-27 16:33:00 +01:00
|
|
|
// TODO: [port] validate the getPersistentData is correct
|
|
|
|
if (orb != null && orb.isAlive() && !orb.getPersistentData().getBoolean(ActuallyAdditions.MODID + "FromSolidified")) {
|
2024-03-02 21:23:08 +01:00
|
|
|
tile.singlePointAmount += orb.getValue();
|
|
|
|
orb.discard();
|
2016-08-12 22:02:20 +02:00
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
if (tile.singlePointAmount >= ItemSolidifiedExperience.SOLID_XP_AMOUNT) {
|
|
|
|
tile.amount += tile.singlePointAmount / ItemSolidifiedExperience.SOLID_XP_AMOUNT;
|
|
|
|
tile.singlePointAmount = 0;
|
|
|
|
tile.setChanged();
|
2016-08-12 22:02:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-12 20:45:29 +02:00
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
ItemStack stack = tile.inv.getStackInSlot(1);
|
2018-06-23 01:39:30 +02:00
|
|
|
if (StackUtil.isValid(stack) && stack.getItem() instanceof ItemSolidifiedExperience) {
|
2024-03-02 21:23:08 +01:00
|
|
|
int remainingSpace = Mth.clamp(Integer.MAX_VALUE - tile.amount, 0, stack.getCount());
|
2018-06-23 01:39:30 +02:00
|
|
|
if (stack.getCount() >= remainingSpace && remainingSpace != 0) {
|
2024-03-02 21:23:08 +01:00
|
|
|
tile.amount += remainingSpace;
|
2018-06-23 01:39:30 +02:00
|
|
|
stack.shrink(remainingSpace);
|
2024-03-02 21:23:08 +01:00
|
|
|
tile.setChanged();
|
2017-07-30 02:53:30 +02:00
|
|
|
}
|
2016-08-12 18:44:09 +02:00
|
|
|
}
|
2015-07-15 00:17:21 +02:00
|
|
|
|
2024-03-02 21:23:08 +01:00
|
|
|
if (tile.lastAmount != tile.amount && tile.sendUpdateWithInterval()) {
|
|
|
|
tile.lastAmount = tile.amount;
|
2015-07-15 00:17:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-03-02 21:23:08 +01:00
|
|
|
|
2015-07-13 23:44:33 +02:00
|
|
|
@Override
|
2018-08-10 05:04:07 +02:00
|
|
|
public IAcceptor getAcceptor() {
|
2021-05-02 18:10:21 +02:00
|
|
|
return (slot, stack, automation) -> slot == 1 && stack.getItem() == ActuallyItems.SOLIDIFIED_EXPERIENCE.get();
|
2015-07-13 23:44:33 +02:00
|
|
|
}
|
2018-06-23 01:39:30 +02:00
|
|
|
|
2017-07-30 02:53:30 +02:00
|
|
|
@Override
|
2021-08-22 17:09:06 +02:00
|
|
|
public void setChanged() {
|
2021-02-26 22:15:48 +01:00
|
|
|
if (this.amount < 0) {
|
|
|
|
this.amount = Integer.MAX_VALUE; //don't u go negative on me weird number
|
|
|
|
}
|
2021-08-22 17:09:06 +02:00
|
|
|
super.setChanged();
|
2017-07-30 02:53:30 +02:00
|
|
|
}
|
2015-07-13 23:44:33 +02:00
|
|
|
|
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public void onButtonPressed(int buttonID, Player player) {
|
2018-06-23 01:39:30 +02:00
|
|
|
if (buttonID < this.buttonAmounts.length) {
|
2016-08-12 22:02:20 +02:00
|
|
|
int playerXP = getPlayerXP(player);
|
2018-06-23 01:39:30 +02:00
|
|
|
if (playerXP > 0) {
|
2021-02-26 22:15:48 +01:00
|
|
|
int xp = this.buttonAmounts[buttonID] == -999
|
|
|
|
? playerXP / ItemSolidifiedExperience.SOLID_XP_AMOUNT
|
|
|
|
: this.buttonAmounts[buttonID];
|
2018-06-23 01:39:30 +02:00
|
|
|
if (this.amount < Integer.MAX_VALUE - xp && playerXP >= ItemSolidifiedExperience.SOLID_XP_AMOUNT * xp) {
|
|
|
|
addPlayerXP(player, -(ItemSolidifiedExperience.SOLID_XP_AMOUNT * xp));
|
2021-08-22 17:09:06 +02:00
|
|
|
if (!this.level.isClientSide) {
|
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
|
|
|
}
|
|
|
|
}
|
2021-03-01 18:46:12 +01:00
|
|
|
|
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public Component getDisplayName() {
|
|
|
|
return TextComponent.EMPTY;
|
2021-03-01 18:46:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
2024-03-02 21:23:08 +01:00
|
|
|
public AbstractContainerMenu createMenu(int windowId, Inventory playerInventory, Player player) {
|
2021-03-01 18:46:12 +01:00
|
|
|
return new ContainerXPSolidifier(windowId, playerInventory, this);
|
|
|
|
}
|
2015-07-13 23:44:33 +02:00
|
|
|
}
|