tcr/java/src/game/block/BlockRedstoneTorch.java

238 lines
7.1 KiB
Java
Executable file

package game.block;
import java.util.List;
import java.util.Map;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import game.init.Blocks;
import game.init.ItemRegistry;
import game.init.SoundEvent;
import game.item.CheatTab;
import game.item.Item;
import game.renderer.particle.ParticleType;
import game.rng.Random;
import game.world.BlockPos;
import game.world.Facing;
import game.world.IWorldAccess;
import game.world.State;
import game.world.World;
import game.world.WorldClient;
import game.world.WorldServer;
public class BlockRedstoneTorch extends BlockTorch
{
private static Map<World, List<BlockRedstoneTorch.Toggle>> toggles = Maps.<World, List<BlockRedstoneTorch.Toggle>>newHashMap();
private final boolean isOn;
private boolean isBurnedOut(WorldServer worldIn, BlockPos pos, boolean turnOff)
{
if (!toggles.containsKey(worldIn))
{
toggles.put(worldIn, Lists.<BlockRedstoneTorch.Toggle>newArrayList());
}
List<BlockRedstoneTorch.Toggle> list = (List)toggles.get(worldIn);
if (turnOff)
{
list.add(new BlockRedstoneTorch.Toggle(pos, worldIn.getTime()));
}
int i = 0;
for (int j = 0; j < list.size(); ++j)
{
BlockRedstoneTorch.Toggle blockredstonetorch$toggle = (BlockRedstoneTorch.Toggle)list.get(j);
if (blockredstonetorch$toggle.pos.equals(pos))
{
++i;
if (i >= 8)
{
return true;
}
}
}
return false;
}
public BlockRedstoneTorch(boolean isOn)
{
this.isOn = isOn;
// this.setTickRandomly(true);
this.setTab((CheatTab)null);
}
/**
* How many world ticks before ticking
*/
public int tickRate(World worldIn, BlockPos pos)
{
return 2;
}
public void onBlockAdded(WorldServer worldIn, BlockPos pos, State state)
{
if (this.isOn)
{
for (Facing enumfacing : Facing.values())
{
worldIn.notifyNeighborsOfStateChange(pos.offset(enumfacing), this);
}
}
}
public void onBlockRemoved(WorldServer worldIn, BlockPos pos, State state)
{
if (this.isOn)
{
for (Facing enumfacing : Facing.values())
{
worldIn.notifyNeighborsOfStateChange(pos.offset(enumfacing), this);
}
}
}
public int getWeakPower(IWorldAccess worldIn, BlockPos pos, State state, Facing side)
{
return this.isOn && state.getValue(FACING) != side ? 15 : 0;
}
private boolean shouldBeOff(World worldIn, BlockPos pos, State state)
{
Facing enumfacing = ((Facing)state.getValue(FACING)).getOpposite();
return worldIn.isSidePowered(pos.offset(enumfacing), enumfacing);
}
/**
* Called randomly when setTickRandomly is set to true (used by e.g. crops to grow, etc.)
*/
public void randomTick(WorldServer worldIn, BlockPos pos, State state, Random random)
{
}
public void updateTick(WorldServer worldIn, BlockPos pos, State state, Random rand)
{
boolean flag = this.shouldBeOff(worldIn, pos, state);
List<BlockRedstoneTorch.Toggle> list = (List)toggles.get(worldIn);
while (list != null && !list.isEmpty() && worldIn.getTime() - ((BlockRedstoneTorch.Toggle)list.get(0)).time > 60L)
{
list.remove(0);
}
if (this.isOn)
{
if (flag)
{
worldIn.setState(pos, Blocks.unlit_redstone_torch.getState().withProperty(FACING, state.getValue(FACING)), 3);
if (this.isBurnedOut(worldIn, pos, true))
{
worldIn.playSound(SoundEvent.FIZZ, (double)((float)pos.getX() + 0.5F), (double)((float)pos.getY() + 0.5F), (double)((float)pos.getZ() + 0.5F), 0.5F);
for (int i = 0; i < 5; ++i)
{
double d0 = (double)pos.getX() + rand.doublev() * 0.6D + 0.2D;
double d1 = (double)pos.getY() + rand.doublev() * 0.6D + 0.2D;
double d2 = (double)pos.getZ() + rand.doublev() * 0.6D + 0.2D;
worldIn.spawnParticle(ParticleType.SMOKE_NORMAL, d0, d1, d2, 0.0D, 0.0D, 0.0D);
}
worldIn.scheduleUpdate(pos, worldIn.getState(pos).getBlock(), 160);
}
}
}
else if (!flag && !this.isBurnedOut(worldIn, pos, false))
{
worldIn.setState(pos, Blocks.redstone_torch.getState().withProperty(FACING, state.getValue(FACING)), 3);
}
}
/**
* Called when a neighboring block changes.
*/
public void onNeighborBlockChange(World worldIn, BlockPos pos, State state, Block neighborBlock)
{
if (!this.onNeighborChangeInternal(worldIn, pos, state))
{
if (this.isOn == this.shouldBeOff(worldIn, pos, state))
{
worldIn.scheduleUpdate(pos, this, this.tickRate(worldIn, pos));
}
}
}
public int getStrongPower(IWorldAccess worldIn, BlockPos pos, State state, Facing side)
{
return side == Facing.DOWN ? this.getWeakPower(worldIn, pos, state, side) : 0;
}
/**
* Get the Item that this Block should drop when harvested.
*/
public Item getItemDropped(State state, Random rand, int fortune)
{
return ItemRegistry.getItemFromBlock(Blocks.redstone_torch);
}
/**
* Can this block provide power. Only wire currently seems to have this change based on its state.
*/
public boolean canProvidePower()
{
return true;
}
public void randomDisplayTick(WorldClient worldIn, BlockPos pos, State state, Random rand)
{
if (this.isOn)
{
double d0 = (double)pos.getX() + 0.5D + (rand.doublev() - 0.5D) * 0.2D;
double d1 = (double)pos.getY() + 0.7D + (rand.doublev() - 0.5D) * 0.2D;
double d2 = (double)pos.getZ() + 0.5D + (rand.doublev() - 0.5D) * 0.2D;
Facing enumfacing = (Facing)state.getValue(FACING);
if (enumfacing.getAxis().isHorizontal())
{
Facing enumfacing1 = enumfacing.getOpposite();
double d3 = 0.27D;
d0 += 0.27D * (double)enumfacing1.getFrontOffsetX();
d1 += 0.22D;
d2 += 0.27D * (double)enumfacing1.getFrontOffsetZ();
}
worldIn.spawnParticle(ParticleType.REDSTONE, d0, d1, d2, 0.0D, 0.0D, 0.0D);
}
}
public Item getItem(World worldIn, BlockPos pos)
{
return ItemRegistry.getItemFromBlock(Blocks.redstone_torch);
}
public boolean isAssociatedBlock(Block other)
{
return other == Blocks.unlit_redstone_torch || other == Blocks.redstone_torch;
}
public boolean isMagnetic() {
return true;
}
static class Toggle
{
BlockPos pos;
long time;
public Toggle(BlockPos pos, long time)
{
this.pos = pos;
this.time = time;
}
}
}