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;
|
2023-02-16 16:24:47 +01:00
|
|
|
import net.minecraft.core.BlockPos;
|
2021-12-04 19:17:21 +01:00
|
|
|
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;
|
2023-02-05 16:13:41 +01:00
|
|
|
import net.minecraft.world.entity.item.ItemEntity;
|
2021-12-04 19:17:21 +01:00
|
|
|
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()) {
|
2023-02-16 16:24:47 +01:00
|
|
|
var bottle = ItemAuraBottle.create(level, offset);
|
|
|
|
if (!bottle.isEmpty())
|
|
|
|
dispense = bottle;
|
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-16 13:06:33 +01:00
|
|
|
});
|
2018-11-08 13:50:45 +01:00
|
|
|
}
|
|
|
|
|
2023-07-08 12:32:27 +02:00
|
|
|
// TODO aura bottle creative
|
|
|
|
/* @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);
|
|
|
|
}
|
|
|
|
}
|
2023-07-08 12:32:27 +02:00
|
|
|
}*/
|
2018-11-08 13:50:45 +01:00
|
|
|
|
|
|
|
@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
|
|
|
}
|
|
|
|
|
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() {
|
2022-06-27 15:24:04 +02:00
|
|
|
return (stack, tintIndex) -> tintIndex > 0 ? ItemAuraBottle.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");
|
|
|
|
}
|
|
|
|
|
2023-02-16 16:24:47 +01:00
|
|
|
public static IAuraType getType(ItemStack stack) {
|
|
|
|
if (!stack.hasTag())
|
|
|
|
return NaturesAuraAPI.TYPE_OTHER;
|
|
|
|
var 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static ItemStack create(Level level, BlockPos pos) {
|
|
|
|
var aura = IAuraChunk.getAuraInArea(level, pos, 30);
|
|
|
|
if (aura <= -100000) {
|
|
|
|
return new ItemStack(ModItems.VACUUM_BOTTLE);
|
|
|
|
} else if (aura >= 100000) {
|
|
|
|
var spot = IAuraChunk.getHighestSpot(level, pos, 30, pos);
|
|
|
|
IAuraChunk.getAuraChunk(level, spot).drainAura(spot, 20000);
|
|
|
|
return ItemAuraBottle.setType(new ItemStack(ModItems.AURA_BOTTLE), IAuraType.forLevel(level));
|
|
|
|
} else {
|
|
|
|
return ItemStack.EMPTY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class EventHandler {
|
2020-01-24 20:49:09 +01:00
|
|
|
|
|
|
|
@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;
|
2022-08-01 16:14:37 +02:00
|
|
|
var player = event.getEntity();
|
2023-07-08 12:32:27 +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)
|
2020-01-24 20:49:09 +01:00
|
|
|
return;
|
2023-07-08 12:32:27 +02:00
|
|
|
var bottle = ItemAuraBottle.create(player.level(), player.blockPosition());
|
2023-02-16 16:24:47 +01:00
|
|
|
if (bottle.isEmpty())
|
2020-01-24 20:49:09 +01:00
|
|
|
return;
|
|
|
|
|
2023-07-08 12:32:27 +02:00
|
|
|
if (!player.level().isClientSide) {
|
2020-01-24 20:49:09 +01:00
|
|
|
held.shrink(1);
|
2023-02-16 16:24:47 +01:00
|
|
|
if (!player.addItem(bottle))
|
2023-07-08 12:32:27 +02:00
|
|
|
player.level().addFreshEntity(new ItemEntity(player.level(), player.getX(), player.getY(), player.getZ(), bottle));
|
2020-01-24 20:49:09 +01:00
|
|
|
|
2023-07-08 12:32:27 +02:00
|
|
|
player.level().playSound(null, player.getX(), player.getY(), player.getZ(),
|
2021-12-04 19:17:21 +01:00
|
|
|
SoundEvents.BOTTLE_FILL_DRAGONBREATH, SoundSource.PLAYERS, 1F, 1F);
|
2020-01-24 20:49:09 +01:00
|
|
|
}
|
2023-02-16 16:24:47 +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
|
|
|
}
|