All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.minecraft.server.BlockMinecartDetector Maven / Gradle / Ivy

package net.minecraft.server;

import com.google.common.base.Predicate;
import org.bukkit.event.block.BlockRedstoneEvent;

import java.util.List;
import java.util.Random;

public class BlockMinecartDetector extends BlockMinecartTrackAbstract {

  public static final BlockStateEnum SHAPE = BlockStateEnum.a("shape", BlockMinecartTrackAbstract.EnumTrackPosition.class, new Predicate() {
    public boolean a(BlockMinecartTrackAbstract.EnumTrackPosition blockminecarttrackabstract_enumtrackposition) {
      return blockminecarttrackabstract_enumtrackposition != BlockMinecartTrackAbstract.EnumTrackPosition.NORTH_EAST && blockminecarttrackabstract_enumtrackposition != BlockMinecartTrackAbstract.EnumTrackPosition.NORTH_WEST && blockminecarttrackabstract_enumtrackposition != BlockMinecartTrackAbstract.EnumTrackPosition.SOUTH_EAST && blockminecarttrackabstract_enumtrackposition != BlockMinecartTrackAbstract.EnumTrackPosition.SOUTH_WEST;
    }

    public boolean apply(Object object) {
      return this.a((BlockMinecartTrackAbstract.EnumTrackPosition) object);
    }
  });
  public static final BlockStateBoolean POWERED = BlockStateBoolean.of("powered");

  public BlockMinecartDetector() {
    super(true);
    this.j(this.blockStateList.getBlockData().set(BlockMinecartDetector.POWERED, Boolean.valueOf(false)).set(BlockMinecartDetector.SHAPE, BlockMinecartTrackAbstract.EnumTrackPosition.NORTH_SOUTH));
    this.a(true);
  }

  public int a(World world) {
    return 20;
  }

  public boolean isPowerSource() {
    return true;
  }

  public void a(World world, BlockPosition pos, IBlockData iblockdata, Entity entity) {
    if (!world.isClientSide) {
      if (!iblockdata.get(BlockMinecartDetector.POWERED).booleanValue()) {
        this.e(world, pos, iblockdata);
      }
    }
  }

  public void a(World world, BlockPosition blockposition, IBlockData iblockdata, Random random) {
  }

  public void b(World world, BlockPosition pos, IBlockData iblockdata, Random random) {
    if (!world.isClientSide && iblockdata.get(BlockMinecartDetector.POWERED).booleanValue()) {
      this.e(world, pos, iblockdata);
    }
  }

  public int a(IBlockAccess iblockaccess, BlockPosition blockposition, IBlockData iblockdata, EnumDirection enumdirection) {
    return iblockdata.get(BlockMinecartDetector.POWERED).booleanValue() ? 15 : 0;
  }

  public int b(IBlockAccess iblockaccess, BlockPosition blockposition, IBlockData data, EnumDirection enumdirection) {
    return !data.get(BlockMinecartDetector.POWERED).booleanValue() ? 0 : (enumdirection == EnumDirection.UP ? 15 : 0);
  }

  private void e(World world, BlockPosition blockposition, IBlockData iblockdata) {
    boolean flag = iblockdata.get(BlockMinecartDetector.POWERED).booleanValue();
    boolean flag1 = false;
    List list = this.a(world, blockposition, EntityMinecartAbstract.class);

    if (!list.isEmpty()) {
      flag1 = true;
    }

    // CraftBukkit start
    if (flag != flag1) {
      org.bukkit.block.Block block = world.getWorld().getBlockAt(blockposition.getX(), blockposition.getY(), blockposition.getZ());

      BlockRedstoneEvent eventRedstone = new BlockRedstoneEvent(block, flag ? 15 : 0, flag1 ? 15 : 0);
      world.getServer().getPluginManager().callEvent(eventRedstone);

      flag1 = eventRedstone.getNewCurrent() > 0;
    }
    // CraftBukkit end

    if (flag1 && !flag) {
      world.setTypeAndData(blockposition, iblockdata.set(BlockMinecartDetector.POWERED, Boolean.valueOf(true)), 3);
      world.applyPhysics(blockposition, this);
      world.applyPhysics(blockposition.down(), this);
      world.b(blockposition, blockposition);
    }

    if (!flag1 && flag) {
      world.setTypeAndData(blockposition, iblockdata.set(BlockMinecartDetector.POWERED, Boolean.valueOf(false)), 3);
      world.applyPhysics(blockposition, this);
      world.applyPhysics(blockposition.down(), this);
      world.b(blockposition, blockposition);
    }

    if (flag1) {
      world.a(blockposition, this, this.a(world));
    }

    world.updateAdjacentComparators(blockposition, this);
  }

  public void onPlace(World world, BlockPosition blockposition, IBlockData iblockdata) {
    super.onPlace(world, blockposition, iblockdata);
    this.e(world, blockposition, iblockdata);
  }

  public IBlockState n() {
    return BlockMinecartDetector.SHAPE;
  }

  public boolean isComplexRedstone() {
    return true;
  }

  public int l(World world, BlockPosition blockposition) {
    if (world.getType(blockposition).get(BlockMinecartDetector.POWERED)) {
      List list = this.a(world, blockposition, EntityMinecartCommandBlock.class);

      if (!list.isEmpty()) {
        return list.get(0).getCommandBlock().j();
      }

      List list1 = this.a(world, blockposition, EntityMinecartAbstract.class, IEntitySelector.c);

      if (!list1.isEmpty()) {
        return Container.b((IInventory) list1.get(0));
      }
    }

    return 0;
  }

  protected  List a(World world, BlockPosition blockposition, Class oclass, Predicate... apredicate) {
    AxisAlignedBB axisalignedbb = this.a(blockposition);

    return apredicate.length != 1 ? world.a(oclass, axisalignedbb) : world.a(oclass, axisalignedbb, apredicate[0]);
  }

  private AxisAlignedBB a(BlockPosition blockposition) {
    float f = 0.2F;

    return new AxisAlignedBB((float) blockposition.getX() + 0.2F, blockposition.getY(), (float) blockposition.getZ() + 0.2F, (float) (blockposition.getX() + 1) - 0.2F, (float) (blockposition.getY() + 1) - 0.2F, (float) (blockposition.getZ() + 1) - 0.2F);
  }

  public IBlockData fromLegacyData(int i) {
    return this.getBlockData().set(BlockMinecartDetector.SHAPE, BlockMinecartTrackAbstract.EnumTrackPosition.a(i & 7)).set(BlockMinecartDetector.POWERED, Boolean.valueOf((i & 8) > 0));
  }

  public int toLegacyData(IBlockData iblockdata) {
    byte b0 = 0;
    int i = b0 | iblockdata.get(BlockMinecartDetector.SHAPE).a();

    if (iblockdata.get(BlockMinecartDetector.POWERED)) {
      i |= 8;
    }

    return i;
  }

  protected BlockStateList getStateList() {
    return new BlockStateList(this, BlockMinecartDetector.SHAPE, BlockMinecartDetector.POWERED);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy