2018-11-08 13:50:45 +01:00
|
|
|
package de.ellpeck.naturesaura.items;
|
|
|
|
|
2018-11-12 01:29:33 +01:00
|
|
|
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
|
2018-11-11 13:26:19 +01:00
|
|
|
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
|
2018-11-12 01:29:33 +01:00
|
|
|
import de.ellpeck.naturesaura.api.aura.type.IAuraType;
|
|
|
|
import de.ellpeck.naturesaura.reg.IColorProvidingItem;
|
|
|
|
import net.minecraft.client.renderer.color.IItemColor;
|
2018-11-08 13:50:45 +01:00
|
|
|
import net.minecraft.creativetab.CreativeTabs;
|
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
|
|
import net.minecraft.init.Items;
|
|
|
|
import net.minecraft.init.SoundEvents;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.util.NonNullList;
|
|
|
|
import net.minecraft.util.ResourceLocation;
|
|
|
|
import net.minecraft.util.SoundCategory;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
|
|
|
import net.minecraft.util.math.RayTraceResult;
|
|
|
|
import net.minecraft.util.text.translation.I18n;
|
|
|
|
import net.minecraftforge.common.MinecraftForge;
|
|
|
|
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
|
|
|
|
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
2018-11-12 01:29:33 +01:00
|
|
|
import net.minecraftforge.fml.relauncher.Side;
|
|
|
|
import net.minecraftforge.fml.relauncher.SideOnly;
|
2018-11-08 13:50:45 +01:00
|
|
|
|
2018-11-12 01:29:33 +01:00
|
|
|
public class ItemAuraBottle extends ItemImpl implements IColorProvidingItem {
|
2018-11-08 13:50:45 +01:00
|
|
|
|
|
|
|
public ItemAuraBottle() {
|
|
|
|
super("aura_bottle");
|
|
|
|
MinecraftForge.EVENT_BUS.register(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@SubscribeEvent
|
|
|
|
public void onRightClick(PlayerInteractEvent.RightClickItem event) {
|
|
|
|
ItemStack held = event.getItemStack();
|
|
|
|
if (held.isEmpty() || held.getItem() != Items.GLASS_BOTTLE)
|
|
|
|
return;
|
|
|
|
EntityPlayer player = event.getEntityPlayer();
|
|
|
|
RayTraceResult ray = this.rayTrace(player.world, player, true);
|
|
|
|
if (ray != null && ray.typeOfHit == RayTraceResult.Type.BLOCK)
|
|
|
|
return;
|
|
|
|
BlockPos pos = player.getPosition();
|
2018-11-11 13:26:19 +01:00
|
|
|
if (IAuraChunk.getAuraInArea(player.world, pos, 30) < 1000)
|
2018-11-08 13:50:45 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (!player.world.isRemote) {
|
|
|
|
held.shrink(1);
|
|
|
|
|
|
|
|
player.inventory.addItemStackToInventory(
|
2018-11-12 01:29:33 +01:00
|
|
|
setType(new ItemStack(this), IAuraType.forWorld(player.world)));
|
2018-11-08 13:50:45 +01:00
|
|
|
|
2018-11-11 13:26:19 +01:00
|
|
|
BlockPos spot = IAuraChunk.getHighestSpot(player.world, pos, 30, pos);
|
|
|
|
IAuraChunk.getAuraChunk(player.world, spot).drainAura(spot, 200);
|
2018-11-08 13:50:45 +01:00
|
|
|
|
|
|
|
player.world.playSound(null, player.posX, player.posY, player.posZ,
|
|
|
|
SoundEvents.ITEM_BOTTLE_FILL_DRAGONBREATH, SoundCategory.PLAYERS, 1F, 1F);
|
|
|
|
}
|
|
|
|
player.swingArm(event.getHand());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void getSubItems(CreativeTabs tab, NonNullList<ItemStack> items) {
|
|
|
|
if (this.isInCreativeTab(tab)) {
|
2018-11-12 01:29:33 +01:00
|
|
|
for (IAuraType type : NaturesAuraAPI.AURA_TYPES.values()) {
|
2018-11-08 13:50:45 +01:00
|
|
|
ItemStack stack = new ItemStack(this);
|
|
|
|
setType(stack, type);
|
|
|
|
items.add(stack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getItemStackDisplayName(ItemStack stack) {
|
2018-11-12 01:29:33 +01:00
|
|
|
return I18n.translateToLocal(this.getUnlocalizedNameInefficiently(stack) + "." + getType(stack).getName() + ".name").trim();
|
2018-11-08 13:50:45 +01:00
|
|
|
}
|
|
|
|
|
2018-11-12 01:29:33 +01:00
|
|
|
public static IAuraType getType(ItemStack stack) {
|
2018-11-08 13:50:45 +01:00
|
|
|
if (!stack.hasTagCompound())
|
2018-11-12 01:29:33 +01:00
|
|
|
return NaturesAuraAPI.TYPE_OTHER;
|
|
|
|
String type = stack.getTagCompound().getString("stored_type");
|
2018-11-08 18:03:58 +01:00
|
|
|
if (type.isEmpty())
|
2018-11-12 01:29:33 +01:00
|
|
|
return NaturesAuraAPI.TYPE_OTHER;
|
|
|
|
return NaturesAuraAPI.AURA_TYPES.get(new ResourceLocation(type));
|
2018-11-08 13:50:45 +01:00
|
|
|
}
|
|
|
|
|
2018-11-12 01:29:33 +01:00
|
|
|
public static ItemStack setType(ItemStack stack, IAuraType type) {
|
2018-11-08 13:50:45 +01:00
|
|
|
if (!stack.hasTagCompound())
|
|
|
|
stack.setTagCompound(new NBTTagCompound());
|
2018-11-12 01:29:33 +01:00
|
|
|
stack.getTagCompound().setString("stored_type", type.getName().toString());
|
2018-11-08 13:50:45 +01:00
|
|
|
return stack;
|
|
|
|
}
|
2018-11-12 01:29:33 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
@SideOnly(Side.CLIENT)
|
|
|
|
public IItemColor getItemColor() {
|
|
|
|
return (stack, tintIndex) -> tintIndex > 0 ? getType(stack).getBottledColor() : 0xFFFFFF;
|
|
|
|
}
|
2018-11-08 13:50:45 +01:00
|
|
|
}
|