From fad1a1b0c9b7bf173f9cd6f6395cb9bb8c3782b5 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sun, 3 Jul 2016 22:53:12 +0200 Subject: [PATCH] Shock suppressor, function --- .../mod/blocks/BlockShockSuppressor.java | 108 +++++++++++++++++ .../mod/event/ClientEvents.java | 5 + .../mod/event/CommonEvents.java | 5 + .../mod/tile/TileEntityShockSuppressor.java | 114 ++++++++++++++++++ 4 files changed, 232 insertions(+) create mode 100644 src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockShockSuppressor.java create mode 100644 src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityShockSuppressor.java diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockShockSuppressor.java b/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockShockSuppressor.java new file mode 100644 index 000000000..8b7c110f8 --- /dev/null +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockShockSuppressor.java @@ -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 affectedBlocks = event.getAffectedBlocks(); + List 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 entitiesToRemove = new ArrayList(); + List posesToRemove = new ArrayList(); + + 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(); + } +} diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/event/ClientEvents.java b/src/main/java/de/ellpeck/actuallyadditions/mod/event/ClientEvents.java index 8f731219f..ad682eb4a 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/event/ClientEvents.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/event/ClientEvents.java @@ -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 diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/event/CommonEvents.java b/src/main/java/de/ellpeck/actuallyadditions/mod/event/CommonEvents.java index c5f9a06f9..603ffe403 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/event/CommonEvents.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/event/CommonEvents.java @@ -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){ diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityShockSuppressor.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityShockSuppressor.java new file mode 100644 index 000000000..b45203bfa --- /dev/null +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityShockSuppressor.java @@ -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 SUPPRESSORS = new ArrayList(); + + 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; + } +}