2016-07-03 22:53:12 +02:00
|
|
|
/*
|
|
|
|
* This file ("BlockShockSuppressor.java") is part of the Actually Additions mod for Minecraft.
|
|
|
|
* It is created and owned by Ellpeck and distributed
|
|
|
|
* under the Actually Additions License to be found at
|
|
|
|
* http://ellpeck.de/actaddlicense
|
|
|
|
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
|
|
|
|
*
|
2017-01-01 16:23:26 +01:00
|
|
|
* © 2015-2017 Ellpeck
|
2016-07-03 22:53:12 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
package de.ellpeck.actuallyadditions.mod.blocks;
|
|
|
|
|
|
|
|
import de.ellpeck.actuallyadditions.mod.blocks.base.BlockContainerBase;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.tile.TileEntityShockSuppressor;
|
2021-03-01 20:14:50 +01:00
|
|
|
import net.minecraft.block.BlockState;
|
2016-07-03 22:53:12 +02:00
|
|
|
import net.minecraft.entity.Entity;
|
|
|
|
import net.minecraft.tileentity.TileEntity;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
2021-03-01 20:14:50 +01:00
|
|
|
import net.minecraft.util.math.shapes.ISelectionContext;
|
|
|
|
import net.minecraft.util.math.shapes.VoxelShape;
|
|
|
|
import net.minecraft.world.IBlockReader;
|
2016-07-03 22:53:12 +02:00
|
|
|
import net.minecraft.world.World;
|
|
|
|
import net.minecraftforge.common.MinecraftForge;
|
|
|
|
import net.minecraftforge.event.world.ExplosionEvent;
|
2021-03-01 20:14:50 +01:00
|
|
|
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
2016-07-03 22:53:12 +02:00
|
|
|
|
2021-02-27 13:24:45 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public class BlockShockSuppressor extends BlockContainerBase {
|
2016-07-03 22:53:12 +02:00
|
|
|
|
2021-02-27 13:24:45 +01:00
|
|
|
public BlockShockSuppressor() {
|
2021-03-01 20:14:50 +01:00
|
|
|
super(ActuallyBlocks.defaultPickProps(0, 20F, 2000.0F));
|
2016-07-03 22:53:12 +02:00
|
|
|
MinecraftForge.EVENT_BUS.register(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@SubscribeEvent
|
2019-05-02 09:10:29 +02:00
|
|
|
public void onExplosion(ExplosionEvent.Detonate event) {
|
2016-07-03 22:53:12 +02:00
|
|
|
World world = event.getWorld();
|
2019-05-02 09:10:29 +02:00
|
|
|
if (!world.isRemote) {
|
2016-07-03 22:53:12 +02:00
|
|
|
List<BlockPos> affectedBlocks = event.getAffectedBlocks();
|
|
|
|
List<Entity> affectedEntities = event.getAffectedEntities();
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
int rangeSq = TileEntityShockSuppressor.RANGE * TileEntityShockSuppressor.RANGE;
|
2016-07-03 22:53:12 +02:00
|
|
|
int use = TileEntityShockSuppressor.USE_PER;
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
for (TileEntityShockSuppressor suppressor : TileEntityShockSuppressor.SUPPRESSORS) {
|
|
|
|
if (!suppressor.isRedstonePowered) {
|
2016-07-03 22:53:12 +02:00
|
|
|
BlockPos supPos = suppressor.getPos();
|
|
|
|
|
2019-02-27 19:53:05 +01:00
|
|
|
List<Entity> entitiesToRemove = new ArrayList<>();
|
|
|
|
List<BlockPos> posesToRemove = new ArrayList<>();
|
2016-07-03 22:53:12 +02:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
for (BlockPos pos : affectedBlocks) {
|
|
|
|
if (pos.distanceSq(supPos) <= rangeSq) {
|
2016-07-03 22:53:12 +02:00
|
|
|
posesToRemove.add(pos);
|
|
|
|
}
|
|
|
|
}
|
2019-05-02 09:10:29 +02:00
|
|
|
for (Entity entity : affectedEntities) {
|
2021-03-01 20:14:50 +01:00
|
|
|
if (entity.getPositionVec().squareDistanceTo(supPos.getX(), supPos.getY(), supPos.getZ()) <= rangeSq) {
|
2016-07-03 22:53:12 +02:00
|
|
|
entitiesToRemove.add(entity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Collections.shuffle(entitiesToRemove);
|
|
|
|
Collections.shuffle(posesToRemove);
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
for (BlockPos pos : posesToRemove) {
|
|
|
|
if (suppressor.storage.getEnergyStored() >= use) {
|
2016-11-23 18:10:55 +01:00
|
|
|
suppressor.storage.extractEnergyInternal(use, false);
|
2016-07-03 22:53:12 +02:00
|
|
|
affectedBlocks.remove(pos);
|
2019-05-02 09:10:29 +02:00
|
|
|
} else {
|
2016-07-03 22:53:12 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-05-02 09:10:29 +02:00
|
|
|
for (Entity entity : entitiesToRemove) {
|
|
|
|
if (suppressor.storage.getEnergyStored() >= use) {
|
2016-11-23 18:10:55 +01:00
|
|
|
suppressor.storage.extractEnergyInternal(use, false);
|
2016-07-03 22:53:12 +02:00
|
|
|
affectedEntities.remove(entity);
|
2019-05-02 09:10:29 +02:00
|
|
|
} else {
|
2016-07-03 22:53:12 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-03-01 20:14:50 +01:00
|
|
|
public TileEntity createNewTileEntity(IBlockReader worldIn) {
|
|
|
|
return new TileEntityShockSuppressor();
|
2016-07-03 22:53:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-03-01 20:14:50 +01:00
|
|
|
public VoxelShape getShape(BlockState state, IBlockReader worldIn, BlockPos pos, ISelectionContext context) {
|
|
|
|
return Shapes.SUPPRESSOR_SHAPE;
|
2016-07-03 22:53:12 +02:00
|
|
|
}
|
|
|
|
}
|