ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockShockSuppressor.java

111 lines
4.4 KiB
Java
Raw Normal View History

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.tile.TileEntityShockSuppressor;
2024-03-02 21:23:08 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.EntityBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
2024-03-04 20:21:48 +01:00
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.level.ExplosionEvent;
2016-07-03 22:53:12 +02:00
2021-11-25 21:27:45 +01:00
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
2024-03-02 21:23:08 +01:00
public class BlockShockSuppressor extends Block implements EntityBlock {
2016-07-03 22:53:12 +02:00
public BlockShockSuppressor() {
2024-03-02 21:23:08 +01:00
super(ActuallyBlocks.defaultPickProps(20F, 2000.0F));
2024-03-04 20:21:48 +01:00
NeoForge.EVENT_BUS.register(this);
2016-07-03 22:53:12 +02:00
}
@SubscribeEvent
2019-05-02 09:10:29 +02:00
public void onExplosion(ExplosionEvent.Detonate event) {
2024-03-03 01:20:53 +01:00
Level world = event.getLevel();
if (!world.isClientSide) {
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) {
BlockPos supPos = suppressor.getBlockPos();
2016-07-03 22:53:12 +02:00
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.distSqr(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) {
if (entity.position().distanceToSqr(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) {
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) {
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;
}
}
}
}
}
}
2021-11-25 21:27:45 +01:00
@Nullable
2016-07-03 22:53:12 +02:00
@Override
2024-03-02 21:23:08 +01:00
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return new TileEntityShockSuppressor(pos, state);
2016-07-03 22:53:12 +02:00
}
2024-03-02 21:23:08 +01:00
@Nullable
2021-11-25 21:27:45 +01:00
@Override
2024-03-02 21:23:08 +01:00
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState blockState, BlockEntityType<T> entityType) {
return level.isClientSide? TileEntityShockSuppressor::clientTick : TileEntityShockSuppressor::serverTick;
2021-11-25 21:27:45 +01:00
}
2016-07-03 22:53:12 +02:00
@Override
2024-03-02 21:23:08 +01:00
public VoxelShape getShape(BlockState state, BlockGetter worldIn, BlockPos pos, CollisionContext context) {
return VoxelShapes.SUPPRESSOR_SHAPE;
2016-07-03 22:53:12 +02:00
}
}