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;
|
2019-10-20 22:30:49 +02:00
|
|
|
import net.minecraft.block.DispenserBlock;
|
|
|
|
import net.minecraft.block.BlockState;
|
2018-11-12 01:29:33 +01:00
|
|
|
import net.minecraft.client.renderer.color.IItemColor;
|
2019-10-20 22:30:49 +02:00
|
|
|
import net.minecraft.item.ItemGroup;
|
|
|
|
import net.minecraft.dispenser.DefaultDispenseItemBehavior;
|
2018-11-16 13:06:33 +01:00
|
|
|
import net.minecraft.dispenser.IBlockSource;
|
2019-10-20 22:30:49 +02:00
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
|
|
|
import net.minecraft.util.SoundEvents;
|
2018-11-08 13:50:45 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
2019-10-20 22:30:49 +02:00
|
|
|
import net.minecraft.nbt.CompoundNBT;
|
|
|
|
import net.minecraft.util.Direction;
|
2018-11-08 13:50:45 +01:00
|
|
|
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;
|
2019-10-20 22:30:49 +02:00
|
|
|
import net.minecraft.client.resources.I18n;
|
2018-11-16 13:06:33 +01:00
|
|
|
import net.minecraft.world.World;
|
2018-11-08 13:50:45 +01:00
|
|
|
import net.minecraftforge.common.MinecraftForge;
|
|
|
|
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
|
2019-10-20 22:30:49 +02:00
|
|
|
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
|
|
|
import net.minecraftforge.api.distmarker.Dist;
|
|
|
|
import net.minecraftforge.api.distmarker.OnlyIn;
|
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);
|
2018-11-16 13:06:33 +01:00
|
|
|
|
2019-10-20 22:30:49 +02:00
|
|
|
DispenserBlock.DISPENSE_BEHAVIOR_REGISTRY.putObject(ModItems.BOTTLE_TWO, new DefaultDispenseItemBehavior() {
|
2018-11-16 13:06:33 +01:00
|
|
|
@Override
|
|
|
|
protected ItemStack dispenseStack(IBlockSource source, ItemStack stack) {
|
|
|
|
World world = source.getWorld();
|
2019-10-20 22:30:49 +02:00
|
|
|
BlockState state = source.getBlockState();
|
|
|
|
Direction facing = state.getValue(DispenserBlock.FACING);
|
2018-11-16 13:06:33 +01:00
|
|
|
BlockPos offset = source.getBlockPos().offset(facing);
|
2019-10-20 22:30:49 +02:00
|
|
|
BlockState offsetState = world.getBlockState(offset);
|
2018-11-16 13:06:33 +01:00
|
|
|
|
|
|
|
ItemStack dispense = stack.splitStack(1);
|
|
|
|
if (offsetState.getBlock().isAir(offsetState, world, offset)) {
|
2019-02-03 22:05:56 +01:00
|
|
|
if (IAuraChunk.getAuraInArea(world, offset, 30) >= 100000) {
|
2018-11-16 13:59:07 +01:00
|
|
|
dispense = setType(new ItemStack(ItemAuraBottle.this), IAuraType.forWorld(world));
|
|
|
|
|
|
|
|
BlockPos spot = IAuraChunk.getHighestSpot(world, offset, 30, offset);
|
2019-01-29 11:46:38 +01:00
|
|
|
IAuraChunk.getAuraChunk(world, spot).drainAura(spot, 20000);
|
2018-11-16 13:59:07 +01:00
|
|
|
}
|
2018-11-16 13:06:33 +01:00
|
|
|
}
|
|
|
|
|
2019-10-20 22:30:49 +02:00
|
|
|
doDispense(world, dispense, 6, facing, DispenserBlock.getDispensePosition(source));
|
2018-11-16 13:06:33 +01:00
|
|
|
return stack;
|
|
|
|
}
|
|
|
|
});
|
2018-11-08 13:50:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@SubscribeEvent
|
|
|
|
public void onRightClick(PlayerInteractEvent.RightClickItem event) {
|
|
|
|
ItemStack held = event.getItemStack();
|
2018-11-20 10:54:38 +01:00
|
|
|
if (held.isEmpty() || held.getItem() != ModItems.BOTTLE_TWO)
|
2018-11-08 13:50:45 +01:00
|
|
|
return;
|
2019-10-20 22:30:49 +02:00
|
|
|
PlayerEntity player = event.getEntityPlayer();
|
2018-11-08 13:50:45 +01:00
|
|
|
RayTraceResult ray = this.rayTrace(player.world, player, true);
|
|
|
|
if (ray != null && ray.typeOfHit == RayTraceResult.Type.BLOCK)
|
|
|
|
return;
|
|
|
|
BlockPos pos = player.getPosition();
|
2019-02-03 22:05:56 +01:00
|
|
|
if (IAuraChunk.getAuraInArea(player.world, pos, 30) < 100000)
|
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);
|
2019-01-29 11:46:38 +01:00
|
|
|
IAuraChunk.getAuraChunk(player.world, spot).drainAura(spot, 20000);
|
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
|
2019-10-20 22:30:49 +02:00
|
|
|
public void getSubItems(ItemGroup tab, NonNullList<ItemStack> items) {
|
2018-11-08 13:50:45 +01:00
|
|
|
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())
|
2019-10-20 22:30:49 +02:00
|
|
|
stack.setTagCompound(new CompoundNBT());
|
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
|
2019-10-20 22:30:49 +02:00
|
|
|
@OnlyIn(Dist.CLIENT)
|
2018-11-12 01:29:33 +01:00
|
|
|
public IItemColor getItemColor() {
|
2018-11-12 01:35:02 +01:00
|
|
|
return (stack, tintIndex) -> tintIndex > 0 ? getType(stack).getColor() : 0xFFFFFF;
|
2018-11-12 01:29:33 +01:00
|
|
|
}
|
2018-11-08 13:50:45 +01:00
|
|
|
}
|