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;
|
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;
|
2021-12-04 19:17:21 +01:00
|
|
|
import net.minecraft.client.color.item.ItemColor;
|
|
|
|
import net.minecraft.core.NonNullList;
|
|
|
|
import net.minecraft.core.dispenser.DefaultDispenseItemBehavior;
|
|
|
|
import net.minecraft.network.chat.Component;
|
|
|
|
import net.minecraft.network.chat.TranslatableComponent;
|
|
|
|
import net.minecraft.resources.ResourceLocation;
|
|
|
|
import net.minecraft.sounds.SoundEvents;
|
|
|
|
import net.minecraft.sounds.SoundSource;
|
|
|
|
import net.minecraft.world.item.CreativeModeTab;
|
|
|
|
import net.minecraft.world.item.Item;
|
|
|
|
import net.minecraft.world.item.ItemStack;
|
|
|
|
import net.minecraft.world.level.ClipContext;
|
|
|
|
import net.minecraft.world.level.Level;
|
|
|
|
import net.minecraft.world.level.block.DispenserBlock;
|
|
|
|
import net.minecraft.world.phys.HitResult;
|
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
|
|
|
|
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");
|
2020-01-24 20:49:09 +01:00
|
|
|
MinecraftForge.EVENT_BUS.register(new EventHandler());
|
2018-11-16 13:06:33 +01:00
|
|
|
|
2021-12-04 19:17:21 +01:00
|
|
|
DispenserBlock.registerBehavior(emptyBottle, (source, stack) -> {
|
2021-12-04 15:40:09 +01:00
|
|
|
Level level = source.getLevel();
|
2021-12-04 19:17:21 +01:00
|
|
|
var state = source.getBlockState();
|
|
|
|
var facing = state.getValue(DispenserBlock.FACING);
|
|
|
|
var offset = source.getPos().relative(facing);
|
|
|
|
var offsetState = level.getBlockState(offset);
|
2018-11-16 13:06:33 +01:00
|
|
|
|
2021-12-04 19:17:21 +01:00
|
|
|
var dispense = stack.split(1);
|
|
|
|
if (offsetState.isAir()) {
|
2021-12-04 15:40:09 +01:00
|
|
|
if (IAuraChunk.getAuraInArea(level, offset, 30) >= 100000) {
|
|
|
|
dispense = setType(new ItemStack(ItemAuraBottle.this), IAuraType.forLevel(level));
|
2018-11-16 13:59:07 +01:00
|
|
|
|
2021-12-04 19:17:21 +01:00
|
|
|
var spot = IAuraChunk.getHighestSpot(level, offset, 30, offset);
|
2021-12-04 15:40:09 +01:00
|
|
|
IAuraChunk.getAuraChunk(level, 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
|
|
|
|
2021-12-04 19:17:21 +01:00
|
|
|
DefaultDispenseItemBehavior.spawnItem(level, dispense, 6, facing, DispenserBlock.getDispensePosition(source));
|
2020-01-22 01:32:26 +01:00
|
|
|
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;
|
2021-12-04 19:17:21 +01:00
|
|
|
var type = stack.getTag().getString("stored_type");
|
2020-02-07 15:22:30 +01:00
|
|
|
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
|
2021-12-04 19:17:21 +01:00
|
|
|
public void fillItemCategory(CreativeModeTab tab, NonNullList<ItemStack> items) {
|
|
|
|
if (this.allowdedIn(tab)) {
|
|
|
|
for (var type : NaturesAuraAPI.AURA_TYPES.values()) {
|
|
|
|
var stack = new ItemStack(this);
|
2018-11-08 13:50:45 +01:00
|
|
|
setType(stack, type);
|
|
|
|
items.add(stack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-04 19:17:21 +01:00
|
|
|
public Component getName(ItemStack stack) {
|
|
|
|
return new TranslatableComponent(stack.getDescriptionId() + "." + 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)
|
2021-12-04 19:17:21 +01:00
|
|
|
public ItemColor 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) {
|
2021-12-04 19:17:21 +01:00
|
|
|
var held = event.getItemStack();
|
2020-01-24 20:49:09 +01:00
|
|
|
if (held.isEmpty() || held.getItem() != ModItems.BOTTLE_TWO_THE_REBOTTLING)
|
|
|
|
return;
|
2021-12-04 19:17:21 +01:00
|
|
|
var player = event.getPlayer();
|
|
|
|
HitResult ray = getPlayerPOVHitResult(player.level, player, ClipContext.Fluid.NONE);
|
|
|
|
if (ray.getType() == HitResult.Type.BLOCK)
|
2020-01-24 20:49:09 +01:00
|
|
|
return;
|
2021-12-04 19:17:21 +01:00
|
|
|
var pos = player.blockPosition();
|
2021-12-04 15:40:09 +01:00
|
|
|
if (IAuraChunk.getAuraInArea(player.level, pos, 30) < 100000)
|
2020-01-24 20:49:09 +01:00
|
|
|
return;
|
|
|
|
|
2021-12-04 15:40:09 +01:00
|
|
|
if (!player.level.isClientSide) {
|
2020-01-24 20:49:09 +01:00
|
|
|
held.shrink(1);
|
|
|
|
|
2021-12-04 19:17:21 +01:00
|
|
|
player.getInventory().add(setType(new ItemStack(ItemAuraBottle.this), IAuraType.forLevel(player.level)));
|
2020-01-24 20:49:09 +01:00
|
|
|
|
2021-12-04 19:17:21 +01:00
|
|
|
var spot = IAuraChunk.getHighestSpot(player.level, pos, 30, pos);
|
2021-12-04 15:40:09 +01:00
|
|
|
IAuraChunk.getAuraChunk(player.level, spot).drainAura(spot, 20000);
|
2020-01-24 20:49:09 +01:00
|
|
|
|
2021-12-04 19:17:21 +01:00
|
|
|
player.level.playSound(null, player.getX(), player.getY(), player.getZ(),
|
|
|
|
SoundEvents.BOTTLE_FILL_DRAGONBREATH, SoundSource.PLAYERS, 1F, 1F);
|
2020-01-24 20:49:09 +01:00
|
|
|
}
|
2021-12-04 19:17:21 +01:00
|
|
|
player.swing(event.getHand());
|
2020-01-24 20:49:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-11-08 13:50:45 +01:00
|
|
|
}
|