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

163 lines
5 KiB
Java
Raw Normal View History

2015-12-13 17:32:06 +01:00
/*
2016-05-16 22:52:27 +02:00
* This file ("TileEntityFireworkBox.java") is part of the Actually Additions mod for Minecraft.
2015-12-13 17:32:06 +01:00
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
2016-05-16 22:52:27 +02:00
* http://ellpeck.de/actaddlicense
2015-12-13 17:32:06 +01:00
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2016-05-16 22:54:42 +02:00
* © 2015-2016 Ellpeck
2015-12-13 17:32:06 +01:00
*/
2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.tile;
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.EnumFacing;
import net.minecraft.util.math.BlockPos;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.math.MathHelper;
2016-05-16 17:00:29 +02:00
import net.minecraft.world.World;
2016-11-26 08:58:42 +01:00
import net.minecraftforge.energy.IEnergyStorage;
2015-12-13 17:32:06 +01:00
2016-11-26 20:43:50 +01:00
public class TileEntityFireworkBox extends TileEntityBase implements IEnergyDisplay{
2015-12-13 17:32:06 +01:00
2015-12-19 10:30:39 +01:00
public static final int USE_PER_SHOT = 300;
2016-11-26 20:43:50 +01:00
public final CustomEnergyStorage storage = new CustomEnergyStorage(20000, 200, 0);
2015-12-13 17:32:06 +01:00
private int timeUntilNextFirework;
private int oldEnergy;
2015-12-13 17:32:06 +01:00
public TileEntityFireworkBox(){
super("fireworkBox");
}
2016-11-02 19:36:32 +01:00
public void spawnFireworks(World world, double x, double y, double z){
2016-05-16 17:00:29 +02:00
int range = 4;
2016-11-02 19:36:32 +01:00
int amount = world.rand.nextInt(5)+1;
2016-05-16 17:00:29 +02:00
for(int i = 0; i < amount; i++){
2016-11-02 19:36:32 +01:00
ItemStack firework = this.makeFirework();
2016-05-16 17:00:29 +02:00
2016-11-26 21:32:27 +01:00
double newX = x+MathHelper.nextDouble(this.world.rand, 0, range*2)-range;
double newZ = z+MathHelper.nextDouble(this.world.rand, 0, range*2)-range;
if(world.isBlockLoaded(new BlockPos(newX, y, newZ))){
EntityFireworkRocket rocket = new EntityFireworkRocket(world, newX, y+0.5, newZ, firework);
world.spawnEntity(rocket);
}
2016-05-16 17:00:29 +02:00
}
}
2016-11-02 19:36:32 +01:00
private ItemStack makeFirework(){
2015-12-13 17:32:06 +01:00
NBTTagList list = new NBTTagList();
2016-11-26 21:32:27 +01:00
int chargesAmount = this.world.rand.nextInt(2)+1;
2015-12-13 17:32:06 +01:00
for(int i = 0; i < chargesAmount; i++){
2016-11-02 19:36:32 +01:00
list.appendTag(this.makeFireworkCharge());
2015-12-13 17:32:06 +01:00
}
NBTTagCompound compound1 = new NBTTagCompound();
compound1.setTag("Explosions", list);
2016-11-26 21:32:27 +01:00
compound1.setByte("Flight", (byte)(this.world.rand.nextInt(3)+1));
2015-12-13 17:32:06 +01:00
NBTTagCompound compound = new NBTTagCompound();
compound.setTag("Fireworks", compound1);
2016-04-20 21:39:03 +02:00
ItemStack firework = new ItemStack(Items.FIREWORKS);
2015-12-13 17:32:06 +01:00
firework.setTagCompound(compound);
return firework;
}
2016-11-02 19:36:32 +01:00
private NBTTagCompound makeFireworkCharge(){
2015-12-13 17:32:06 +01:00
NBTTagCompound compound = new NBTTagCompound();
2016-11-26 21:32:27 +01:00
if(this.world.rand.nextFloat() >= 0.65F){
if(this.world.rand.nextFloat() >= 0.5F){
2015-12-13 17:32:06 +01:00
compound.setBoolean("Flicker", true);
}
else{
compound.setBoolean("Trail", true);
}
}
2016-11-26 21:32:27 +01:00
int[] colors = new int[MathHelper.getInt(this.world.rand, 1, 6)];
2015-12-13 17:32:06 +01:00
for(int i = 0; i < colors.length; i++){
2016-11-26 21:32:27 +01:00
colors[i] = ItemDye.DYE_COLORS[this.world.rand.nextInt(ItemDye.DYE_COLORS.length)];
2015-12-13 17:32:06 +01:00
}
compound.setIntArray("Colors", colors);
2016-11-26 21:32:27 +01:00
compound.setByte("Type", (byte)this.world.rand.nextInt(5));
2015-12-13 17:32:06 +01:00
return compound;
}
2016-06-01 00:39:35 +02:00
@Override
public void writeSyncableNBT(NBTTagCompound compound, NBTType type){
super.writeSyncableNBT(compound, type);
2016-06-01 00:39:35 +02:00
this.storage.writeToNBT(compound);
}
@Override
public void readSyncableNBT(NBTTagCompound compound, NBTType type){
super.readSyncableNBT(compound, type);
2016-06-01 00:39:35 +02:00
this.storage.readFromNBT(compound);
}
@Override
public void updateEntity(){
super.updateEntity();
2016-11-26 21:32:27 +01:00
if(!this.world.isRemote){
if(!this.isRedstonePowered && !this.isPulseMode){
2016-06-01 00:39:35 +02:00
if(this.timeUntilNextFirework > 0){
this.timeUntilNextFirework--;
if(this.timeUntilNextFirework <= 0){
this.doWork();
}
}
else{
this.timeUntilNextFirework = 100;
}
}
if(this.oldEnergy != this.storage.getEnergyStored() && this.sendUpdateWithInterval()){
this.oldEnergy = this.storage.getEnergyStored();
}
}
}
private void doWork(){
if(this.storage.getEnergyStored() >= USE_PER_SHOT){
2016-11-26 21:32:27 +01:00
this.spawnFireworks(this.world, this.pos.getX(), this.pos.getY(), this.pos.getZ());
2016-06-01 00:39:35 +02:00
this.storage.extractEnergyInternal(USE_PER_SHOT, false);
2016-06-01 00:39:35 +02:00
}
}
@Override
public boolean isRedstoneToggle(){
return true;
}
@Override
public void activateOnPulse(){
this.doWork();
}
@Override
public CustomEnergyStorage getEnergyStorage(){
return this.storage;
}
@Override
public boolean needsHoldShift(){
return false;
}
2016-11-26 08:58:42 +01:00
@Override
public IEnergyStorage getEnergyStorage(EnumFacing facing){
return this.storage;
}
2015-12-13 17:32:06 +01:00
}