ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/tile/TileEntityFireworkBox.java

179 lines
5.5 KiB
Java
Raw Normal View History

2015-12-13 17:32:06 +01:00
/*
* 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
2016-01-03 16:05:51 +01:00
* http://ellpeck.de/actaddlicense/
2015-12-13 17:32:06 +01:00
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2016-01-03 16:05:51 +01:00
* © 2016 Ellpeck
2015-12-13 17:32:06 +01:00
*/
2015-12-30 22:02:15 +01:00
package de.ellpeck.actuallyadditions.tile;
2015-12-13 17:32:06 +01:00
import cofh.api.energy.EnergyStorage;
import cofh.api.energy.IEnergyReceiver;
2015-12-21 22:22:02 +01:00
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
2015-12-30 22:02:15 +01:00
import de.ellpeck.actuallyadditions.util.Util;
2015-12-13 17:32:06 +01:00
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, IRedstoneToggle, IEnergyDisplay, IEnergySaver{
2015-12-13 17:32:06 +01:00
2015-12-19 10:30:39 +01:00
public static final int USE_PER_SHOT = 300;
2015-12-13 17:32:06 +01:00
public EnergyStorage storage = new EnergyStorage(20000);
private int timeUntilNextFirework;
2015-12-19 10:30:39 +01:00
private boolean activateOnceWithSignal;
private int oldEnergy;
2015-12-13 17:32:06 +01:00
@Override
public void updateEntity(){
if(!this.worldObj.isRemote){
if(!this.isRedstonePowered && !this.activateOnceWithSignal){
if(this.timeUntilNextFirework > 0){
this.timeUntilNextFirework--;
if(this.timeUntilNextFirework <= 0){
this.doWork();
}
}
else{
this.timeUntilNextFirework = 100;
2015-12-13 17:32:06 +01:00
}
}
if(this.oldEnergy != this.storage.getEnergyStored() && this.sendUpdateWithInterval()){
this.oldEnergy = this.storage.getEnergyStored();
2015-12-13 17:32:06 +01:00
}
}
}
private void doWork(){
if(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);
}
}
2015-12-13 17:32:06 +01:00
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 void writeSyncableNBT(NBTTagCompound compound, boolean sync){
super.writeSyncableNBT(compound, sync);
this.storage.writeToNBT(compound);
}
@Override
public void readSyncableNBT(NBTTagCompound compound, boolean sync){
super.readSyncableNBT(compound, sync);
this.storage.readFromNBT(compound);
}
2015-12-13 17:32:06 +01:00
@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;
}
@Override
public void toggle(boolean to){
this.activateOnceWithSignal = to;
}
@Override
public boolean isPulseMode(){
return this.activateOnceWithSignal;
}
@Override
public void activateOnPulse(){
this.doWork();
}
@Override
public int getEnergy(){
return this.storage.getEnergyStored();
}
@Override
public void setEnergy(int energy){
this.storage.setEnergyStored(energy);
}
@Override
2015-12-21 22:22:02 +01:00
@SideOnly(Side.CLIENT)
public int getMaxEnergy(){
return this.storage.getMaxEnergyStored();
}
2015-12-13 17:32:06 +01:00
}