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;
|
2020-01-28 18:08:56 +01:00
|
|
|
import de.ellpeck.naturesaura.blocks.tiles.ModTileEntities;
|
2019-03-12 19:34:59 +01:00
|
|
|
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-29 01:34:58 +01:00
|
|
|
import de.ellpeck.naturesaura.data.BlockStateGenerator;
|
2020-01-22 23:21:52 +01:00
|
|
|
import de.ellpeck.naturesaura.packet.PacketHandler;
|
|
|
|
import de.ellpeck.naturesaura.packet.PacketParticles;
|
2020-01-29 01:34:58 +01:00
|
|
|
import de.ellpeck.naturesaura.reg.ICustomBlockState;
|
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;
|
2020-01-28 18:08:56 +01:00
|
|
|
import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher;
|
2019-03-12 19:34:59 +01:00
|
|
|
import net.minecraft.entity.Entity;
|
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2020-01-28 18:08:56 +01:00
|
|
|
import net.minecraft.tileentity.TileEntityType;
|
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;
|
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-28 18:08:56 +01:00
|
|
|
import java.util.function.Function;
|
2020-01-28 20:36:08 +01:00
|
|
|
import java.util.function.Supplier;
|
2020-01-28 18:08:56 +01:00
|
|
|
|
2020-01-29 01:34:58 +01:00
|
|
|
public class BlockProjectileGenerator extends BlockContainerImpl implements ITESRProvider<TileEntityProjectileGenerator>, ICustomBlockState {
|
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());
|
2019-04-06 17:16:42 +02:00
|
|
|
if (amount == null || amount <= 0)
|
2019-03-12 19:34:59 +01:00
|
|
|
return;
|
2020-01-27 00:11:16 +01:00
|
|
|
if (!generator.canGenerateRightNow(35, amount))
|
|
|
|
return;
|
2019-03-12 19:34:59 +01:00
|
|
|
|
2020-01-29 21:19:00 +01:00
|
|
|
while (amount > 0) {
|
|
|
|
BlockPos spot = IAuraChunk.getLowestSpot(entity.world, pos, 35, pos);
|
|
|
|
amount -= IAuraChunk.getAuraChunk(entity.world, spot).storeAura(spot, amount);
|
|
|
|
}
|
2019-03-12 19:34:59 +01:00
|
|
|
|
2020-01-22 23:21:52 +01:00
|
|
|
PacketHandler.sendToAllAround(entity.world, pos, 32,
|
2020-01-28 18:08:56 +01:00
|
|
|
new PacketParticles((float) entity.getPosX(), (float) entity.getPosY(), (float) entity.getPosZ(), 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
|
2020-01-28 20:36:08 +01:00
|
|
|
public Tuple<TileEntityType<TileEntityProjectileGenerator>, Supplier<Function<? super TileEntityRendererDispatcher, ? extends TileEntityRenderer<? super TileEntityProjectileGenerator>>>> getTESR() {
|
|
|
|
return new Tuple<>(ModTileEntities.PROJECTILE_GENERATOR, () -> RenderProjectileGenerator::new);
|
2020-01-23 19:20:47 +01:00
|
|
|
}
|
2020-01-29 01:34:58 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void generateCustomBlockState(BlockStateGenerator generator) {
|
|
|
|
generator.simpleBlock(this, generator.models().cubeBottomTop(this.getBaseName(),
|
|
|
|
generator.modLoc("block/" + this.getBaseName()),
|
|
|
|
generator.modLoc("block/" + this.getBaseName() + "_top"),
|
|
|
|
generator.modLoc("block/" + this.getBaseName() + "_top")));
|
|
|
|
}
|
2019-03-12 19:34:59 +01:00
|
|
|
}
|