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

131 lines
5.3 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;
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.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");
MinecraftForge.EVENT_BUS.register(new EventHandler());
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);
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) {
2022-06-27 15:24:04 +02:00
dispense = ItemAuraBottle.setType(new ItemStack(ItemAuraBottle.this), IAuraType.forLevel(level));
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);
}
2020-01-22 01:32:26 +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-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) {
2022-06-27 15:24:04 +02:00
if (this.allowedIn(tab)) {
2021-12-04 19:17:21 +01:00
for (var type : NaturesAuraAPI.AURA_TYPES.values()) {
var stack = new ItemStack(this);
2022-06-27 15:24:04 +02:00
ItemAuraBottle.setType(stack, type);
2018-11-08 13:50:45 +01:00
items.add(stack);
}
}
}
@Override
2021-12-04 19:17:21 +01:00
public Component getName(ItemStack stack) {
2022-06-27 15:24:04 +02:00
return Component.translatable(stack.getDescriptionId() + "." + ItemAuraBottle.getType(stack).getName());
2018-11-08 13:50:45 +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() {
2022-06-27 15:24:04 +02:00
return (stack, tintIndex) -> tintIndex > 0 ? ItemAuraBottle.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) {
2021-12-04 19:17:21 +01:00
var held = event.getItemStack();
if (held.isEmpty() || held.getItem() != ModItems.BOTTLE_TWO_THE_REBOTTLING)
return;
2021-12-04 19:17:21 +01:00
var player = event.getPlayer();
2022-06-27 15:24:04 +02:00
HitResult ray = Item.getPlayerPOVHitResult(player.level, player, ClipContext.Fluid.NONE);
2021-12-04 19:17:21 +01:00
if (ray.getType() == HitResult.Type.BLOCK)
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)
return;
2021-12-04 15:40:09 +01:00
if (!player.level.isClientSide) {
held.shrink(1);
2022-06-27 15:24:04 +02:00
player.getInventory().add(ItemAuraBottle.setType(new ItemStack(ItemAuraBottle.this), IAuraType.forLevel(player.level)));
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);
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);
}
2021-12-04 19:17:21 +01:00
player.swing(event.getHand());
}
}
2018-11-08 13:50:45 +01:00
}