From 00aa812a200a4021f7d3236e3a7912ae3ae117a6 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sun, 13 Dec 2015 17:32:06 +0100 Subject: [PATCH] Firework box --- .../blocks/BlockFireworkBox.java | 85 ++++++++++++ .../actuallyadditions/blocks/InitBlocks.java | 3 + .../tile/TileEntityBase.java | 1 + .../tile/TileEntityFireworkBox.java | 121 ++++++++++++++++++ 4 files changed, 210 insertions(+) create mode 100644 src/main/java/ellpeck/actuallyadditions/blocks/BlockFireworkBox.java create mode 100644 src/main/java/ellpeck/actuallyadditions/tile/TileEntityFireworkBox.java diff --git a/src/main/java/ellpeck/actuallyadditions/blocks/BlockFireworkBox.java b/src/main/java/ellpeck/actuallyadditions/blocks/BlockFireworkBox.java new file mode 100644 index 000000000..83e8acbbb --- /dev/null +++ b/src/main/java/ellpeck/actuallyadditions/blocks/BlockFireworkBox.java @@ -0,0 +1,85 @@ +/* + * This file ("BlockFireworkBox.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://github.com/Ellpeck/ActuallyAdditions/blob/master/README.md + * View the source code at https://github.com/Ellpeck/ActuallyAdditions + * + * © 2015 Ellpeck + */ + +package ellpeck.actuallyadditions.blocks; + +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; +import ellpeck.actuallyadditions.blocks.base.BlockContainerBase; +import ellpeck.actuallyadditions.tile.TileEntityFireworkBox; +import ellpeck.actuallyadditions.util.ModUtil; +import net.minecraft.block.Block; +import net.minecraft.block.material.Material; +import net.minecraft.client.renderer.texture.IIconRegister; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.EnumRarity; +import net.minecraft.item.ItemStack; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.ChatComponentText; +import net.minecraft.util.IIcon; +import net.minecraft.world.World; + +public class BlockFireworkBox extends BlockContainerBase{ + + @SideOnly(Side.CLIENT) + private IIcon topIcon; + @SideOnly(Side.CLIENT) + private IIcon bottomIcon; + + public BlockFireworkBox(String name){ + super(Material.rock, name); + this.setHarvestLevel("pickaxe", 0); + this.setHardness(1.5F); + this.setResistance(10.0F); + this.setStepSound(soundTypeStone); + } + + @Override + public TileEntity createNewTileEntity(World world, int par2){ + return new TileEntityFireworkBox(); + } + + @Override + @SideOnly(Side.CLIENT) + public IIcon getIcon(int side, int meta){ + return side <= 1 ? (side == 0 ? this.bottomIcon : this.topIcon) : this.blockIcon; + } + + @Override + public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int par6, float par7, float par8, float par9){ + if(!world.isRemote){ + TileEntityFireworkBox box = (TileEntityFireworkBox)world.getTileEntity(x, y, z); + if(box != null){ + player.addChatComponentMessage(new ChatComponentText(box.storage.getEnergyStored()+"/"+box.storage.getMaxEnergyStored()+" RF")); + } + return true; + } + return true; + } + + @Override + @SideOnly(Side.CLIENT) + public void registerBlockIcons(IIconRegister iconReg){ + this.blockIcon = iconReg.registerIcon(ModUtil.MOD_ID_LOWER+":"+this.getBaseName()); + this.topIcon = iconReg.registerIcon(ModUtil.MOD_ID_LOWER+":"+this.getBaseName()+"Top"); + this.bottomIcon = iconReg.registerIcon(ModUtil.MOD_ID_LOWER+":"+this.getBaseName()+"Bottom"); + } + + @Override + public EnumRarity getRarity(ItemStack stack){ + return EnumRarity.rare; + } + + @Override + public void breakBlock(World world, int x, int y, int z, Block block, int par6){ + this.dropInventory(world, x, y, z); + super.breakBlock(world, x, y, z, block, par6); + } +} diff --git a/src/main/java/ellpeck/actuallyadditions/blocks/InitBlocks.java b/src/main/java/ellpeck/actuallyadditions/blocks/InitBlocks.java index f207eacff..4df420f78 100644 --- a/src/main/java/ellpeck/actuallyadditions/blocks/InitBlocks.java +++ b/src/main/java/ellpeck/actuallyadditions/blocks/InitBlocks.java @@ -110,9 +110,12 @@ public class InitBlocks{ public static Block blockBookletStand; public static Block blockMiner; + public static Block blockFireworkBox; + public static void init(){ ModUtil.LOGGER.info("Initializing Blocks..."); + blockFireworkBox = new BlockFireworkBox("blockFireworkBox"); blockMiner = new BlockMiner("blockMiner"); blockBookletStand = new BlockBookletStand("blockBookStand"); blockAtomicReconstructor = new BlockAtomicReconstructor("blockAtomicReconstructor"); diff --git a/src/main/java/ellpeck/actuallyadditions/tile/TileEntityBase.java b/src/main/java/ellpeck/actuallyadditions/tile/TileEntityBase.java index c3a99aa47..bed9ff530 100644 --- a/src/main/java/ellpeck/actuallyadditions/tile/TileEntityBase.java +++ b/src/main/java/ellpeck/actuallyadditions/tile/TileEntityBase.java @@ -71,6 +71,7 @@ public abstract class TileEntityBase extends TileEntity{ GameRegistry.registerTileEntity(TileEntityAtomicReconstructor.class, ModUtil.MOD_ID_LOWER+":tileEntityAtomicReconstructor"); GameRegistry.registerTileEntity(TileEntityBookletStand.class, ModUtil.MOD_ID_LOWER+":tileEntityBookletStand"); GameRegistry.registerTileEntity(TileEntityMiner.class, ModUtil.MOD_ID_LOWER+":tileEntityMiner"); + GameRegistry.registerTileEntity(TileEntityFireworkBox.class, ModUtil.MOD_ID_LOWER+":tileEntityFireworkBox"); } @Override diff --git a/src/main/java/ellpeck/actuallyadditions/tile/TileEntityFireworkBox.java b/src/main/java/ellpeck/actuallyadditions/tile/TileEntityFireworkBox.java new file mode 100644 index 000000000..4df78ef62 --- /dev/null +++ b/src/main/java/ellpeck/actuallyadditions/tile/TileEntityFireworkBox.java @@ -0,0 +1,121 @@ +/* + * This file ("TileEntityFireworkBox.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://github.com/Ellpeck/ActuallyAdditions/blob/master/README.md + * View the source code at https://github.com/Ellpeck/ActuallyAdditions + * + * © 2015 Ellpeck + */ + +package ellpeck.actuallyadditions.tile; + +import cofh.api.energy.EnergyStorage; +import cofh.api.energy.IEnergyReceiver; +import ellpeck.actuallyadditions.util.Util; +import net.minecraft.entity.item.EntityFireworkRocket; +import net.minecraft.init.Items; +import net.minecraft.item.ItemDye; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; +import net.minecraft.util.MathHelper; +import net.minecraftforge.common.util.ForgeDirection; + +public class TileEntityFireworkBox extends TileEntityBase implements IEnergyReceiver{ + + public EnergyStorage storage = new EnergyStorage(20000); + + private int timeUntilNextFirework; + + public static final int USE_PER_SHOT = 300; + + @Override + public void updateEntity(){ + if(!this.worldObj.isRemote){ + if(this.timeUntilNextFirework > 0){ + this.timeUntilNextFirework--; + if(this.timeUntilNextFirework <= 0 && this.storage.getEnergyStored() >= USE_PER_SHOT){ + int range = 4; + int amount = Util.RANDOM.nextInt(5)+1; + for(int i = 0; i < amount; i++){ + ItemStack firework = this.makeFirework(); + + double x = this.xCoord+MathHelper.getRandomDoubleInRange(Util.RANDOM, 0, range*2)-range; + double z = this.zCoord+MathHelper.getRandomDoubleInRange(Util.RANDOM, 0, range*2)-range; + EntityFireworkRocket rocket = new EntityFireworkRocket(this.worldObj, x, this.yCoord+0.5, z, firework); + this.worldObj.spawnEntityInWorld(rocket); + } + + this.storage.extractEnergy(USE_PER_SHOT, false); + } + } + else{ + this.timeUntilNextFirework = 100; + } + } + } + + private ItemStack makeFirework(){ + NBTTagList list = new NBTTagList(); + int chargesAmount = Util.RANDOM.nextInt(2)+1; + for(int i = 0; i < chargesAmount; i++){ + list.appendTag(this.makeFireworkCharge()); + } + + NBTTagCompound compound1 = new NBTTagCompound(); + compound1.setTag("Explosions", list); + compound1.setByte("Flight", (byte)(Util.RANDOM.nextInt(3)+1)); + + NBTTagCompound compound = new NBTTagCompound(); + compound.setTag("Fireworks", compound1); + + ItemStack firework = new ItemStack(Items.fireworks); + firework.setTagCompound(compound); + + return firework; + } + + private NBTTagCompound makeFireworkCharge(){ + NBTTagCompound compound = new NBTTagCompound(); + + if(Util.RANDOM.nextFloat() >= 0.65F){ + if(Util.RANDOM.nextFloat() >= 0.5F){ + compound.setBoolean("Flicker", true); + } + else{ + compound.setBoolean("Trail", true); + } + } + + int[] colors = new int[MathHelper.getRandomIntegerInRange(Util.RANDOM, 1, 6)]; + for(int i = 0; i < colors.length; i++){ + colors[i] = ItemDye.field_150922_c[Util.RANDOM.nextInt(ItemDye.field_150922_c.length)]; + } + compound.setIntArray("Colors", colors); + + compound.setByte("Type", (byte)Util.RANDOM.nextInt(5)); + + return compound; + } + + @Override + public int receiveEnergy(ForgeDirection from, int maxReceive, boolean simulate){ + return this.storage.receiveEnergy(maxReceive, simulate); + } + + @Override + public int getEnergyStored(ForgeDirection from){ + return this.storage.getEnergyStored(); + } + + @Override + public int getMaxEnergyStored(ForgeDirection from){ + return this.storage.getMaxEnergyStored(); + } + + @Override + public boolean canConnectEnergy(ForgeDirection from){ + return true; + } +}