mirror of
https://github.com/Ellpeck/ActuallyAdditions.git
synced 2024-11-22 15:18:34 +01:00
Shock suppressor, function
This commit is contained in:
parent
a5e10de4b3
commit
fad1a1b0c9
4 changed files with 232 additions and 0 deletions
|
@ -0,0 +1,108 @@
|
|||
/*
|
||||
* 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
|
||||
*
|
||||
* © 2015-2016 Ellpeck
|
||||
*/
|
||||
|
||||
package de.ellpeck.actuallyadditions.mod.blocks;
|
||||
|
||||
import de.ellpeck.actuallyadditions.mod.blocks.base.BlockContainerBase;
|
||||
import de.ellpeck.actuallyadditions.mod.tile.TileEntityShockSuppressor;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.block.SoundType;
|
||||
import net.minecraft.block.material.Material;
|
||||
import net.minecraft.block.state.IBlockState;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.item.EnumRarity;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.event.world.ExplosionEvent;
|
||||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class BlockShockSuppressor extends BlockContainerBase{
|
||||
|
||||
public BlockShockSuppressor(String name){
|
||||
super(Material.ROCK, name);
|
||||
this.setHarvestLevel("pickaxe", 0);
|
||||
this.setHardness(20.0F);
|
||||
this.setResistance(2000.0F);
|
||||
this.setSoundType(SoundType.STONE);
|
||||
|
||||
MinecraftForge.EVENT_BUS.register(this);
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void onExplosion(ExplosionEvent.Detonate event){
|
||||
World world = event.getWorld();
|
||||
if(!world.isRemote){
|
||||
List<BlockPos> affectedBlocks = event.getAffectedBlocks();
|
||||
List<Entity> affectedEntities = event.getAffectedEntities();
|
||||
|
||||
int rangeSq = TileEntityShockSuppressor.RANGE*TileEntityShockSuppressor.RANGE;
|
||||
int use = TileEntityShockSuppressor.USE_PER;
|
||||
|
||||
for(TileEntityShockSuppressor suppressor : TileEntityShockSuppressor.SUPPRESSORS){
|
||||
if(!suppressor.isRedstonePowered){
|
||||
BlockPos supPos = suppressor.getPos();
|
||||
|
||||
List<Entity> entitiesToRemove = new ArrayList<Entity>();
|
||||
List<BlockPos> posesToRemove = new ArrayList<BlockPos>();
|
||||
|
||||
for(BlockPos pos : affectedBlocks){
|
||||
if(pos.distanceSq(supPos) <= rangeSq){
|
||||
posesToRemove.add(pos);
|
||||
}
|
||||
}
|
||||
for(Entity entity : affectedEntities){
|
||||
if(entity.getPositionVector().squareDistanceTo(supPos.getX(), supPos.getY(), supPos.getZ()) <= rangeSq){
|
||||
entitiesToRemove.add(entity);
|
||||
}
|
||||
}
|
||||
|
||||
Collections.shuffle(entitiesToRemove);
|
||||
Collections.shuffle(posesToRemove);
|
||||
|
||||
for(BlockPos pos : posesToRemove){
|
||||
if(suppressor.storage.getEnergyStored() >= use){
|
||||
suppressor.storage.extractEnergy(use, false);
|
||||
affectedBlocks.remove(pos);
|
||||
}
|
||||
else{
|
||||
break;
|
||||
}
|
||||
}
|
||||
for(Entity entity : entitiesToRemove){
|
||||
if(suppressor.storage.getEnergyStored() >= use){
|
||||
suppressor.storage.extractEnergy(use, false);
|
||||
affectedEntities.remove(entity);
|
||||
}
|
||||
else{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public EnumRarity getRarity(ItemStack stack){
|
||||
return EnumRarity.RARE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TileEntity createNewTileEntity(World worldIn, int meta){
|
||||
return new TileEntityShockSuppressor();
|
||||
}
|
||||
}
|
|
@ -31,6 +31,7 @@ import net.minecraft.tileentity.TileEntity;
|
|||
import net.minecraft.util.math.RayTraceResult;
|
||||
import net.minecraft.util.text.TextFormatting;
|
||||
import net.minecraftforge.client.event.RenderGameOverlayEvent;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.event.entity.player.ItemTooltipEvent;
|
||||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
|
||||
import net.minecraftforge.fml.relauncher.Side;
|
||||
|
@ -45,6 +46,10 @@ public class ClientEvents{
|
|||
private static final String ADVANCED_INFO_TEXT_PRE = TextFormatting.DARK_GRAY+" ";
|
||||
private static final String ADVANCED_INFO_HEADER_PRE = TextFormatting.GRAY+" -";
|
||||
|
||||
public ClientEvents(){
|
||||
MinecraftForge.EVENT_BUS.register(this);
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void onTooltipEvent(ItemTooltipEvent event){
|
||||
//Advanced Item Info
|
||||
|
|
|
@ -28,6 +28,7 @@ import net.minecraft.init.Blocks;
|
|||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
|
||||
import net.minecraftforge.event.entity.living.LivingDeathEvent;
|
||||
import net.minecraftforge.event.entity.living.LivingDropsEvent;
|
||||
|
@ -39,6 +40,10 @@ import java.util.Locale;
|
|||
|
||||
public class CommonEvents{
|
||||
|
||||
public CommonEvents(){
|
||||
MinecraftForge.EVENT_BUS.register(this);
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void livingDeathEvent(LivingDeathEvent event){
|
||||
if(event.getEntityLiving().worldObj != null && !event.getEntityLiving().worldObj.isRemote && event.getEntityLiving() instanceof EntityPlayer){
|
||||
|
|
|
@ -0,0 +1,114 @@
|
|||
/*
|
||||
* This file ("TileEntityShockSuppressor.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
|
||||
*
|
||||
* © 2015-2016 Ellpeck
|
||||
*/
|
||||
|
||||
package de.ellpeck.actuallyadditions.mod.tile;
|
||||
|
||||
import cofh.api.energy.EnergyStorage;
|
||||
import cofh.api.energy.IEnergyReceiver;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class TileEntityShockSuppressor extends TileEntityBase implements IEnergyReceiver, IEnergyDisplay{
|
||||
|
||||
public static final List<TileEntityShockSuppressor> SUPPRESSORS = new ArrayList<TileEntityShockSuppressor>();
|
||||
|
||||
public static final int USE_PER = 300;
|
||||
public static final int RANGE = 5;
|
||||
|
||||
public EnergyStorage storage = new EnergyStorage(300000);
|
||||
private int oldEnergy;
|
||||
|
||||
public TileEntityShockSuppressor(){
|
||||
super("shockSuppressor");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChunkUnload(){
|
||||
super.onChunkUnload();
|
||||
|
||||
if(!this.worldObj.isRemote){
|
||||
SUPPRESSORS.remove(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invalidate(){
|
||||
super.invalidate();
|
||||
|
||||
if(!this.worldObj.isRemote){
|
||||
SUPPRESSORS.remove(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateEntity(){
|
||||
super.updateEntity();
|
||||
|
||||
if(!this.worldObj.isRemote){
|
||||
if(!this.isInvalid() && !SUPPRESSORS.contains(this)){
|
||||
SUPPRESSORS.add(this);
|
||||
}
|
||||
|
||||
if(this.oldEnergy != this.storage.getEnergyStored() && this.sendUpdateWithInterval()){
|
||||
this.oldEnergy = this.storage.getEnergyStored();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeSyncableNBT(NBTTagCompound compound, NBTType type){
|
||||
super.writeSyncableNBT(compound, type);
|
||||
this.storage.writeToNBT(compound);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readSyncableNBT(NBTTagCompound compound, NBTType type){
|
||||
super.readSyncableNBT(compound, type);
|
||||
this.storage.readFromNBT(compound);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int receiveEnergy(EnumFacing from, int maxReceive, boolean simulate){
|
||||
return this.storage.receiveEnergy(maxReceive, simulate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getEnergyStored(EnumFacing from){
|
||||
return this.storage.getEnergyStored();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaxEnergyStored(EnumFacing from){
|
||||
return this.storage.getMaxEnergyStored();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canConnectEnergy(EnumFacing from){
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getEnergy(){
|
||||
return this.storage.getEnergyStored();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaxEnergy(){
|
||||
return this.storage.getMaxEnergyStored();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean needsHoldShift(){
|
||||
return false;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue