NaturesAura/src/main/java/de/ellpeck/naturesaura/items/ItemAuraBottle.java

134 lines
5.4 KiB
Java
Raw Normal View History

2018-11-08 13:50:45 +01:00
package de.ellpeck.naturesaura.items;
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
2018-11-11 13:26:19 +01:00
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
import de.ellpeck.naturesaura.api.aura.type.IAuraType;
2020-01-29 01:34:58 +01:00
import de.ellpeck.naturesaura.data.ItemModelGenerator;
import de.ellpeck.naturesaura.reg.IColorProvidingItem;
2020-01-29 01:34:58 +01:00
import de.ellpeck.naturesaura.reg.ICustomItemModel;
2019-10-20 22:30:49 +02:00
import net.minecraft.block.BlockState;
2020-01-22 01:32:26 +01:00
import net.minecraft.block.DispenserBlock;
import net.minecraft.client.renderer.color.IItemColor;
2019-10-20 22:30:49 +02:00
import net.minecraft.entity.player.PlayerEntity;
2020-01-22 01:32:26 +01:00
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
2018-11-08 13:50:45 +01:00
import net.minecraft.item.ItemStack;
2020-01-22 01:32:26 +01:00
import net.minecraft.util.*;
2018-11-08 13:50:45 +01:00
import net.minecraft.util.math.BlockPos;
2019-11-04 19:08:49 +01:00
import net.minecraft.util.math.RayTraceContext;
2018-11-08 13:50:45 +01:00
import net.minecraft.util.math.RayTraceResult;
2019-11-04 19:08:49 +01:00
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.World;
2020-01-22 01:32:26 +01:00
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
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;
2018-11-08 13:50:45 +01:00
2019-11-04 19:08:49 +01:00
import static net.minecraft.dispenser.DefaultDispenseItemBehavior.doDispense;
2018-11-08 13:50:45 +01:00
2020-01-29 01:34:58 +01:00
public class ItemAuraBottle extends ItemImpl implements IColorProvidingItem, ICustomItemModel {
2019-11-04 19:08:49 +01:00
2020-01-26 01:41:49 +01:00
public ItemAuraBottle(Item emptyBottle) {
2020-02-07 23:50:16 +01:00
super("aura_bottle");
MinecraftForge.EVENT_BUS.register(new EventHandler());
2020-01-22 01:32:26 +01:00
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);
2020-01-22 01:32:26 +01:00
ItemStack dispense = stack.split(1);
if (offsetState.getBlock().isAir(offsetState, world, offset)) {
if (IAuraChunk.getAuraInArea(world, offset, 30) >= 100000) {
2020-01-26 01:41:49 +01:00
dispense = setType(new ItemStack(ItemAuraBottle.this), IAuraType.forWorld(world));
2020-01-22 01:32:26 +01:00
BlockPos spot = IAuraChunk.getHighestSpot(world, offset, 30, offset);
IAuraChunk.getAuraChunk(world, spot).drainAura(spot, 20000);
}
2020-01-22 01:32:26 +01:00
}
2020-01-22 01:32:26 +01:00
doDispense(world, dispense, 6, facing, DispenserBlock.getDispensePosition(source));
return stack;
});
2018-11-08 13:50:45 +01:00
}
2020-02-07 15:22:30 +01:00
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;
}
2018-11-08 13:50:45 +01:00
@Override
2019-11-04 19:08:49 +01:00
public void fillItemGroup(ItemGroup tab, NonNullList<ItemStack> items) {
if (this.isInGroup(tab)) {
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
2019-11-04 19:08:49 +01:00
public ITextComponent getDisplayName(ItemStack stack) {
2020-01-23 19:20:47 +01:00
return new TranslationTextComponent(stack.getTranslationKey() + "." + getType(stack).getName());
2018-11-08 13:50:45 +01:00
}
@Override
2019-10-20 22:30:49 +02:00
@OnlyIn(Dist.CLIENT)
public IItemColor getItemColor() {
return (stack, tintIndex) -> tintIndex > 0 ? getType(stack).getColor() : 0xFFFFFF;
}
2020-01-29 01:34:58 +01:00
@Override
public void generateCustomItemModel(ItemModelGenerator generator) {
generator.withExistingParent(this.getBaseName(), "item/generated")
.texture("layer0", "item/" + this.getBaseName())
.texture("layer1", "item/" + this.getBaseName() + "_overlay");
}
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(
2020-01-26 01:41:49 +01:00
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);
2020-01-28 18:08:56 +01:00
player.world.playSound(null, player.getPosX(), player.getPosY(), player.getPosZ(),
SoundEvents.ITEM_BOTTLE_FILL_DRAGONBREATH, SoundCategory.PLAYERS, 1F, 1F);
}
player.swingArm(event.getHand());
}
}
2018-11-08 13:50:45 +01:00
}