コード例 #1
0
  public static BlockEvent.NeighborNotifyEvent createBlockNeighborNotifyEvent(Event event) {
    if (!(event instanceof NotifyNeighborBlockEvent)) {
      throw new IllegalArgumentException(
          "Event " + event.getClass() + " is not a valid NotifyNeighborBlockEvent.");
    }

    NotifyNeighborBlockEvent spongeEvent = (NotifyNeighborBlockEvent) event;
    Optional<BlockSnapshot> blockSnapshot = spongeEvent.getCause().first(BlockSnapshot.class);
    if (!blockSnapshot.isPresent() || !blockSnapshot.get().getLocation().isPresent()) {
      return null;
    }

    EnumSet<EnumFacing> facings = EnumSet.noneOf(EnumFacing.class);
    for (Map.Entry<Direction, BlockState> mapEntry : spongeEvent.getNeighbors().entrySet()) {
      facings.add(DirectionFacingProvider.getInstance().get(mapEntry.getKey()).get());
    }

    IBlockState state = (IBlockState) blockSnapshot.get().getState();
    BlockPos pos = VecHelper.toBlockPos(blockSnapshot.get().getLocation().get());
    net.minecraft.world.World world =
        (net.minecraft.world.World) blockSnapshot.get().getLocation().get().getExtent();

    final NeighborNotifyEvent forgeEvent = new NeighborNotifyEvent(world, pos, state, facings);
    return forgeEvent;
  }
コード例 #2
0
  @Overwrite
  public static PlayerInteractEvent onPlayerInteract(
      EntityPlayer player,
      Action action,
      net.minecraft.world.World world,
      BlockPos pos,
      EnumFacing face) {
    if (world.isRemote) {
      PlayerInteractEvent event = new PlayerInteractEvent(player, action, pos, face, world);
      MinecraftForge.EVENT_BUS.post(event);
      return event;
    }

    InteractBlockEvent event = null;
    if (action == Action.LEFT_CLICK_BLOCK) {
      event =
          SpongeEventFactory.createInteractBlockEventPrimary(
              SpongeImpl.getGame(),
              Cause.of(player),
              Optional.empty(),
              ((World) world).createSnapshot(VecHelper.toVector(pos)),
              face == null
                  ? Direction.NONE
                  : DirectionFacingProvider.getInstance().getKey(face).get());
    } else {
      event =
          SpongeEventFactory.createInteractBlockEventSecondary(
              SpongeImpl.getGame(),
              Cause.of(player),
              Optional.empty(),
              ((World) world).createSnapshot(VecHelper.toVector(pos)),
              face == null
                  ? Direction.NONE
                  : DirectionFacingProvider.getInstance().getKey(face).get());
    }

    SpongeImpl.postEvent(event);

    return (PlayerInteractEvent) SpongeForgeEventFactory.lastForgeEvent;
  }
コード例 #3
0
  private static PlayerInteractEvent createPlayerInteractEvent(Event event) {
    if (!(event instanceof InteractBlockEvent)) {
      throw new IllegalArgumentException("Event " + event + " is not a valid InteractBlockEvent.");
    }

    InteractBlockEvent spongeEvent = (InteractBlockEvent) event;
    Optional<Player> player = spongeEvent.getCause().first(Player.class);
    if (!player.isPresent()) {
      return null;
    }

    BlockPos pos =
        VecHelper.toBlockPos(spongeEvent.getTargetBlock().getLocation().get().getPosition());
    Optional<EnumFacing> face =
        DirectionFacingProvider.getInstance().get(spongeEvent.getTargetSide());
    Action action = null;
    if (spongeEvent instanceof InteractBlockEvent.Primary) {
      action = Action.LEFT_CLICK_BLOCK;
    } else if (spongeEvent instanceof InteractBlockEvent.Secondary) {
      if (spongeEvent.getTargetBlock().getState().getType() == BlockTypes.AIR) {
        action = Action.RIGHT_CLICK_AIR;
      } else {
        action = Action.RIGHT_CLICK_BLOCK;
      }
    } else { // attempt to determine action
      EntityPlayer entityplayer = (EntityPlayer) player.get();
      action = Action.RIGHT_CLICK_BLOCK;
      if (entityplayer.isUsingItem()) {
        action = Action.LEFT_CLICK_BLOCK;
      } else if (entityplayer.worldObj.isAirBlock(pos)) {
        action = Action.RIGHT_CLICK_AIR;
      }
    }

    PlayerInteractEvent forgeEvent =
        new PlayerInteractEvent(
            (EntityPlayer) player.get(),
            action,
            pos,
            face.isPresent() ? face.get() : null,
            (net.minecraft.world.World) player.get().getWorld());
    return forgeEvent;
  }