2018-11-08 13:50:45 +01:00
|
|
|
package de.ellpeck.naturesaura.items;
|
|
|
|
|
2019-11-04 19:08:49 +01:00
|
|
|
import de.ellpeck.naturesaura.NaturesAura;
|
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;
|
2020-01-29 01:34:58 +01:00
|
|
|
import de.ellpeck.naturesaura.data.ItemModelGenerator;
|
2018-11-12 01:29:33 +01:00
|
|
|
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;
|
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.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;
|
2018-11-16 13:06:33 +01:00
|
|
|
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) {
|
2019-11-04 19:08:49 +01:00
|
|
|
super("aura_bottle", new Properties().group(NaturesAura.CREATIVE_TAB));
|
2020-01-24 20:49:09 +01:00
|
|
|
MinecraftForge.EVENT_BUS.register(new EventHandler());
|
2018-11-16 13:06:33 +01:00
|
|
|
|
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);
|
2018-11-16 13:06:33 +01:00
|
|
|
|
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));
|
2018-11-16 13:59:07 +01:00
|
|
|
|
2020-01-22 01:32:26 +01:00
|
|
|
BlockPos spot = IAuraChunk.getHighestSpot(world, offset, 30, offset);
|
|
|
|
IAuraChunk.getAuraChunk(world, spot).drainAura(spot, 20000);
|
2018-11-16 13:06:33 +01:00
|
|
|
}
|
2020-01-22 01:32:26 +01:00
|
|
|
}
|
2018-11-16 13:06:33 +01:00
|
|
|
|
2020-01-22 01:32:26 +01:00
|
|
|
doDispense(world, dispense, 6, facing, DispenserBlock.getDispensePosition(source));
|
|
|
|
return stack;
|
2018-11-16 13:06:33 +01:00
|
|
|
});
|
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)) {
|
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
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2020-01-24 20:49:09 +01:00
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2020-01-24 20:49:09 +01:00
|
|
|
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)));
|
2020-01-24 20:49:09 +01:00
|
|
|
|
|
|
|
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(),
|
2020-01-24 20:49:09 +01:00
|
|
|
SoundEvents.ITEM_BOTTLE_FILL_DRAGONBREATH, SoundCategory.PLAYERS, 1F, 1F);
|
|
|
|
}
|
|
|
|
player.swingArm(event.getHand());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-11-08 13:50:45 +01:00
|
|
|
}
|