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

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

package net.minecraft.server;

import org.bukkit.event.block.BlockRedstoneEvent;

import java.util.Iterator;

public class BlockLever extends Block {

  public static final BlockStateEnum FACING = BlockStateEnum.of("facing", BlockLever.EnumLeverPosition.class);
  public static final BlockStateBoolean POWERED = BlockStateBoolean.of("powered");

  protected BlockLever() {
    super(Material.ORIENTABLE);
    this.j(this.blockStateList.getBlockData().set(BlockLever.FACING, BlockLever.EnumLeverPosition.NORTH).set(BlockLever.POWERED, false));
    this.a(CreativeModeTab.d);
  }

  protected static boolean a(World world, BlockPosition blockposition, EnumDirection enumdirection) {
    return BlockButtonAbstract.a(world, blockposition, enumdirection);
  }

  public static int a(EnumDirection enumdirection) {
    switch (BlockLever.SyntheticClass_1.a[enumdirection.ordinal()]) {
      case 1:
        return 0;

      case 2:
        return 5;

      case 3:
        return 4;

      case 4:
        return 3;

      case 5:
        return 2;

      case 6:
        return 1;

      default:
        return -1;
    }
  }

  public AxisAlignedBB a(World world, BlockPosition blockposition, IBlockData iblockdata) {
    return null;
  }

  public boolean c() {
    return false;
  }

  public boolean d() {
    return false;
  }

  public boolean canPlace(World world, BlockPosition blockposition, EnumDirection enumdirection) {
    return a(world, blockposition, enumdirection.opposite());
  }

  public boolean canPlace(World world, BlockPosition blockposition) {
    EnumDirection[] aenumdirection = EnumDirection.values();
    int i = aenumdirection.length;
  
    for (EnumDirection enumdirection : aenumdirection) {
      if (a(world, blockposition, enumdirection)) {
        return true;
      }
    }

    return false;
  }

  public IBlockData getPlacedState(World world, BlockPosition blockposition, EnumDirection enumdirection, float f, float f1, float f2, int i, EntityLiving entityliving) {
    IBlockData iblockdata = this.getBlockData().set(BlockLever.POWERED, false);

    if (a(world, blockposition, enumdirection.opposite())) {
      return iblockdata.set(BlockLever.FACING, BlockLever.EnumLeverPosition.a(enumdirection, entityliving.getDirection()));
    } else {
      Iterator iterator = EnumDirection.EnumDirectionLimit.HORIZONTAL.iterator();

      EnumDirection enumdirection1;

      do {
        if (!iterator.hasNext()) {
          if (World.a(world, blockposition.down())) {
            return iblockdata.set(BlockLever.FACING, BlockLever.EnumLeverPosition.a(EnumDirection.UP, entityliving.getDirection()));
          }

          return iblockdata;
        }

        enumdirection1 = iterator.next();
      } while (enumdirection1 == enumdirection || !a(world, blockposition, enumdirection1.opposite()));

      return iblockdata.set(BlockLever.FACING, BlockLever.EnumLeverPosition.a(enumdirection1, entityliving.getDirection()));
    }
  }

  public void doPhysics(World world, BlockPosition pos, IBlockData iblockdata, Block block) {
    if (this.e(world, pos, iblockdata) && !a(world, pos, iblockdata.get(BlockLever.FACING).c().opposite())) {
      this.b(world, pos, iblockdata, 0);
      world.setAir(pos);
    }

  }

  private boolean e(World world, BlockPosition blockposition, IBlockData iblockdata) {
    if (this.canPlace(world, blockposition)) {
      return true;
    } else {
      this.b(world, blockposition, iblockdata, 0);
      world.setAir(blockposition);
      return false;
    }
  }

  public void updateShape(IBlockAccess iblockaccess, BlockPosition blockposition) {
    float f = 0.1875F;

    switch (BlockLever.SyntheticClass_1.b[iblockaccess.getType(blockposition).get(BlockLever.FACING).ordinal()]) {
      case 1:
        this.a(0.0F, 0.2F, 0.5F - f, f * 2.0F, 0.8F, 0.5F + f);
        break;

      case 2:
        this.a(1.0F - f * 2.0F, 0.2F, 0.5F - f, 1.0F, 0.8F, 0.5F + f);
        break;

      case 3:
        this.a(0.5F - f, 0.2F, 0.0F, 0.5F + f, 0.8F, f * 2.0F);
        break;

      case 4:
        this.a(0.5F - f, 0.2F, 1.0F - f * 2.0F, 0.5F + f, 0.8F, 1.0F);
        break;

      case 5:
      case 6:
        f = 0.25F;
        this.a(0.5F - f, 0.0F, 0.5F - f, 0.5F + f, 0.6F, 0.5F + f);
        break;

      case 7:
      case 8:
        f = 0.25F;
        this.a(0.5F - f, 0.4F, 0.5F - f, 0.5F + f, 1.0F, 0.5F + f);
    }

  }

  public boolean interact(World world, BlockPosition blockposition, IBlockData iblockdata, EntityHuman entityhuman, EnumDirection enumdirection, float f, float f1, float f2) {
    if (!world.isClientSide) {
      // CraftBukkit start - Interact Lever
      boolean powered = iblockdata.get(POWERED);
      org.bukkit.block.Block block = world.getWorld().getBlockAt(blockposition.getX(), blockposition.getY(), blockposition.getZ());
      int old = (powered) ? 15 : 0;
      int current = (!powered) ? 15 : 0;
    
      BlockRedstoneEvent eventRedstone = new BlockRedstoneEvent(block, old, current);
      world.getServer().getPluginManager().callEvent(eventRedstone);
    
      if ((eventRedstone.getNewCurrent() > 0) == (powered)) {
        return true;
      }
      // CraftBukkit end
    
      iblockdata = iblockdata.a(BlockLever.POWERED);
      world.setTypeAndData(blockposition, iblockdata, 3);
      world.makeSound((double) blockposition.getX() + 0.5D, (double) blockposition.getY() + 0.5D, (double) blockposition.getZ() + 0.5D, "random.click", 0.3F, iblockdata.get(BlockLever.POWERED) ? 0.6F : 0.5F);
      world.applyPhysics(blockposition, this);
      EnumDirection enumdirection1 = iblockdata.get(BlockLever.FACING).c();
    
      world.applyPhysics(blockposition.shift(enumdirection1.opposite()), this);
    }
    return true;
  }

  public void remove(World world, BlockPosition blockposition, IBlockData iblockdata) {
    if (iblockdata.get(BlockLever.POWERED)) {
      world.applyPhysics(blockposition, this);
      EnumDirection enumdirection = iblockdata.get(BlockLever.FACING).c();

      world.applyPhysics(blockposition.shift(enumdirection.opposite()), this);
    }

    super.remove(world, blockposition, iblockdata);
  }

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

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

  public boolean isPowerSource() {
    return true;
  }

  public IBlockData fromLegacyData(int i) {
    return this.getBlockData().set(BlockLever.FACING, BlockLever.EnumLeverPosition.a(i & 7)).set(BlockLever.POWERED, (i & 8) > 0);
  }

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

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

    return i;
  }

  protected BlockStateList getStateList() {
    return new BlockStateList(this, BlockLever.FACING, BlockLever.POWERED);
  }

  public enum EnumLeverPosition implements INamable {

    DOWN_X(0, "down_x", EnumDirection.DOWN), EAST(1, "east", EnumDirection.EAST), WEST(2, "west", EnumDirection.WEST), SOUTH(3, "south", EnumDirection.SOUTH), NORTH(4, "north", EnumDirection.NORTH), UP_Z(5, "up_z", EnumDirection.UP), UP_X(6, "up_x", EnumDirection.UP), DOWN_Z(7, "down_z", EnumDirection.DOWN);

    private static final BlockLever.EnumLeverPosition[] i = new BlockLever.EnumLeverPosition[values().length];

    static {
      BlockLever.EnumLeverPosition[] ablocklever_enumleverposition = values();
      int i = ablocklever_enumleverposition.length;
  
      for (EnumLeverPosition blocklever_enumleverposition : ablocklever_enumleverposition) {
        EnumLeverPosition.i[blocklever_enumleverposition.a()] = blocklever_enumleverposition;
      }

    }

    private final int j;
    private final String k;
    private final EnumDirection l;

    EnumLeverPosition(int i, String s, EnumDirection enumdirection) {
      this.j = i;
      this.k = s;
      this.l = enumdirection;
    }

    public static BlockLever.EnumLeverPosition a(int i) {
      if (i < 0 || i >= BlockLever.EnumLeverPosition.i.length) {
        i = 0;
      }

      return BlockLever.EnumLeverPosition.i[i];
    }

    public static BlockLever.EnumLeverPosition a(EnumDirection enumdirection, EnumDirection enumdirection1) {
      switch (BlockLever.SyntheticClass_1.a[enumdirection.ordinal()]) {
        case 1:
          switch (BlockLever.SyntheticClass_1.c[enumdirection1.k().ordinal()]) {
            case 1:
              return BlockLever.EnumLeverPosition.DOWN_X;

            case 2:
              return BlockLever.EnumLeverPosition.DOWN_Z;

            default:
              throw new IllegalArgumentException("Invalid entityFacing " + enumdirection1 + " for facing " + enumdirection);
          }

        case 2:
          switch (BlockLever.SyntheticClass_1.c[enumdirection1.k().ordinal()]) {
            case 1:
              return BlockLever.EnumLeverPosition.UP_X;

            case 2:
              return BlockLever.EnumLeverPosition.UP_Z;

            default:
              throw new IllegalArgumentException("Invalid entityFacing " + enumdirection1 + " for facing " + enumdirection);
          }

        case 3:
          return BlockLever.EnumLeverPosition.NORTH;

        case 4:
          return BlockLever.EnumLeverPosition.SOUTH;

        case 5:
          return BlockLever.EnumLeverPosition.WEST;

        case 6:
          return BlockLever.EnumLeverPosition.EAST;

        default:
          throw new IllegalArgumentException("Invalid facing: " + enumdirection);
      }
    }

    public int a() {
      return this.j;
    }

    public EnumDirection c() {
      return this.l;
    }

    public String toString() {
      return this.k;
    }

    public String getName() {
      return this.k;
    }
  }

  static class SyntheticClass_1 {

    static final int[] a;
    static final int[] b;
    static final int[] c = new int[EnumDirection.EnumAxis.values().length];

    static {
      try {
        BlockLever.SyntheticClass_1.c[EnumDirection.EnumAxis.X.ordinal()] = 1;
      } catch (NoSuchFieldError ignored) {
      }

      try {
        BlockLever.SyntheticClass_1.c[EnumDirection.EnumAxis.Z.ordinal()] = 2;
      } catch (NoSuchFieldError ignored) {
      }

      b = new int[BlockLever.EnumLeverPosition.values().length];

      try {
        BlockLever.SyntheticClass_1.b[BlockLever.EnumLeverPosition.EAST.ordinal()] = 1;
      } catch (NoSuchFieldError ignored) {
      }

      try {
        BlockLever.SyntheticClass_1.b[BlockLever.EnumLeverPosition.WEST.ordinal()] = 2;
      } catch (NoSuchFieldError ignored) {
      }

      try {
        BlockLever.SyntheticClass_1.b[BlockLever.EnumLeverPosition.SOUTH.ordinal()] = 3;
      } catch (NoSuchFieldError ignored) {
      }

      try {
        BlockLever.SyntheticClass_1.b[BlockLever.EnumLeverPosition.NORTH.ordinal()] = 4;
      } catch (NoSuchFieldError ignored) {
      }

      try {
        BlockLever.SyntheticClass_1.b[BlockLever.EnumLeverPosition.UP_Z.ordinal()] = 5;
      } catch (NoSuchFieldError ignored) {
      }

      try {
        BlockLever.SyntheticClass_1.b[BlockLever.EnumLeverPosition.UP_X.ordinal()] = 6;
      } catch (NoSuchFieldError ignored) {
      }

      try {
        BlockLever.SyntheticClass_1.b[BlockLever.EnumLeverPosition.DOWN_X.ordinal()] = 7;
      } catch (NoSuchFieldError ignored) {
      }

      try {
        BlockLever.SyntheticClass_1.b[BlockLever.EnumLeverPosition.DOWN_Z.ordinal()] = 8;
      } catch (NoSuchFieldError ignored) {
      }

      a = new int[EnumDirection.values().length];

      try {
        BlockLever.SyntheticClass_1.a[EnumDirection.DOWN.ordinal()] = 1;
      } catch (NoSuchFieldError ignored) {
      }

      try {
        BlockLever.SyntheticClass_1.a[EnumDirection.UP.ordinal()] = 2;
      } catch (NoSuchFieldError ignored) {
      }

      try {
        BlockLever.SyntheticClass_1.a[EnumDirection.NORTH.ordinal()] = 3;
      } catch (NoSuchFieldError ignored) {
      }

      try {
        BlockLever.SyntheticClass_1.a[EnumDirection.SOUTH.ordinal()] = 4;
      } catch (NoSuchFieldError ignored) {
      }

      try {
        BlockLever.SyntheticClass_1.a[EnumDirection.WEST.ordinal()] = 5;
      } catch (NoSuchFieldError ignored) {
      }

      try {
        BlockLever.SyntheticClass_1.a[EnumDirection.EAST.ordinal()] = 6;
      } catch (NoSuchFieldError ignored) {
      }

    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy