mirror of
https://github.com/GTNewHorizons/Botania.git
synced 2026-11-25 22:44:44 +00:00
242 lines
6.3 KiB
Java
242 lines
6.3 KiB
Java
/*
|
|
* This class is distributed as part of the Botania Mod.
|
|
* Get the Source Code in github:
|
|
* https://github.com/Vazkii/Botania
|
|
*
|
|
* Botania is Open Source and distributed under the
|
|
* Botania License: http://botaniamod.net/license.php
|
|
*/
|
|
package vazkii.botania.common.item;
|
|
|
|
import net.minecraft.entity.Entity;
|
|
import net.minecraft.entity.player.PlayerEntity;
|
|
import net.minecraft.item.DyeColor;
|
|
import net.minecraft.item.Item;
|
|
import net.minecraft.item.ItemStack;
|
|
import net.minecraft.item.ItemUseContext;
|
|
import net.minecraft.server.MinecraftServer;
|
|
import net.minecraft.tileentity.TileEntity;
|
|
import net.minecraft.util.ActionResultType;
|
|
import net.minecraft.util.ResourceLocation;
|
|
import net.minecraft.util.SoundCategory;
|
|
import net.minecraft.util.math.BlockPos;
|
|
import net.minecraft.world.World;
|
|
import net.minecraft.world.dimension.DimensionType;
|
|
import net.minecraftforge.fml.server.ServerLifecycleHooks;
|
|
|
|
import vazkii.botania.api.mana.IManaItem;
|
|
import vazkii.botania.api.mana.IManaPool;
|
|
import vazkii.botania.api.mana.IManaTooltipDisplay;
|
|
import vazkii.botania.api.wand.ICoordBoundItem;
|
|
import vazkii.botania.common.block.tile.mana.TilePool;
|
|
import vazkii.botania.common.core.handler.ModSounds;
|
|
import vazkii.botania.common.core.helper.ItemNBTHelper;
|
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
public class ItemManaMirror extends Item implements IManaItem, ICoordBoundItem, IManaTooltipDisplay {
|
|
|
|
private static final String TAG_MANA = "mana";
|
|
private static final String TAG_MANA_BACKLOG = "manaBacklog";
|
|
|
|
private static final String TAG_POS_X = "posX";
|
|
private static final String TAG_POS_Y = "posY";
|
|
private static final String TAG_POS_Z = "posZ";
|
|
private static final String TAG_DIM = "dim";
|
|
|
|
private static final DummyPool fallbackPool = new DummyPool();
|
|
|
|
public ItemManaMirror(Properties props) {
|
|
super(props);
|
|
}
|
|
|
|
@Override
|
|
public boolean showDurabilityBar(ItemStack stack) {
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public double getDurabilityForDisplay(ItemStack stack) {
|
|
return 1 - getManaFractionForDisplay(stack);
|
|
}
|
|
|
|
@Override
|
|
public void inventoryTick(ItemStack stack, World world, Entity entity, int slot, boolean selected) {
|
|
if (world.isRemote) {
|
|
return;
|
|
}
|
|
|
|
IManaPool pool = getManaPool(stack);
|
|
if (!(pool instanceof DummyPool)) {
|
|
if (pool == null) {
|
|
setMana(stack, 0);
|
|
} else {
|
|
pool.receiveMana(getManaBacklog(stack));
|
|
setManaBacklog(stack, 0);
|
|
setMana(stack, pool.getCurrentMana());
|
|
}
|
|
}
|
|
}
|
|
|
|
@Nonnull
|
|
@Override
|
|
public ActionResultType onItemUse(ItemUseContext ctx) {
|
|
World world = ctx.getWorld();
|
|
PlayerEntity player = ctx.getPlayer();
|
|
|
|
if (player != null && player.isSneaking() && !world.isRemote) {
|
|
TileEntity tile = world.getTileEntity(ctx.getPos());
|
|
if (tile instanceof IManaPool) {
|
|
bindPool(ctx.getItem(), tile);
|
|
world.playSound(null, player.getX(), player.getY(), player.getZ(), ModSounds.ding, SoundCategory.PLAYERS, 1F, 1F);
|
|
return ActionResultType.SUCCESS;
|
|
}
|
|
}
|
|
|
|
return ActionResultType.PASS;
|
|
}
|
|
|
|
@Override
|
|
public int getMana(ItemStack stack) {
|
|
return ItemNBTHelper.getInt(stack, TAG_MANA, 0);
|
|
}
|
|
|
|
public void setMana(ItemStack stack, int mana) {
|
|
ItemNBTHelper.setInt(stack, TAG_MANA, Math.max(0, mana));
|
|
}
|
|
|
|
public int getManaBacklog(ItemStack stack) {
|
|
return ItemNBTHelper.getInt(stack, TAG_MANA_BACKLOG, 0);
|
|
}
|
|
|
|
public void setManaBacklog(ItemStack stack, int backlog) {
|
|
ItemNBTHelper.setInt(stack, TAG_MANA_BACKLOG, backlog);
|
|
}
|
|
|
|
@Override
|
|
public int getMaxMana(ItemStack stack) {
|
|
return TilePool.MAX_MANA;
|
|
}
|
|
|
|
@Override
|
|
public void addMana(ItemStack stack, int mana) {
|
|
setMana(stack, getMana(stack) + mana);
|
|
setManaBacklog(stack, getManaBacklog(stack) + mana);
|
|
}
|
|
|
|
public void bindPool(ItemStack stack, TileEntity pool) {
|
|
ItemNBTHelper.setInt(stack, TAG_POS_X, pool == null ? 0 : pool.getPos().getX());
|
|
ItemNBTHelper.setInt(stack, TAG_POS_Y, pool == null ? -1 : pool.getPos().getY());
|
|
ItemNBTHelper.setInt(stack, TAG_POS_Z, pool == null ? 0 : pool.getPos().getZ());
|
|
ItemNBTHelper.setString(stack, TAG_DIM, pool == null ? "" : DimensionType.getKey(pool.getWorld().getDimension().getType()).toString());
|
|
}
|
|
|
|
public BlockPos getPoolCoords(ItemStack stack) {
|
|
int x = ItemNBTHelper.getInt(stack, TAG_POS_X, 0);
|
|
int y = ItemNBTHelper.getInt(stack, TAG_POS_Y, -1);
|
|
int z = ItemNBTHelper.getInt(stack, TAG_POS_Z, 0);
|
|
return new BlockPos(x, y, z);
|
|
}
|
|
|
|
public String getDimension(ItemStack stack) {
|
|
return ItemNBTHelper.getString(stack, TAG_DIM, "");
|
|
}
|
|
|
|
public IManaPool getManaPool(ItemStack stack) {
|
|
MinecraftServer server = ServerLifecycleHooks.getCurrentServer();
|
|
if (server == null) {
|
|
return fallbackPool;
|
|
}
|
|
|
|
BlockPos coords = getPoolCoords(stack);
|
|
if (coords.getY() == -1) {
|
|
return null;
|
|
}
|
|
|
|
ResourceLocation dim = new ResourceLocation(getDimension(stack));
|
|
DimensionType type = DimensionType.byName(dim);
|
|
if (type != null) {
|
|
World world = server.getWorld(type);
|
|
TileEntity tile = world.getTileEntity(coords);
|
|
if (tile instanceof IManaPool) {
|
|
return (IManaPool) tile;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public boolean canReceiveManaFromPool(ItemStack stack, TileEntity pool) {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public boolean canReceiveManaFromItem(ItemStack stack, ItemStack otherStack) {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public boolean canExportManaToPool(ItemStack stack, TileEntity pool) {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public boolean canExportManaToItem(ItemStack stack, ItemStack otherStack) {
|
|
return true;
|
|
}
|
|
|
|
private static class DummyPool implements IManaPool {
|
|
|
|
@Override
|
|
public boolean isFull() {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public void receiveMana(int mana) {}
|
|
|
|
@Override
|
|
public boolean canReceiveManaFromBursts() {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public int getCurrentMana() {
|
|
return 0;
|
|
}
|
|
|
|
@Override
|
|
public boolean isOutputtingPower() {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public DyeColor getColor() {
|
|
return DyeColor.WHITE;
|
|
}
|
|
|
|
@Override
|
|
public void setColor(DyeColor color) {}
|
|
|
|
}
|
|
|
|
@Override
|
|
public boolean isNoExport(ItemStack stack) {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public BlockPos getBinding(ItemStack stack) {
|
|
IManaPool pool = getManaPool(stack);
|
|
|
|
return pool == null || pool instanceof DummyPool ? null : getPoolCoords(stack);
|
|
}
|
|
|
|
@Override
|
|
public float getManaFractionForDisplay(ItemStack stack) {
|
|
return (float) getMana(stack) / (float) getMaxMana(stack);
|
|
}
|
|
|
|
}
|