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

125 lines
5 KiB
Java
Raw Normal View History

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;
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;
import de.ellpeck.naturesaura.reg.IColorProvidingItem;
2019-10-20 22:30:49 +02:00
import net.minecraft.block.DispenserBlock;
import net.minecraft.block.BlockState;
import net.minecraft.client.renderer.color.IItemColor;
2019-10-20 22:30:49 +02:00
import net.minecraft.item.ItemGroup;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.SoundEvents;
2018-11-08 13:50:45 +01:00
import net.minecraft.item.ItemStack;
2019-10-20 22:30:49 +02:00
import net.minecraft.util.Direction;
2018-11-08 13:50:45 +01:00
import net.minecraft.util.NonNullList;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundCategory;
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;
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;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
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
2019-11-04 19:08:49 +01:00
public class AuraBottle extends ItemImpl implements IColorProvidingItem {
public AuraBottle() {
super("aura_bottle", new Properties().group(NaturesAura.CREATIVE_TAB));
2018-11-08 13:50:45 +01:00
MinecraftForge.EVENT_BUS.register(this);
2019-11-04 19:08:49 +01:00
DispenserBlock.registerDispenseBehavior(ModItems.BOTTLE_TWO, (source, stack) -> {
World world = source.getWorld();
2019-10-20 22:30:49 +02:00
BlockState state = source.getBlockState();
2019-11-04 19:08:49 +01:00
Direction facing = state.get(DispenserBlock.FACING);
BlockPos offset = source.getBlockPos().offset(facing);
2019-10-20 22:30:49 +02:00
BlockState offsetState = world.getBlockState(offset);
2019-11-04 19:08:49 +01:00
ItemStack dispense = stack.split(1);
if (offsetState.getBlock().isAir(offsetState, world, offset)) {
if (IAuraChunk.getAuraInArea(world, offset, 30) >= 100000) {
2019-11-04 19:08:49 +01:00
dispense = setType(new ItemStack(AuraBottle.this), IAuraType.forWorld(world));
BlockPos spot = IAuraChunk.getHighestSpot(world, offset, 30, offset);
IAuraChunk.getAuraChunk(world, spot).drainAura(spot, 20000);
}
}
2019-10-20 22:30:49 +02:00
doDispense(world, dispense, 6, facing, DispenserBlock.getDispensePosition(source));
return stack;
});
2018-11-08 13:50:45 +01:00
}
@SubscribeEvent
public void onRightClick(PlayerInteractEvent.RightClickItem event) {
ItemStack held = event.getItemStack();
2018-11-20 10:54:38 +01:00
if (held.isEmpty() || held.getItem() != ModItems.BOTTLE_TWO)
2018-11-08 13:50:45 +01:00
return;
2019-11-04 19:08:49 +01:00
PlayerEntity player = event.getPlayer();
RayTraceResult ray = rayTrace(player.world, player, RayTraceContext.FluidMode.NONE);
if (ray.getType() == RayTraceResult.Type.BLOCK)
2018-11-08 13:50:45 +01:00
return;
BlockPos pos = player.getPosition();
if (IAuraChunk.getAuraInArea(player.world, pos, 30) < 100000)
2018-11-08 13:50:45 +01:00
return;
if (!player.world.isRemote) {
held.shrink(1);
player.inventory.addItemStackToInventory(
setType(new ItemStack(this), IAuraType.forWorld(player.world)));
2018-11-08 13:50:45 +01:00
2018-11-11 13:26:19 +01:00
BlockPos spot = IAuraChunk.getHighestSpot(player.world, pos, 30, pos);
IAuraChunk.getAuraChunk(player.world, spot).drainAura(spot, 20000);
2018-11-08 13:50:45 +01:00
player.world.playSound(null, player.posX, player.posY, player.posZ,
SoundEvents.ITEM_BOTTLE_FILL_DRAGONBREATH, SoundCategory.PLAYERS, 1F, 1F);
}
player.swingArm(event.getHand());
}
@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) {
return new TranslationTextComponent(stack.getTranslationKey() + "." + getType(stack).getName() + ".name");
2018-11-08 13:50:45 +01:00
}
public static IAuraType getType(ItemStack stack) {
2019-11-04 19:08:49 +01:00
if (!stack.hasTag())
return NaturesAuraAPI.TYPE_OTHER;
2019-11-04 19:08:49 +01:00
String type = stack.getTag().getString("stored_type");
if (type.isEmpty())
return NaturesAuraAPI.TYPE_OTHER;
return NaturesAuraAPI.AURA_TYPES.get(new ResourceLocation(type));
2018-11-08 13:50:45 +01:00
}
public static ItemStack setType(ItemStack stack, IAuraType type) {
2019-11-04 19:08:49 +01:00
stack.getOrCreateTag().putString("stored_type", type.getName().toString());
2018-11-08 13:50:45 +01:00
return stack;
}
@Override
2019-10-20 22:30:49 +02:00
@OnlyIn(Dist.CLIENT)
public IItemColor getItemColor() {
return (stack, tintIndex) -> tintIndex > 0 ? getType(stack).getColor() : 0xFFFFFF;
}
2018-11-08 13:50:45 +01:00
}