NaturesAura/src/main/java/de/ellpeck/naturesaura/items/ItemAuraBottle.java
2020-01-28 18:08:56 +01:00

125 lines
5 KiB
Java

package de.ellpeck.naturesaura.items;
import de.ellpeck.naturesaura.NaturesAura;
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
import de.ellpeck.naturesaura.api.aura.type.IAuraType;
import de.ellpeck.naturesaura.reg.IColorProvidingItem;
import net.minecraft.block.BlockState;
import net.minecraft.block.DispenserBlock;
import net.minecraft.client.renderer.color.IItemColor;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
import net.minecraft.util.*;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceContext;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.World;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import static net.minecraft.dispenser.DefaultDispenseItemBehavior.doDispense;
public class ItemAuraBottle extends ItemImpl implements IColorProvidingItem {
public ItemAuraBottle(Item emptyBottle) {
super("aura_bottle", new Properties().group(NaturesAura.CREATIVE_TAB));
MinecraftForge.EVENT_BUS.register(new EventHandler());
DispenserBlock.registerDispenseBehavior(emptyBottle, (source, stack) -> {
World world = source.getWorld();
BlockState state = source.getBlockState();
Direction facing = state.get(DispenserBlock.FACING);
BlockPos offset = source.getBlockPos().offset(facing);
BlockState offsetState = world.getBlockState(offset);
ItemStack dispense = stack.split(1);
if (offsetState.getBlock().isAir(offsetState, world, offset)) {
if (IAuraChunk.getAuraInArea(world, offset, 30) >= 100000) {
dispense = setType(new ItemStack(ItemAuraBottle.this), IAuraType.forWorld(world));
BlockPos spot = IAuraChunk.getHighestSpot(world, offset, 30, offset);
IAuraChunk.getAuraChunk(world, spot).drainAura(spot, 20000);
}
}
doDispense(world, dispense, 6, facing, DispenserBlock.getDispensePosition(source));
return stack;
});
}
@Override
public void fillItemGroup(ItemGroup tab, NonNullList<ItemStack> items) {
if (this.isInGroup(tab)) {
for (IAuraType type : NaturesAuraAPI.AURA_TYPES.values()) {
ItemStack stack = new ItemStack(this);
setType(stack, type);
items.add(stack);
}
}
}
@Override
public ITextComponent getDisplayName(ItemStack stack) {
return new TranslationTextComponent(stack.getTranslationKey() + "." + getType(stack).getName());
}
public static IAuraType getType(ItemStack stack) {
if (!stack.hasTag())
return NaturesAuraAPI.TYPE_OTHER;
String type = stack.getTag().getString("stored_type");
if (type.isEmpty())
return NaturesAuraAPI.TYPE_OTHER;
return NaturesAuraAPI.AURA_TYPES.get(new ResourceLocation(type));
}
public static ItemStack setType(ItemStack stack, IAuraType type) {
stack.getOrCreateTag().putString("stored_type", type.getName().toString());
return stack;
}
@Override
@OnlyIn(Dist.CLIENT)
public IItemColor getItemColor() {
return (stack, tintIndex) -> tintIndex > 0 ? getType(stack).getColor() : 0xFFFFFF;
}
private class EventHandler {
@SubscribeEvent
public void onRightClick(PlayerInteractEvent.RightClickItem event) {
ItemStack held = event.getItemStack();
if (held.isEmpty() || held.getItem() != ModItems.BOTTLE_TWO_THE_REBOTTLING)
return;
PlayerEntity player = event.getPlayer();
RayTraceResult ray = rayTrace(player.world, player, RayTraceContext.FluidMode.NONE);
if (ray.getType() == RayTraceResult.Type.BLOCK)
return;
BlockPos pos = player.getPosition();
if (IAuraChunk.getAuraInArea(player.world, pos, 30) < 100000)
return;
if (!player.world.isRemote) {
held.shrink(1);
player.inventory.addItemStackToInventory(
setType(new ItemStack(ItemAuraBottle.this), IAuraType.forWorld(player.world)));
BlockPos spot = IAuraChunk.getHighestSpot(player.world, pos, 30, pos);
IAuraChunk.getAuraChunk(player.world, spot).drainAura(spot, 20000);
player.world.playSound(null, player.getPosX(), player.getPosY(), player.getPosZ(),
SoundEvents.ITEM_BOTTLE_FILL_DRAGONBREATH, SoundCategory.PLAYERS, 1F, 1F);
}
player.swingArm(event.getHand());
}
}
}