NaturesAura/src/main/java/de/ellpeck/naturesaura/blocks/BlockProjectileGenerator.java

78 lines
3.4 KiB
Java
Raw Normal View History

2019-03-12 19:34:59 +01:00
package de.ellpeck.naturesaura.blocks;
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
import de.ellpeck.naturesaura.blocks.tiles.TileEntityProjectileGenerator;
2020-01-23 19:20:47 +01:00
import de.ellpeck.naturesaura.blocks.tiles.render.RenderProjectileGenerator;
2020-01-22 23:21:52 +01:00
import de.ellpeck.naturesaura.packet.PacketHandler;
import de.ellpeck.naturesaura.packet.PacketParticles;
2020-01-23 19:20:47 +01:00
import de.ellpeck.naturesaura.reg.ITESRProvider;
2019-03-12 19:34:59 +01:00
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
2020-01-23 19:20:47 +01:00
import net.minecraft.client.renderer.tileentity.TileEntityRenderer;
2019-03-12 19:34:59 +01:00
import net.minecraft.entity.Entity;
import net.minecraft.tileentity.TileEntity;
2019-03-31 23:53:53 +02:00
import net.minecraft.util.SoundCategory;
2020-01-21 21:04:44 +01:00
import net.minecraft.util.SoundEvents;
2020-01-23 19:20:47 +01:00
import net.minecraft.util.Tuple;
2019-03-12 19:34:59 +01:00
import net.minecraft.util.math.BlockPos;
2020-01-21 21:04:44 +01:00
import net.minecraft.util.math.BlockRayTraceResult;
2020-01-22 23:21:52 +01:00
import net.minecraft.util.math.RayTraceResult;
2020-01-23 19:20:47 +01:00
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
2019-03-12 19:34:59 +01:00
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.ProjectileImpactEvent;
2019-10-20 22:30:49 +02:00
import net.minecraftforge.eventbus.api.SubscribeEvent;
2019-03-12 19:34:59 +01:00
2020-01-23 19:20:47 +01:00
public class BlockProjectileGenerator extends BlockContainerImpl implements ITESRProvider {
2019-03-12 19:34:59 +01:00
public BlockProjectileGenerator() {
2020-01-22 01:32:26 +01:00
super("projectile_generator", TileEntityProjectileGenerator::new, ModBlocks.prop(Material.ROCK).hardnessAndResistance(2.5F).sound(SoundType.STONE));
2019-03-12 19:34:59 +01:00
MinecraftForge.EVENT_BUS.register(this);
}
@SubscribeEvent
public void onProjectileImpact(ProjectileImpactEvent event) {
Entity entity = event.getEntity();
if (entity.world.isRemote)
return;
2020-01-22 23:21:52 +01:00
RayTraceResult ray = event.getRayTraceResult();
if (!(ray instanceof BlockRayTraceResult))
return;
BlockRayTraceResult blockRay = (BlockRayTraceResult) ray;
BlockPos pos = blockRay.getPos();
2019-03-12 19:34:59 +01:00
if (pos == null)
return;
TileEntity tile = entity.world.getTileEntity(pos);
if (!(tile instanceof TileEntityProjectileGenerator))
return;
TileEntityProjectileGenerator generator = (TileEntityProjectileGenerator) tile;
2020-01-22 23:21:52 +01:00
if (generator.nextSide != blockRay.getFace())
2019-03-12 19:34:59 +01:00
return;
2020-01-24 17:05:41 +01:00
Integer amount = NaturesAuraAPI.PROJECTILE_GENERATIONS.get(entity.getType());
if (amount == null || amount <= 0)
2019-03-12 19:34:59 +01:00
return;
if (!generator.canGenerateRightNow(35, amount))
return;
2019-03-12 19:34:59 +01:00
BlockPos spot = IAuraChunk.getLowestSpot(entity.world, pos, 35, pos);
IAuraChunk.getAuraChunk(entity.world, spot).storeAura(spot, amount);
2020-01-22 23:21:52 +01:00
PacketHandler.sendToAllAround(entity.world, pos, 32,
2020-01-26 00:16:06 +01:00
new PacketParticles((float) entity.posX, (float) entity.posY, (float) entity.posZ, PacketParticles.Type.PROJECTILE_GEN, pos.getX(), pos.getY(), pos.getZ()));
2020-01-21 21:04:44 +01:00
entity.world.playSound(null, pos.getX(), pos.getY(), pos.getZ(), SoundEvents.ENTITY_ENDER_EYE_LAUNCH, SoundCategory.BLOCKS, 0.8F, 1F);
2019-03-31 23:53:53 +02:00
2019-03-12 19:34:59 +01:00
generator.nextSide = generator.nextSide.rotateY();
generator.sendToClients();
2020-01-21 21:04:44 +01:00
entity.remove();
2019-03-12 19:34:59 +01:00
event.setCanceled(true);
}
2019-03-31 23:53:53 +02:00
2020-01-23 19:20:47 +01:00
@Override
2019-10-20 22:30:49 +02:00
@OnlyIn(Dist.CLIENT)
public Tuple<Class, TileEntityRenderer> getTESR() {
2019-03-31 23:53:53 +02:00
return new Tuple<>(TileEntityProjectileGenerator.class, new RenderProjectileGenerator());
2020-01-23 19:20:47 +01:00
}
2019-03-12 19:34:59 +01:00
}